Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ 8cf17ca7

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.config;
11

    
12
import android.content.SharedPreferences;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.util.DisplayMetrics;
16
import android.view.View;
17
import android.view.ViewGroup;
18
import android.view.WindowManager;
19
import android.widget.LinearLayout;
20

    
21
import androidx.appcompat.app.AppCompatActivity;
22
import androidx.preference.PreferenceManager;
23

    
24
import org.distorted.dialogs.RubikDialogError;
25
import org.distorted.library.main.DistortedLibrary;
26
import org.distorted.main.MainActivity;
27
import org.distorted.main.R;
28
import org.distorted.objectlib.main.InitAssets;
29
import org.distorted.objectlib.main.ObjectControl;
30
import org.distorted.objectlib.main.TwistyObject;
31
import org.distorted.objects.RubikObject;
32
import org.distorted.objects.RubikObjectList;
33
import org.distorted.os.OSInterface;
34

    
35
import java.io.InputStream;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class ConfigActivity extends AppCompatActivity
40
{
41
    private static final int ACTIVITY_NUMBER = 7;
42
    private static final float RATIO_BAR  = MainActivity.RATIO_BAR;
43
    public static final int FLAGS = MainActivity.FLAGS;
44

    
45
    private static int mScreenWidth, mScreenHeight;
46
    private ConfigScreen mScreen;
47
    private ConfigScreenPane mPane;
48
    private int mCurrentApiVersion;
49
    private int mObjectOrdinal;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    @Override
54
    protected void onCreate(Bundle savedState)
55
      {
56
      super.onCreate(savedState);
57
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
58
      setTheme(R.style.MaterialThemeNoActionBar);
59
      setContentView(R.layout.config);
60

    
61
      Bundle b = getIntent().getExtras();
62

    
63
      if(b != null) mObjectOrdinal = b.getInt("obj");
64

    
65
      DisplayMetrics displaymetrics = new DisplayMetrics();
66
      getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
67
      mScreenWidth =displaymetrics.widthPixels;
68
      mScreenHeight=displaymetrics.heightPixels;
69

    
70
      hideNavigationBar();
71
      cutoutHack();
72
      computeBarHeights();
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// this does not include possible insets
77

    
78
    private void computeBarHeights()
79
      {
80
      int barHeight = (int)(mScreenHeight*RATIO_BAR);
81
      LinearLayout layout = findViewById(R.id.lowerBar);
82
      ViewGroup.LayoutParams params = layout.getLayoutParams();
83
      params.height = barHeight;
84
      layout.setLayoutParams(params);
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    private void hideNavigationBar()
90
      {
91
      mCurrentApiVersion = Build.VERSION.SDK_INT;
92

    
93
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT)
94
        {
95
        final View decorView = getWindow().getDecorView();
96

    
97
        decorView.setSystemUiVisibility(FLAGS);
98

    
99
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener()
100
          {
101
          @Override
102
          public void onSystemUiVisibilityChange(int visibility)
103
            {
104
            if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
105
              {
106
              decorView.setSystemUiVisibility(FLAGS);
107
              }
108
            }
109
          });
110
        }
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
// do not avoid cutouts
115

    
116
    private void cutoutHack()
117
      {
118
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
119
        {
120
        getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
121
        }
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    @Override
127
    public void onWindowFocusChanged(boolean hasFocus)
128
      {
129
      super.onWindowFocusChanged(hasFocus);
130

    
131
      if(mCurrentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus)
132
        {
133
        getWindow().getDecorView().setSystemUiVisibility(FLAGS);
134
        }
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    @Override
140
    protected void onPause() 
141
      {
142
      super.onPause();
143
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
144
      view.onPause();
145
      savePreferences();
146
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
    @Override
152
    protected void onResume() 
153
      {
154
      super.onResume();
155
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
156
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
157
      view.onResume();
158

    
159
      if( mScreen==null ) mScreen = new ConfigScreen();
160
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
161
      mPane = new ConfigScreenPane(this,mObjectOrdinal);
162

    
163
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
164
        {
165
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
166
        changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
167
        }
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    void repaintPuzzleFace(int cubit, int face)
173
      {
174
      if( mPane!=null )
175
        {
176
        int color  = mPane.getCurrentColor();
177
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
178
        ObjectControl control = view.getObjectControl();
179
        control.repaintPuzzleFace(cubit,face,color);
180
        }
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    @Override
186
    protected void onDestroy() 
187
      {
188
      super.onDestroy();
189
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
    void OpenGLError()
195
      {
196
      RubikDialogError errDiag = new RubikDialogError();
197
      errDiag.show(getSupportFragmentManager(), null);
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
203
      {
204
      if( object!=null )
205
        {
206
        int iconMode           = TwistyObject.MODE_NORM;
207
        InputStream jsonStream = object.getObjectStream(this);
208
        InputStream meshStream = object.getMeshStream(this);
209
        String name            = object.getUpperName();
210
        InitAssets asset       = new InitAssets(jsonStream,meshStream,null);
211
        control.changeIfDifferent(ordinal,name,iconMode,asset);
212
        }
213
      }
214

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

    
217
    private void savePreferences()
218
      {
219
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
220
      SharedPreferences.Editor editor = preferences.edit();
221

    
222
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
223
      OSInterface os = view.getInterface();
224
      os.setEditor(editor);
225
      view.getObjectControl().savePreferences();
226

    
227
      editor.apply();
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
// PUBLIC API
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    public ConfigRenderer getRenderer()
235
      {
236
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
237
      return view.getRenderer();
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    public void recreateColors()
243
      {
244
      ObjectControl control = getControl();
245
      control.recreateColors();
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
    public void recreateStickers(int borders, int corners)
251
      {
252
      ObjectControl control = getControl();
253

    
254
      float border_step = ConfigScreenPane.BORDER_STEPS[borders];
255
      float corner_step = ConfigScreenPane.CORNER_STEPS[corners];
256

    
257
      control.recreateSticker(border_step,corner_step);
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public void resetUI()
263
      {
264
      mPane.resetUI(this);
265
      }
266

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

    
269
    public int getScreenWidthInPixels()
270
      {
271
      return mScreenWidth;
272
      }
273

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

    
276
    public int getScreenHeightInPixels()
277
      {
278
      return mScreenHeight;
279
      }
280

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

    
283
    public ObjectControl getControl()
284
      {
285
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
286
      return view.getObjectControl();
287
      }
288
}
(1-1/6)