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.util.DisplayMetrics;
|
27
|
import android.view.Gravity;
|
28
|
import android.view.LayoutInflater;
|
29
|
import android.view.View;
|
30
|
import android.widget.AdapterView;
|
31
|
import android.widget.ArrayAdapter;
|
32
|
import android.widget.Button;
|
33
|
import android.widget.ImageButton;
|
34
|
import android.widget.LinearLayout;
|
35
|
import android.widget.PopupWindow;
|
36
|
import androidx.appcompat.widget.AppCompatSpinner;
|
37
|
|
38
|
import org.distorted.dialogs.RubikDialogEffects;
|
39
|
import org.distorted.main.R;
|
40
|
import org.distorted.main.RubikActivity;
|
41
|
import org.distorted.objects.RubikObjectList;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener
|
46
|
{
|
47
|
private static final int DEF_LEVEL = 1;
|
48
|
public static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal();
|
49
|
public static final int DEF_SIZE = 3;
|
50
|
|
51
|
private ImageButton mObjButton, mSettingsButton;
|
52
|
private Button mBackButton, mSolveButton, mPlayButton;
|
53
|
private PopupWindow mPopup;
|
54
|
private int mObject = DEF_OBJECT;
|
55
|
private int mSize = DEF_SIZE;
|
56
|
private int mLayoutWidth;
|
57
|
private LinearLayout mLayout;
|
58
|
private AppCompatSpinner mLevelSpinner;
|
59
|
private ArrayAdapter<String> mSpinnerAdapter;
|
60
|
private int mLevelValue;
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
void leaveState(RubikActivity act)
|
65
|
{
|
66
|
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
void enterState(final RubikActivity act)
|
72
|
{
|
73
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
74
|
final float scale = metrics.density;
|
75
|
|
76
|
// TOP ////////////////////////////
|
77
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
78
|
layoutTop.removeAllViews();
|
79
|
|
80
|
setupObjectButton(act,scale);
|
81
|
layoutTop.addView(mObjButton);
|
82
|
setupLevelSpinner(act,scale);
|
83
|
layoutTop.addView(mLevelSpinner);
|
84
|
setupPlayButton(act,scale);
|
85
|
layoutTop.addView(mPlayButton);
|
86
|
|
87
|
// BOT ////////////////////////////
|
88
|
|
89
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
90
|
layoutLeft.removeAllViews();
|
91
|
|
92
|
setupSettingsButton(act,scale);
|
93
|
layoutLeft.addView(mSettingsButton);
|
94
|
setupSolveButton(act,scale);
|
95
|
layoutLeft.addView(mSolveButton);
|
96
|
|
97
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
98
|
layoutRight.removeAllViews();
|
99
|
|
100
|
setupBackButton(act,scale);
|
101
|
layoutRight.addView(mBackButton);
|
102
|
|
103
|
setupPopupWindow(act, scale);
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
private void setupObjectButton(final RubikActivity act, final float scale)
|
109
|
{
|
110
|
int padding = (int)(3*scale + 0.5f);
|
111
|
LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
112
|
mObjButton = new ImageButton(act);
|
113
|
mObjButton.setLayoutParams(objectParams);
|
114
|
mObjButton.setPadding(padding,0,padding,0);
|
115
|
mObjButton.setImageResource(R.drawable.cube_menu);
|
116
|
|
117
|
mObjButton.setOnClickListener( new View.OnClickListener()
|
118
|
{
|
119
|
@Override
|
120
|
public void onClick(View view)
|
121
|
{
|
122
|
if( act.getPreRender().canPlay() )
|
123
|
{
|
124
|
int total = RubikObjectList.getTotal();
|
125
|
boolean vertical = act.isVertical();
|
126
|
mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
|
127
|
|
128
|
int width = view.getWidth();
|
129
|
int layhei = mLayoutWidth * (vertical? total:1);
|
130
|
int laywid = mLayoutWidth * (vertical? 1:total);
|
131
|
|
132
|
mPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
|
133
|
|
134
|
if( android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 )
|
135
|
{
|
136
|
mPopup.update(view, laywid, layhei);
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
});
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
private void setupSettingsButton(final RubikActivity act, final float scale)
|
146
|
{
|
147
|
int padding = (int)(3*scale + 0.5f);
|
148
|
LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
149
|
mSettingsButton = new ImageButton(act);
|
150
|
mSettingsButton.setLayoutParams(objectParams);
|
151
|
mSettingsButton.setPadding(padding,0,padding,0);
|
152
|
mSettingsButton.setImageResource(R.drawable.settings);
|
153
|
|
154
|
mSettingsButton.setOnClickListener( new View.OnClickListener()
|
155
|
{
|
156
|
@Override
|
157
|
public void onClick(View view)
|
158
|
{
|
159
|
RubikDialogEffects settings = new RubikDialogEffects();
|
160
|
settings.show(act.getSupportFragmentManager(), null);
|
161
|
}
|
162
|
});
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
private void setupLevelSpinner(final RubikActivity act, final float scale)
|
168
|
{
|
169
|
int spinnerPadding = (int)(scale* 10 + 0.5f);
|
170
|
int spinnerMargin = (int)(scale* 3 + 0.5f);
|
171
|
int spinnerLength = (int)(scale*150 + 0.5f);
|
172
|
LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(spinnerLength,LinearLayout.LayoutParams.MATCH_PARENT);
|
173
|
spinnerLayoutParams.topMargin = spinnerMargin;
|
174
|
spinnerLayoutParams.bottomMargin = spinnerMargin;
|
175
|
spinnerLayoutParams.leftMargin = spinnerMargin;
|
176
|
spinnerLayoutParams.rightMargin = 2*spinnerMargin;
|
177
|
|
178
|
mLevelSpinner = new AppCompatSpinner(act);
|
179
|
mLevelSpinner.setLayoutParams(spinnerLayoutParams);
|
180
|
mLevelSpinner.setPadding(spinnerPadding,0,spinnerPadding,0);
|
181
|
mLevelSpinner.setBackgroundResource(R.drawable.spinner);
|
182
|
mLevelSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
183
|
|
184
|
mLevelSpinner.setOnItemSelectedListener(this);
|
185
|
int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
|
186
|
int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
|
187
|
String[] levels = new String[maxLevel];
|
188
|
|
189
|
for(int i=0; i<maxLevel; i++)
|
190
|
{
|
191
|
levels[i] = act.getString(R.string.lv_placeholder,i+1);
|
192
|
}
|
193
|
|
194
|
if( mLevelValue>maxLevel ) mLevelValue=1;
|
195
|
|
196
|
mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
|
197
|
mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
198
|
mLevelSpinner.setAdapter(mSpinnerAdapter);
|
199
|
mLevelSpinner.setSelection(mLevelValue-1);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
private void setupPlayButton(final RubikActivity act, final float scale)
|
205
|
{
|
206
|
int padding = (int)(3*scale + 0.5f);
|
207
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
208
|
mPlayButton = new Button(act);
|
209
|
mPlayButton.setLayoutParams(backParams);
|
210
|
mPlayButton.setPadding(padding,0,padding,0);
|
211
|
mPlayButton.setText(R.string.play);
|
212
|
|
213
|
mPlayButton.setOnClickListener( new View.OnClickListener()
|
214
|
{
|
215
|
@Override
|
216
|
public void onClick(View v)
|
217
|
{
|
218
|
act.getPreRender().scrambleObject(mLevelValue);
|
219
|
}
|
220
|
});
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
private void setupSolveButton(final RubikActivity act, final float scale)
|
226
|
{
|
227
|
int padding = (int)(3*scale + 0.5f);
|
228
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
229
|
mSolveButton = new Button(act);
|
230
|
mSolveButton.setLayoutParams(backParams);
|
231
|
mSolveButton.setPadding(padding,0,padding,0);
|
232
|
mSolveButton.setText(R.string.solve);
|
233
|
|
234
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
235
|
{
|
236
|
@Override
|
237
|
public void onClick(View v)
|
238
|
{
|
239
|
act.getPreRender().solveObject();
|
240
|
}
|
241
|
});
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
private void setupBackButton(final RubikActivity act, final float scale)
|
247
|
{
|
248
|
int padding = (int)(3*scale + 0.5f);
|
249
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
250
|
mBackButton = new Button(act);
|
251
|
mBackButton.setLayoutParams(backParams);
|
252
|
mBackButton.setPadding(padding,0,padding,0);
|
253
|
mBackButton.setText(R.string.back);
|
254
|
|
255
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
256
|
{
|
257
|
@Override
|
258
|
public void onClick(View v)
|
259
|
{
|
260
|
if( act.getPreRender().canPlay() ) RubikState.goBack(act);
|
261
|
}
|
262
|
});
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
private void setupPopupWindow(final RubikActivity act, final float scale)
|
268
|
{
|
269
|
LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
270
|
final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
|
271
|
mLayout = layout.findViewById(R.id.popup);
|
272
|
|
273
|
mPopup = new PopupWindow(act);
|
274
|
mPopup.setContentView(layout);
|
275
|
mPopup.setFocusable(true);
|
276
|
int margin = (int)(5*scale + 0.5f);
|
277
|
|
278
|
BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
|
279
|
int cubeWidth = bd.getIntrinsicWidth();
|
280
|
mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
|
281
|
|
282
|
for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
|
283
|
{
|
284
|
final RubikObjectList list = RubikObjectList.getObject(object);
|
285
|
final int[] sizes = list.getSizes();
|
286
|
int[] icons = list.getIconIDs();
|
287
|
int len = sizes.length;
|
288
|
final int obj = object;
|
289
|
|
290
|
for(int i=0; i<len; i++)
|
291
|
{
|
292
|
final int size = i;
|
293
|
|
294
|
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
|
295
|
p.setMargins(margin, margin, margin, margin);
|
296
|
|
297
|
ImageButton button = new ImageButton(act);
|
298
|
button.setLayoutParams(p);
|
299
|
|
300
|
button.setBackgroundResource(icons[i]);
|
301
|
button.setOnClickListener( new View.OnClickListener()
|
302
|
{
|
303
|
@Override
|
304
|
public void onClick(View v)
|
305
|
{
|
306
|
if( act.getPreRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
|
307
|
{
|
308
|
mObject = obj;
|
309
|
mSize = sizes[size];
|
310
|
act.changeObject(list,sizes[size], true);
|
311
|
adjustSpinner(act);
|
312
|
}
|
313
|
|
314
|
mPopup.dismiss();
|
315
|
}
|
316
|
});
|
317
|
|
318
|
mLayout.addView(button);
|
319
|
}
|
320
|
}
|
321
|
}
|
322
|
|
323
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324
|
|
325
|
public void savePreferences(SharedPreferences.Editor editor)
|
326
|
{
|
327
|
editor.putInt("statePlay_level" , mLevelValue);
|
328
|
editor.putInt("statePlay_object", mObject);
|
329
|
editor.putInt("statePlay_size" , mSize);
|
330
|
|
331
|
if( mPopup!=null )
|
332
|
{
|
333
|
mPopup.dismiss();
|
334
|
mPopup = null;
|
335
|
}
|
336
|
}
|
337
|
|
338
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
339
|
|
340
|
public void restorePreferences(SharedPreferences preferences)
|
341
|
{
|
342
|
mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
|
343
|
mObject = preferences.getInt("statePlay_object", DEF_OBJECT);
|
344
|
mSize = preferences.getInt("statePlay_size" , DEF_SIZE );
|
345
|
}
|
346
|
|
347
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
348
|
|
349
|
public boolean setObjectAndSize(RubikActivity act, RubikObjectList obj, int size)
|
350
|
{
|
351
|
if( mObject!=obj.ordinal() || mSize != size )
|
352
|
{
|
353
|
boolean success = false;
|
354
|
|
355
|
for( int s: obj.getSizes() )
|
356
|
if( s==size )
|
357
|
{
|
358
|
success = true;
|
359
|
break;
|
360
|
}
|
361
|
|
362
|
if( success )
|
363
|
{
|
364
|
mObject = obj.ordinal();
|
365
|
mSize = size;
|
366
|
|
367
|
if( mLevelSpinner!=null ) adjustSpinner(act);
|
368
|
}
|
369
|
|
370
|
return success;
|
371
|
}
|
372
|
|
373
|
return true;
|
374
|
}
|
375
|
|
376
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
377
|
|
378
|
private void adjustSpinner(RubikActivity act)
|
379
|
{
|
380
|
int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
|
381
|
int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
|
382
|
String[] levels = new String[maxLevel];
|
383
|
|
384
|
for(int i=0; i<maxLevel; i++)
|
385
|
{
|
386
|
levels[i] = act.getString(R.string.lv_placeholder,i+1);
|
387
|
}
|
388
|
|
389
|
mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
|
390
|
mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
391
|
mLevelSpinner.setAdapter(mSpinnerAdapter);
|
392
|
}
|
393
|
|
394
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
395
|
|
396
|
int getLevel()
|
397
|
{
|
398
|
return mLevelValue;
|
399
|
}
|
400
|
|
401
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402
|
|
403
|
public int getObject()
|
404
|
{
|
405
|
return mObject;
|
406
|
}
|
407
|
|
408
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
409
|
|
410
|
public int getSize()
|
411
|
{
|
412
|
return mSize;
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
|
417
|
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
|
418
|
{
|
419
|
mLevelValue = pos+1;
|
420
|
}
|
421
|
|
422
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
423
|
|
424
|
public void onNothingSelected(AdapterView<?> parent) { }
|
425
|
}
|