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