Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ d876c3bc

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
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 707f79ff Leszek Koltunski
import java.io.InputStream;
23
24 88b94310 Leszek Koltunski
import android.content.Intent;
25 a41e3c94 Leszek Koltunski
import android.content.SharedPreferences;
26 7cb8d4b0 Leszek Koltunski
import android.graphics.Bitmap;
27 9530f6b0 Leszek Koltunski
import android.os.Build;
28
import android.os.Bundle;
29 a41e3c94 Leszek Koltunski
import android.preference.PreferenceManager;
30 9530f6b0 Leszek Koltunski
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 81493402 Leszek Koltunski
import org.distorted.external.RubikFiles;
42 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.main.R;
44 48314d6a Leszek Koltunski
import org.distorted.main.RubikActivity;
45 707f79ff Leszek Koltunski
import org.distorted.objectlib.main.TwistyJson;
46
import org.distorted.objectlib.main.TwistyObject;
47 9530f6b0 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
public class BandagedCreatorActivity extends AppCompatActivity
51
{
52
    private static final int ACTIVITY_NUMBER = 3;
53 28cb1607 Leszek Koltunski
    private static final float RATIO_BAR   = 0.10f;
54 b9d062cf Leszek Koltunski
    private static final float RATIO_BUT   = 0.07f;
55 83e021c5 Leszek Koltunski
    static final float RATIO_SCROLL= 0.30f;
56 9530f6b0 Leszek Koltunski
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 b9d062cf Leszek Koltunski
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
102 48314d6a Leszek Koltunski
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
103 bc7e49ec Leszek Koltunski
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
104 b9d062cf Leszek Koltunski
      int padding      = (int)(mScreenHeight* RubikActivity.PADDING);
105 9530f6b0 Leszek Koltunski
106 b9d062cf Leszek Koltunski
      LinearLayout botLayout = findViewById(R.id.lowerBar);
107
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
108 9530f6b0 Leszek Koltunski
      paramsL.height = barHeight;
109 b9d062cf Leszek Koltunski
      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 9530f6b0 Leszek Koltunski
117 9f8d4c92 Leszek Koltunski
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
118
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
119
      paramsT.height = viewHeight;
120 b9d062cf Leszek Koltunski
      topLayout.setPadding(padding,padding,padding,padding);
121 9f8d4c92 Leszek Koltunski
      topLayout.setLayoutParams(paramsT);
122 48314d6a Leszek Koltunski
123
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
124
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
125
      paramsC.height = objectHeight;
126
      creator.setLayoutParams(paramsC);
127 9530f6b0 Leszek Koltunski
      }
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 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
186 9530f6b0 Leszek Koltunski
      view.onPause();
187
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
188 13a3dfa9 Leszek Koltunski
      savePreferences();
189 9530f6b0 Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    @Override
194
    protected void onResume() 
195
      {
196
      super.onResume();
197
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
198 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
199 9530f6b0 Leszek Koltunski
      view.onResume();
200
201
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
202 bebd7af5 Leszek Koltunski
      mScreen.onAttachedToWindow(this);
203 7cb8d4b0 Leszek Koltunski
204 a41e3c94 Leszek Koltunski
      restorePreferences();
205 7cb8d4b0 Leszek Koltunski
      BandagedCreatorWorkerThread.create(this);
206 9530f6b0 Leszek Koltunski
      }
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 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
    private void savePreferences()
228
      {
229
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
230
      SharedPreferences.Editor editor = preferences.edit();
231 b9d062cf Leszek Koltunski
      mScreen.savePreferences(editor);
232 13a3dfa9 Leszek Koltunski
      editor.apply();
233
      }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237
    private void restorePreferences()
238
      {
239
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
240 b9d062cf Leszek Koltunski
      mScreen.restorePreferences(this,preferences);
241 13a3dfa9 Leszek Koltunski
      }
242
243 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
    public FirebaseAnalytics getAnalytics()
248
      {
249
      return mFirebaseAnalytics;
250
      }
251
252 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
261
262
    public BandagedCreatorRenderer getRenderer()
263
      {
264
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
265
      return view.getRenderer();
266
      }
267
268 bfb59352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
    public boolean objectDoesntExist(String name)
271
      {
272
      return mScreen.objectDoesntExist(name);
273
      }
274
275 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
    public void addObject(String name)
278
      {
279 bfb59352 Leszek Koltunski
      mScreen.addObject(this,name);
280 e48ad1af Leszek Koltunski
      }
281
282 d3d639b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 85770de9 Leszek Koltunski
    public void deleteObject(String name)
285 d3d639b1 Leszek Koltunski
      {
286 81493402 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
287 707f79ff Leszek Koltunski
      InputStream jsonStream = files.openFile(this,name+"_object.json");
288
289 213c15de Leszek Koltunski
      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 707f79ff Leszek Koltunski
300
      mScreen.deleteObject(this,name);
301
      files.deleteIcon(this,name);
302
      files.deleteJsonObject(this,name);
303 d3d639b1 Leszek Koltunski
      }
304
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
    public void playObject(String name)
308
      {
309 88b94310 Leszek Koltunski
      Intent intent = new Intent(this, BandagedPlayActivity.class);
310
      intent.putExtra("name", name);
311
      startActivity(intent);
312 d3d639b1 Leszek Koltunski
      }
313
314 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316 7cb8d4b0 Leszek Koltunski
    public void iconCreationDone(Bitmap bmp)
317 9530f6b0 Leszek Koltunski
      {
318 20b60ad9 Leszek Koltunski
      mScreen.iconCreationDone(this,bmp);
319 9530f6b0 Leszek Koltunski
      }
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
}