Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 109a2b68

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.bandaged;
21

    
22
import java.io.InputStream;
23

    
24
import android.content.Intent;
25
import android.content.SharedPreferences;
26
import android.graphics.Bitmap;
27
import android.os.Build;
28
import android.os.Bundle;
29
import android.preference.PreferenceManager;
30
import android.util.DisplayMetrics;
31
import android.view.View;
32
import android.view.ViewGroup;
33
import android.view.WindowManager;
34
import android.widget.LinearLayout;
35

    
36
import androidx.appcompat.app.AppCompatActivity;
37

    
38
import com.google.firebase.analytics.FirebaseAnalytics;
39

    
40
import org.distorted.dialogs.RubikDialogError;
41
import org.distorted.external.RubikFiles;
42
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45
import org.distorted.objectlib.main.TwistyJson;
46
import org.distorted.objectlib.main.TwistyObject;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class BandagedCreatorActivity extends AppCompatActivity
51
{
52
    private static final int ACTIVITY_NUMBER = 3;
53
    private static final float RATIO_BAR   = 0.10f;
54
    private static final float RATIO_BUT   = 0.07f;
55
    static final float RATIO_SCROLL= 0.30f;
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 BandagedCreatorScreen mScreen;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

    
81
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
82

    
83
      DisplayMetrics displaymetrics = new DisplayMetrics();
84
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
85
      mScreenWidth =displaymetrics.widthPixels;
86
      mScreenHeight=displaymetrics.heightPixels;
87
      mScreenHeight = (int)(1.07f*mScreenHeight); // add 7% for the upper bar
88
                                                  // which is not yet hidden.
89
                                                  // TODO: figure this out exactly.
90
      hideNavigationBar();
91
      cutoutHack();
92
      computeHeights();
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// this does not include possible insets
97

    
98
    private void computeHeights()
99
      {
100
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
101
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
102
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
103
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
104
      int padding      = (int)(mScreenHeight* RubikActivity.PADDING);
105

    
106
      LinearLayout botLayout = findViewById(R.id.lowerBar);
107
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
108
      paramsL.height = barHeight;
109
      botLayout.setLayoutParams(paramsL);
110

    
111
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
112
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
113
      paramsB.height = butHeight;
114
      butLayout.setPadding(padding,0,padding,padding);
115
      butLayout.setLayoutParams(paramsB);
116

    
117
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
118
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
119
      paramsT.height = viewHeight;
120
      topLayout.setPadding(padding,padding,padding,padding);
121
      topLayout.setLayoutParams(paramsT);
122

    
123
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
124
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
125
      paramsC.height = objectHeight;
126
      creator.setLayoutParams(paramsC);
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    private void hideNavigationBar()
132
      {
133
      mCurrentApiVersion = Build.VERSION.SDK_INT;
134

    
135
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
136
        {
137
        final View decorView = getWindow().getDecorView();
138

    
139
        decorView.setSystemUiVisibility(FLAGS);
140

    
141
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
142
          {
143
          @Override
144
          public void onSystemUiVisibilityChange(int visibility)
145
            {
146
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
147
              {
148
              decorView.setSystemUiVisibility(FLAGS);
149
              }
150
            }
151
          });
152
        }
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// do not avoid cutouts
157

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    @Override
169
    public void onWindowFocusChanged(boolean hasFocus)
170
      {
171
      super.onWindowFocusChanged(hasFocus);
172

    
173
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
174
        {
175
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
176
        }
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
    
181
    @Override
182
    protected void onPause() 
183
      {
184
      super.onPause();
185
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
186
      view.onPause();
187
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
188
      savePreferences();
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    @Override
194
    protected void onResume() 
195
      {
196
      super.onResume();
197
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
198
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
199
      view.onResume();
200

    
201
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
202
      mScreen.onAttachedToWindow(this);
203

    
204
      restorePreferences();
205
      BandagedCreatorWorkerThread.create(this);
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210
    @Override
211
    protected void onDestroy() 
212
      {
213
      super.onDestroy();
214
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    void OpenGLError()
220
      {
221
      RubikDialogError errDiag = new RubikDialogError();
222
      errDiag.show(getSupportFragmentManager(), null);
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    private void savePreferences()
228
      {
229
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
230
      SharedPreferences.Editor editor = preferences.edit();
231
      mScreen.savePreferences(editor);
232
      editor.apply();
233
      }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
    private void restorePreferences()
238
      {
239
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
240
      mScreen.restorePreferences(this,preferences);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public FirebaseAnalytics getAnalytics()
248
      {
249
      return mFirebaseAnalytics;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public void changeObject(int x, int y, int z)
255
      {
256
      BandagedCreatorRenderer renderer = getRenderer();
257
      renderer.changeObject(x,y,z);
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public BandagedCreatorRenderer getRenderer()
263
      {
264
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
265
      return view.getRenderer();
266
      }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
    public boolean objectDoesntExist(String name)
271
      {
272
      return mScreen.objectDoesntExist(name);
273
      }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
    public void addObject(String name)
278
      {
279
      mScreen.addObject(this,name);
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public void deleteObject(String name)
285
      {
286
      RubikFiles files = RubikFiles.getInstance();
287
      InputStream jsonStream = files.openFile(this,name+"_object.json");
288

    
289
      if( jsonStream!=null )
290
        {
291
        int meshState= TwistyObject.MESH_NICE;
292
        int iconMode = TwistyObject.MODE_NORM;
293
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
294
        SharedPreferences.Editor editor = preferences.edit();
295
        TwistyObject object = new TwistyJson( jsonStream, meshState, iconMode, null, null, 1.0f, null);
296
        object.removePreferences(editor);
297
        editor.apply();
298
        }
299

    
300
      mScreen.deleteObject(this,name);
301
      files.deleteIcon(this,name);
302
      files.deleteJsonObject(this,name);
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    public void playObject(String name)
308
      {
309
      Intent intent = new Intent(this, BandagedPlayActivity.class);
310
      intent.putExtra("name", name);
311
      startActivity(intent);
312
      }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
    public void iconCreationDone(Bitmap bmp)
317
      {
318
      mScreen.iconCreationDone(this,bmp);
319
      }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
    public int getScreenWidthInPixels()
324
      {
325
      return mScreenWidth;
326
      }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
    public int getScreenHeightInPixels()
331
      {
332
      return mScreenHeight;
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

    
352
      return 3;
353
      }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

    
361
      switch(size)
362
        {
363
        case 0 : return small;
364
        case 1 : return medium;
365
        case 2 : return big;
366
        default: return huge;
367
        }
368
      }
369
}
(1-1/13)