Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 5a4ee169

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import static android.view.View.LAYOUT_DIRECTION_RTL;
13

    
14
import java.io.InputStream;
15

    
16
import android.content.Intent;
17
import android.content.SharedPreferences;
18
import android.content.res.Configuration;
19
import android.graphics.Bitmap;
20
import android.os.Build;
21
import android.os.Bundle;
22
import android.preference.PreferenceManager;
23
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
import org.distorted.external.RubikFiles;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.main.R;
35
import org.distorted.main.RubikActivity;
36
import org.distorted.objectlib.main.InitAssets;
37
import org.distorted.objectlib.main.TwistyJson;
38
import org.distorted.objectlib.main.TwistyObject;
39
import org.distorted.os.OSInterface;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class BandagedCreatorActivity extends AppCompatActivity
44
{
45
    private static final int ACTIVITY_NUMBER = 3;
46
    private static final float RATIO_BAR   = 0.10f;
47
    private static final float RATIO_BUT   = 0.07f;
48
    static final float RATIO_SCROLL= 0.30f;
49

    
50
    public static final float SPINNER_TEXT_SIZE  = 0.03f;
51

    
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
    private boolean mRTL;
62
    private int mObjectOrdinal;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    @Override
67
    protected void onCreate(Bundle savedState)
68
      {
69
      super.onCreate(savedState);
70
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
71
      setTheme(R.style.MaterialThemeNoActionBar);
72
      setContentView(R.layout.bandaged);
73

    
74
      DisplayMetrics displaymetrics = new DisplayMetrics();
75
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
76
      mScreenWidth =displaymetrics.widthPixels;
77
      mScreenHeight=displaymetrics.heightPixels;
78

    
79
      final Configuration config = getResources().getConfiguration();
80
      final int layoutDirection = config.getLayoutDirection();
81
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
82

    
83
      Bundle b = getIntent().getExtras();
84
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
85

    
86
      hideNavigationBar();
87
      cutoutHack();
88
      computeHeights();
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// this does not include possible insets
93

    
94
    private void computeHeights()
95
      {
96
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
97
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
98
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
99
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
100
      int padding      = (int)(mScreenHeight*RubikActivity.PADDING);
101

    
102
      LinearLayout botLayout = findViewById(R.id.lowerBar);
103
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
104
      paramsL.height = barHeight;
105
      botLayout.setLayoutParams(paramsL);
106

    
107
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
108
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
109
      paramsB.height = butHeight;
110
      butLayout.setPadding(padding,0,padding,padding);
111
      butLayout.setLayoutParams(paramsB);
112

    
113
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
114
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
115
      paramsT.height = viewHeight;
116
      topLayout.setPadding(padding,padding,padding,padding);
117
      topLayout.setLayoutParams(paramsT);
118

    
119
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
120
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
121
      paramsC.height = objectHeight;
122
      creator.setLayoutParams(paramsC);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    private void hideNavigationBar()
128
      {
129
      mCurrentApiVersion = Build.VERSION.SDK_INT;
130

    
131
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
132
        {
133
        final View decorView = getWindow().getDecorView();
134

    
135
        decorView.setSystemUiVisibility(FLAGS);
136

    
137
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
138
          {
139
          @Override
140
          public void onSystemUiVisibilityChange(int visibility)
141
            {
142
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
143
              {
144
              decorView.setSystemUiVisibility(FLAGS);
145
              }
146
            }
147
          });
148
        }
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// do not avoid cutouts
153

    
154
    private void cutoutHack()
155
      {
156
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
157
        {
158
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
    @Override
165
    public void onWindowFocusChanged(boolean hasFocus)
166
      {
167
      super.onWindowFocusChanged(hasFocus);
168

    
169
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
170
        {
171
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
172
        }
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177
    @Override
178
    protected void onPause() 
179
      {
180
      super.onPause();
181
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
182
      view.onPause();
183
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
184
      savePreferences();
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189
    @Override
190
    protected void onResume() 
191
      {
192
      super.onResume();
193
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
194
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
195
      view.onResume();
196

    
197
      if( mScreen==null )
198
        {
199
        int ordinal = getObjectOrdinal();
200
        mScreen = new BandagedCreatorScreen(ordinal);
201
        }
202

    
203
      mScreen.onAttachedToWindow(this);
204

    
205
      restorePreferences();
206
      BandagedCreatorWorkerThread.create(this);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211
    @Override
212
    protected void onDestroy() 
213
      {
214
      super.onDestroy();
215
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    void OpenGLError()
221
      {
222
      RubikDialogError errDiag = new RubikDialogError();
223
      errDiag.show(getSupportFragmentManager(), null);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    private void savePreferences()
229
      {
230
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
231
      SharedPreferences.Editor editor = preferences.edit();
232
      mScreen.savePreferences(editor);
233
      editor.apply();
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    private void restorePreferences()
239
      {
240
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
241
      mScreen.restorePreferences(this,preferences);
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
// PUBLIC API
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
    public void changeObject(int x, int y, int z)
249
      {
250
      BandagedCreatorRenderer renderer = getRenderer();
251
      renderer.changeObject(x,y,z);
252
      }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
    public boolean isRTL()
257
      {
258
      return mRTL;
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    public BandagedCreatorRenderer getRenderer()
264
      {
265
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
266
      return view.getRenderer();
267
      }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
    public boolean objectDoesntExist(String name)
272
      {
273
      return mScreen.objectDoesntExist(name);
274
      }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
    public void addObject(String name)
279
      {
280
      mScreen.addObject(this,name);
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
    public void deleteObject(String name)
286
      {
287
      RubikFiles files = RubikFiles.getInstance();
288
      InputStream jsonStream = files.openFile(this,name+"_object.json");
289
      InitAssets assets = new InitAssets(jsonStream,null,null);
290

    
291
      if( !assets.noJsonStream() )
292
        {
293
        TwistyObject object = new TwistyJson( TwistyObject.MESH_NICE, TwistyObject.MODE_NORM, null, null, 1.0f, assets);
294

    
295
        if( !object.getError() )
296
          {
297
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
298
          SharedPreferences.Editor editor = preferences.edit();
299
          OSInterface os = new OSInterface(this,null);
300
          os.setEditor(editor);
301
          object.removePreferences(os);
302
          editor.apply();
303
          }
304
        }
305

    
306
      mScreen.deleteObject(this,name);
307
      files.deleteIcon(this,name);
308
      files.deleteJsonObject(this,name);
309
      }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
    public void playObject(String name)
314
      {
315
      Intent intent = new Intent(this, BandagedPlayActivity.class);
316
      intent.putExtra("name", name);
317
      startActivity(intent);
318
      }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    public void iconCreationDone(Bitmap bmp)
323
      {
324
      mScreen.iconCreationDone(this,bmp);
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    public int getScreenWidthInPixels()
330
      {
331
      return mScreenWidth;
332
      }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
    public int getScreenHeightInPixels()
337
      {
338
      return mScreenHeight;
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
    public int getObjectOrdinal()
344
      {
345
      return mObjectOrdinal;
346
      }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
    public static int getDrawableSize()
351
      {
352
      if( mScreenHeight<1000 ) return 0;
353
      if( mScreenHeight<1600 ) return 1;
354
      if( mScreenHeight<1900 ) return 2;
355
      return 3;
356
      }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
    public static int getDrawable(int small, int medium, int big, int huge)
361
      {
362
      int size = getDrawableSize();
363

    
364
      switch(size)
365
        {
366
        case 0 : return small;
367
        case 1 : return medium;
368
        case 2 : return big;
369
        default: return huge;
370
        }
371
      }
372
}
(1-1/12)