Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 2876aeb6

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
49
    public static final float SPINNER_TEXT_SIZE  = 0.03f;
50

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

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

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

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

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

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

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

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

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

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

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

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

    
130
        decorView.setSystemUiVisibility(FLAGS);
131

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
// do not avoid cutouts
148

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

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

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

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

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

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

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

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

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

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

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// PUBLIC API
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

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

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

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

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

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

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

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

    
285
        if( !object.getError() )
286
          {
287
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
288
          SharedPreferences.Editor editor = preferences.edit();
289
          object.removePreferences(editor);
290
          editor.apply();
291
          }
292
        }
293

    
294
      mScreen.deleteObject(this,name);
295
      files.deleteIcon(this,name);
296
      files.deleteJsonObject(this,name);
297
      }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    public void iconCreationDone(Bitmap bmp)
311
      {
312
      mScreen.iconCreationDone(this,bmp);
313
      }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
    public int getScreenWidthInPixels()
318
      {
319
      return mScreenWidth;
320
      }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
    public int getScreenHeightInPixels()
325
      {
326
      return mScreenHeight;
327
      }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

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

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
    public static int getDrawable(int small, int medium, int big, int huge)
342
      {
343
      int size = getDrawableSize();
344

    
345
      switch(size)
346
        {
347
        case 0 : return small;
348
        case 1 : return medium;
349
        case 2 : return big;
350
        default: return huge;
351
        }
352
      }
353
}
(1-1/13)