1 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2020 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
3782bd44
|
Leszek Koltunski
|
// 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 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
fcd5b990
|
Leszek Koltunski
|
package org.distorted.screens;
|
11 |
7289fd6c
|
Leszek Koltunski
|
|
12 |
3f7a4363
|
Leszek Koltunski
|
import java.lang.ref.WeakReference;
|
13 |
|
|
|
14 |
7289fd6c
|
Leszek Koltunski
|
import android.content.SharedPreferences;
|
15 |
|
|
import android.graphics.Bitmap;
|
16 |
|
|
import android.graphics.Canvas;
|
17 |
|
|
import android.graphics.Paint;
|
18 |
473611ee
|
Leszek Koltunski
|
import android.graphics.PorterDuff;
|
19 |
|
|
import android.graphics.drawable.Drawable;
|
20 |
ca292407
|
Leszek Koltunski
|
import android.os.Bundle;
|
21 |
66e777b0
|
Leszek Koltunski
|
import androidx.core.content.ContextCompat;
|
22 |
7289fd6c
|
Leszek Koltunski
|
import android.view.View;
|
23 |
|
|
import android.widget.ImageButton;
|
24 |
|
|
import android.widget.LinearLayout;
|
25 |
|
|
|
26 |
2afc6754
|
Leszek Koltunski
|
import org.distorted.objectlib.main.ObjectControl;
|
27 |
3f7a4363
|
Leszek Koltunski
|
import org.distorted.objectlib.main.TwistyObject;
|
28 |
|
|
|
29 |
ca292407
|
Leszek Koltunski
|
import org.distorted.dialogs.RubikDialogSolverError;
|
30 |
55e6be1d
|
Leszek Koltunski
|
import org.distorted.helpers.TransparentImageButton;
|
31 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.main.R;
|
32 |
|
|
import org.distorted.main.RubikActivity;
|
33 |
400ff34d
|
Leszek Koltunski
|
import org.distorted.objects.RubikObjectList;
|
34 |
373fa45f
|
Leszek Koltunski
|
import org.distorted.solvers.ImplementedSolversList;
|
35 |
a304ee64
|
Leszek Koltunski
|
import org.distorted.solvers.SolverMain;
|
36 |
7289fd6c
|
Leszek Koltunski
|
|
37 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38 |
|
|
|
39 |
fcd5b990
|
Leszek Koltunski
|
public class RubikScreenSolver extends RubikScreenAbstract
|
40 |
7289fd6c
|
Leszek Koltunski
|
{
|
41 |
|
|
private static Bitmap[] mBitmap;
|
42 |
|
|
private ImageButton[] mColorButton;
|
43 |
dd874ae8
|
Leszek Koltunski
|
private TransparentImageButton mBackButton, mSolveButton;
|
44 |
5660465b
|
Leszek Koltunski
|
private boolean mSolving;
|
45 |
1f9772f3
|
Leszek Koltunski
|
private int mCurrentColor;
|
46 |
41748f19
|
Leszek Koltunski
|
private int[] mFaceColors;
|
47 |
|
|
private int mNumFaces;
|
48 |
7ea57482
|
Leszek Koltunski
|
private float mBitmapSize;
|
49 |
373fa45f
|
Leszek Koltunski
|
private WeakReference<RubikActivity> mWeakAct;
|
50 |
|
|
|
51 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
f5da732a
|
Leszek Koltunski
|
void leaveScreen(RubikActivity act)
|
54 |
7289fd6c
|
Leszek Koltunski
|
{
|
55 |
9523ae28
|
Leszek Koltunski
|
ObjectControl control = act.getControl();
|
56 |
|
|
control.unsetLock();
|
57 |
7289fd6c
|
Leszek Koltunski
|
}
|
58 |
|
|
|
59 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60 |
|
|
|
61 |
f5da732a
|
Leszek Koltunski
|
void enterScreen(final RubikActivity act)
|
62 |
7289fd6c
|
Leszek Koltunski
|
{
|
63 |
9523ae28
|
Leszek Koltunski
|
ObjectControl control = act.getControl();
|
64 |
|
|
control.setLock(false);
|
65 |
|
|
|
66 |
e3c74c0f
|
Leszek Koltunski
|
float width = act.getScreenWidthInPixels();
|
67 |
6d4d56cb
|
Leszek Koltunski
|
float heigh = act.getScreenHeightInPixels();
|
68 |
7ea57482
|
Leszek Koltunski
|
|
69 |
|
|
int sizeV = (int)(heigh*RubikActivity.SOLVER_BMP_V_SIZE);
|
70 |
|
|
int sizeH = (int)(width*RubikActivity.SOLVER_BMP_H_SIZE);
|
71 |
|
|
|
72 |
|
|
mBitmapSize = Math.min(sizeV,sizeH);
|
73 |
373fa45f
|
Leszek Koltunski
|
mWeakAct = new WeakReference<>(act);
|
74 |
5660465b
|
Leszek Koltunski
|
mSolving = false;
|
75 |
|
|
|
76 |
d433b50e
|
Leszek Koltunski
|
int currentObject= ImplementedSolversList.getObject(0);
|
77 |
b20e89d2
|
Leszek Koltunski
|
act.changeIfDifferent(currentObject,control);
|
78 |
33f6abfd
|
Leszek Koltunski
|
control.solveOnly();
|
79 |
|
|
|
80 |
1088a219
|
Leszek Koltunski
|
RubikObjectList.setCurrObject(currentObject);
|
81 |
7289fd6c
|
Leszek Koltunski
|
|
82 |
4ab3d91b
|
Leszek Koltunski
|
generateFaceColors();
|
83 |
20931cf6
|
Leszek Koltunski
|
|
84 |
7289fd6c
|
Leszek Koltunski
|
// TOP ////////////////////////////
|
85 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
86 |
|
|
layoutTop.removeAllViews();
|
87 |
|
|
|
88 |
af133d41
|
Leszek Koltunski
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
|
89 |
|
|
|
90 |
|
|
LinearLayout layoutLeft = new LinearLayout(act);
|
91 |
|
|
layoutLeft.setLayoutParams(paramsL);
|
92 |
|
|
LinearLayout layoutMid = new LinearLayout(act);
|
93 |
|
|
layoutMid.setLayoutParams(paramsL);
|
94 |
|
|
LinearLayout layoutRight = new LinearLayout(act);
|
95 |
|
|
layoutRight.setLayoutParams(paramsL);
|
96 |
|
|
|
97 |
41748f19
|
Leszek Koltunski
|
if( mNumFaces>0 )
|
98 |
|
|
{
|
99 |
7ebd72f7
|
Leszek Koltunski
|
setupBitmaps();
|
100 |
ad0c8e0e
|
Leszek Koltunski
|
setupColorButtons(act,width);
|
101 |
41748f19
|
Leszek Koltunski
|
markButton(act);
|
102 |
|
|
}
|
103 |
473611ee
|
Leszek Koltunski
|
|
104 |
7289fd6c
|
Leszek Koltunski
|
for(ImageButton button: mColorButton) layoutTop.addView(button);
|
105 |
|
|
|
106 |
|
|
// BOT ////////////////////////////
|
107 |
92843d3b
|
Leszek Koltunski
|
LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
|
108 |
|
|
layoutBot.removeAllViews();
|
109 |
264af0ad
|
Leszek Koltunski
|
|
110 |
dd874ae8
|
Leszek Koltunski
|
setupSolveButton(act);
|
111 |
|
|
setupBackButton(act);
|
112 |
7289fd6c
|
Leszek Koltunski
|
|
113 |
af133d41
|
Leszek Koltunski
|
layoutLeft.addView(mSolveButton);
|
114 |
|
|
layoutRight.addView(mBackButton);
|
115 |
|
|
|
116 |
|
|
layoutBot.addView(layoutLeft);
|
117 |
|
|
layoutBot.addView(layoutMid);
|
118 |
|
|
layoutBot.addView(layoutRight);
|
119 |
7289fd6c
|
Leszek Koltunski
|
}
|
120 |
|
|
|
121 |
4ab3d91b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122 |
|
|
|
123 |
|
|
public void generateFaceColors()
|
124 |
|
|
{
|
125 |
|
|
mFaceColors= new int[] { TwistyObject.COLOR_YELLOW,
|
126 |
|
|
TwistyObject.COLOR_WHITE,
|
127 |
|
|
TwistyObject.COLOR_BLUE ,
|
128 |
|
|
TwistyObject.COLOR_GREEN,
|
129 |
|
|
TwistyObject.COLOR_RED ,
|
130 |
|
|
TwistyObject.COLOR_ORANGE };
|
131 |
|
|
mNumFaces = 6;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
5660465b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135 |
7289fd6c
|
Leszek Koltunski
|
|
136 |
7ebd72f7
|
Leszek Koltunski
|
private void setupBitmaps()
|
137 |
7289fd6c
|
Leszek Koltunski
|
{
|
138 |
1bd07548
|
Leszek Koltunski
|
final int SIZE = (int)mBitmapSize;
|
139 |
|
|
final float R = SIZE*0.15f;
|
140 |
|
|
final float M = SIZE*0.08f;
|
141 |
7289fd6c
|
Leszek Koltunski
|
|
142 |
41748f19
|
Leszek Koltunski
|
mBitmap = new Bitmap[mNumFaces];
|
143 |
7289fd6c
|
Leszek Koltunski
|
|
144 |
|
|
Paint paint = new Paint();
|
145 |
|
|
paint.setColor(0xff008800);
|
146 |
|
|
paint.setStyle(Paint.Style.FILL);
|
147 |
|
|
|
148 |
|
|
paint.setAntiAlias(true);
|
149 |
|
|
paint.setTextAlign(Paint.Align.CENTER);
|
150 |
|
|
paint.setStyle(Paint.Style.FILL);
|
151 |
|
|
|
152 |
41748f19
|
Leszek Koltunski
|
for(int i=0; i<mNumFaces; i++)
|
153 |
7289fd6c
|
Leszek Koltunski
|
{
|
154 |
|
|
mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
|
155 |
|
|
Canvas canvas = new Canvas(mBitmap[i]);
|
156 |
|
|
|
157 |
|
|
paint.setColor(0xff000000);
|
158 |
|
|
canvas.drawRect(0, 0, SIZE, SIZE, paint);
|
159 |
|
|
|
160 |
41748f19
|
Leszek Koltunski
|
paint.setColor(mFaceColors[i]);
|
161 |
7289fd6c
|
Leszek Koltunski
|
canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166 |
|
|
|
167 |
ad0c8e0e
|
Leszek Koltunski
|
private void setupColorButtons(final RubikActivity act, final float width)
|
168 |
7289fd6c
|
Leszek Koltunski
|
{
|
169 |
41748f19
|
Leszek Koltunski
|
mColorButton = new ImageButton[mNumFaces];
|
170 |
ad0c8e0e
|
Leszek Koltunski
|
int padding = (int)(width*RubikActivity.PADDING);
|
171 |
c651024f
|
Leszek Koltunski
|
int margin = (int)(width*RubikActivity.SMALL_MARGIN);
|
172 |
7289fd6c
|
Leszek Koltunski
|
|
173 |
41748f19
|
Leszek Koltunski
|
for(int i=0; i<mNumFaces; i++)
|
174 |
7289fd6c
|
Leszek Koltunski
|
{
|
175 |
|
|
final int ii = i;
|
176 |
ad0c8e0e
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
177 |
|
|
params.topMargin = margin;
|
178 |
|
|
params.bottomMargin = margin;
|
179 |
|
|
params.leftMargin = margin;
|
180 |
|
|
params.rightMargin = margin;
|
181 |
|
|
|
182 |
7289fd6c
|
Leszek Koltunski
|
mColorButton[i] = new ImageButton(act);
|
183 |
ad0c8e0e
|
Leszek Koltunski
|
mColorButton[i].setLayoutParams(params);
|
184 |
7289fd6c
|
Leszek Koltunski
|
mColorButton[i].setPadding(padding,0,padding,0);
|
185 |
|
|
mColorButton[i].setImageBitmap(mBitmap[i]);
|
186 |
|
|
|
187 |
|
|
mColorButton[i].setOnClickListener( new View.OnClickListener()
|
188 |
|
|
{
|
189 |
|
|
@Override
|
190 |
|
|
public void onClick(View view)
|
191 |
|
|
{
|
192 |
473611ee
|
Leszek Koltunski
|
mCurrentColor = ii;
|
193 |
|
|
markButton(act);
|
194 |
7289fd6c
|
Leszek Koltunski
|
}
|
195 |
|
|
});
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
264af0ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200 |
|
|
|
201 |
dd874ae8
|
Leszek Koltunski
|
private void setupSolveButton(final RubikActivity act)
|
202 |
264af0ad
|
Leszek Koltunski
|
{
|
203 |
23ac2462
|
Leszek Koltunski
|
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);
|
204 |
b600ccd9
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
205 |
|
|
mSolveButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
|
206 |
264af0ad
|
Leszek Koltunski
|
|
207 |
|
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
208 |
|
|
{
|
209 |
|
|
@Override
|
210 |
|
|
public void onClick(View v)
|
211 |
|
|
{
|
212 |
5660465b
|
Leszek Koltunski
|
if( !mSolving )
|
213 |
|
|
{
|
214 |
|
|
mSolving = true;
|
215 |
9c2f0c91
|
Leszek Koltunski
|
TwistyObject object = act.getObject();
|
216 |
ecf3f149
|
Leszek Koltunski
|
SolverMain solver = new SolverMain( act.getResources(), object );
|
217 |
f0336037
|
Leszek Koltunski
|
solver.start();
|
218 |
5660465b
|
Leszek Koltunski
|
}
|
219 |
264af0ad
|
Leszek Koltunski
|
}
|
220 |
|
|
});
|
221 |
|
|
}
|
222 |
|
|
|
223 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224 |
|
|
|
225 |
dd874ae8
|
Leszek Koltunski
|
private void setupBackButton(final RubikActivity act)
|
226 |
7289fd6c
|
Leszek Koltunski
|
{
|
227 |
4fb1fc0d
|
Leszek Koltunski
|
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);
|
228 |
b600ccd9
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
229 |
|
|
mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
230 |
7289fd6c
|
Leszek Koltunski
|
|
231 |
|
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
232 |
|
|
{
|
233 |
|
|
@Override
|
234 |
|
|
public void onClick(View v)
|
235 |
|
|
{
|
236 |
2afc6754
|
Leszek Koltunski
|
ObjectControl control = act.getControl();
|
237 |
|
|
control.resetAllTextureMaps();
|
238 |
fcd5b990
|
Leszek Koltunski
|
ScreenList.goBack(act);
|
239 |
7289fd6c
|
Leszek Koltunski
|
}
|
240 |
|
|
});
|
241 |
|
|
}
|
242 |
|
|
|
243 |
473611ee
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244 |
|
|
|
245 |
|
|
private void markButton(RubikActivity act)
|
246 |
|
|
{
|
247 |
41748f19
|
Leszek Koltunski
|
for(int b=0; b<mNumFaces; b++)
|
248 |
473611ee
|
Leszek Koltunski
|
{
|
249 |
|
|
Drawable d = mColorButton[b].getBackground();
|
250 |
|
|
|
251 |
|
|
if( b==mCurrentColor )
|
252 |
|
|
{
|
253 |
|
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
254 |
|
|
}
|
255 |
|
|
else
|
256 |
|
|
{
|
257 |
|
|
d.clearColorFilter();
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
|
262 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263 |
|
|
|
264 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
265 |
|
|
{
|
266 |
1f9772f3
|
Leszek Koltunski
|
editor.putInt("stateSolver_color", mCurrentColor);
|
267 |
7289fd6c
|
Leszek Koltunski
|
}
|
268 |
|
|
|
269 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
270 |
|
|
|
271 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
272 |
|
|
{
|
273 |
1f9772f3
|
Leszek Koltunski
|
mCurrentColor = preferences.getInt("stateSolver_color", 0);
|
274 |
7289fd6c
|
Leszek Koltunski
|
}
|
275 |
473611ee
|
Leszek Koltunski
|
|
276 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
277 |
|
|
|
278 |
|
|
public int getCurrentColor()
|
279 |
|
|
{
|
280 |
|
|
return mCurrentColor;
|
281 |
|
|
}
|
282 |
f0336037
|
Leszek Koltunski
|
|
283 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
284 |
|
|
|
285 |
373fa45f
|
Leszek Koltunski
|
public void setSolved(final String moves)
|
286 |
f0336037
|
Leszek Koltunski
|
{
|
287 |
|
|
mSolving = false;
|
288 |
373fa45f
|
Leszek Koltunski
|
final RubikActivity act = mWeakAct.get();
|
289 |
f0336037
|
Leszek Koltunski
|
|
290 |
373fa45f
|
Leszek Koltunski
|
if( act!=null )
|
291 |
f0336037
|
Leszek Koltunski
|
{
|
292 |
373fa45f
|
Leszek Koltunski
|
act.runOnUiThread(new Runnable()
|
293 |
f0336037
|
Leszek Koltunski
|
{
|
294 |
373fa45f
|
Leszek Koltunski
|
@Override
|
295 |
|
|
public void run()
|
296 |
|
|
{
|
297 |
f5da732a
|
Leszek Koltunski
|
ScreenList.switchScreen(act, ScreenList.SOLU);
|
298 |
|
|
RubikScreenSolution solution = (RubikScreenSolution) ScreenList.SOLU.getScreenClass();
|
299 |
373fa45f
|
Leszek Koltunski
|
solution.setupMoves(act, moves);
|
300 |
|
|
}
|
301 |
|
|
});
|
302 |
|
|
}
|
303 |
f0336037
|
Leszek Koltunski
|
}
|
304 |
|
|
|
305 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306 |
|
|
|
307 |
373fa45f
|
Leszek Koltunski
|
public void displayErrorDialog( String message)
|
308 |
f0336037
|
Leszek Koltunski
|
{
|
309 |
|
|
mSolving = false;
|
310 |
373fa45f
|
Leszek Koltunski
|
RubikActivity act = mWeakAct.get();
|
311 |
f0336037
|
Leszek Koltunski
|
|
312 |
373fa45f
|
Leszek Koltunski
|
if( act!=null )
|
313 |
|
|
{
|
314 |
|
|
RubikDialogSolverError dialog = new RubikDialogSolverError();
|
315 |
|
|
Bundle bundle = new Bundle();
|
316 |
|
|
bundle.putString("error", message );
|
317 |
|
|
dialog.setArguments(bundle);
|
318 |
|
|
dialog.show( act.getSupportFragmentManager(), null);
|
319 |
|
|
}
|
320 |
f0336037
|
Leszek Koltunski
|
}
|
321 |
7289fd6c
|
Leszek Koltunski
|
}
|