Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 70688a23

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 b3786da4 leszek
195 9530f6b0 Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
196 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
197 9530f6b0 Leszek Koltunski
      view.onResume();
198
199 7da27d2b leszek
      if( mScreen==null )
200
        {
201
        int ordinal = getObjectOrdinal();
202
        mScreen = new BandagedCreatorScreen(ordinal);
203
        }
204
205 bebd7af5 Leszek Koltunski
      mScreen.onAttachedToWindow(this);
206 7cb8d4b0 Leszek Koltunski
207 a41e3c94 Leszek Koltunski
      restorePreferences();
208 7cb8d4b0 Leszek Koltunski
      BandagedCreatorWorkerThread.create(this);
209 9530f6b0 Leszek Koltunski
      }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
    
213
    @Override
214
    protected void onDestroy() 
215
      {
216
      super.onDestroy();
217
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
218
      }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222
    void OpenGLError()
223
      {
224
      RubikDialogError errDiag = new RubikDialogError();
225
      errDiag.show(getSupportFragmentManager(), null);
226
      }
227
228 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
    private void savePreferences()
231
      {
232
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
233
      SharedPreferences.Editor editor = preferences.edit();
234 b9d062cf Leszek Koltunski
      mScreen.savePreferences(editor);
235 13a3dfa9 Leszek Koltunski
      editor.apply();
236
      }
237
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
    private void restorePreferences()
241
      {
242
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
243 b9d062cf Leszek Koltunski
      mScreen.restorePreferences(this,preferences);
244 13a3dfa9 Leszek Koltunski
      }
245
246 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
247
// PUBLIC API
248 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
    public void changeObject(int x, int y, int z)
251
      {
252
      BandagedCreatorRenderer renderer = getRenderer();
253
      renderer.changeObject(x,y,z);
254
      }
255
256 2ceeb6b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
257
258
    public boolean isRTL()
259
      {
260
      return mRTL;
261
      }
262
263 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265
    public BandagedCreatorRenderer getRenderer()
266
      {
267
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
268
      return view.getRenderer();
269
      }
270
271 bfb59352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273
    public boolean objectDoesntExist(String name)
274
      {
275
      return mScreen.objectDoesntExist(name);
276
      }
277
278 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
279
280
    public void addObject(String name)
281
      {
282 bfb59352 Leszek Koltunski
      mScreen.addObject(this,name);
283 e48ad1af Leszek Koltunski
      }
284
285 d3d639b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
286
287 85770de9 Leszek Koltunski
    public void deleteObject(String name)
288 d3d639b1 Leszek Koltunski
      {
289 81493402 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
290 707f79ff Leszek Koltunski
      InputStream jsonStream = files.openFile(this,name+"_object.json");
291 2876aeb6 Leszek Koltunski
      InitAssets assets = new InitAssets(jsonStream,null,null);
292 707f79ff Leszek Koltunski
293 9234018b Leszek Koltunski
      if( !assets.noJsonStream() )
294 213c15de Leszek Koltunski
        {
295 a8be1a23 Leszek Koltunski
        TwistyObject object = new TwistyJson( TwistyObject.MESH_NICE, TwistyObject.MODE_NORM, null, null, 1.0f, assets);
296
297
        if( !object.getError() )
298
          {
299
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
300
          SharedPreferences.Editor editor = preferences.edit();
301 88451205 Leszek Koltunski
          OSInterface os = new OSInterface(this,null);
302 05e4c778 Leszek Koltunski
          os.setEditor(editor);
303 21a1bb5d Leszek Koltunski
          object.removePreferences(os);
304 a8be1a23 Leszek Koltunski
          editor.apply();
305
          }
306 213c15de Leszek Koltunski
        }
307 707f79ff Leszek Koltunski
308
      mScreen.deleteObject(this,name);
309
      files.deleteIcon(this,name);
310
      files.deleteJsonObject(this,name);
311 d3d639b1 Leszek Koltunski
      }
312
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
315
    public void playObject(String name)
316
      {
317 88b94310 Leszek Koltunski
      Intent intent = new Intent(this, BandagedPlayActivity.class);
318
      intent.putExtra("name", name);
319
      startActivity(intent);
320 d3d639b1 Leszek Koltunski
      }
321
322 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324 7cb8d4b0 Leszek Koltunski
    public void iconCreationDone(Bitmap bmp)
325 9530f6b0 Leszek Koltunski
      {
326 20b60ad9 Leszek Koltunski
      mScreen.iconCreationDone(this,bmp);
327 9530f6b0 Leszek Koltunski
      }
328
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331
    public int getScreenWidthInPixels()
332
      {
333
      return mScreenWidth;
334
      }
335
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
338
    public int getScreenHeightInPixels()
339
      {
340
      return mScreenHeight;
341
      }
342
343 7da27d2b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
344
345
    public int getObjectOrdinal()
346
      {
347 5a4ee169 leszek
      return mObjectOrdinal;
348 7da27d2b leszek
      }
349 9530f6b0 Leszek Koltunski
}