Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 39176a1f

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 39176a1f Leszek Koltunski
    public static final float DIALOG_BUTTON_SIZE = 0.06f;
58
    public static final float MENU_BIG_TEXT_SIZE = 0.05f;
59
    public static final float SPINNER_TEXT_SIZE  = 0.03f;
60 9530f6b0 Leszek Koltunski
61
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
62
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
63
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
64
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
65
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
66
67
    private FirebaseAnalytics mFirebaseAnalytics;
68
    private static int mScreenWidth, mScreenHeight;
69
    private int mCurrentApiVersion;
70
    private BandagedCreatorScreen mScreen;
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
    @Override
75
    protected void onCreate(Bundle savedState)
76
      {
77
      super.onCreate(savedState);
78
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
79
      setTheme(R.style.MaterialThemeNoActionBar);
80
      setContentView(R.layout.bandaged);
81
82
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
83
84
      DisplayMetrics displaymetrics = new DisplayMetrics();
85 7fe59aa5 Leszek Koltunski
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
86 9530f6b0 Leszek Koltunski
      mScreenWidth =displaymetrics.widthPixels;
87
      mScreenHeight=displaymetrics.heightPixels;
88 7fe59aa5 Leszek Koltunski
89 9530f6b0 Leszek Koltunski
      hideNavigationBar();
90
      cutoutHack();
91
      computeHeights();
92
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// this does not include possible insets
96
97
    private void computeHeights()
98
      {
99
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
100 b9d062cf Leszek Koltunski
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
101 48314d6a Leszek Koltunski
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
102 bc7e49ec Leszek Koltunski
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
103 7fe59aa5 Leszek Koltunski
      int padding      = (int)(mScreenHeight*RubikActivity.PADDING);
104 9530f6b0 Leszek Koltunski
105 b9d062cf Leszek Koltunski
      LinearLayout botLayout = findViewById(R.id.lowerBar);
106
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
107 9530f6b0 Leszek Koltunski
      paramsL.height = barHeight;
108 b9d062cf Leszek Koltunski
      botLayout.setLayoutParams(paramsL);
109
110
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
111
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
112
      paramsB.height = butHeight;
113
      butLayout.setPadding(padding,0,padding,padding);
114
      butLayout.setLayoutParams(paramsB);
115 9530f6b0 Leszek Koltunski
116 9f8d4c92 Leszek Koltunski
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
117
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
118
      paramsT.height = viewHeight;
119 b9d062cf Leszek Koltunski
      topLayout.setPadding(padding,padding,padding,padding);
120 9f8d4c92 Leszek Koltunski
      topLayout.setLayoutParams(paramsT);
121 48314d6a Leszek Koltunski
122
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
123
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
124
      paramsC.height = objectHeight;
125
      creator.setLayoutParams(paramsC);
126 9530f6b0 Leszek Koltunski
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
    private void hideNavigationBar()
131
      {
132
      mCurrentApiVersion = Build.VERSION.SDK_INT;
133
134
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
135
        {
136
        final View decorView = getWindow().getDecorView();
137
138
        decorView.setSystemUiVisibility(FLAGS);
139
140
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
141
          {
142
          @Override
143
          public void onSystemUiVisibilityChange(int visibility)
144
            {
145
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
146
              {
147
              decorView.setSystemUiVisibility(FLAGS);
148
              }
149
            }
150
          });
151
        }
152
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// do not avoid cutouts
156
157
    private void cutoutHack()
158
      {
159
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
160
        {
161
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
162
        }
163
      }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
    @Override
168
    public void onWindowFocusChanged(boolean hasFocus)
169
      {
170
      super.onWindowFocusChanged(hasFocus);
171
172
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
173
        {
174
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
175
        }
176
      }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
    
180
    @Override
181
    protected void onPause() 
182
      {
183
      super.onPause();
184 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
185 9530f6b0 Leszek Koltunski
      view.onPause();
186
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
187 13a3dfa9 Leszek Koltunski
      savePreferences();
188 9530f6b0 Leszek Koltunski
      }
189
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
    
192
    @Override
193
    protected void onResume() 
194
      {
195
      super.onResume();
196
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
197 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
198 9530f6b0 Leszek Koltunski
      view.onResume();
199
200
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
201 bebd7af5 Leszek Koltunski
      mScreen.onAttachedToWindow(this);
202 7cb8d4b0 Leszek Koltunski
203 a41e3c94 Leszek Koltunski
      restorePreferences();
204 7cb8d4b0 Leszek Koltunski
      BandagedCreatorWorkerThread.create(this);
205 9530f6b0 Leszek Koltunski
      }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
    
209
    @Override
210
    protected void onDestroy() 
211
      {
212
      super.onDestroy();
213
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
214
      }
215
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
    void OpenGLError()
219
      {
220
      RubikDialogError errDiag = new RubikDialogError();
221
      errDiag.show(getSupportFragmentManager(), null);
222
      }
223
224 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
    private void savePreferences()
227
      {
228
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
229
      SharedPreferences.Editor editor = preferences.edit();
230 b9d062cf Leszek Koltunski
      mScreen.savePreferences(editor);
231 13a3dfa9 Leszek Koltunski
      editor.apply();
232
      }
233
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236
    private void restorePreferences()
237
      {
238
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
239 b9d062cf Leszek Koltunski
      mScreen.restorePreferences(this,preferences);
240 13a3dfa9 Leszek Koltunski
      }
241
242 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
243
// PUBLIC API
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246
    public FirebaseAnalytics getAnalytics()
247
      {
248
      return mFirebaseAnalytics;
249
      }
250
251 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    public void changeObject(int x, int y, int z)
254
      {
255
      BandagedCreatorRenderer renderer = getRenderer();
256
      renderer.changeObject(x,y,z);
257
      }
258
259 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
    public BandagedCreatorRenderer getRenderer()
262
      {
263
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
264
      return view.getRenderer();
265
      }
266
267 bfb59352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269
    public boolean objectDoesntExist(String name)
270
      {
271
      return mScreen.objectDoesntExist(name);
272
      }
273
274 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276
    public void addObject(String name)
277
      {
278 bfb59352 Leszek Koltunski
      mScreen.addObject(this,name);
279 e48ad1af Leszek Koltunski
      }
280
281 d3d639b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283 85770de9 Leszek Koltunski
    public void deleteObject(String name)
284 d3d639b1 Leszek Koltunski
      {
285 81493402 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
286 707f79ff Leszek Koltunski
      InputStream jsonStream = files.openFile(this,name+"_object.json");
287
288 213c15de Leszek Koltunski
      if( jsonStream!=null )
289
        {
290
        int meshState= TwistyObject.MESH_NICE;
291
        int iconMode = TwistyObject.MODE_NORM;
292
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
293
        SharedPreferences.Editor editor = preferences.edit();
294
        TwistyObject object = new TwistyJson( jsonStream, meshState, iconMode, null, null, 1.0f, null);
295
        object.removePreferences(editor);
296
        editor.apply();
297
        }
298 707f79ff Leszek Koltunski
299
      mScreen.deleteObject(this,name);
300
      files.deleteIcon(this,name);
301
      files.deleteJsonObject(this,name);
302 d3d639b1 Leszek Koltunski
      }
303
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306
    public void playObject(String name)
307
      {
308 88b94310 Leszek Koltunski
      Intent intent = new Intent(this, BandagedPlayActivity.class);
309
      intent.putExtra("name", name);
310
      startActivity(intent);
311 d3d639b1 Leszek Koltunski
      }
312
313 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
314
315 7cb8d4b0 Leszek Koltunski
    public void iconCreationDone(Bitmap bmp)
316 9530f6b0 Leszek Koltunski
      {
317 20b60ad9 Leszek Koltunski
      mScreen.iconCreationDone(this,bmp);
318 9530f6b0 Leszek Koltunski
      }
319
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321
322
    public int getScreenWidthInPixels()
323
      {
324
      return mScreenWidth;
325
      }
326
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329
    public int getScreenHeightInPixels()
330
      {
331
      return mScreenHeight;
332
      }
333
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
336
    public static int getDrawableSize()
337
      {
338
      if( mScreenHeight<1000 )
339
        {
340
        return 0;
341
        }
342
      if( mScreenHeight<1600 )
343
        {
344
        return 1;
345
        }
346
      if( mScreenHeight<1900 )
347
        {
348
        return 2;
349
        }
350
351
      return 3;
352
      }
353
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356
    public static int getDrawable(int small, int medium, int big, int huge)
357
      {
358
      int size = getDrawableSize();
359
360
      switch(size)
361
        {
362
        case 0 : return small;
363
        case 1 : return medium;
364
        case 2 : return big;
365
        default: return huge;
366
        }
367
      }
368
}