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.states;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.content.SharedPreferences;
|
24
|
import android.graphics.drawable.BitmapDrawable;
|
25
|
import android.os.Bundle;
|
26
|
import android.util.TypedValue;
|
27
|
import android.view.Gravity;
|
28
|
import android.view.LayoutInflater;
|
29
|
import android.view.View;
|
30
|
import android.widget.Button;
|
31
|
import android.widget.GridLayout;
|
32
|
import android.widget.ImageButton;
|
33
|
import android.widget.LinearLayout;
|
34
|
import android.widget.PopupWindow;
|
35
|
|
36
|
import org.distorted.dialogs.RubikDialogAbout;
|
37
|
import org.distorted.dialogs.RubikDialogScores;
|
38
|
import org.distorted.main.R;
|
39
|
import org.distorted.main.RubikActivity;
|
40
|
import org.distorted.main.RubikPreRender;
|
41
|
import org.distorted.objects.RubikObject;
|
42
|
import org.distorted.objects.RubikObjectList;
|
43
|
import org.distorted.scores.RubikScores;
|
44
|
|
45
|
import java.util.ArrayList;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
public class RubikStatePlay extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
|
50
|
{
|
51
|
private static final int DURATION_MILLIS = 750;
|
52
|
private static final int LEVELS_SHOWN = 10;
|
53
|
public static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal();
|
54
|
public static final int DEF_SIZE = 3;
|
55
|
|
56
|
private static int[] BUTTON_LABELS = { R.string.scores, R.string.patterns, R.string.solver, R.string.about };
|
57
|
private static final int NUM_BUTTONS = BUTTON_LABELS.length;
|
58
|
|
59
|
private ImageButton mObjButton, mMenuButton, mPrevButton, mSolveButton, mLockButton;
|
60
|
private Button mPlayButton;
|
61
|
private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
|
62
|
private int mObject = DEF_OBJECT;
|
63
|
private int mSize = DEF_SIZE;
|
64
|
private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
|
65
|
private int mLevelValue;
|
66
|
private float mButtonSize, mMenuItemSize, mMenuTextSize;
|
67
|
private int mColCount, mRowCount;
|
68
|
private LinearLayout mPlayLayout;
|
69
|
|
70
|
private ArrayList<Move> mMoves;
|
71
|
private boolean mCanPrevMove;
|
72
|
|
73
|
private static class Move
|
74
|
{
|
75
|
private int mAxis, mRow, mAngle;
|
76
|
|
77
|
Move(int axis, int row, int angle)
|
78
|
{
|
79
|
mAxis = axis;
|
80
|
mRow = row;
|
81
|
mAngle= angle;
|
82
|
}
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
void leaveState(RubikActivity act)
|
88
|
{
|
89
|
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
void enterState(final RubikActivity act)
|
95
|
{
|
96
|
float width = act.getScreenWidthInPixels();
|
97
|
mMenuTextSize = width*RubikActivity.MENU_MEDIUM_TEXT_SIZE;
|
98
|
mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
|
99
|
mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
|
100
|
|
101
|
mCanPrevMove = true;
|
102
|
|
103
|
if( mMoves==null ) mMoves = new ArrayList<>();
|
104
|
else mMoves.clear();
|
105
|
|
106
|
mRowCount = RubikObjectList.getRowCount();
|
107
|
mColCount = RubikObjectList.getColumnCount();
|
108
|
|
109
|
// TOP ////////////////////////////
|
110
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
111
|
layoutTop.removeAllViews();
|
112
|
|
113
|
setupObjectWindow(act,width);
|
114
|
setupObjectButton(act,width);
|
115
|
layoutTop.addView(mObjButton);
|
116
|
|
117
|
setupMenuWindow(act,width);
|
118
|
setupMenuButton(act,width);
|
119
|
layoutTop.addView(mMenuButton);
|
120
|
|
121
|
setupPlayWindow(act,width);
|
122
|
setupPlayButton(act,width);
|
123
|
layoutTop.addView(mPlayButton);
|
124
|
|
125
|
// BOT ////////////////////////////
|
126
|
LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
|
127
|
layoutBot.removeAllViews();
|
128
|
|
129
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
|
130
|
|
131
|
LinearLayout layoutLeft = new LinearLayout(act);
|
132
|
layoutLeft.setLayoutParams(params);
|
133
|
LinearLayout layoutMid = new LinearLayout(act);
|
134
|
layoutMid.setLayoutParams(params);
|
135
|
LinearLayout layoutRight = new LinearLayout(act);
|
136
|
layoutRight.setLayoutParams(params);
|
137
|
|
138
|
setupPrevButton(act,width);
|
139
|
layoutLeft.addView(mPrevButton);
|
140
|
setupLockButton(act,width);
|
141
|
layoutMid.addView(mLockButton);
|
142
|
setupSolveButton(act,width);
|
143
|
layoutRight.addView(mSolveButton);
|
144
|
|
145
|
layoutBot.addView(layoutLeft);
|
146
|
layoutBot.addView(layoutMid);
|
147
|
layoutBot.addView(layoutRight);
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
private void setupObjectButton(final RubikActivity act, final float width)
|
153
|
{
|
154
|
final int padding = (int)(width*RubikActivity.PADDING);
|
155
|
final int margin = (int)(width*RubikActivity.MARGIN);
|
156
|
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);
|
157
|
|
158
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
|
159
|
params.topMargin = margin;
|
160
|
params.bottomMargin = margin;
|
161
|
params.leftMargin = margin;
|
162
|
params.rightMargin = margin;
|
163
|
|
164
|
mObjButton = new ImageButton(act);
|
165
|
mObjButton.setLayoutParams(params);
|
166
|
mObjButton.setPadding(padding,0,padding,0);
|
167
|
mObjButton.setImageResource(icon);
|
168
|
|
169
|
mObjButton.setOnClickListener( new View.OnClickListener()
|
170
|
{
|
171
|
@Override
|
172
|
public void onClick(View view)
|
173
|
{
|
174
|
if( act.getPreRender().canPlay() )
|
175
|
{
|
176
|
mObjectPopup.setFocusable(false);
|
177
|
mObjectPopup.update();
|
178
|
|
179
|
View popupView = mObjectPopup.getContentView();
|
180
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
181
|
|
182
|
mObjectPopup.showAsDropDown(view, margin, margin, Gravity.LEFT);
|
183
|
mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*mRowCount);
|
184
|
|
185
|
mObjectPopup.setFocusable(true);
|
186
|
mObjectPopup.update();
|
187
|
|
188
|
}
|
189
|
}
|
190
|
});
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
private void setupPlayButton(final RubikActivity act, final float width)
|
196
|
{
|
197
|
final int padding = (int)(width*RubikActivity.PADDING);
|
198
|
final int margin = (int)(width*RubikActivity.MARGIN);
|
199
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
|
200
|
params.topMargin = margin;
|
201
|
params.bottomMargin = margin;
|
202
|
params.leftMargin = margin;
|
203
|
params.rightMargin = margin;
|
204
|
|
205
|
mPlayButton = new Button(act);
|
206
|
mPlayButton.setLayoutParams(params);
|
207
|
mPlayButton.setPadding(padding,0,padding,0);
|
208
|
mPlayButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
209
|
mPlayButton.setText(R.string.play);
|
210
|
|
211
|
mPlayButton.setOnClickListener( new View.OnClickListener()
|
212
|
{
|
213
|
@Override
|
214
|
public void onClick(View view)
|
215
|
{
|
216
|
mPlayPopup.setFocusable(false);
|
217
|
mPlayPopup.update();
|
218
|
|
219
|
View popupView = mPlayPopup.getContentView();
|
220
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
221
|
|
222
|
final int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
|
223
|
final int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
|
224
|
final int levelsShown = Math.min(maxLevel,LEVELS_SHOWN);
|
225
|
|
226
|
mPlayPopup.showAsDropDown(view, margin, margin, Gravity.RIGHT);
|
227
|
mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+2*margin));
|
228
|
|
229
|
mPlayPopup.setFocusable(true);
|
230
|
mPlayPopup.update();
|
231
|
}
|
232
|
});
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
private void setupMenuButton(final RubikActivity act, final float width)
|
238
|
{
|
239
|
final int padding = (int)(width*RubikActivity.PADDING);
|
240
|
final int margin = (int)(width*RubikActivity.MARGIN);
|
241
|
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);
|
242
|
|
243
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
|
244
|
params.topMargin = margin;
|
245
|
params.bottomMargin = margin;
|
246
|
params.leftMargin = margin;
|
247
|
params.rightMargin = margin;
|
248
|
|
249
|
mMenuButton = new ImageButton(act);
|
250
|
mMenuButton.setLayoutParams(params);
|
251
|
mMenuButton.setPadding(padding,0,padding,0);
|
252
|
mMenuButton.setImageResource(icon);
|
253
|
|
254
|
mMenuButton.setOnClickListener( new View.OnClickListener()
|
255
|
{
|
256
|
@Override
|
257
|
public void onClick(View view)
|
258
|
{
|
259
|
if( act.getPreRender().canPlay() )
|
260
|
{
|
261
|
mMenuPopup.setFocusable(false);
|
262
|
mMenuPopup.update();
|
263
|
|
264
|
View popupView = mMenuPopup.getContentView();
|
265
|
popupView.setSystemUiVisibility(RubikActivity.FLAGS);
|
266
|
|
267
|
mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
|
268
|
mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
|
269
|
|
270
|
mMenuPopup.setFocusable(true);
|
271
|
mMenuPopup.update();
|
272
|
}
|
273
|
}
|
274
|
});
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
private void setupSolveButton(final RubikActivity act, final float width)
|
280
|
{
|
281
|
int padding = (int)(width*RubikActivity.PADDING);
|
282
|
int margin = (int)(width*RubikActivity.MARGIN);
|
283
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
|
284
|
|
285
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
|
286
|
params.topMargin = margin;
|
287
|
params.bottomMargin = margin;
|
288
|
params.leftMargin = margin;
|
289
|
params.rightMargin = margin;
|
290
|
|
291
|
mSolveButton = new ImageButton(act);
|
292
|
mSolveButton.setLayoutParams(params);
|
293
|
mSolveButton.setPadding(padding,0,padding,0);
|
294
|
mSolveButton.setImageResource(icon);
|
295
|
|
296
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
297
|
{
|
298
|
@Override
|
299
|
public void onClick(View v)
|
300
|
{
|
301
|
act.getPreRender().solveObject();
|
302
|
mMoves.clear();
|
303
|
}
|
304
|
});
|
305
|
}
|
306
|
|
307
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
308
|
|
309
|
private void setupLockButton(final RubikActivity act, final float width)
|
310
|
{
|
311
|
int padding = (int)(width*RubikActivity.PADDING);
|
312
|
int margin = (int)(width*RubikActivity.MARGIN);
|
313
|
|
314
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
|
315
|
params.topMargin = margin;
|
316
|
params.bottomMargin = margin;
|
317
|
params.leftMargin = margin;
|
318
|
params.rightMargin = margin;
|
319
|
|
320
|
mLockButton = new ImageButton(act);
|
321
|
mLockButton.setLayoutParams(params);
|
322
|
mLockButton.setPadding(padding,0,padding,0);
|
323
|
mLockButton.setImageResource(getLockIcon(act));
|
324
|
|
325
|
mLockButton.setOnClickListener( new View.OnClickListener()
|
326
|
{
|
327
|
@Override
|
328
|
public void onClick(View v)
|
329
|
{
|
330
|
toggleLock(act);
|
331
|
}
|
332
|
});
|
333
|
}
|
334
|
|
335
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
336
|
|
337
|
private void setupPrevButton(final RubikActivity act, final float width)
|
338
|
{
|
339
|
int padding = (int)(width*RubikActivity.PADDING);
|
340
|
int margin = (int)(width*RubikActivity.MARGIN);
|
341
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_back,R.drawable.ui_medium_cube_back, R.drawable.ui_big_cube_back, R.drawable.ui_huge_cube_back);
|
342
|
|
343
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
|
344
|
params.topMargin = margin;
|
345
|
params.bottomMargin = margin;
|
346
|
params.leftMargin = margin;
|
347
|
params.rightMargin = margin;
|
348
|
|
349
|
mPrevButton = new ImageButton(act);
|
350
|
mPrevButton.setLayoutParams(params);
|
351
|
mPrevButton.setPadding(padding,0,padding,0);
|
352
|
mPrevButton.setImageResource(icon);
|
353
|
|
354
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
355
|
{
|
356
|
@Override
|
357
|
public void onClick(View v)
|
358
|
{
|
359
|
RubikPreRender pre = act.getPreRender();
|
360
|
backMove(pre);
|
361
|
}
|
362
|
});
|
363
|
}
|
364
|
|
365
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366
|
|
367
|
private void setupObjectWindow(final RubikActivity act, final float width)
|
368
|
{
|
369
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
370
|
final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
|
371
|
GridLayout objectGrid = layout.findViewById(R.id.objectGrid);
|
372
|
|
373
|
int[] indices = RubikObjectList.getIndices();
|
374
|
|
375
|
GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
|
376
|
GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
|
377
|
|
378
|
objectGrid.setColumnCount(mColCount);
|
379
|
objectGrid.setRowCount(mRowCount);
|
380
|
|
381
|
int[] nextInColumn = new int[mColCount];
|
382
|
|
383
|
for(int row=0; row<mRowCount; row++)
|
384
|
{
|
385
|
rowSpecs[row] = GridLayout.spec(row);
|
386
|
}
|
387
|
for(int col=0; col<mColCount; col++)
|
388
|
{
|
389
|
colSpecs[col] = GridLayout.spec(col);
|
390
|
nextInColumn[col] =0;
|
391
|
}
|
392
|
|
393
|
mObjectPopup = new PopupWindow(act);
|
394
|
mObjectPopup.setContentView(layout);
|
395
|
mObjectPopup.setFocusable(true);
|
396
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
|
397
|
|
398
|
BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
|
399
|
int cubeWidth = bd.getIntrinsicWidth();
|
400
|
int margin = (int)(width*RubikActivity.LARGE_MARGIN);
|
401
|
mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
|
402
|
|
403
|
for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
|
404
|
{
|
405
|
final RubikObjectList list = RubikObjectList.getObject(object);
|
406
|
final int[] sizes = list.getSizes();
|
407
|
int[] icons = list.getIconIDs();
|
408
|
int len = sizes.length;
|
409
|
final int obj = object;
|
410
|
int col = indices[object];
|
411
|
|
412
|
for(int i=0; i<len; i++)
|
413
|
{
|
414
|
final int index = i;
|
415
|
|
416
|
ImageButton button = new ImageButton(act);
|
417
|
button.setBackgroundResource(icons[i]);
|
418
|
button.setOnClickListener( new View.OnClickListener()
|
419
|
{
|
420
|
@Override
|
421
|
public void onClick(View v)
|
422
|
{
|
423
|
if( act.getPreRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
|
424
|
{
|
425
|
mObject = obj;
|
426
|
mSize = sizes[index];
|
427
|
act.changeObject(list,sizes[index], true);
|
428
|
adjustLevels(act);
|
429
|
mMoves.clear();
|
430
|
}
|
431
|
|
432
|
mObjectPopup.dismiss();
|
433
|
}
|
434
|
});
|
435
|
|
436
|
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[nextInColumn[col]],colSpecs[col]);
|
437
|
params.bottomMargin = margin;
|
438
|
params.topMargin = margin;
|
439
|
params.leftMargin = margin;
|
440
|
params.rightMargin = margin;
|
441
|
|
442
|
nextInColumn[col]++;
|
443
|
|
444
|
objectGrid.addView(button, params);
|
445
|
}
|
446
|
}
|
447
|
}
|
448
|
|
449
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
450
|
|
451
|
private void setupMenuWindow(final RubikActivity act, final float width)
|
452
|
{
|
453
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
454
|
final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
|
455
|
LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
|
456
|
|
457
|
mMenuPopup = new PopupWindow(act);
|
458
|
mMenuPopup.setContentView(layout);
|
459
|
mMenuPopup.setFocusable(true);
|
460
|
int margin = (int)(width*RubikActivity.MARGIN);
|
461
|
int padding = (int)(width*RubikActivity.PADDING);
|
462
|
|
463
|
mMenuLayoutWidth = (int)(width/2);
|
464
|
mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
|
465
|
|
466
|
LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
|
467
|
pT.setMargins(margin, 0, margin, margin);
|
468
|
LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
|
469
|
pM.setMargins(margin, margin, margin, margin);
|
470
|
LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
|
471
|
pB.setMargins(margin, margin, margin, 2*margin);
|
472
|
|
473
|
for(int i=0; i<NUM_BUTTONS; i++)
|
474
|
{
|
475
|
final int but = i;
|
476
|
Button button = new Button(act);
|
477
|
button.setLayoutParams(pT);//i==0 ? pT : (i==NUM_BUTTONS-1 ? pB : pM));
|
478
|
button.setText(BUTTON_LABELS[i]);
|
479
|
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
|
480
|
|
481
|
button.setOnClickListener( new View.OnClickListener()
|
482
|
{
|
483
|
@Override
|
484
|
public void onClick(View v)
|
485
|
{
|
486
|
mMenuPopup.dismiss();
|
487
|
MenuAction(act,but);
|
488
|
}
|
489
|
});
|
490
|
|
491
|
menuLayout.addView(button);
|
492
|
}
|
493
|
}
|
494
|
|
495
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
496
|
|
497
|
private void setupPlayWindow(final RubikActivity act, final float width)
|
498
|
{
|
499
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
500
|
final View layout = layoutInflater.inflate(R.layout.popup_play, null);
|
501
|
mPlayLayout = layout.findViewById(R.id.playGrid);
|
502
|
|
503
|
mPlayLayoutWidth = (int)(width*0.4f);
|
504
|
|
505
|
mPlayPopup = new PopupWindow(act);
|
506
|
mPlayPopup.setContentView(layout);
|
507
|
mPlayPopup.setFocusable(true);
|
508
|
|
509
|
adjustLevels(act);
|
510
|
}
|
511
|
|
512
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
513
|
|
514
|
private void backMove(RubikPreRender pre)
|
515
|
{
|
516
|
if( mCanPrevMove )
|
517
|
{
|
518
|
int numMoves = mMoves.size();
|
519
|
|
520
|
if( numMoves>0 )
|
521
|
{
|
522
|
Move move = mMoves.remove(numMoves-1);
|
523
|
RubikObject object = pre.getObject();
|
524
|
|
525
|
int axis = move.mAxis;
|
526
|
int row = (1<<move.mRow);
|
527
|
int angle = move.mAngle;
|
528
|
int numRot= Math.abs(angle*object.getBasicAngle()/360);
|
529
|
|
530
|
if( angle!=0 )
|
531
|
{
|
532
|
mCanPrevMove = false;
|
533
|
pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
|
534
|
}
|
535
|
else
|
536
|
{
|
537
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
538
|
}
|
539
|
}
|
540
|
}
|
541
|
}
|
542
|
|
543
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
544
|
|
545
|
private void MenuAction(RubikActivity act, int button)
|
546
|
{
|
547
|
switch(button)
|
548
|
{
|
549
|
case 0: RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
|
550
|
int object = play.getObject();
|
551
|
int size = play.getSize();
|
552
|
int sizeIndex = RubikObjectList.getSizeIndex(object,size);
|
553
|
|
554
|
Bundle bundle = new Bundle();
|
555
|
bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
|
556
|
bundle.putBoolean("submitting", false);
|
557
|
|
558
|
RubikDialogScores scores = new RubikDialogScores();
|
559
|
scores.setArguments(bundle);
|
560
|
scores.show(act.getSupportFragmentManager(), null);
|
561
|
break;
|
562
|
case 1: RubikState.switchState(act,RubikState.PATT);
|
563
|
break;
|
564
|
case 2: RubikState.switchState(act,RubikState.SVER);
|
565
|
break;
|
566
|
case 3: RubikDialogAbout diag = new RubikDialogAbout();
|
567
|
diag.show(act.getSupportFragmentManager(), null);
|
568
|
break;
|
569
|
}
|
570
|
}
|
571
|
|
572
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
573
|
|
574
|
private void toggleLock(RubikActivity act)
|
575
|
{
|
576
|
act.toggleLock();
|
577
|
mLockButton.setImageResource(getLockIcon(act));
|
578
|
}
|
579
|
|
580
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
581
|
|
582
|
private int getLockIcon(RubikActivity act)
|
583
|
{
|
584
|
if( act.retLocked() )
|
585
|
{
|
586
|
return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
|
587
|
}
|
588
|
else
|
589
|
{
|
590
|
return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
|
591
|
}
|
592
|
}
|
593
|
|
594
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
595
|
|
596
|
public void savePreferences(SharedPreferences.Editor editor)
|
597
|
{
|
598
|
editor.putInt("statePlay_object", mObject);
|
599
|
editor.putInt("statePlay_size" , mSize);
|
600
|
|
601
|
if( mObjectPopup!=null )
|
602
|
{
|
603
|
mObjectPopup.dismiss();
|
604
|
mObjectPopup = null;
|
605
|
}
|
606
|
|
607
|
if( mMenuPopup!=null )
|
608
|
{
|
609
|
mMenuPopup.dismiss();
|
610
|
mMenuPopup = null;
|
611
|
}
|
612
|
|
613
|
if( mPlayPopup!=null )
|
614
|
{
|
615
|
mPlayPopup.dismiss();
|
616
|
mPlayPopup = null;
|
617
|
}
|
618
|
}
|
619
|
|
620
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
621
|
|
622
|
public void restorePreferences(SharedPreferences preferences)
|
623
|
{
|
624
|
mObject = preferences.getInt("statePlay_object", DEF_OBJECT);
|
625
|
mSize = preferences.getInt("statePlay_size" , DEF_SIZE );
|
626
|
}
|
627
|
|
628
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
629
|
|
630
|
public boolean setObjectAndSize(RubikActivity act, RubikObjectList obj, int size)
|
631
|
{
|
632
|
if( mObject!=obj.ordinal() || mSize != size )
|
633
|
{
|
634
|
boolean success = false;
|
635
|
|
636
|
for( int s: obj.getSizes() )
|
637
|
if( s==size )
|
638
|
{
|
639
|
success = true;
|
640
|
break;
|
641
|
}
|
642
|
|
643
|
if( success )
|
644
|
{
|
645
|
mObject = obj.ordinal();
|
646
|
mSize = size;
|
647
|
|
648
|
if( mPlayLayout!=null ) adjustLevels(act);
|
649
|
}
|
650
|
|
651
|
return success;
|
652
|
}
|
653
|
|
654
|
return true;
|
655
|
}
|
656
|
|
657
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
658
|
|
659
|
private void adjustLevels(final RubikActivity act)
|
660
|
{
|
661
|
int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
|
662
|
int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
|
663
|
String[] levels = new String[maxLevel];
|
664
|
|
665
|
for(int i=0; i<maxLevel; i++)
|
666
|
{
|
667
|
levels[i] = act.getString(R.string.lv_placeholder,i+1);
|
668
|
}
|
669
|
|
670
|
if( mLevelValue>maxLevel ) mLevelValue=1;
|
671
|
|
672
|
float width = act.getScreenWidthInPixels();
|
673
|
int margin = (int)(width*RubikActivity.MARGIN);
|
674
|
int padding = (int)(width*RubikActivity.PADDING);
|
675
|
|
676
|
LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
|
677
|
pM.setMargins(margin, 0, margin, margin);
|
678
|
LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
|
679
|
pT.setMargins(margin, margin, margin, margin);
|
680
|
LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
|
681
|
pB.setMargins(margin, margin, margin, 2*margin);
|
682
|
|
683
|
mPlayLayout.removeAllViews();
|
684
|
|
685
|
int realSize= RubikObjectList.getSizeIndex(mObject,mSize);
|
686
|
RubikScores scores = RubikScores.getInstance();
|
687
|
|
688
|
for(int i=0; i<maxLevel; i++)
|
689
|
{
|
690
|
final int but = i;
|
691
|
Button button = new Button(act);
|
692
|
button.setLayoutParams(i==0 ? pT : (i==maxLevel-1 ? pB : pM));
|
693
|
button.setText(levels[i]);
|
694
|
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
|
695
|
|
696
|
int icon = scores.isSolved(mObject, realSize, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
|
697
|
button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
|
698
|
|
699
|
button.setOnClickListener( new View.OnClickListener()
|
700
|
{
|
701
|
@Override
|
702
|
public void onClick(View v)
|
703
|
{
|
704
|
mPlayPopup.dismiss();
|
705
|
mLevelValue = but+1;
|
706
|
act.getPreRender().scrambleObject(mLevelValue);
|
707
|
}
|
708
|
});
|
709
|
|
710
|
mPlayLayout.addView(button);
|
711
|
}
|
712
|
}
|
713
|
|
714
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
715
|
|
716
|
int getLevel()
|
717
|
{
|
718
|
return mLevelValue;
|
719
|
}
|
720
|
|
721
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
722
|
|
723
|
public void addMove(int axis, int row, int angle)
|
724
|
{
|
725
|
mMoves.add(new Move(axis,row,angle));
|
726
|
}
|
727
|
|
728
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
729
|
|
730
|
public int getObject()
|
731
|
{
|
732
|
return mObject;
|
733
|
}
|
734
|
|
735
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
736
|
|
737
|
public int getSize()
|
738
|
{
|
739
|
return mSize;
|
740
|
}
|
741
|
|
742
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
743
|
|
744
|
public void onActionFinished(final long effectID)
|
745
|
{
|
746
|
mCanPrevMove = true;
|
747
|
}
|
748
|
}
|