Project

General

Profile

Download (12.9 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ 4893ad8a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.config;
21

    
22
import android.os.Build;
23
import android.os.Bundle;
24
import android.util.DisplayMetrics;
25
import android.view.View;
26
import android.view.ViewGroup;
27
import android.view.WindowManager;
28
import android.widget.LinearLayout;
29

    
30
import androidx.appcompat.app.AppCompatActivity;
31

    
32
import com.google.firebase.analytics.FirebaseAnalytics;
33

    
34
import org.distorted.dialogs.RubikDialogError;
35
import org.distorted.dmesh.ObjectMesh;
36
import org.distorted.jsons.ObjectJson;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.main.R;
40
import org.distorted.objectlib.main.ObjectControl;
41
import org.distorted.objectlib.main.ShapeDodecahedron;
42
import org.distorted.objectlib.main.ShapeHexahedron;
43
import org.distorted.objectlib.main.ShapeOctahedron;
44
import org.distorted.objectlib.main.ShapeTetrahedron;
45
import org.distorted.objects.RubikObject;
46
import org.distorted.objects.RubikObjectList;
47

    
48
import java.io.InputStream;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class ConfigActivity extends AppCompatActivity
53
{
54
    private static final int ACTIVITY_NUMBER = 2;
55
    private static final float RATIO_BAR  = 0.10f;
56

    
57
    public static final float DIALOG_BUTTON_SIZE  = 0.06f;
58
    public static final float MENU_BIG_TEXT_SIZE  = 0.05f;
59

    
60
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
61
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
62
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
63
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
64
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
65

    
66
    private FirebaseAnalytics mFirebaseAnalytics;
67
    private static int mScreenWidth, mScreenHeight;
68
    private int mCurrentApiVersion;
69
    private ConfigScreen mScreen;
70
    private int mObjectOrdinal;
71
    private int mHeightBar;
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
    @Override
76
    protected void onCreate(Bundle savedState)
77
      {
78
      super.onCreate(savedState);
79
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
80
      setTheme(R.style.MaterialThemeNoActionBar);
81
      setContentView(R.layout.config);
82

    
83
      Bundle b = getIntent().getExtras();
84

    
85
      if(b != null) mObjectOrdinal = b.getInt("obj");
86

    
87
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
88

    
89
      DisplayMetrics displaymetrics = new DisplayMetrics();
90
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
91
      mScreenWidth =displaymetrics.widthPixels;
92
      mScreenHeight=displaymetrics.heightPixels;
93
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
94
                                                  // which is not yet hidden.
95
                                                  // TODO: figure this out exactly.
96
      hideNavigationBar();
97
      cutoutHack();
98
      computeBarHeights();
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// this does not include possible insets
103

    
104
    private void computeBarHeights()
105
      {
106
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
107
      mHeightBar = barHeight;
108

    
109
      LinearLayout layout = findViewById(R.id.lowerBar);
110
      ViewGroup.LayoutParams params = layout.getLayoutParams();
111
      params.height = barHeight;
112
      layout.setLayoutParams(params);
113
      }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
    private void hideNavigationBar()
118
      {
119
      mCurrentApiVersion = Build.VERSION.SDK_INT;
120

    
121
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
122
        {
123
        final View decorView = getWindow().getDecorView();
124

    
125
        decorView.setSystemUiVisibility(FLAGS);
126

    
127
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
128
          {
129
          @Override
130
          public void onSystemUiVisibilityChange(int visibility)
131
            {
132
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
133
              {
134
              decorView.setSystemUiVisibility(FLAGS);
135
              }
136
            }
137
          });
138
        }
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
    @Override
144
    public void onAttachedToWindow()
145
      {
146
      super.onAttachedToWindow();
147

    
148
      if( mScreen==null ) mScreen = new ConfigScreen();
149
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// do not avoid cutouts
154

    
155
    private void cutoutHack()
156
      {
157
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
158
        {
159
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
160
        }
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
    @Override
166
    public void onWindowFocusChanged(boolean hasFocus)
167
      {
168
      super.onWindowFocusChanged(hasFocus);
169

    
170
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
171
        {
172
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
173
        }
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
    @Override
179
    protected void onPause() 
180
      {
181
      super.onPause();
182
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
183
      view.onPause();
184
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189
    @Override
190
    protected void onResume() 
191
      {
192
      super.onResume();
193
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
194
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
195
      view.onResume();
196

    
197
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
198
        {
199
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
200
        changeIfDifferent(object,view.getObjectControl());
201
        }
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
    
206
    @Override
207
    protected void onDestroy() 
208
      {
209
      super.onDestroy();
210
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    void OpenGLError()
216
      {
217
      RubikDialogError errDiag = new RubikDialogError();
218
      errDiag.show(getSupportFragmentManager(), null);
219
      }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    private Static4D getDefaultRotation(int numFaces)
224
      {
225
      switch(numFaces)
226
        {
227
        case  4: return ShapeTetrahedron.DEFAULT_ROT;
228
        case  6: return ShapeHexahedron.DEFAULT_ROT;
229
        case  8: return ShapeOctahedron.DEFAULT_ROT;
230
        case 12: return ShapeDodecahedron.DEFAULT_ROT;
231
        }
232

    
233
      return null;
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    void rotateObject(int numFaces)
239
      {
240
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
241
      ObjectControl control = view.getObjectControl();
242
      Static4D rot = getDefaultRotation(numFaces);
243
      
244
      if( rot!=null ) control.rotateNow(rot);
245
      }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
    private void changeIfDifferent(RubikObject object,ObjectControl control)
250
      {
251
      if( object!=null )
252
        {
253
        int jsonID = object.getJsonID();
254
        int meshID = object.getMeshID();
255

    
256
        int meshState = object.getMeshState();
257

    
258
        InputStream jsonStream = ObjectJson.getStream(jsonID,this);
259
        InputStream meshStream = ObjectMesh.getStream(meshID,this);
260

    
261
        control.changeIfDifferent(object.getOrdinal(),meshState,jsonStream,meshStream);
262
        }
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
// PUBLIC API
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public void changeObject(RubikObject object)
270
      {
271
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
272
      ObjectControl control = view.getObjectControl();
273
      changeIfDifferent(object,control);
274
      }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
    public void changeMeshState(RubikObject object)
279
      {
280
      if( object!=null )
281
        {
282
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
283
        ObjectControl control = view.getObjectControl();
284

    
285
        int jsonID = object.getJsonID();
286
        int meshID = object.getMeshID();
287

    
288
        int meshState = object.getMeshState();
289

    
290
        InputStream jsonStream = ObjectJson.getStream(jsonID,this);
291
        InputStream meshStream = ObjectMesh.getStream(meshID,this);
292

    
293
        control.changeObject(object.getOrdinal(),meshState,jsonStream,meshStream);
294
        }
295
      }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
    public FirebaseAnalytics getAnalytics()
300
      {
301
      return mFirebaseAnalytics;
302
      }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
    public int getHeightBar()
307
      {
308
      return mHeightBar;
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    public int getScreenWidthInPixels()
314
      {
315
      return mScreenWidth;
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    public int getScreenHeightInPixels()
321
      {
322
      return mScreenHeight;
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    public ObjectControl getControl()
328
      {
329
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
330
      return view.getObjectControl();
331
      }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
    public static int getDrawableSize()
336
      {
337
      if( mScreenHeight<1000 )
338
        {
339
        return 0;
340
        }
341
      if( mScreenHeight<1600 )
342
        {
343
        return 1;
344
        }
345
      if( mScreenHeight<1900 )
346
        {
347
        return 2;
348
        }
349

    
350
      return 3;
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
    public static int getDrawable(int small, int medium, int big, int huge)
356
      {
357
      int size = getDrawableSize();
358

    
359
      switch(size)
360
        {
361
        case 0 : return small;
362
        case 1 : return medium;
363
        case 2 : return big;
364
        default: return huge;
365
        }
366
      }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
    public boolean isVertical()
371
      {
372
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
373
      return view.isVertical();
374
      }
375
}
(1-1/6)