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.playui;
|
11
|
|
12
|
import java.io.InputStream;
|
13
|
|
14
|
import android.content.SharedPreferences;
|
15
|
import android.os.Build;
|
16
|
import android.os.Bundle;
|
17
|
import android.view.DisplayCutout;
|
18
|
import android.view.ViewGroup;
|
19
|
import android.widget.LinearLayout;
|
20
|
|
21
|
import androidx.preference.PreferenceManager;
|
22
|
|
23
|
import com.google.firebase.analytics.FirebaseAnalytics;
|
24
|
|
25
|
import org.distorted.dialogs.RubikDialogScores;
|
26
|
import org.distorted.external.RubikScores;
|
27
|
import org.distorted.helpers.BaseActivity;
|
28
|
import org.distorted.library.main.DistortedLibrary;
|
29
|
import org.distorted.objectlib.main.InitAssets;
|
30
|
import org.distorted.objectlib.main.ObjectControl;
|
31
|
import org.distorted.objectlib.main.TwistyObject;
|
32
|
import org.distorted.dialogs.RubikDialogError;
|
33
|
import org.distorted.external.RubikFiles;
|
34
|
import org.distorted.main.R;
|
35
|
import org.distorted.objects.RubikObject;
|
36
|
import org.distorted.objects.RubikObjectList;
|
37
|
import org.distorted.os.OSInterface;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public class PlayActivity extends BaseActivity implements RubikDialogScores.ScoresInvoker
|
42
|
{
|
43
|
public static final float TITLE_TEXT_SIZE= 0.060f;
|
44
|
private static final int ACTIVITY_NUMBER = 6;
|
45
|
private static final float RATIO_INSET = 0.09f;
|
46
|
|
47
|
private static final String KEY_FREE = "movesController_free";
|
48
|
private static final String KEY_SOLV = "movesController_solv";
|
49
|
|
50
|
private String mObjectName;
|
51
|
private int mNumScrambles;
|
52
|
private boolean mObjectLocal;
|
53
|
private int mObjectOrdinal;
|
54
|
private int mLevel;
|
55
|
private boolean mModeFree;
|
56
|
private boolean mJustStarted;
|
57
|
private FirebaseAnalytics mFirebaseAnalytics;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
@Override
|
62
|
protected void onCreate(Bundle savedState)
|
63
|
{
|
64
|
super.onCreate(savedState);
|
65
|
DistortedLibrary.onCreate(ACTIVITY_NUMBER);
|
66
|
setContentView(R.layout.play);
|
67
|
|
68
|
mJustStarted = true;
|
69
|
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
|
70
|
|
71
|
Bundle b = getIntent().getExtras();
|
72
|
|
73
|
if( b!=null )
|
74
|
{
|
75
|
mLevel = b.getInt("level");
|
76
|
mObjectName = b.getString("name");
|
77
|
mNumScrambles = b.getInt("scrambles");
|
78
|
mObjectLocal = b.getBoolean("local");
|
79
|
mObjectOrdinal = b.getInt("ordinal");
|
80
|
}
|
81
|
else
|
82
|
{
|
83
|
mLevel = -1;
|
84
|
mObjectName = "";
|
85
|
mNumScrambles = 0;
|
86
|
mObjectLocal = true;
|
87
|
mObjectOrdinal = 0;
|
88
|
}
|
89
|
|
90
|
mModeFree = (mLevel<0);
|
91
|
|
92
|
computeScreenDimensions();
|
93
|
hideNavigationBar();
|
94
|
cutoutHack();
|
95
|
computeUpperBarHeight(RATIO_BAR);
|
96
|
computeLowerBarHeight(RATIO_BAR);
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
@Override
|
102
|
public void onAttachedToWindow()
|
103
|
{
|
104
|
super.onAttachedToWindow();
|
105
|
|
106
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
107
|
{
|
108
|
DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
|
109
|
int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
|
110
|
|
111
|
LinearLayout layoutHid = findViewById(R.id.hiddenBar);
|
112
|
ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
|
113
|
paramsHid.height = (int)(insetHeight*RATIO_INSET);
|
114
|
layoutHid.setLayoutParams(paramsHid);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
@Override
|
121
|
protected void onPause()
|
122
|
{
|
123
|
super.onPause();
|
124
|
PlayView view = findViewById(R.id.playView);
|
125
|
view.onPause();
|
126
|
savePreferences();
|
127
|
DistortedLibrary.onPause(ACTIVITY_NUMBER);
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
@Override
|
133
|
protected void onResume()
|
134
|
{
|
135
|
super.onResume();
|
136
|
DistortedLibrary.onResume(ACTIVITY_NUMBER);
|
137
|
PlayView view = findViewById(R.id.playView);
|
138
|
ObjectControl control = view.getObjectControl();
|
139
|
view.onResume();
|
140
|
|
141
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
142
|
restorePreferences(preferences);
|
143
|
|
144
|
ScreenList sl = mJustStarted ?
|
145
|
(mModeFree ? ScreenList.FREE : ScreenList.SCRA) :
|
146
|
ScreenList.getCurrentScreen();
|
147
|
|
148
|
ScreenList.switchScreen(this,sl);
|
149
|
|
150
|
if( !mJustStarted ) restoreMoves(preferences);
|
151
|
|
152
|
if( mObjectName.length()>0 )
|
153
|
{
|
154
|
changeIfDifferent(mObjectName,mObjectLocal,mObjectOrdinal,control);
|
155
|
}
|
156
|
|
157
|
if( mJustStarted && !mModeFree )
|
158
|
{
|
159
|
control.scrambleObject(mNumScrambles);
|
160
|
}
|
161
|
|
162
|
mJustStarted = false;
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
@Override
|
168
|
protected void onDestroy()
|
169
|
{
|
170
|
super.onDestroy();
|
171
|
DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
|
176
|
private void savePreferences()
|
177
|
{
|
178
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
179
|
SharedPreferences.Editor editor = preferences.edit();
|
180
|
|
181
|
for( int i=0; i<ScreenList.LENGTH; i++ )
|
182
|
{
|
183
|
ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
|
184
|
}
|
185
|
|
186
|
ScreenList.savePreferences(editor);
|
187
|
|
188
|
ScreenList curr = ScreenList.getCurrentScreen();
|
189
|
|
190
|
if( curr==ScreenList.FREE )
|
191
|
{
|
192
|
ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
|
193
|
free.saveMovePreferences(KEY_FREE,editor);
|
194
|
}
|
195
|
if( curr==ScreenList.SOLV )
|
196
|
{
|
197
|
ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
|
198
|
solv.saveMovePreferences(KEY_SOLV,editor);
|
199
|
}
|
200
|
|
201
|
PlayView view = findViewById(R.id.playView);
|
202
|
OSInterface os = view.getInterface();
|
203
|
os.setEditor(editor);
|
204
|
view.getObjectControl().savePreferences();
|
205
|
|
206
|
RubikScores scores = RubikScores.getInstance();
|
207
|
scores.savePreferences(editor);
|
208
|
|
209
|
editor.apply();
|
210
|
}
|
211
|
|
212
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
213
|
|
214
|
private void restorePreferences(SharedPreferences preferences)
|
215
|
{
|
216
|
for( int i=0; i<ScreenList.LENGTH; i++)
|
217
|
{
|
218
|
ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
|
219
|
}
|
220
|
|
221
|
if( !mJustStarted ) ScreenList.restorePreferences(preferences);
|
222
|
|
223
|
PlayView view = findViewById(R.id.playView);
|
224
|
OSInterface os = view.getInterface();
|
225
|
os.setPreferences(preferences);
|
226
|
view.getObjectControl().restorePreferences();
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
|
231
|
private void restoreMoves(SharedPreferences preferences)
|
232
|
{
|
233
|
ScreenList curr = ScreenList.getCurrentScreen();
|
234
|
|
235
|
if( curr==ScreenList.FREE )
|
236
|
{
|
237
|
ScreenFree free = (ScreenFree) ScreenList.FREE.getScreenClass();
|
238
|
free.restoreMovePreferences(this,KEY_FREE,preferences);
|
239
|
}
|
240
|
if( curr==ScreenList.SOLV )
|
241
|
{
|
242
|
ScreenSolving solv = (ScreenSolving) ScreenList.SOLV.getScreenClass();
|
243
|
solv.restoreMovePreferences(this,KEY_SOLV,preferences);
|
244
|
}
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
void OpenGLError()
|
250
|
{
|
251
|
RubikDialogError errDiag = new RubikDialogError();
|
252
|
errDiag.show(getSupportFragmentManager(), null);
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
|
257
|
private void changeIfDifferent(String name, boolean local, int ordinal, ObjectControl control)
|
258
|
{
|
259
|
if( local )
|
260
|
{
|
261
|
RubikFiles files = RubikFiles.getInstance();
|
262
|
int iconMode = TwistyObject.MODE_NORM;
|
263
|
InputStream jsonStream = files.openFile(this, name+"_object.json");
|
264
|
InitAssets asset = new InitAssets(jsonStream, null, null);
|
265
|
control.changeIfDifferent(ordinal,name,iconMode,asset);
|
266
|
}
|
267
|
else
|
268
|
{
|
269
|
RubikObject object = RubikObjectList.getObject(ordinal);
|
270
|
int iconMode = TwistyObject.MODE_NORM;
|
271
|
InputStream jsonStream = object==null ? null : object.getObjectStream(this);
|
272
|
InputStream meshStream = object==null ? null : object.getMeshStream(this);
|
273
|
PlayView view = findViewById(R.id.playView);
|
274
|
OSInterface os = view.getInterface();
|
275
|
InitAssets asset = new InitAssets(jsonStream, meshStream, os);
|
276
|
control.changeIfDifferent(ordinal, name, iconMode, asset);
|
277
|
}
|
278
|
}
|
279
|
|
280
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281
|
// PUBLIC API
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
public FirebaseAnalytics getAnalytics()
|
285
|
{
|
286
|
return mFirebaseAnalytics;
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
|
291
|
public int getNumScrambles()
|
292
|
{
|
293
|
return mNumScrambles;
|
294
|
}
|
295
|
|
296
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
297
|
|
298
|
public int getLevel()
|
299
|
{
|
300
|
return mLevel;
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
|
305
|
public int getObjectOrdinal()
|
306
|
{
|
307
|
return mObjectOrdinal;
|
308
|
}
|
309
|
|
310
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
311
|
|
312
|
public ObjectControl getControl()
|
313
|
{
|
314
|
PlayView view = findViewById(R.id.playView);
|
315
|
return view.getObjectControl();
|
316
|
}
|
317
|
}
|