1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.screens;
|
11
|
|
12
|
import static org.distorted.objectlib.main.TwistyObject.*;
|
13
|
|
14
|
import java.lang.ref.WeakReference;
|
15
|
|
16
|
import android.content.SharedPreferences;
|
17
|
import android.graphics.Bitmap;
|
18
|
import android.graphics.Canvas;
|
19
|
import android.graphics.Paint;
|
20
|
import android.graphics.PorterDuff;
|
21
|
import android.graphics.drawable.Drawable;
|
22
|
import android.os.Bundle;
|
23
|
import androidx.core.content.ContextCompat;
|
24
|
import android.view.View;
|
25
|
import android.widget.ImageButton;
|
26
|
import android.widget.LinearLayout;
|
27
|
|
28
|
import org.distorted.objectlib.main.ObjectControl;
|
29
|
import org.distorted.objectlib.main.ObjectSignatures;
|
30
|
import org.distorted.objectlib.main.TwistyObject;
|
31
|
|
32
|
import org.distorted.dialogs.RubikDialogSolverError;
|
33
|
import org.distorted.helpers.TransparentImageButton;
|
34
|
import org.distorted.main.R;
|
35
|
import org.distorted.main.RubikActivity;
|
36
|
import org.distorted.objectlib.shape.ShapeDiamond;
|
37
|
import org.distorted.objectlib.shape.ShapeDodecahedron;
|
38
|
import org.distorted.objectlib.shape.ShapeHexahedron;
|
39
|
import org.distorted.objectlib.shape.ShapeIcosahedron;
|
40
|
import org.distorted.objectlib.shape.ShapeOctahedron;
|
41
|
import org.distorted.objectlib.shape.ShapeTetrahedron;
|
42
|
import org.distorted.objects.RubikObjectList;
|
43
|
import org.distorted.solvers.ImplementedSolversList;
|
44
|
import org.distorted.solvers.SolverMain;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
public class RubikScreenSolver extends RubikScreenAbstract
|
49
|
{
|
50
|
private static final int RESET_DURATION = 1000;
|
51
|
private static final int MODE_NORMAL = 0;
|
52
|
private static final int MODE_DINO_4 = 1;
|
53
|
|
54
|
private static Bitmap[] mBitmap;
|
55
|
private ImageButton[] mColorButton;
|
56
|
private TransparentImageButton mResetButton,mBackButton, mSolveButton;
|
57
|
private boolean mSolving;
|
58
|
private int mCurrentColor, mCurrentButton;
|
59
|
private int[] mFaceColors;
|
60
|
private int mColorMode;
|
61
|
private int mNumColors;
|
62
|
private float mBitmapSize;
|
63
|
private WeakReference<RubikActivity> mWeakAct;
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
void leaveScreen(RubikActivity act)
|
68
|
{
|
69
|
ObjectControl control = act.getControl();
|
70
|
control.unsetLock();
|
71
|
}
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
void enterScreen(final RubikActivity act)
|
76
|
{
|
77
|
ObjectControl control = act.getControl();
|
78
|
control.setLock(false);
|
79
|
|
80
|
float width = act.getScreenWidthInPixels();
|
81
|
float heigh = act.getScreenHeightInPixels();
|
82
|
|
83
|
mWeakAct = new WeakReference<>(act);
|
84
|
mSolving = false;
|
85
|
|
86
|
int solverIndex= act.getSolverIndex();
|
87
|
ImplementedSolversList currentSolver = ImplementedSolversList.getSolver(solverIndex);
|
88
|
int currentObject = currentSolver.getObject();
|
89
|
act.changeIfDifferent(currentObject,control);
|
90
|
control.solveOnly();
|
91
|
RubikObjectList.setCurrObject(currentObject);
|
92
|
|
93
|
generateFaceColors(currentObject);
|
94
|
|
95
|
final float BUTTON_RATIO = 0.75f;
|
96
|
int sizeV = (int)(heigh*RubikActivity.RATIO_BAR*BUTTON_RATIO);
|
97
|
int sizeH = (int)((width/mNumColors)*BUTTON_RATIO);
|
98
|
mBitmapSize = Math.min(sizeV,sizeH);
|
99
|
|
100
|
// TOP ////////////////////////////
|
101
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
102
|
layoutTop.removeAllViews();
|
103
|
|
104
|
if( mNumColors>0 )
|
105
|
{
|
106
|
setupBitmaps();
|
107
|
setupColorButtons(act,width);
|
108
|
markButton(act);
|
109
|
}
|
110
|
|
111
|
for(ImageButton button: mColorButton) layoutTop.addView(button);
|
112
|
|
113
|
// BOT ////////////////////////////
|
114
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
|
115
|
|
116
|
LinearLayout layoutL = new LinearLayout(act);
|
117
|
layoutL.setLayoutParams(params);
|
118
|
LinearLayout layoutM = new LinearLayout(act);
|
119
|
layoutM.setLayoutParams(params);
|
120
|
LinearLayout layoutR = new LinearLayout(act);
|
121
|
layoutR.setLayoutParams(params);
|
122
|
|
123
|
LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
|
124
|
layoutBot.removeAllViews();
|
125
|
|
126
|
setupResetButton(act);
|
127
|
setupSolveButton(act);
|
128
|
setupBackButton(act);
|
129
|
|
130
|
layoutL.addView(mResetButton);
|
131
|
layoutM.addView(mSolveButton);
|
132
|
layoutR.addView(mBackButton);
|
133
|
|
134
|
layoutBot.addView(layoutL);
|
135
|
layoutBot.addView(layoutM);
|
136
|
layoutBot.addView(layoutR);
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
// This doesn't quite work in many cases, but in case of the solvers that will pop up in foreseeable
|
141
|
// future it should be ok.
|
142
|
|
143
|
public void generateFaceColors(int object)
|
144
|
{
|
145
|
mColorMode = MODE_NORMAL;
|
146
|
|
147
|
if( object== ObjectSignatures.PYRA_3 ||
|
148
|
object== ObjectSignatures.PYRA_4 ||
|
149
|
object== ObjectSignatures.PYRA_5 ||
|
150
|
object== ObjectSignatures.PDUO_2 ||
|
151
|
object== ObjectSignatures.JING_2 ||
|
152
|
object== ObjectSignatures.MORP_2 ||
|
153
|
object== ObjectSignatures.MORP_3 ||
|
154
|
object== ObjectSignatures.MORP_4 )
|
155
|
{
|
156
|
mNumColors = ShapeTetrahedron.NUM_FACES;
|
157
|
mFaceColors = ShapeTetrahedron.FACE_COLORS;
|
158
|
}
|
159
|
else if( object== ObjectSignatures.DIAM_2 ||
|
160
|
object== ObjectSignatures.DIAM_3 ||
|
161
|
object== ObjectSignatures.DIAM_4 ||
|
162
|
object== ObjectSignatures.TRAJ_3 ||
|
163
|
object== ObjectSignatures.TRAJ_4 ||
|
164
|
object== ObjectSignatures.PDIA_3 )
|
165
|
{
|
166
|
mNumColors = ShapeOctahedron.NUM_FACES;
|
167
|
mFaceColors = ShapeOctahedron.FACE_COLORS;
|
168
|
}
|
169
|
else if( object== ObjectSignatures.CRYS_3 ||
|
170
|
object== ObjectSignatures.STAR_3 ||
|
171
|
object== ObjectSignatures.PENT_2 ||
|
172
|
object== ObjectSignatures.KILO_3 ||
|
173
|
object== ObjectSignatures.KILO_5 ||
|
174
|
object== ObjectSignatures.MEGA_3 ||
|
175
|
object== ObjectSignatures.MEGA_5 )
|
176
|
{
|
177
|
mNumColors = ShapeDodecahedron.NUM_FACES;
|
178
|
mFaceColors = ShapeDodecahedron.FACE_COLORS;
|
179
|
}
|
180
|
else if( object== ObjectSignatures.BALL_4 )
|
181
|
{
|
182
|
mNumColors = ShapeDiamond.NUM_FACES;
|
183
|
mFaceColors = ShapeDiamond.FACE_COLORS;
|
184
|
}
|
185
|
else if( object== ObjectSignatures.ICOS_2 )
|
186
|
{
|
187
|
mNumColors = ShapeIcosahedron.NUM_FACES;
|
188
|
mFaceColors = ShapeIcosahedron.FACE_COLORS;
|
189
|
}
|
190
|
else if( object== ObjectSignatures.DIN4_3 )
|
191
|
{
|
192
|
mNumColors = 4;
|
193
|
mFaceColors = new int[] { COLOR_YELLOW, COLOR_RED, COLOR_BLUE, COLOR_WHITE };
|
194
|
mColorMode = MODE_DINO_4;
|
195
|
}
|
196
|
else
|
197
|
{
|
198
|
mNumColors = ShapeHexahedron.NUM_FACES;
|
199
|
mFaceColors = ShapeHexahedron.FACE_COLORS;
|
200
|
}
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
private void setupBitmaps()
|
206
|
{
|
207
|
final int SIZE = (int)mBitmapSize;
|
208
|
final float R = SIZE*0.15f;
|
209
|
final float M = SIZE*0.08f;
|
210
|
|
211
|
mBitmap = new Bitmap[mNumColors];
|
212
|
|
213
|
Paint paint = new Paint();
|
214
|
paint.setColor(0xff008800);
|
215
|
paint.setStyle(Paint.Style.FILL);
|
216
|
|
217
|
paint.setAntiAlias(true);
|
218
|
paint.setTextAlign(Paint.Align.CENTER);
|
219
|
paint.setStyle(Paint.Style.FILL);
|
220
|
|
221
|
for(int i=0; i<mNumColors; i++)
|
222
|
{
|
223
|
mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
|
224
|
Canvas canvas = new Canvas(mBitmap[i]);
|
225
|
|
226
|
paint.setColor(0xff000000);
|
227
|
canvas.drawRect(0, 0, SIZE, SIZE, paint);
|
228
|
|
229
|
paint.setColor(mFaceColors[i]);
|
230
|
canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
|
231
|
}
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
|
236
|
private int translateColor(int color)
|
237
|
{
|
238
|
if( mColorMode==MODE_DINO_4 )
|
239
|
{
|
240
|
int realColor = mFaceColors[color];
|
241
|
int[] hexColors = ShapeHexahedron.FACE_COLORS;
|
242
|
|
243
|
for(int i=0; i<6; i++)
|
244
|
if( hexColors[i]==realColor ) return i;
|
245
|
}
|
246
|
|
247
|
return color;
|
248
|
}
|
249
|
|
250
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251
|
|
252
|
private void setupColorButtons(final RubikActivity act, final float width)
|
253
|
{
|
254
|
mColorButton = new ImageButton[mNumColors];
|
255
|
int padding = (int)(width*RubikActivity.PADDING);
|
256
|
int margin = (int)(width*RubikActivity.SMALL_MARGIN);
|
257
|
|
258
|
for(int i=0; i<mNumColors; i++)
|
259
|
{
|
260
|
final int ii = i;
|
261
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
262
|
params.topMargin = margin;
|
263
|
params.bottomMargin = margin;
|
264
|
params.leftMargin = margin;
|
265
|
params.rightMargin = margin;
|
266
|
|
267
|
mColorButton[i] = new ImageButton(act);
|
268
|
mColorButton[i].setLayoutParams(params);
|
269
|
mColorButton[i].setPadding(padding,0,padding,0);
|
270
|
mColorButton[i].setImageBitmap(mBitmap[i]);
|
271
|
|
272
|
mColorButton[i].setOnClickListener( new View.OnClickListener()
|
273
|
{
|
274
|
@Override
|
275
|
public void onClick(View view)
|
276
|
{
|
277
|
mCurrentColor = translateColor(ii);
|
278
|
mCurrentButton= ii;
|
279
|
markButton(act);
|
280
|
}
|
281
|
});
|
282
|
}
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
private void setupResetButton(final RubikActivity act)
|
288
|
{
|
289
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_reset,R.drawable.ui_medium_reset, R.drawable.ui_big_reset, R.drawable.ui_huge_reset);
|
290
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
291
|
mResetButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
292
|
|
293
|
mResetButton.setOnClickListener( new View.OnClickListener()
|
294
|
{
|
295
|
@Override
|
296
|
public void onClick(View v)
|
297
|
{
|
298
|
ObjectControl control = act.getControl();
|
299
|
control.resetTextureMapsEffect(RESET_DURATION);
|
300
|
}
|
301
|
});
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
|
306
|
private void setupSolveButton(final RubikActivity act)
|
307
|
{
|
308
|
final int icon = RubikActivity.getDrawable(R.drawable.ui_small_solve,R.drawable.ui_medium_solve, R.drawable.ui_big_solve, R.drawable.ui_huge_solve);
|
309
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
310
|
mSolveButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
|
311
|
|
312
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
313
|
{
|
314
|
@Override
|
315
|
public void onClick(View v)
|
316
|
{
|
317
|
if( !mSolving )
|
318
|
{
|
319
|
mSolving = true;
|
320
|
TwistyObject object = act.getObject();
|
321
|
SolverMain solver = new SolverMain( act.getInterface(), act.getResources(), object );
|
322
|
solver.start();
|
323
|
}
|
324
|
}
|
325
|
});
|
326
|
}
|
327
|
|
328
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
329
|
|
330
|
private void setupBackButton(final RubikActivity act)
|
331
|
{
|
332
|
final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
|
333
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
334
|
mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
335
|
|
336
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
337
|
{
|
338
|
@Override
|
339
|
public void onClick(View v)
|
340
|
{
|
341
|
ObjectControl control = act.getControl();
|
342
|
control.resetAllTextureMaps();
|
343
|
ScreenList.goBack(act);
|
344
|
}
|
345
|
});
|
346
|
}
|
347
|
|
348
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
349
|
|
350
|
private void markButton(RubikActivity act)
|
351
|
{
|
352
|
for(int b=0; b<mNumColors; b++)
|
353
|
{
|
354
|
Drawable d = mColorButton[b].getBackground();
|
355
|
|
356
|
if( b==mCurrentButton )
|
357
|
{
|
358
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
359
|
}
|
360
|
else
|
361
|
{
|
362
|
d.clearColorFilter();
|
363
|
}
|
364
|
}
|
365
|
}
|
366
|
|
367
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
368
|
|
369
|
public void savePreferences(SharedPreferences.Editor editor)
|
370
|
{
|
371
|
editor.putInt("stateSolver_color" , mCurrentColor );
|
372
|
editor.putInt("stateSolver_button", mCurrentButton);
|
373
|
}
|
374
|
|
375
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
376
|
|
377
|
public void restorePreferences(SharedPreferences preferences)
|
378
|
{
|
379
|
mCurrentColor = preferences.getInt("stateSolver_color" , 0);
|
380
|
mCurrentButton= preferences.getInt("stateSolver_button", 0);
|
381
|
}
|
382
|
|
383
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
384
|
|
385
|
public int getCurrentColor()
|
386
|
{
|
387
|
return mCurrentColor;
|
388
|
}
|
389
|
|
390
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
391
|
|
392
|
public void setSolved(final String moves)
|
393
|
{
|
394
|
mSolving = false;
|
395
|
final RubikActivity act = mWeakAct.get();
|
396
|
|
397
|
if( act!=null )
|
398
|
{
|
399
|
act.runOnUiThread(new Runnable()
|
400
|
{
|
401
|
@Override
|
402
|
public void run()
|
403
|
{
|
404
|
ScreenList.switchScreen(act, ScreenList.SOLU);
|
405
|
RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
|
406
|
solution.setupMoves(act, moves);
|
407
|
}
|
408
|
});
|
409
|
}
|
410
|
}
|
411
|
|
412
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
413
|
|
414
|
public void setSolved(final int[][] moves)
|
415
|
{
|
416
|
mSolving = false;
|
417
|
final RubikActivity act = mWeakAct.get();
|
418
|
|
419
|
if( act!=null )
|
420
|
{
|
421
|
act.runOnUiThread(new Runnable()
|
422
|
{
|
423
|
@Override
|
424
|
public void run()
|
425
|
{
|
426
|
ScreenList.switchScreen(act, ScreenList.SOLU);
|
427
|
RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
|
428
|
solution.setupMoves(act, moves);
|
429
|
}
|
430
|
});
|
431
|
}
|
432
|
}
|
433
|
|
434
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435
|
|
436
|
public void displayErrorDialog(String message)
|
437
|
{
|
438
|
mSolving = false;
|
439
|
RubikActivity act = mWeakAct.get();
|
440
|
|
441
|
if( act!=null )
|
442
|
{
|
443
|
RubikDialogSolverError dialog = new RubikDialogSolverError();
|
444
|
Bundle bundle = new Bundle();
|
445
|
bundle.putString("argument", message );
|
446
|
dialog.setArguments(bundle);
|
447
|
dialog.show( act.getSupportFragmentManager(), null);
|
448
|
}
|
449
|
}
|
450
|
}
|