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.SharedPreferences;
|
23
|
import android.graphics.Bitmap;
|
24
|
import android.graphics.Canvas;
|
25
|
import android.graphics.Paint;
|
26
|
import android.graphics.PorterDuff;
|
27
|
import android.graphics.drawable.Drawable;
|
28
|
import android.os.Bundle;
|
29
|
import androidx.core.content.ContextCompat;
|
30
|
import android.util.TypedValue;
|
31
|
import android.view.View;
|
32
|
import android.widget.Button;
|
33
|
import android.widget.ImageButton;
|
34
|
import android.widget.LinearLayout;
|
35
|
|
36
|
import org.distorted.dialogs.RubikDialogSolverError;
|
37
|
import org.distorted.main.R;
|
38
|
import org.distorted.main.RubikActivity;
|
39
|
import org.distorted.main.RubikPreRender;
|
40
|
import org.distorted.objects.RubikObject;
|
41
|
import org.distorted.objects.RubikObjectList;
|
42
|
import org.distorted.solvers.ImplementedSolversList;
|
43
|
import org.distorted.solvers.SolverMain;
|
44
|
|
45
|
import java.lang.ref.WeakReference;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
public class RubikStateSolver extends RubikStateAbstract
|
50
|
{
|
51
|
private static Bitmap[] mBitmap;
|
52
|
private ImageButton[] mColorButton;
|
53
|
private Button mBackButton, mSolveButton;
|
54
|
private boolean mSolving;
|
55
|
private int mCurrentColor;
|
56
|
private int[] mFaceColors;
|
57
|
private int mNumFaces;
|
58
|
private float mTitleSize, mButtonSize, mBitmapSize;
|
59
|
|
60
|
private RubikObjectList mCurrentObject;
|
61
|
private int mCurrentObjectSize;
|
62
|
|
63
|
private WeakReference<RubikActivity> mWeakAct;
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
void leaveState(RubikActivity act)
|
68
|
{
|
69
|
|
70
|
}
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
|
74
|
void enterState(final RubikActivity act)
|
75
|
{
|
76
|
float width = act.getScreenWidthInPixels();
|
77
|
float heigh = act.getScreenHeightInPixels();
|
78
|
mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
|
79
|
mTitleSize = width*RubikActivity.TITLE_TEXT_SIZE;
|
80
|
mBitmapSize = heigh*RubikActivity.BITMAP_TEXT_SIZE;
|
81
|
|
82
|
mWeakAct = new WeakReference<>(act);
|
83
|
|
84
|
mSolving = false;
|
85
|
|
86
|
mCurrentObject = ImplementedSolversList.getObject(0);
|
87
|
mCurrentObjectSize = ImplementedSolversList.getObjectSize(0);
|
88
|
|
89
|
act.setupObject(mCurrentObject, mCurrentObjectSize, null);
|
90
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
91
|
play.setObjectAndSize(act, mCurrentObject, mCurrentObjectSize);
|
92
|
|
93
|
mFaceColors = RubikObjectList.retFaceColors(mCurrentObject);
|
94
|
mNumFaces = mFaceColors!=null ? mFaceColors.length : 0;
|
95
|
|
96
|
// TOP ////////////////////////////
|
97
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
98
|
layoutTop.removeAllViews();
|
99
|
|
100
|
if( mNumFaces>0 )
|
101
|
{
|
102
|
setupBitmaps();
|
103
|
setupColorButtons(act,width);
|
104
|
markButton(act);
|
105
|
}
|
106
|
|
107
|
for(ImageButton button: mColorButton) layoutTop.addView(button);
|
108
|
|
109
|
// BOT ////////////////////////////
|
110
|
setupSolveButton(act,width);
|
111
|
|
112
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
113
|
layoutLeft.removeAllViews();
|
114
|
layoutLeft.addView(mSolveButton);
|
115
|
|
116
|
setupBackButton(act,width);
|
117
|
|
118
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
119
|
layoutRight.removeAllViews();
|
120
|
layoutRight.addView(mBackButton);
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
private void setupBitmaps()
|
126
|
{
|
127
|
final int SIZE = (int)mBitmapSize;
|
128
|
final float R = SIZE*0.15f;
|
129
|
final float M = SIZE*0.08f;
|
130
|
|
131
|
mBitmap = new Bitmap[mNumFaces];
|
132
|
|
133
|
Paint paint = new Paint();
|
134
|
paint.setColor(0xff008800);
|
135
|
paint.setStyle(Paint.Style.FILL);
|
136
|
|
137
|
paint.setAntiAlias(true);
|
138
|
paint.setTextAlign(Paint.Align.CENTER);
|
139
|
paint.setStyle(Paint.Style.FILL);
|
140
|
|
141
|
for(int i=0; i<mNumFaces; i++)
|
142
|
{
|
143
|
mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
|
144
|
Canvas canvas = new Canvas(mBitmap[i]);
|
145
|
|
146
|
paint.setColor(0xff000000);
|
147
|
canvas.drawRect(0, 0, SIZE, SIZE, paint);
|
148
|
|
149
|
paint.setColor(mFaceColors[i]);
|
150
|
canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
|
151
|
}
|
152
|
}
|
153
|
|
154
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155
|
|
156
|
private void setupColorButtons(final RubikActivity act, final float width)
|
157
|
{
|
158
|
mColorButton = new ImageButton[mNumFaces];
|
159
|
int padding = (int)(width*RubikActivity.PADDING);
|
160
|
int margin = (int)(width*RubikActivity.MARGIN);
|
161
|
|
162
|
for(int i=0; i<mNumFaces; i++)
|
163
|
{
|
164
|
final int ii = i;
|
165
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
166
|
params.topMargin = margin;
|
167
|
params.bottomMargin = margin;
|
168
|
params.leftMargin = margin;
|
169
|
params.rightMargin = margin;
|
170
|
|
171
|
mColorButton[i] = new ImageButton(act);
|
172
|
mColorButton[i].setLayoutParams(params);
|
173
|
mColorButton[i].setPadding(padding,0,padding,0);
|
174
|
mColorButton[i].setImageBitmap(mBitmap[i]);
|
175
|
|
176
|
mColorButton[i].setOnClickListener( new View.OnClickListener()
|
177
|
{
|
178
|
@Override
|
179
|
public void onClick(View view)
|
180
|
{
|
181
|
mCurrentColor = ii;
|
182
|
markButton(act);
|
183
|
}
|
184
|
});
|
185
|
}
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
private void setupSolveButton(final RubikActivity act, final float width)
|
191
|
{
|
192
|
int padding = (int)(width*RubikActivity.PADDING);
|
193
|
int margin = (int)(width*RubikActivity.MARGIN);
|
194
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
195
|
params.topMargin = margin;
|
196
|
params.bottomMargin = margin;
|
197
|
params.leftMargin = margin;
|
198
|
params.rightMargin = margin;
|
199
|
|
200
|
mSolveButton = new Button(act);
|
201
|
mSolveButton.setLayoutParams(params);
|
202
|
mSolveButton.setPadding(padding,0,padding,0);
|
203
|
mSolveButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
204
|
mSolveButton.setText(R.string.solve);
|
205
|
|
206
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
207
|
{
|
208
|
@Override
|
209
|
public void onClick(View v)
|
210
|
{
|
211
|
if( !mSolving )
|
212
|
{
|
213
|
mSolving = true;
|
214
|
RubikObject object = act.getObject();
|
215
|
String objectString = object.retObjectString();
|
216
|
SolverMain solver = new SolverMain( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
|
217
|
solver.start();
|
218
|
}
|
219
|
}
|
220
|
});
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
private void setupBackButton(final RubikActivity act, final float width)
|
226
|
{
|
227
|
int padding = (int)(width*RubikActivity.PADDING);
|
228
|
int margin = (int)(width*RubikActivity.MARGIN);
|
229
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
230
|
params.topMargin = margin;
|
231
|
params.bottomMargin = margin;
|
232
|
params.leftMargin = margin;
|
233
|
params.rightMargin = margin;
|
234
|
|
235
|
mBackButton = new Button(act);
|
236
|
mBackButton.setLayoutParams(params);
|
237
|
mBackButton.setPadding(padding,0,padding,0);
|
238
|
mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
239
|
mBackButton.setText(R.string.back);
|
240
|
|
241
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
242
|
{
|
243
|
@Override
|
244
|
public void onClick(View v)
|
245
|
{
|
246
|
RubikPreRender pre = act.getPreRender();
|
247
|
pre.resetAllTextureMaps();
|
248
|
RubikState.goBack(act);
|
249
|
}
|
250
|
});
|
251
|
}
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
private void markButton(RubikActivity act)
|
256
|
{
|
257
|
for(int b=0; b<mNumFaces; b++)
|
258
|
{
|
259
|
Drawable d = mColorButton[b].getBackground();
|
260
|
|
261
|
if( b==mCurrentColor )
|
262
|
{
|
263
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
264
|
}
|
265
|
else
|
266
|
{
|
267
|
d.clearColorFilter();
|
268
|
}
|
269
|
}
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
public void savePreferences(SharedPreferences.Editor editor)
|
275
|
{
|
276
|
editor.putInt("stateSolver_color", mCurrentColor);
|
277
|
}
|
278
|
|
279
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
280
|
|
281
|
public void restorePreferences(SharedPreferences preferences)
|
282
|
{
|
283
|
mCurrentColor = preferences.getInt("stateSolver_color", 0);
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
|
288
|
public int getCurrentColor()
|
289
|
{
|
290
|
return mCurrentColor;
|
291
|
}
|
292
|
|
293
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294
|
|
295
|
public void setSolved(final String moves)
|
296
|
{
|
297
|
mSolving = false;
|
298
|
final RubikActivity act = mWeakAct.get();
|
299
|
|
300
|
if( act!=null )
|
301
|
{
|
302
|
act.runOnUiThread(new Runnable()
|
303
|
{
|
304
|
@Override
|
305
|
public void run()
|
306
|
{
|
307
|
RubikState.switchState(act,RubikState.SOLU);
|
308
|
RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
|
309
|
solution.setupMoves(act, moves);
|
310
|
}
|
311
|
});
|
312
|
}
|
313
|
}
|
314
|
|
315
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316
|
|
317
|
public void displayErrorDialog( String message)
|
318
|
{
|
319
|
mSolving = false;
|
320
|
RubikActivity act = mWeakAct.get();
|
321
|
|
322
|
if( act!=null )
|
323
|
{
|
324
|
RubikDialogSolverError dialog = new RubikDialogSolverError();
|
325
|
Bundle bundle = new Bundle();
|
326
|
bundle.putString("error", message );
|
327
|
dialog.setArguments(bundle);
|
328
|
dialog.show( act.getSupportFragmentManager(), null);
|
329
|
}
|
330
|
}
|
331
|
}
|