Project

General

Profile

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

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

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

    
71
      Bundle b = getIntent().getExtras();
72
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
73

    
74
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
75
      setTheme(R.style.MaterialThemeNoActionBar);
76
      setContentView(R.layout.bandaged);
77

    
78
      DisplayMetrics displaymetrics = new DisplayMetrics();
79
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
80
      mScreenWidth =displaymetrics.widthPixels;
81
      mScreenHeight=displaymetrics.heightPixels;
82

    
83
      final Configuration config = getResources().getConfiguration();
84
      final int layoutDirection = config.getLayoutDirection();
85
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
86

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

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

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

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

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

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

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

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

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

    
136
        decorView.setSystemUiVisibility(FLAGS);
137

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
// do not avoid cutouts
154

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

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

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

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

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

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

    
204
      mScreen.onAttachedToWindow(this);
205

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

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

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
// PUBLIC API
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

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

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

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

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

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

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

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

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

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

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

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

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