1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.screens;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.content.SharedPreferences;
|
24
|
import android.content.res.Resources;
|
25
|
import android.graphics.drawable.BitmapDrawable;
|
26
|
import android.os.Build;
|
27
|
import android.os.Bundle;
|
28
|
import android.util.TypedValue;
|
29
|
import android.view.Gravity;
|
30
|
import android.view.LayoutInflater;
|
31
|
import android.view.View;
|
32
|
import android.widget.Button;
|
33
|
import android.widget.GridLayout;
|
34
|
import android.widget.ImageButton;
|
35
|
import android.widget.LinearLayout;
|
36
|
import android.widget.PopupWindow;
|
37
|
|
38
|
import org.distorted.objectlib.main.ObjectControl;
|
39
|
|
40
|
import org.distorted.main.R;
|
41
|
import org.distorted.main.RubikActivity;
|
42
|
import org.distorted.dialogs.RubikDialogAbout;
|
43
|
import org.distorted.dialogs.RubikDialogPattern;
|
44
|
import org.distorted.dialogs.RubikDialogScores;
|
45
|
import org.distorted.dialogs.RubikDialogTutorial;
|
46
|
import org.distorted.helpers.TransparentButton;
|
47
|
import org.distorted.helpers.TransparentImageButton;
|
48
|
import org.distorted.network.RubikScores;
|
49
|
import org.distorted.objects.RubikObject;
|
50
|
import org.distorted.objects.RubikObjectList;
|
51
|
|
52
|
import static android.view.View.inflate;
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
public class RubikScreenPlay extends RubikScreenBase
|
57
|
{
|
58
|
public static final int NUM_COLUMNS = 5;
|
59
|
public static final int LEVELS_SHOWN = 10;
|
60
|
|
61
|
private static final int[] BUTTON_LABELS = { R.string.scores,
|
62
|
R.string.patterns,
|
63
|
R.string.solver,
|
64
|
R.string.tutorials,
|
65
|
R.string.about };
|
66
|
|
67
|
private static final int NUM_BUTTONS = BUTTON_LABELS.length;
|
68
|
private static final float LAST_BUTTON = 1.5f;
|
69
|
private static final int[] mLocation = new int[2];
|
70
|
|
71
|
private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
|
72
|
private TransparentButton mPlayButton;
|
73
|
private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
|
74
|
private LinearLayout mPlayLayout;
|
75
|
private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
|
76
|
private int mLevelValue;
|
77
|
private float mButtonSize, mMenuItemSize, mMenuTextSize;
|
78
|
private int mColCount, mRowCount, mMaxRowCount;
|
79
|
private int mUpperBarHeight;
|
80
|
private boolean mShouldReactToEndOfScrambling;
|
81
|
private int mBottomHeight;
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
void leaveScreen(RubikActivity act)
|
86
|
{
|
87
|
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
void enterScreen(final RubikActivity act)
|
93
|
{
|
94
|
int numObjects = RubikObjectList.getNumObjects();
|
95
|
float width = act.getScreenWidthInPixels();
|
96
|
mUpperBarHeight = act.getHeightUpperBar();
|
97
|
|
98
|
mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
|
99
|
mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
|
100
|
mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
|
101
|
|
102
|
mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
|
103
|
mColCount = NUM_COLUMNS;
|
104
|
|
105
|
// TOP ////////////////////////////
|
106
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
107
|
layoutTop.removeAllViews();
|
108
|
|
109
|
setupObjectButton(act,width);
|
110
|
layoutTop.addView(mObjButton);
|
111
|
|
112
|
setupMenuButton(act,width);
|
113
|
layoutTop.addView(mMenuButton);
|
114
|
|
115
|
setupPlayButton(act,width);
|
116
|
layoutTop.addView(mPlayButton);
|
117
|
|
118
|
setupSolveButton(act);
|
119
|
setupScrambleButton(act);
|
120
|
createBottomPane(act,mSolveButton,mScrambleButton);
|
121
|
}
|
122
|
|
123
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
private void setupObjectButton(final RubikActivity act, final float width)
|
126
|
{
|
127
|
final int margin = (int)(width*RubikActivity.MARGIN);
|
128
|
final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
|
129
|
final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
|
130
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
131
|
mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
132
|
|
133
|
mObjButton.setOnClickListener( new View.OnClickListener()
|
134
|
{
|
135
|
@Override
|
136
|
public void onClick(View view)
|
137
|
{
|
138
|
if( mObjectPopup==null )
|
139
|
{
|
140
|
float width = act.getScreenWidthInPixels();
|
141
|
float height= act.getScreenHeightInPixels();
|
142
|
setupObjectWindow(act,width,height);
|
143
|
}
|
144
|
|
145
|
if( act.getControl().isUINotBlocked())
|
146
|
{
|
147
|
int rowCount = Math.min(mMaxRowCount,mRowCount);
|
148
|
View popupView = mObjectPopup.getContentView();
|
149
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
150
|
displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
|
151
|
}
|
152
|
}
|
153
|
});
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
|
158
|
private void setupPlayButton(final RubikActivity act, final float width)
|
159
|
{
|
160
|
final int margin = (int)(width*RubikActivity.MARGIN);
|
161
|
|
162
|
mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
|
163
|
|
164
|
mPlayButton.setOnClickListener( new View.OnClickListener()
|
165
|
{
|
166
|
@Override
|
167
|
public void onClick(View view)
|
168
|
{
|
169
|
if( mPlayPopup==null )
|
170
|
{
|
171
|
float width = act.getScreenWidthInPixels();
|
172
|
setupPlayWindow(act,width);
|
173
|
}
|
174
|
|
175
|
if( act.getControl().isUINotBlocked())
|
176
|
{
|
177
|
adjustSolvedIcons();
|
178
|
float height= act.getScreenHeightInPixels();
|
179
|
final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
|
180
|
View popupView = mPlayPopup.getContentView();
|
181
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
182
|
final int object = RubikObjectList.getCurrObject();
|
183
|
final int dbLevel = RubikObjectList.getDBLevel(object);
|
184
|
final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
|
185
|
final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
|
186
|
final int realHeight = Math.min(popupHeight,maxHeight);
|
187
|
displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
|
188
|
}
|
189
|
}
|
190
|
});
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
private void setupMenuButton(final RubikActivity act, final float width)
|
196
|
{
|
197
|
final int margin = (int)(width*RubikActivity.MARGIN);
|
198
|
final int icon = RubikActivity.getDrawable(R.drawable.ui_small_menu,R.drawable.ui_medium_menu, R.drawable.ui_big_menu, R.drawable.ui_huge_menu);
|
199
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
200
|
mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
201
|
|
202
|
mMenuButton.setOnClickListener( new View.OnClickListener()
|
203
|
{
|
204
|
@Override
|
205
|
public void onClick(View view)
|
206
|
{
|
207
|
if( mMenuPopup==null )
|
208
|
{
|
209
|
float width = act.getScreenWidthInPixels();
|
210
|
setupMenuWindow(act,width);
|
211
|
}
|
212
|
|
213
|
if( act.getControl().isUINotBlocked())
|
214
|
{
|
215
|
View popupView = mMenuPopup.getContentView();
|
216
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
217
|
displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
|
218
|
}
|
219
|
}
|
220
|
});
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
private void setupObjectWindow(final RubikActivity act, final float width, final float height)
|
226
|
{
|
227
|
int cubeWidth = (int)(width/9);
|
228
|
int margin = (int)(width*RubikActivity.LARGE_MARGIN);
|
229
|
mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
|
230
|
mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
|
231
|
|
232
|
LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
|
233
|
GridLayout objectGrid = view.findViewById(R.id.objectGrid);
|
234
|
|
235
|
GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
|
236
|
GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
|
237
|
|
238
|
objectGrid.setColumnCount(mColCount);
|
239
|
objectGrid.setRowCount(mRowCount);
|
240
|
|
241
|
LinearLayout bottomLayout = view.findViewById(R.id.bottomLayout);
|
242
|
setupBottomLayout(act,bottomLayout,(NUM_COLUMNS-2)*mObjectSize);
|
243
|
|
244
|
mObjectPopup = new PopupWindow(act);
|
245
|
mObjectPopup.setFocusable(true);
|
246
|
mObjectPopup.setContentView(view);
|
247
|
|
248
|
int[] nextInRow = new int[mRowCount];
|
249
|
|
250
|
for(int row=0; row<mRowCount; row++)
|
251
|
{
|
252
|
rowSpecs[row] = GridLayout.spec(row);
|
253
|
nextInRow[row]= 0;
|
254
|
}
|
255
|
for(int col=0; col<mColCount; col++)
|
256
|
{
|
257
|
colSpecs[col] = GridLayout.spec(col);
|
258
|
}
|
259
|
|
260
|
int numObjects = RubikObjectList.getNumObjects();
|
261
|
|
262
|
for(int object=0; object<numObjects; object++)
|
263
|
{
|
264
|
final RubikObject robject = RubikObjectList.getObject(object);
|
265
|
int icons = robject==null ? 0 : robject.getIconID();
|
266
|
int row = object/NUM_COLUMNS;
|
267
|
final int ordinal = robject==null ? 0 : robject.getOrdinal();
|
268
|
|
269
|
ImageButton button = new ImageButton(act);
|
270
|
button.setBackgroundResource(icons);
|
271
|
button.setOnClickListener( new View.OnClickListener()
|
272
|
{
|
273
|
@Override
|
274
|
public void onClick(View v)
|
275
|
{
|
276
|
if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
|
277
|
{
|
278
|
RubikObjectList.setCurrObject(act,ordinal);
|
279
|
act.changeObject(ordinal,true);
|
280
|
if( mPlayLayout!=null ) adjustLevels(act);
|
281
|
mMovesController.clearMoves(act);
|
282
|
}
|
283
|
|
284
|
mObjectPopup.dismiss();
|
285
|
}
|
286
|
});
|
287
|
|
288
|
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
|
289
|
params.bottomMargin = margin;
|
290
|
params.topMargin = margin;
|
291
|
params.leftMargin = margin;
|
292
|
params.rightMargin = margin;
|
293
|
|
294
|
params.width = cubeWidth;
|
295
|
params.height= cubeWidth;
|
296
|
|
297
|
nextInRow[row]++;
|
298
|
|
299
|
objectGrid.addView(button, params);
|
300
|
}
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
|
305
|
private void setupBottomLayout(final RubikActivity act, final LinearLayout layout, int width)
|
306
|
{
|
307
|
int iconD = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
|
308
|
int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
|
309
|
|
310
|
ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
|
311
|
Button buttonNul = layout.findViewById(R.id.buttonNul);
|
312
|
ImageButton buttonDet = layout.findViewById(R.id.buttonDet);
|
313
|
|
314
|
buttonTut.setImageResource(iconT);
|
315
|
buttonDet.setImageResource(iconD);
|
316
|
buttonNul.setVisibility(View.INVISIBLE);
|
317
|
|
318
|
buttonNul.setWidth(width);
|
319
|
|
320
|
Resources res = act.getResources();
|
321
|
BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconD);
|
322
|
mBottomHeight = bd.getIntrinsicHeight();
|
323
|
|
324
|
TypedValue outValue = new TypedValue();
|
325
|
act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
|
326
|
buttonTut.setBackgroundResource(outValue.resourceId);
|
327
|
buttonDet.setBackgroundResource(outValue.resourceId);
|
328
|
|
329
|
buttonTut.setOnClickListener( new View.OnClickListener()
|
330
|
{
|
331
|
@Override
|
332
|
public void onClick(View v)
|
333
|
{
|
334
|
if( mObjectPopup!=null ) mObjectPopup.dismiss();
|
335
|
RubikDialogTutorial tDiag = new RubikDialogTutorial();
|
336
|
tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
|
337
|
}
|
338
|
});
|
339
|
|
340
|
buttonDet.setOnClickListener( new View.OnClickListener()
|
341
|
{
|
342
|
@Override
|
343
|
public void onClick(View v)
|
344
|
{
|
345
|
if( mObjectPopup!=null ) mObjectPopup.dismiss();
|
346
|
int currObject = RubikObjectList.getCurrObject();
|
347
|
act.switchConfig(currObject);
|
348
|
}
|
349
|
});
|
350
|
}
|
351
|
|
352
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
353
|
|
354
|
private void setupMenuWindow(final RubikActivity act, final float width)
|
355
|
{
|
356
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
357
|
final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
|
358
|
LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
|
359
|
|
360
|
mMenuPopup = new PopupWindow(act);
|
361
|
mMenuPopup.setContentView(layout);
|
362
|
mMenuPopup.setFocusable(true);
|
363
|
int margin = (int)(width*RubikActivity.MARGIN);
|
364
|
int padding = (int)(width*RubikActivity.PADDING);
|
365
|
|
366
|
mMenuLayoutWidth = (int)(width/2);
|
367
|
mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
|
368
|
|
369
|
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
|
370
|
|
371
|
for(int i=0; i<NUM_BUTTONS; i++)
|
372
|
{
|
373
|
final int but = i;
|
374
|
Button button = new Button(act);
|
375
|
button.setLayoutParams(p);
|
376
|
button.setText(BUTTON_LABELS[i]);
|
377
|
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
|
378
|
|
379
|
button.setOnClickListener( new View.OnClickListener()
|
380
|
{
|
381
|
@Override
|
382
|
public void onClick(View v)
|
383
|
{
|
384
|
mMenuPopup.dismiss();
|
385
|
MenuAction(act,but);
|
386
|
}
|
387
|
});
|
388
|
|
389
|
menuLayout.addView(button);
|
390
|
}
|
391
|
}
|
392
|
|
393
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
394
|
|
395
|
private void setupPlayWindow(final RubikActivity act, final float width)
|
396
|
{
|
397
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
398
|
final View layout = layoutInflater.inflate(R.layout.popup_play, null);
|
399
|
mPlayLayout = layout.findViewById(R.id.playGrid);
|
400
|
|
401
|
mPlayLayoutWidth = (int)(width*0.4f);
|
402
|
|
403
|
mPlayPopup = new PopupWindow(act);
|
404
|
mPlayPopup.setContentView(layout);
|
405
|
mPlayPopup.setFocusable(true);
|
406
|
|
407
|
adjustLevels(act);
|
408
|
}
|
409
|
|
410
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
411
|
|
412
|
private void MenuAction(RubikActivity act, int button)
|
413
|
{
|
414
|
switch(button)
|
415
|
{
|
416
|
case 0: Bundle sBundle = new Bundle();
|
417
|
int currObject = RubikObjectList.getCurrObject();
|
418
|
sBundle.putInt("tab", currObject );
|
419
|
sBundle.putBoolean("submitting", false);
|
420
|
RubikDialogScores scores = new RubikDialogScores();
|
421
|
scores.setArguments(sBundle);
|
422
|
scores.show(act.getSupportFragmentManager(), null);
|
423
|
break;
|
424
|
case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
|
425
|
pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
|
426
|
break;
|
427
|
case 2: ScreenList.switchScreen(act, ScreenList.SVER);
|
428
|
break;
|
429
|
case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
|
430
|
tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
|
431
|
break;
|
432
|
case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
|
433
|
aDiag.show(act.getSupportFragmentManager(), null);
|
434
|
break;
|
435
|
}
|
436
|
}
|
437
|
|
438
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439
|
|
440
|
void setupSolveButton(final RubikActivity act)
|
441
|
{
|
442
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
|
443
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
444
|
mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
|
445
|
|
446
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
447
|
{
|
448
|
@Override
|
449
|
public void onClick(View v)
|
450
|
{
|
451
|
act.getControl().solveObject();
|
452
|
mMovesController.clearMoves(act);
|
453
|
}
|
454
|
});
|
455
|
}
|
456
|
|
457
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
458
|
|
459
|
private void setupScrambleButton(final RubikActivity act)
|
460
|
{
|
461
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble_new,R.drawable.ui_medium_cube_scramble_new, R.drawable.ui_big_cube_scramble_new, R.drawable.ui_huge_cube_scramble_new);
|
462
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
463
|
mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
|
464
|
|
465
|
mScrambleButton.setOnClickListener( new View.OnClickListener()
|
466
|
{
|
467
|
@Override
|
468
|
public void onClick(View v)
|
469
|
{
|
470
|
int currObject = RubikObjectList.getCurrObject();
|
471
|
RubikObject object = RubikObjectList.getObject(currObject);
|
472
|
int numScrambles = object==null ? 0 : object.getNumScramble();
|
473
|
mShouldReactToEndOfScrambling = false;
|
474
|
act.getControl().scrambleObject(numScrambles);
|
475
|
}
|
476
|
});
|
477
|
}
|
478
|
|
479
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
480
|
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
|
481
|
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
|
482
|
// going to be called)
|
483
|
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
|
484
|
|
485
|
public void savePreferences(SharedPreferences.Editor editor)
|
486
|
{
|
487
|
editor.putInt("play_LevelValue", mLevelValue );
|
488
|
|
489
|
if( mObjectPopup!=null )
|
490
|
{
|
491
|
mObjectPopup.dismiss();
|
492
|
mObjectPopup = null;
|
493
|
}
|
494
|
|
495
|
if( mMenuPopup!=null )
|
496
|
{
|
497
|
mMenuPopup.dismiss();
|
498
|
mMenuPopup = null;
|
499
|
}
|
500
|
|
501
|
if( mPlayPopup!=null )
|
502
|
{
|
503
|
mPlayPopup.dismiss();
|
504
|
mPlayPopup = null;
|
505
|
}
|
506
|
}
|
507
|
|
508
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
509
|
|
510
|
public void restorePreferences(SharedPreferences preferences)
|
511
|
{
|
512
|
mLevelValue = preferences.getInt("play_LevelValue", 0);
|
513
|
}
|
514
|
|
515
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
516
|
|
517
|
public void setCurrObject(RubikActivity act)
|
518
|
{
|
519
|
if( mPlayLayout!=null ) adjustLevels(act);
|
520
|
}
|
521
|
|
522
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
523
|
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
|
524
|
|
525
|
private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
|
526
|
{
|
527
|
View topLayout = act.findViewById(R.id.relativeLayout);
|
528
|
boolean isFullScreen;
|
529
|
|
530
|
if( topLayout!=null )
|
531
|
{
|
532
|
topLayout.getLocationOnScreen(mLocation);
|
533
|
isFullScreen = (mLocation[1]==0);
|
534
|
}
|
535
|
else
|
536
|
{
|
537
|
isFullScreen = true;
|
538
|
}
|
539
|
|
540
|
try
|
541
|
{
|
542
|
// if on Android 11 or we are fullscreen
|
543
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
|
544
|
{
|
545
|
window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
|
546
|
window.update(view, w, h);
|
547
|
}
|
548
|
else // Android 10 or below in pop-up mode or split-screen mode
|
549
|
{
|
550
|
view.getLocationOnScreen(mLocation);
|
551
|
int width = view.getWidth();
|
552
|
int height = view.getHeight();
|
553
|
int x = mLocation[0]+(width-w)/2;
|
554
|
int y = mLocation[1]+height+yoff;
|
555
|
|
556
|
window.showAsDropDown(view);
|
557
|
window.update(x,y,w,h);
|
558
|
}
|
559
|
}
|
560
|
catch( IllegalArgumentException iae )
|
561
|
{
|
562
|
// ignore, this means window is 'not attached to window manager' -
|
563
|
// which most probably is because we are already exiting the app.
|
564
|
}
|
565
|
}
|
566
|
|
567
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
568
|
|
569
|
private void adjustSolvedIcons()
|
570
|
{
|
571
|
if( mPlayLayout!=null )
|
572
|
{
|
573
|
int currObject = RubikObjectList.getCurrObject();
|
574
|
int dbLevel = RubikObjectList.getDBLevel(currObject);
|
575
|
int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
|
576
|
RubikScores scores = RubikScores.getInstance();
|
577
|
|
578
|
for(int i=0; i<numLevel; i++)
|
579
|
{
|
580
|
int level = i<numLevel-1 ? i+1 : dbLevel;
|
581
|
Button button = (Button)mPlayLayout.getChildAt(i);
|
582
|
int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
|
583
|
button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
|
584
|
}
|
585
|
}
|
586
|
}
|
587
|
|
588
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
589
|
|
590
|
private void adjustLevels(final RubikActivity act)
|
591
|
{
|
592
|
int currObject = RubikObjectList.getCurrObject();
|
593
|
int dbLevel = RubikObjectList.getDBLevel(currObject);
|
594
|
RubikObject object = RubikObjectList.getObject(currObject);
|
595
|
int numScrambles = object==null ? 0 : object.getNumScramble();
|
596
|
int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
|
597
|
String[] levels = new String[numLevel];
|
598
|
|
599
|
for(int i=0; i<numLevel-1; i++)
|
600
|
{
|
601
|
levels[i] = act.getString(R.string.lv_placeholder,i+1);
|
602
|
}
|
603
|
|
604
|
if( numLevel>0 )
|
605
|
{
|
606
|
levels[numLevel-1] = act.getString(R.string.level_full);
|
607
|
}
|
608
|
|
609
|
if( mLevelValue>dbLevel || mLevelValue<1 ||
|
610
|
(mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
|
611
|
{
|
612
|
mLevelValue=1;
|
613
|
}
|
614
|
|
615
|
float width = act.getScreenWidthInPixels();
|
616
|
int margin = (int)(width*RubikActivity.MARGIN);
|
617
|
int padding = (int)(width*RubikActivity.PADDING);
|
618
|
int butWidth = mPlayLayoutWidth - 2*padding;
|
619
|
int butHeight= (int)mMenuItemSize;
|
620
|
int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
|
621
|
|
622
|
LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
|
623
|
pM.setMargins(margin, 0, margin, margin);
|
624
|
LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
|
625
|
pT.setMargins(margin, margin, margin, margin);
|
626
|
LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH );
|
627
|
pB.setMargins(margin, margin, margin, 2*margin);
|
628
|
|
629
|
mPlayLayout.removeAllViews();
|
630
|
|
631
|
RubikScores scores = RubikScores.getInstance();
|
632
|
|
633
|
for(int i=0; i<numLevel; i++)
|
634
|
{
|
635
|
final int level = i<numLevel-1 ? i+1 : dbLevel;
|
636
|
final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
|
637
|
Button button = new Button(act);
|
638
|
button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
|
639
|
button.setText(levels[i]);
|
640
|
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
|
641
|
|
642
|
int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
|
643
|
button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
|
644
|
|
645
|
button.setOnClickListener( new View.OnClickListener()
|
646
|
{
|
647
|
@Override
|
648
|
public void onClick(View v)
|
649
|
{
|
650
|
ObjectControl control = act.getControl();
|
651
|
|
652
|
if(control.isUINotBlocked())
|
653
|
{
|
654
|
if( mPlayPopup!=null ) mPlayPopup.dismiss();
|
655
|
mLevelValue = level;
|
656
|
mShouldReactToEndOfScrambling = true;
|
657
|
control.scrambleObject(scrambles);
|
658
|
}
|
659
|
}
|
660
|
});
|
661
|
|
662
|
mPlayLayout.addView(button);
|
663
|
}
|
664
|
}
|
665
|
|
666
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
667
|
|
668
|
public int getLevel()
|
669
|
{
|
670
|
return mLevelValue;
|
671
|
}
|
672
|
|
673
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
674
|
|
675
|
public boolean shouldReactToEndOfScrambling()
|
676
|
{
|
677
|
return mShouldReactToEndOfScrambling;
|
678
|
}
|
679
|
}
|