Project

General

Profile

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

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

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