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
|
String key ="";
|
159
|
|
160
|
if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
|
161
|
{
|
162
|
RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
|
163
|
changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
|
164
|
if( object!=null ) key = object.getUpperName();
|
165
|
}
|
166
|
|
167
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
168
|
restorePreferences(preferences);
|
169
|
|
170
|
if( mScreen==null ) mScreen = new ConfigScreen();
|
171
|
mScreen.onAttachedToWindow(this,mObjectOrdinal);
|
172
|
|
173
|
ObjectControl control = view.getObjectControl();
|
174
|
OSInterface os = view.getInterface();
|
175
|
TwistyObject obj = control.getObject();
|
176
|
|
177
|
mPane = new ConfigScreenPane(this,key,os);
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
void repaintPuzzleFace(int cubit, int face)
|
183
|
{
|
184
|
if( mPane!=null )
|
185
|
{
|
186
|
int color = mPane.getCurrentColor();
|
187
|
ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
|
188
|
ObjectControl control = view.getObjectControl();
|
189
|
control.repaintPuzzleFace(cubit,face,color);
|
190
|
}
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
@Override
|
196
|
protected void onDestroy()
|
197
|
{
|
198
|
super.onDestroy();
|
199
|
DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
void OpenGLError()
|
205
|
{
|
206
|
RubikDialogError errDiag = new RubikDialogError();
|
207
|
errDiag.show(getSupportFragmentManager(), null);
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
|
213
|
{
|
214
|
if( object!=null )
|
215
|
{
|
216
|
int iconMode = TwistyObject.MODE_NORM;
|
217
|
InputStream jsonStream = object.getObjectStream(this);
|
218
|
InputStream meshStream = object.getMeshStream(this);
|
219
|
String name = object.getUpperName();
|
220
|
InitAssets asset = new InitAssets(jsonStream,meshStream,null);
|
221
|
control.changeIfDifferent(ordinal,name,iconMode,asset);
|
222
|
}
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
private void savePreferences()
|
228
|
{
|
229
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
230
|
SharedPreferences.Editor editor = preferences.edit();
|
231
|
|
232
|
ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
|
233
|
OSInterface os = view.getInterface();
|
234
|
os.setEditor(editor);
|
235
|
view.getObjectControl().savePreferences();
|
236
|
|
237
|
editor.apply();
|
238
|
}
|
239
|
|
240
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241
|
|
242
|
private void restorePreferences(SharedPreferences preferences)
|
243
|
{
|
244
|
ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
|
245
|
OSInterface os = view.getInterface();
|
246
|
os.setPreferences(preferences);
|
247
|
view.getObjectControl().restorePreferences();
|
248
|
}
|
249
|
|
250
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251
|
// PUBLIC API
|
252
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253
|
|
254
|
public ConfigRenderer getRenderer()
|
255
|
{
|
256
|
ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
|
257
|
return view.getRenderer();
|
258
|
}
|
259
|
|
260
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261
|
|
262
|
public void recreateColors()
|
263
|
{
|
264
|
ObjectControl control = getControl();
|
265
|
control.recreateColors();
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
|
270
|
public void recreateStickers(int borders, int corners)
|
271
|
{
|
272
|
ObjectControl control = getControl();
|
273
|
|
274
|
float border_step = ConfigScreenPane.BORDER_STEPS[borders];
|
275
|
float corner_step = ConfigScreenPane.CORNER_STEPS[corners];
|
276
|
|
277
|
control.recreateSticker(border_step,corner_step);
|
278
|
}
|
279
|
|
280
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281
|
|
282
|
public void resetUI()
|
283
|
{
|
284
|
mPane.resetUI(this);
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
public int getScreenWidthInPixels()
|
290
|
{
|
291
|
return mScreenWidth;
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
public int getScreenHeightInPixels()
|
297
|
{
|
298
|
return mScreenHeight;
|
299
|
}
|
300
|
|
301
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302
|
|
303
|
public ObjectControl getControl()
|
304
|
{
|
305
|
ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
|
306
|
return view.getObjectControl();
|
307
|
}
|
308
|
}
|