Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 5d99af6c

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