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