Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 39176a1f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.bandaged;
21

    
22
import java.io.InputStream;
23

    
24
import android.content.Intent;
25
import android.content.SharedPreferences;
26
import android.graphics.Bitmap;
27
import android.os.Build;
28
import android.os.Bundle;
29
import android.preference.PreferenceManager;
30
import android.util.DisplayMetrics;
31
import android.view.View;
32
import android.view.ViewGroup;
33
import android.view.WindowManager;
34
import android.widget.LinearLayout;
35

    
36
import androidx.appcompat.app.AppCompatActivity;
37

    
38
import com.google.firebase.analytics.FirebaseAnalytics;
39

    
40
import org.distorted.dialogs.RubikDialogError;
41
import org.distorted.external.RubikFiles;
42
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45
import org.distorted.objectlib.main.TwistyJson;
46
import org.distorted.objectlib.main.TwistyObject;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class BandagedCreatorActivity extends AppCompatActivity
51
{
52
    private static final int ACTIVITY_NUMBER = 3;
53
    private static final float RATIO_BAR   = 0.10f;
54
    private static final float RATIO_BUT   = 0.07f;
55
    static final float RATIO_SCROLL= 0.30f;
56

    
57
    public static final float DIALOG_BUTTON_SIZE = 0.06f;
58
    public static final float MENU_BIG_TEXT_SIZE = 0.05f;
59
    public static final float SPINNER_TEXT_SIZE  = 0.03f;
60

    
61
    public static final int FLAGS =  View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
62
                                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
63
                                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
64
                                   | View.SYSTEM_UI_FLAG_FULLSCREEN
65
                                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
66

    
67
    private FirebaseAnalytics mFirebaseAnalytics;
68
    private static int mScreenWidth, mScreenHeight;
69
    private int mCurrentApiVersion;
70
    private BandagedCreatorScreen mScreen;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
    @Override
75
    protected void onCreate(Bundle savedState)
76
      {
77
      super.onCreate(savedState);
78
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
79
      setTheme(R.style.MaterialThemeNoActionBar);
80
      setContentView(R.layout.bandaged);
81

    
82
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
83

    
84
      DisplayMetrics displaymetrics = new DisplayMetrics();
85
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
86
      mScreenWidth =displaymetrics.widthPixels;
87
      mScreenHeight=displaymetrics.heightPixels;
88

    
89
      hideNavigationBar();
90
      cutoutHack();
91
      computeHeights();
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
// this does not include possible insets
96

    
97
    private void computeHeights()
98
      {
99
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
100
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
101
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
102
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
103
      int padding      = (int)(mScreenHeight*RubikActivity.PADDING);
104

    
105
      LinearLayout botLayout = findViewById(R.id.lowerBar);
106
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
107
      paramsL.height = barHeight;
108
      botLayout.setLayoutParams(paramsL);
109

    
110
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
111
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
112
      paramsB.height = butHeight;
113
      butLayout.setPadding(padding,0,padding,padding);
114
      butLayout.setLayoutParams(paramsB);
115

    
116
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
117
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
118
      paramsT.height = viewHeight;
119
      topLayout.setPadding(padding,padding,padding,padding);
120
      topLayout.setLayoutParams(paramsT);
121

    
122
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
123
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
124
      paramsC.height = objectHeight;
125
      creator.setLayoutParams(paramsC);
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    private void hideNavigationBar()
131
      {
132
      mCurrentApiVersion = Build.VERSION.SDK_INT;
133

    
134
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
135
        {
136
        final View decorView = getWindow().getDecorView();
137

    
138
        decorView.setSystemUiVisibility(FLAGS);
139

    
140
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
141
          {
142
          @Override
143
          public void onSystemUiVisibilityChange(int visibility)
144
            {
145
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
146
              {
147
              decorView.setSystemUiVisibility(FLAGS);
148
              }
149
            }
150
          });
151
        }
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
// do not avoid cutouts
156

    
157
    private void cutoutHack()
158
      {
159
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
160
        {
161
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
162
        }
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    @Override
168
    public void onWindowFocusChanged(boolean hasFocus)
169
      {
170
      super.onWindowFocusChanged(hasFocus);
171

    
172
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
173
        {
174
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
175
        }
176
      }
177

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
    
192
    @Override
193
    protected void onResume() 
194
      {
195
      super.onResume();
196
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
197
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
198
      view.onResume();
199

    
200
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
201
      mScreen.onAttachedToWindow(this);
202

    
203
      restorePreferences();
204
      BandagedCreatorWorkerThread.create(this);
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
    
209
    @Override
210
    protected void onDestroy() 
211
      {
212
      super.onDestroy();
213
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
214
      }
215

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

    
218
    void OpenGLError()
219
      {
220
      RubikDialogError errDiag = new RubikDialogError();
221
      errDiag.show(getSupportFragmentManager(), null);
222
      }
223

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

    
226
    private void savePreferences()
227
      {
228
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
229
      SharedPreferences.Editor editor = preferences.edit();
230
      mScreen.savePreferences(editor);
231
      editor.apply();
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
    private void restorePreferences()
237
      {
238
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
239
      mScreen.restorePreferences(this,preferences);
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
// PUBLIC API
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    public FirebaseAnalytics getAnalytics()
247
      {
248
      return mFirebaseAnalytics;
249
      }
250

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

    
253
    public void changeObject(int x, int y, int z)
254
      {
255
      BandagedCreatorRenderer renderer = getRenderer();
256
      renderer.changeObject(x,y,z);
257
      }
258

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

    
261
    public BandagedCreatorRenderer getRenderer()
262
      {
263
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
264
      return view.getRenderer();
265
      }
266

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

    
269
    public boolean objectDoesntExist(String name)
270
      {
271
      return mScreen.objectDoesntExist(name);
272
      }
273

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

    
276
    public void addObject(String name)
277
      {
278
      mScreen.addObject(this,name);
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
    public void deleteObject(String name)
284
      {
285
      RubikFiles files = RubikFiles.getInstance();
286
      InputStream jsonStream = files.openFile(this,name+"_object.json");
287

    
288
      if( jsonStream!=null )
289
        {
290
        int meshState= TwistyObject.MESH_NICE;
291
        int iconMode = TwistyObject.MODE_NORM;
292
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
293
        SharedPreferences.Editor editor = preferences.edit();
294
        TwistyObject object = new TwistyJson( jsonStream, meshState, iconMode, null, null, 1.0f, null);
295
        object.removePreferences(editor);
296
        editor.apply();
297
        }
298

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

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

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

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

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

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
    public int getScreenWidthInPixels()
323
      {
324
      return mScreenWidth;
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
    public int getScreenHeightInPixels()
330
      {
331
      return mScreenHeight;
332
      }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
    public static int getDrawableSize()
337
      {
338
      if( mScreenHeight<1000 )
339
        {
340
        return 0;
341
        }
342
      if( mScreenHeight<1600 )
343
        {
344
        return 1;
345
        }
346
      if( mScreenHeight<1900 )
347
        {
348
        return 2;
349
        }
350

    
351
      return 3;
352
      }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
    public static int getDrawable(int small, int medium, int big, int huge)
357
      {
358
      int size = getDrawableSize();
359

    
360
      switch(size)
361
        {
362
        case 0 : return small;
363
        case 1 : return medium;
364
        case 2 : return big;
365
        default: return huge;
366
        }
367
      }
368
}
(1-1/13)