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