Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ c7238c67

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 org.distorted.dialogs.RubikDialogError;
29
import org.distorted.external.RubikFiles;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.main.R;
32
import org.distorted.main.RubikActivity;
33
import org.distorted.objectlib.main.TwistyJson;
34
import org.distorted.objectlib.main.TwistyObject;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

    
45
    public static final float DIALOG_BUTTON_SIZE = 0.06f;
46
    public static final float MENU_BIG_TEXT_SIZE = 0.05f;
47
    public static final float SPINNER_TEXT_SIZE  = 0.03f;
48

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

    
55
    private static int mScreenWidth, mScreenHeight;
56
    private int mCurrentApiVersion;
57
    private BandagedCreatorScreen mScreen;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
69
      DisplayMetrics displaymetrics = new DisplayMetrics();
70
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
71
      mScreenWidth =displaymetrics.widthPixels;
72
      mScreenHeight=displaymetrics.heightPixels;
73

    
74
      hideNavigationBar();
75
      cutoutHack();
76
      computeHeights();
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// this does not include possible insets
81

    
82
    private void computeHeights()
83
      {
84
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
85
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
86
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
87
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
88
      int padding      = (int)(mScreenHeight*RubikActivity.PADDING);
89

    
90
      LinearLayout botLayout = findViewById(R.id.lowerBar);
91
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
92
      paramsL.height = barHeight;
93
      botLayout.setLayoutParams(paramsL);
94

    
95
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
96
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
97
      paramsB.height = butHeight;
98
      butLayout.setPadding(padding,0,padding,padding);
99
      butLayout.setLayoutParams(paramsB);
100

    
101
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
102
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
103
      paramsT.height = viewHeight;
104
      topLayout.setPadding(padding,padding,padding,padding);
105
      topLayout.setLayoutParams(paramsT);
106

    
107
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
108
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
109
      paramsC.height = objectHeight;
110
      creator.setLayoutParams(paramsC);
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
    private void hideNavigationBar()
116
      {
117
      mCurrentApiVersion = Build.VERSION.SDK_INT;
118

    
119
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
120
        {
121
        final View decorView = getWindow().getDecorView();
122

    
123
        decorView.setSystemUiVisibility(FLAGS);
124

    
125
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
126
          {
127
          @Override
128
          public void onSystemUiVisibilityChange(int visibility)
129
            {
130
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
131
              {
132
              decorView.setSystemUiVisibility(FLAGS);
133
              }
134
            }
135
          });
136
        }
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
// do not avoid cutouts
141

    
142
    private void cutoutHack()
143
      {
144
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
145
        {
146
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    @Override
153
    public void onWindowFocusChanged(boolean hasFocus)
154
      {
155
      super.onWindowFocusChanged(hasFocus);
156

    
157
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
158
        {
159
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
160
        }
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
    @Override
166
    protected void onPause() 
167
      {
168
      super.onPause();
169
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
170
      view.onPause();
171
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
172
      savePreferences();
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177
    @Override
178
    protected void onResume() 
179
      {
180
      super.onResume();
181
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
182
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
183
      view.onResume();
184

    
185
      if( mScreen==null ) mScreen = new BandagedCreatorScreen();
186
      mScreen.onAttachedToWindow(this);
187

    
188
      restorePreferences();
189
      BandagedCreatorWorkerThread.create(this);
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    @Override
195
    protected void onDestroy() 
196
      {
197
      super.onDestroy();
198
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    void OpenGLError()
204
      {
205
      RubikDialogError errDiag = new RubikDialogError();
206
      errDiag.show(getSupportFragmentManager(), null);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    private void savePreferences()
212
      {
213
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
214
      SharedPreferences.Editor editor = preferences.edit();
215
      mScreen.savePreferences(editor);
216
      editor.apply();
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    private void restorePreferences()
222
      {
223
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
224
      mScreen.restorePreferences(this,preferences);
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
// PUBLIC API
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    public void changeObject(int x, int y, int z)
232
      {
233
      BandagedCreatorRenderer renderer = getRenderer();
234
      renderer.changeObject(x,y,z);
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
    public BandagedCreatorRenderer getRenderer()
240
      {
241
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
242
      return view.getRenderer();
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public boolean objectDoesntExist(String name)
248
      {
249
      return mScreen.objectDoesntExist(name);
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public void addObject(String name)
255
      {
256
      mScreen.addObject(this,name);
257
      }
258

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

    
261
    public void deleteObject(String name)
262
      {
263
      RubikFiles files = RubikFiles.getInstance();
264
      InputStream jsonStream = files.openFile(this,name+"_object.json");
265

    
266
      if( jsonStream!=null )
267
        {
268
        int meshState= TwistyObject.MESH_NICE;
269
        int iconMode = TwistyObject.MODE_NORM;
270
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
271
        SharedPreferences.Editor editor = preferences.edit();
272
        TwistyObject object = new TwistyJson( jsonStream, meshState, iconMode, null, null, 1.0f, null);
273
        if( !object.getError() ) object.removePreferences(editor);
274
        editor.apply();
275
        }
276

    
277
      mScreen.deleteObject(this,name);
278
      files.deleteIcon(this,name);
279
      files.deleteJsonObject(this,name);
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public void playObject(String name)
285
      {
286
      Intent intent = new Intent(this, BandagedPlayActivity.class);
287
      intent.putExtra("name", name);
288
      startActivity(intent);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public void iconCreationDone(Bitmap bmp)
294
      {
295
      mScreen.iconCreationDone(this,bmp);
296
      }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
    public int getScreenWidthInPixels()
301
      {
302
      return mScreenWidth;
303
      }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
    public int getScreenHeightInPixels()
308
      {
309
      return mScreenHeight;
310
      }
311

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

    
314
    public static int getDrawableSize()
315
      {
316
      if( mScreenHeight<1000 )
317
        {
318
        return 0;
319
        }
320
      if( mScreenHeight<1600 )
321
        {
322
        return 1;
323
        }
324
      if( mScreenHeight<1900 )
325
        {
326
        return 2;
327
        }
328

    
329
      return 3;
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    public static int getDrawable(int small, int medium, int big, int huge)
335
      {
336
      int size = getDrawableSize();
337

    
338
      switch(size)
339
        {
340
        case 0 : return small;
341
        case 1 : return medium;
342
        case 2 : return big;
343
        default: return huge;
344
        }
345
      }
346
}
(1-1/13)