Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ f404152d

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