Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 50e6c5d6

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.TwistyJson;
37
import org.distorted.objectlib.main.TwistyObject;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
48
    public static final float SPINNER_TEXT_SIZE  = 0.03f;
49

    
50
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
51
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
52
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
53
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
54
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
55

    
56
    private static int mScreenWidth, mScreenHeight;
57
    private int mCurrentApiVersion;
58
    private BandagedCreatorScreen mScreen;
59
    private boolean mRTL;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

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

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

    
80
      hideNavigationBar();
81
      cutoutHack();
82
      computeHeights();
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
// this does not include possible insets
87

    
88
    private void computeHeights()
89
      {
90
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
91
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
92
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
93
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
94
      int padding      = (int)(mScreenHeight*RubikActivity.PADDING);
95

    
96
      LinearLayout botLayout = findViewById(R.id.lowerBar);
97
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
98
      paramsL.height = barHeight;
99
      botLayout.setLayoutParams(paramsL);
100

    
101
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
102
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
103
      paramsB.height = butHeight;
104
      butLayout.setPadding(padding,0,padding,padding);
105
      butLayout.setLayoutParams(paramsB);
106

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

    
113
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
114
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
115
      paramsC.height = objectHeight;
116
      creator.setLayoutParams(paramsC);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    private void hideNavigationBar()
122
      {
123
      mCurrentApiVersion = Build.VERSION.SDK_INT;
124

    
125
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
126
        {
127
        final View decorView = getWindow().getDecorView();
128

    
129
        decorView.setSystemUiVisibility(FLAGS);
130

    
131
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
132
          {
133
          @Override
134
          public void onSystemUiVisibilityChange(int visibility)
135
            {
136
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
137
              {
138
              decorView.setSystemUiVisibility(FLAGS);
139
              }
140
            }
141
          });
142
        }
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// do not avoid cutouts
147

    
148
    private void cutoutHack()
149
      {
150
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
151
        {
152
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
153
        }
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    @Override
159
    public void onWindowFocusChanged(boolean hasFocus)
160
      {
161
      super.onWindowFocusChanged(hasFocus);
162

    
163
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
164
        {
165
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
166
        }
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171
    @Override
172
    protected void onPause() 
173
      {
174
      super.onPause();
175
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
176
      view.onPause();
177
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
178
      savePreferences();
179
      }
180

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

    
191
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
192
      mScreen.onAttachedToWindow(this);
193

    
194
      restorePreferences();
195
      BandagedCreatorWorkerThread.create(this);
196
      }
197

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// PUBLIC API
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public boolean isRTL()
246
      {
247
      return mRTL;
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

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

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    public void deleteObject(String name)
275
      {
276
      RubikFiles files = RubikFiles.getInstance();
277
      InputStream jsonStream = files.openFile(this,name+"_object.json");
278

    
279
      if( jsonStream!=null )
280
        {
281
        int meshState= TwistyObject.MESH_NICE;
282
        int iconMode = TwistyObject.MODE_NORM;
283
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
284
        SharedPreferences.Editor editor = preferences.edit();
285
        TwistyObject object = new TwistyJson( jsonStream, meshState, iconMode, null, null, 1.0f, null);
286
        if( !object.getError() ) object.removePreferences(editor);
287
        editor.apply();
288
        }
289

    
290
      mScreen.deleteObject(this,name);
291
      files.deleteIcon(this,name);
292
      files.deleteJsonObject(this,name);
293
      }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
    public void playObject(String name)
298
      {
299
      Intent intent = new Intent(this, BandagedPlayActivity.class);
300
      intent.putExtra("name", name);
301
      startActivity(intent);
302
      }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
    public void iconCreationDone(Bitmap bmp)
307
      {
308
      mScreen.iconCreationDone(this,bmp);
309
      }
310

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

    
313
    public int getScreenWidthInPixels()
314
      {
315
      return mScreenWidth;
316
      }
317

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

    
320
    public int getScreenHeightInPixels()
321
      {
322
      return mScreenHeight;
323
      }
324

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

    
327
    public static int getDrawableSize()
328
      {
329
      if( mScreenHeight<1000 ) return 0;
330
      if( mScreenHeight<1600 ) return 1;
331
      if( mScreenHeight<1900 ) return 2;
332
      return 3;
333
      }
334

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

    
337
    public static int getDrawable(int small, int medium, int big, int huge)
338
      {
339
      int size = getDrawableSize();
340

    
341
      switch(size)
342
        {
343
        case 0 : return small;
344
        case 1 : return medium;
345
        case 2 : return big;
346
        default: return huge;
347
        }
348
      }
349
}
(1-1/13)