Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 7fe59aa5

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

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

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

    
81
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
82

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

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

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

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

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

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

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

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

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

    
137
        decorView.setSystemUiVisibility(FLAGS);
138

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// do not avoid cutouts
155

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

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

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

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

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

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

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

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

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// PUBLIC API
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

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

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

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

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

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

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

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

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

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

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

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

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

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

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

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

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

    
350
      return 3;
351
      }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

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

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