Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 2683f0c4

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