Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 9fd4c04a

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