Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorActivity.java @ 337f4660

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.util.DisplayMetrics;
23
import android.view.View;
24
import android.view.ViewGroup;
25
import android.view.WindowManager;
26
import android.widget.LinearLayout;
27

    
28
import androidx.appcompat.app.AppCompatActivity;
29
import androidx.preference.PreferenceManager;
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.MainActivity;
35
import org.distorted.main.R;
36
import org.distorted.main_old.RubikActivity;
37
import org.distorted.objectlib.main.InitAssets;
38
import org.distorted.objectlib.main.TwistyJson;
39
import org.distorted.objectlib.main.TwistyObject;
40
import org.distorted.os.OSInterface;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class BandagedCreatorActivity extends AppCompatActivity
45
{
46
    private static final int ACTIVITY_NUMBER    = 3;
47
    private static final float RATIO_BAR        = MainActivity.RATIO_BAR;
48
    private static final float RATIO_BUT        = 0.07f;
49
    static final float RATIO_SCROLL             = 0.30f;
50
    public static final float SPINNER_TEXT_SIZE = 0.03f;
51
    public static final int FLAGS               = RubikActivity.FLAGS;
52

    
53
    private static int mScreenWidth, mScreenHeight;
54
    private int mCurrentApiVersion;
55
    private BandagedCreatorScreen mScreen;
56
    private boolean mRTL;
57
    private int mObjectOrdinal;
58

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

    
61
    @Override
62
    protected void onCreate(Bundle savedState)
63
      {
64
      super.onCreate(savedState);
65

    
66
      Bundle b = getIntent().getExtras();
67
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
68

    
69
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
70
      setTheme(R.style.MaterialThemeNoActionBar);
71
      setContentView(R.layout.bandaged);
72

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

    
78
      final Configuration config = getResources().getConfiguration();
79
      final int layoutDirection = config.getLayoutDirection();
80
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
81

    
82
      hideNavigationBar();
83
      cutoutHack();
84
      computeHeights();
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// this does not include possible insets
89

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

    
98
      LinearLayout botLayout = findViewById(R.id.lowerBar);
99
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
100
      paramsL.height = barHeight;
101
      botLayout.setLayoutParams(paramsL);
102

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

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

    
115
      BandagedCreatorView creator = findViewById(R.id.bandagedCreatorObjectView);
116
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
117
      paramsC.height = objectHeight;
118
      creator.setLayoutParams(paramsC);
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    private void hideNavigationBar()
124
      {
125
      mCurrentApiVersion = Build.VERSION.SDK_INT;
126

    
127
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
128
        {
129
        final View decorView = getWindow().getDecorView();
130

    
131
        decorView.setSystemUiVisibility(FLAGS);
132

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
// do not avoid cutouts
149

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    @Override
161
    public void onWindowFocusChanged(boolean hasFocus)
162
      {
163
      super.onWindowFocusChanged(hasFocus);
164

    
165
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
166
        {
167
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
168
        }
169
      }
170

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
    @Override
186
    protected void onResume() 
187
      {
188
      super.onResume();
189

    
190
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
191
      BandagedCreatorView view = findViewById(R.id.bandagedCreatorObjectView);
192
      view.onResume();
193

    
194
      if( mScreen==null )
195
        {
196
        int ordinal = getObjectOrdinal();
197
        mScreen = new BandagedCreatorScreen(ordinal);
198
        }
199

    
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 void changeObject(int x, int y, int z)
246
      {
247
      BandagedCreatorRenderer renderer = getRenderer();
248
      renderer.changeObject(x,y,z);
249
      }
250

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

    
253
    public boolean isRTL()
254
      {
255
      return mRTL;
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
      InitAssets assets = new InitAssets(jsonStream,null,null);
287

    
288
      if( !assets.noJsonStream() )
289
        {
290
        TwistyObject object = new TwistyJson( TwistyObject.MODE_NORM, null, null, 1.0f, assets);
291

    
292
        if( !object.getError() )
293
          {
294
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
295
          SharedPreferences.Editor editor = preferences.edit();
296
          OSInterface os = new OSInterface(this,null);
297
          os.setEditor(editor);
298
          object.removePreferences(os);
299
          editor.apply();
300
          }
301
        }
302

    
303
      mScreen.deleteObject(this,name);
304
      files.deleteIcon(this,name);
305
      files.deleteJsonObject(this,name);
306
      }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
    public void playObject(String name)
311
      {
312
      Intent intent = new Intent(this, BandagedPlayActivity.class);
313
      intent.putExtra("name", name);
314
      startActivity(intent);
315
      }
316

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

    
319
    public void iconCreationDone(Bitmap bmp)
320
      {
321
      mScreen.iconCreationDone(this,bmp);
322
      }
323

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

    
326
    public int getScreenWidthInPixels()
327
      {
328
      return mScreenWidth;
329
      }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
    public int getScreenHeightInPixels()
334
      {
335
      return mScreenHeight;
336
      }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
    public int getObjectOrdinal()
341
      {
342
      return mObjectOrdinal;
343
      }
344
}
(1-1/12)