Project

General

Profile

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

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

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

    
82
      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
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
94
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
95
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
96
      int padding      = (int)(mScreenHeight*RubikActivity.PADDING);
97

    
98
      LinearLayout botLayout = findViewById(R.id.lowerBar);
99
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
100
      paramsL.height = barHeight;
101
      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

    
109
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
110
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
111
      paramsT.height = viewHeight;
112
      topLayout.setPadding(padding,padding,padding,padding);
113
      topLayout.setLayoutParams(paramsT);
114

    
115
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
116
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
117
      paramsC.height = objectHeight;
118
      creator.setLayoutParams(paramsC);
119
      }
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
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
178
      view.onPause();
179
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
180
      savePreferences();
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    @Override
186
    protected void onResume() 
187
      {
188
      super.onResume();
189
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
190
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
191
      view.onResume();
192

    
193
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
194
      mScreen.onAttachedToWindow(this);
195

    
196
      restorePreferences();
197
      BandagedCreatorWorkerThread.create(this);
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
    
202
    @Override
203
    protected void onDestroy() 
204
      {
205
      super.onDestroy();
206
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    void OpenGLError()
212
      {
213
      RubikDialogError errDiag = new RubikDialogError();
214
      errDiag.show(getSupportFragmentManager(), null);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    private void savePreferences()
220
      {
221
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
222
      SharedPreferences.Editor editor = preferences.edit();
223
      mScreen.savePreferences(editor);
224
      editor.apply();
225
      }
226

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

    
229
    private void restorePreferences()
230
      {
231
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
232
      mScreen.restorePreferences(this,preferences);
233
      }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
// PUBLIC API
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
    public void changeObject(int x, int y, int z)
240
      {
241
      BandagedCreatorRenderer renderer = getRenderer();
242
      renderer.changeObject(x,y,z);
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public boolean isRTL()
248
      {
249
      return mRTL;
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public BandagedCreatorRenderer getRenderer()
255
      {
256
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
257
      return view.getRenderer();
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public boolean objectDoesntExist(String name)
263
      {
264
      return mScreen.objectDoesntExist(name);
265
      }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public void addObject(String name)
270
      {
271
      mScreen.addObject(this,name);
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public void deleteObject(String name)
277
      {
278
      RubikFiles files = RubikFiles.getInstance();
279
      InputStream jsonStream = files.openFile(this,name+"_object.json");
280
      InitAssets assets = new InitAssets(jsonStream,null,null);
281

    
282
      if( !assets.noJsonStream() )
283
        {
284
        TwistyObject object = new TwistyJson( TwistyObject.MESH_NICE, TwistyObject.MODE_NORM, null, null, 1.0f, assets);
285

    
286
        if( !object.getError() )
287
          {
288
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
289
          SharedPreferences.Editor editor = preferences.edit();
290
          OSInterface os = new OSInterface(this,null);
291
          os.setEditor(editor);
292
          object.removePreferences(os);
293
          editor.apply();
294
          }
295
        }
296

    
297
      mScreen.deleteObject(this,name);
298
      files.deleteIcon(this,name);
299
      files.deleteJsonObject(this,name);
300
      }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
    public void playObject(String name)
305
      {
306
      Intent intent = new Intent(this, BandagedPlayActivity.class);
307
      intent.putExtra("name", name);
308
      startActivity(intent);
309
      }
310

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

    
313
    public void iconCreationDone(Bitmap bmp)
314
      {
315
      mScreen.iconCreationDone(this,bmp);
316
      }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
    public int getScreenWidthInPixels()
321
      {
322
      return mScreenWidth;
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    public int getScreenHeightInPixels()
328
      {
329
      return mScreenHeight;
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    public static int getDrawableSize()
335
      {
336
      if( mScreenHeight<1000 ) return 0;
337
      if( mScreenHeight<1600 ) return 1;
338
      if( mScreenHeight<1900 ) return 2;
339
      return 3;
340
      }
341

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

    
344
    public static int getDrawable(int small, int medium, int big, int huge)
345
      {
346
      int size = getDrawableSize();
347

    
348
      switch(size)
349
        {
350
        case 0 : return small;
351
        case 1 : return medium;
352
        case 2 : return big;
353
        default: return huge;
354
        }
355
      }
356
}
(1-1/14)