Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 7fe59aa5

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