Project

General

Profile

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

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

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
import android.util.DisplayMetrics;
23
import android.view.View;
24
import android.view.ViewGroup;
25
import android.view.WindowManager;
26
import android.widget.LinearLayout;
27
28
import androidx.appcompat.app.AppCompatActivity;
29 ada8ab26 leszek
import androidx.preference.PreferenceManager;
30 9530f6b0 Leszek Koltunski
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 1c04d054 leszek
import org.distorted.main.MainActivity;
35 9530f6b0 Leszek Koltunski
import org.distorted.main.R;
36 1c04d054 leszek
import org.distorted.main_old.RubikActivity;
37 9234018b Leszek Koltunski
import org.distorted.objectlib.main.InitAssets;
38 707f79ff Leszek Koltunski
import org.distorted.objectlib.main.TwistyJson;
39
import org.distorted.objectlib.main.TwistyObject;
40 21a1bb5d Leszek Koltunski
import org.distorted.os.OSInterface;
41 9530f6b0 Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
public class BandagedCreatorActivity extends AppCompatActivity
45
{
46 ada8ab26 leszek
    private static final int ACTIVITY_NUMBER    = 3;
47 1c04d054 leszek
    private static final float RATIO_BAR        = MainActivity.RATIO_BAR;
48 ada8ab26 leszek
    private static final float RATIO_BUT        = 0.07f;
49
    static final float RATIO_SCROLL             = 0.30f;
50
    public static final float SPINNER_TEXT_SIZE = 0.03f;
51
    public static final int FLAGS               = RubikActivity.FLAGS;
52 9530f6b0 Leszek Koltunski
53
    private static int mScreenWidth, mScreenHeight;
54
    private int mCurrentApiVersion;
55
    private BandagedCreatorScreen mScreen;
56 2ceeb6b5 Leszek Koltunski
    private boolean mRTL;
57 5a4ee169 leszek
    private int mObjectOrdinal;
58 9530f6b0 Leszek Koltunski
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
    @Override
62
    protected void onCreate(Bundle savedState)
63
      {
64
      super.onCreate(savedState);
65 5597ccee leszek
66
      Bundle b = getIntent().getExtras();
67
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
68
69 9530f6b0 Leszek Koltunski
      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 b3786da4 leszek
190 9530f6b0 Leszek Koltunski
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
191 48314d6a Leszek Koltunski
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
192 9530f6b0 Leszek Koltunski
      view.onResume();
193
194 7da27d2b leszek
      if( mScreen==null )
195
        {
196
        int ordinal = getObjectOrdinal();
197
        mScreen = new BandagedCreatorScreen(ordinal);
198
        }
199
200 bebd7af5 Leszek Koltunski
      mScreen.onAttachedToWindow(this);
201 7cb8d4b0 Leszek Koltunski
202 a41e3c94 Leszek Koltunski
      restorePreferences();
203 7cb8d4b0 Leszek Koltunski
      BandagedCreatorWorkerThread.create(this);
204 9530f6b0 Leszek Koltunski
      }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
    
208
    @Override
209
    protected void onDestroy() 
210
      {
211
      super.onDestroy();
212
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
213
      }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
    void OpenGLError()
218
      {
219
      RubikDialogError errDiag = new RubikDialogError();
220
      errDiag.show(getSupportFragmentManager(), null);
221
      }
222
223 13a3dfa9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225
    private void savePreferences()
226
      {
227
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
228
      SharedPreferences.Editor editor = preferences.edit();
229 b9d062cf Leszek Koltunski
      mScreen.savePreferences(editor);
230 13a3dfa9 Leszek Koltunski
      editor.apply();
231
      }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235
    private void restorePreferences()
236
      {
237
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
238 b9d062cf Leszek Koltunski
      mScreen.restorePreferences(this,preferences);
239 13a3dfa9 Leszek Koltunski
      }
240
241 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// PUBLIC API
243 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
    public void changeObject(int x, int y, int z)
246
      {
247
      BandagedCreatorRenderer renderer = getRenderer();
248
      renderer.changeObject(x,y,z);
249
      }
250
251 2ceeb6b5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    public boolean isRTL()
254
      {
255
      return mRTL;
256
      }
257
258 50ec342b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260
    public BandagedCreatorRenderer getRenderer()
261
      {
262
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
263
      return view.getRenderer();
264
      }
265
266 bfb59352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268
    public boolean objectDoesntExist(String name)
269
      {
270
      return mScreen.objectDoesntExist(name);
271
      }
272
273 e48ad1af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
    public void addObject(String name)
276
      {
277 bfb59352 Leszek Koltunski
      mScreen.addObject(this,name);
278 e48ad1af Leszek Koltunski
      }
279
280 d3d639b1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282 85770de9 Leszek Koltunski
    public void deleteObject(String name)
283 d3d639b1 Leszek Koltunski
      {
284 81493402 Leszek Koltunski
      RubikFiles files = RubikFiles.getInstance();
285 707f79ff Leszek Koltunski
      InputStream jsonStream = files.openFile(this,name+"_object.json");
286 2876aeb6 Leszek Koltunski
      InitAssets assets = new InitAssets(jsonStream,null,null);
287 707f79ff Leszek Koltunski
288 9234018b Leszek Koltunski
      if( !assets.noJsonStream() )
289 213c15de Leszek Koltunski
        {
290 337f4660 leszek
        TwistyObject object = new TwistyJson( TwistyObject.MODE_NORM, null, null, 1.0f, assets);
291 a8be1a23 Leszek Koltunski
292
        if( !object.getError() )
293
          {
294
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
295
          SharedPreferences.Editor editor = preferences.edit();
296 88451205 Leszek Koltunski
          OSInterface os = new OSInterface(this,null);
297 05e4c778 Leszek Koltunski
          os.setEditor(editor);
298 21a1bb5d Leszek Koltunski
          object.removePreferences(os);
299 a8be1a23 Leszek Koltunski
          editor.apply();
300
          }
301 213c15de Leszek Koltunski
        }
302 707f79ff Leszek Koltunski
303
      mScreen.deleteObject(this,name);
304
      files.deleteIcon(this,name);
305
      files.deleteJsonObject(this,name);
306 d3d639b1 Leszek Koltunski
      }
307
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
    public void playObject(String name)
311
      {
312 88b94310 Leszek Koltunski
      Intent intent = new Intent(this, BandagedPlayActivity.class);
313
      intent.putExtra("name", name);
314
      startActivity(intent);
315 d3d639b1 Leszek Koltunski
      }
316
317 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
318
319 7cb8d4b0 Leszek Koltunski
    public void iconCreationDone(Bitmap bmp)
320 9530f6b0 Leszek Koltunski
      {
321 20b60ad9 Leszek Koltunski
      mScreen.iconCreationDone(this,bmp);
322 9530f6b0 Leszek Koltunski
      }
323
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326
    public int getScreenWidthInPixels()
327
      {
328
      return mScreenWidth;
329
      }
330
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
    public int getScreenHeightInPixels()
334
      {
335
      return mScreenHeight;
336
      }
337
338 7da27d2b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
    public int getObjectOrdinal()
341
      {
342 5a4ee169 leszek
      return mObjectOrdinal;
343 7da27d2b leszek
      }
344 9530f6b0 Leszek Koltunski
}