Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 70688a23

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

    
195
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
196
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
197
      view.onResume();
198

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

    
205
      mScreen.onAttachedToWindow(this);
206

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

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

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
// PUBLIC API
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

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

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

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

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

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

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

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

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

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

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

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

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

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
    public int getObjectOrdinal()
346
      {
347
      return mObjectOrdinal;
348
      }
349
}
(1-1/12)