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