Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 306aa049

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 java.io.InputStream;
13

    
14
import android.content.Intent;
15
import android.content.SharedPreferences;
16
import android.graphics.Bitmap;
17
import android.os.Build;
18
import android.os.Bundle;
19
import android.preference.PreferenceManager;
20
import android.util.DisplayMetrics;
21
import android.view.View;
22
import android.view.ViewGroup;
23
import android.view.WindowManager;
24
import android.widget.LinearLayout;
25

    
26
import androidx.appcompat.app.AppCompatActivity;
27

    
28
import com.google.firebase.analytics.FirebaseAnalytics;
29

    
30
import org.distorted.dialogs.RubikDialogError;
31
import org.distorted.external.RubikFiles;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.main.R;
34
import org.distorted.main.RubikActivity;
35
import org.distorted.objectlib.main.TwistyJson;
36
import org.distorted.objectlib.main.TwistyObject;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

    
47
    public static final float DIALOG_BUTTON_SIZE = 0.06f;
48
    public static final float MENU_BIG_TEXT_SIZE = 0.05f;
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 FirebaseAnalytics mFirebaseAnalytics;
58
    private static int mScreenWidth, mScreenHeight;
59
    private int mCurrentApiVersion;
60
    private BandagedCreatorScreen mScreen;
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
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
73

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

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

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

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

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

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

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

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

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

    
128
        decorView.setSystemUiVisibility(FLAGS);
129

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// do not avoid cutouts
146

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

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

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

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

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

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

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
// PUBLIC API
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
    public FirebaseAnalytics getAnalytics()
237
      {
238
      return mFirebaseAnalytics;
239
      }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
    public void changeObject(int x, int y, int z)
244
      {
245
      BandagedCreatorRenderer renderer = getRenderer();
246
      renderer.changeObject(x,y,z);
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

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

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

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

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

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

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

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

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
    public static int getDrawableSize()
327
      {
328
      if( mScreenHeight<1000 )
329
        {
330
        return 0;
331
        }
332
      if( mScreenHeight<1600 )
333
        {
334
        return 1;
335
        }
336
      if( mScreenHeight<1900 )
337
        {
338
        return 2;
339
        }
340

    
341
      return 3;
342
      }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

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

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