Project

General

Profile

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

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

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