Project

General

Profile

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

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

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