1 |
264af0ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
1f9772f3
|
Leszek Koltunski
|
package org.distorted.states;
|
21 |
264af0ad
|
Leszek Koltunski
|
|
22 |
|
|
import android.content.SharedPreferences;
|
23 |
e3c74c0f
|
Leszek Koltunski
|
import android.util.TypedValue;
|
24 |
264af0ad
|
Leszek Koltunski
|
import android.view.Gravity;
|
25 |
|
|
import android.view.LayoutInflater;
|
26 |
|
|
import android.view.View;
|
27 |
|
|
import android.widget.Button;
|
28 |
|
|
import android.widget.ImageButton;
|
29 |
|
|
import android.widget.LinearLayout;
|
30 |
|
|
import android.widget.TextView;
|
31 |
|
|
|
32 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.main.R;
|
33 |
|
|
import org.distorted.main.RubikActivity;
|
34 |
5a4d4fba
|
Leszek Koltunski
|
import org.distorted.main.RubikPreRender;
|
35 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.objects.RubikObject;
|
36 |
7f84a768
|
Leszek Koltunski
|
import org.distorted.patterns.RubikPattern;
|
37 |
264af0ad
|
Leszek Koltunski
|
|
38 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
|
40 |
5a4d4fba
|
Leszek Koltunski
|
public class RubikStateSolution extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
|
41 |
264af0ad
|
Leszek Koltunski
|
{
|
42 |
7f84a768
|
Leszek Koltunski
|
private static final int DURATION_MILLIS = 750;
|
43 |
|
|
|
44 |
264af0ad
|
Leszek Koltunski
|
private Button mBackButton;
|
45 |
|
|
private ImageButton mPrevButton, mNextButton;
|
46 |
|
|
private TextView mMovesText;
|
47 |
7f84a768
|
Leszek Koltunski
|
private int[][] mMoves;
|
48 |
|
|
private int mCurrMove, mNumMoves;
|
49 |
|
|
private boolean mCanRotate;
|
50 |
e3c74c0f
|
Leszek Koltunski
|
private float mButtonSize, mTitleSize;
|
51 |
264af0ad
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
|
54 |
|
|
void leaveState(RubikActivity act)
|
55 |
|
|
{
|
56 |
1f9772f3
|
Leszek Koltunski
|
RubikObject object = act.getObject();
|
57 |
|
|
object.solve();
|
58 |
264af0ad
|
Leszek Koltunski
|
}
|
59 |
|
|
|
60 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61 |
|
|
|
62 |
|
|
void enterState(final RubikActivity act)
|
63 |
|
|
{
|
64 |
e3c74c0f
|
Leszek Koltunski
|
float width = act.getScreenWidthInPixels();
|
65 |
|
|
mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
|
66 |
|
|
mTitleSize = width*RubikActivity.TITLE_TEXT_SIZE;
|
67 |
|
|
|
68 |
264af0ad
|
Leszek Koltunski
|
LayoutInflater inflater = act.getLayoutInflater();
|
69 |
|
|
|
70 |
|
|
// TOP ////////////////////////////
|
71 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
72 |
|
|
layoutTop.removeAllViews();
|
73 |
|
|
|
74 |
|
|
final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
|
75 |
e3c74c0f
|
Leszek Koltunski
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
|
76 |
264af0ad
|
Leszek Koltunski
|
text.setText(R.string.solution);
|
77 |
|
|
layoutTop.addView(text);
|
78 |
|
|
|
79 |
|
|
// BOT ////////////////////////////
|
80 |
ad0c8e0e
|
Leszek Koltunski
|
setupPrevButton(act,width);
|
81 |
|
|
setupNextButton(act,width);
|
82 |
|
|
setupTextView(act,width);
|
83 |
264af0ad
|
Leszek Koltunski
|
|
84 |
|
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
85 |
|
|
layoutLeft.removeAllViews();
|
86 |
|
|
layoutLeft.addView(mPrevButton);
|
87 |
|
|
layoutLeft.addView(mMovesText);
|
88 |
|
|
layoutLeft.addView(mNextButton);
|
89 |
|
|
|
90 |
ad0c8e0e
|
Leszek Koltunski
|
setupBackButton(act,width);
|
91 |
264af0ad
|
Leszek Koltunski
|
|
92 |
|
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
93 |
|
|
layoutRight.removeAllViews();
|
94 |
|
|
layoutRight.addView(mBackButton);
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98 |
|
|
|
99 |
ad0c8e0e
|
Leszek Koltunski
|
private void setupPrevButton(final RubikActivity act, final float width)
|
100 |
264af0ad
|
Leszek Koltunski
|
{
|
101 |
ad0c8e0e
|
Leszek Koltunski
|
int padding = (int)(width*RubikActivity.PADDING);
|
102 |
|
|
int margin = (int)(width*RubikActivity.MARGIN);
|
103 |
31a9f38d
|
Leszek Koltunski
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
|
104 |
f6a9c538
|
Leszek Koltunski
|
|
105 |
264af0ad
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
106 |
ad0c8e0e
|
Leszek Koltunski
|
params.topMargin = margin;
|
107 |
|
|
params.bottomMargin = margin;
|
108 |
|
|
params.leftMargin = margin;
|
109 |
|
|
params.rightMargin = margin;
|
110 |
|
|
|
111 |
264af0ad
|
Leszek Koltunski
|
mPrevButton = new ImageButton(act);
|
112 |
|
|
mPrevButton.setLayoutParams(params);
|
113 |
|
|
mPrevButton.setPadding(padding,0,padding,0);
|
114 |
f6a9c538
|
Leszek Koltunski
|
mPrevButton.setImageResource(icon);
|
115 |
264af0ad
|
Leszek Koltunski
|
|
116 |
|
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
117 |
|
|
{
|
118 |
|
|
@Override
|
119 |
|
|
public void onClick(View v)
|
120 |
|
|
{
|
121 |
5a4d4fba
|
Leszek Koltunski
|
RubikPreRender pre = act.getPreRender();
|
122 |
|
|
backMove(pre);
|
123 |
7f84a768
|
Leszek Koltunski
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
124 |
264af0ad
|
Leszek Koltunski
|
}
|
125 |
|
|
});
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
129 |
|
|
|
130 |
ad0c8e0e
|
Leszek Koltunski
|
private void setupNextButton(final RubikActivity act, final float width)
|
131 |
264af0ad
|
Leszek Koltunski
|
{
|
132 |
ad0c8e0e
|
Leszek Koltunski
|
int padding = (int)(width*RubikActivity.PADDING);
|
133 |
|
|
int margin = (int)(width*RubikActivity.MARGIN);
|
134 |
31a9f38d
|
Leszek Koltunski
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
|
135 |
f6a9c538
|
Leszek Koltunski
|
|
136 |
264af0ad
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
137 |
ad0c8e0e
|
Leszek Koltunski
|
params.topMargin = margin;
|
138 |
|
|
params.bottomMargin = margin;
|
139 |
|
|
params.leftMargin = margin;
|
140 |
|
|
params.rightMargin = margin;
|
141 |
|
|
|
142 |
264af0ad
|
Leszek Koltunski
|
mNextButton = new ImageButton(act);
|
143 |
|
|
mNextButton.setLayoutParams(params);
|
144 |
|
|
mNextButton.setPadding(padding,0,padding,0);
|
145 |
f6a9c538
|
Leszek Koltunski
|
mNextButton.setImageResource(icon);
|
146 |
264af0ad
|
Leszek Koltunski
|
|
147 |
|
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
148 |
|
|
{
|
149 |
|
|
@Override
|
150 |
|
|
public void onClick(View v)
|
151 |
|
|
{
|
152 |
5a4d4fba
|
Leszek Koltunski
|
RubikPreRender pre = act.getPreRender();
|
153 |
|
|
makeMove(pre);
|
154 |
7f84a768
|
Leszek Koltunski
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
155 |
264af0ad
|
Leszek Koltunski
|
}
|
156 |
|
|
});
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160 |
|
|
|
161 |
ad0c8e0e
|
Leszek Koltunski
|
private void setupTextView(final RubikActivity act, final float width)
|
162 |
264af0ad
|
Leszek Koltunski
|
{
|
163 |
ad0c8e0e
|
Leszek Koltunski
|
int padding = (int)(width*RubikActivity.PADDING);
|
164 |
|
|
int margin = (int)(width*RubikActivity.MARGIN);
|
165 |
264af0ad
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
|
166 |
ad0c8e0e
|
Leszek Koltunski
|
params.topMargin = margin;
|
167 |
|
|
params.bottomMargin = margin;
|
168 |
|
|
params.leftMargin = margin;
|
169 |
|
|
params.rightMargin = margin;
|
170 |
264af0ad
|
Leszek Koltunski
|
|
171 |
|
|
mMovesText = new TextView(act);
|
172 |
|
|
mMovesText.setTextSize(20);
|
173 |
|
|
mMovesText.setLayoutParams(params);
|
174 |
|
|
mMovesText.setPadding(padding,0,padding,0);
|
175 |
|
|
mMovesText.setGravity(Gravity.CENTER);
|
176 |
e3c74c0f
|
Leszek Koltunski
|
mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
177 |
7f84a768
|
Leszek Koltunski
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
178 |
264af0ad
|
Leszek Koltunski
|
}
|
179 |
|
|
|
180 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181 |
|
|
|
182 |
ad0c8e0e
|
Leszek Koltunski
|
private void setupBackButton(final RubikActivity act, final float width)
|
183 |
264af0ad
|
Leszek Koltunski
|
{
|
184 |
ad0c8e0e
|
Leszek Koltunski
|
int padding = (int)(width*RubikActivity.PADDING);
|
185 |
|
|
int margin = (int)(width*RubikActivity.MARGIN);
|
186 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
187 |
|
|
params.topMargin = margin;
|
188 |
|
|
params.bottomMargin = margin;
|
189 |
|
|
params.leftMargin = margin;
|
190 |
|
|
params.rightMargin = margin;
|
191 |
|
|
|
192 |
264af0ad
|
Leszek Koltunski
|
mBackButton = new Button(act);
|
193 |
ad0c8e0e
|
Leszek Koltunski
|
mBackButton.setLayoutParams(params);
|
194 |
264af0ad
|
Leszek Koltunski
|
mBackButton.setPadding(padding,0,padding,0);
|
195 |
e3c74c0f
|
Leszek Koltunski
|
mBackButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
196 |
264af0ad
|
Leszek Koltunski
|
mBackButton.setText(R.string.back);
|
197 |
|
|
|
198 |
|
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
199 |
|
|
{
|
200 |
|
|
@Override
|
201 |
|
|
public void onClick(View v)
|
202 |
|
|
{
|
203 |
|
|
RubikState.goBack(act);
|
204 |
|
|
}
|
205 |
|
|
});
|
206 |
|
|
}
|
207 |
|
|
|
208 |
7f84a768
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209 |
|
|
|
210 |
5a4d4fba
|
Leszek Koltunski
|
private void makeMove(RubikPreRender pre)
|
211 |
7f84a768
|
Leszek Koltunski
|
{
|
212 |
|
|
if( mCanRotate )
|
213 |
|
|
{
|
214 |
|
|
mCurrMove++;
|
215 |
|
|
|
216 |
|
|
if( mCurrMove>mNumMoves )
|
217 |
|
|
{
|
218 |
|
|
mCurrMove= 0;
|
219 |
5a4d4fba
|
Leszek Koltunski
|
pre.initializeObject(null);
|
220 |
7f84a768
|
Leszek Koltunski
|
}
|
221 |
|
|
else
|
222 |
|
|
{
|
223 |
|
|
int axis = mMoves[mCurrMove-1][0];
|
224 |
|
|
int rowBitmap= mMoves[mCurrMove-1][1];
|
225 |
|
|
int bareAngle= mMoves[mCurrMove-1][2];
|
226 |
5a4d4fba
|
Leszek Koltunski
|
int angle = bareAngle*(360/pre.getObject().getBasicAngle());
|
227 |
7f84a768
|
Leszek Koltunski
|
int numRot = Math.abs(bareAngle);
|
228 |
|
|
|
229 |
|
|
if( angle!=0 )
|
230 |
|
|
{
|
231 |
|
|
mCanRotate = false;
|
232 |
5a4d4fba
|
Leszek Koltunski
|
pre.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
|
233 |
7f84a768
|
Leszek Koltunski
|
}
|
234 |
|
|
else
|
235 |
|
|
{
|
236 |
|
|
android.util.Log.e("solution", "error: solution contains angle 0");
|
237 |
|
|
}
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
else
|
241 |
|
|
{
|
242 |
|
|
android.util.Log.e("solution", "failed to make move!");
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247 |
|
|
|
248 |
5a4d4fba
|
Leszek Koltunski
|
private void backMove(RubikPreRender pre)
|
249 |
7f84a768
|
Leszek Koltunski
|
{
|
250 |
|
|
if( mCanRotate )
|
251 |
|
|
{
|
252 |
|
|
mCurrMove--;
|
253 |
|
|
|
254 |
|
|
if( mCurrMove<0 )
|
255 |
|
|
{
|
256 |
|
|
mCurrMove=mNumMoves;
|
257 |
5a4d4fba
|
Leszek Koltunski
|
pre.initializeObject(mMoves);
|
258 |
7f84a768
|
Leszek Koltunski
|
}
|
259 |
|
|
else
|
260 |
|
|
{
|
261 |
|
|
int axis = mMoves[mCurrMove][0];
|
262 |
|
|
int rowBitmap= mMoves[mCurrMove][1];
|
263 |
|
|
int bareAngle= mMoves[mCurrMove][2];
|
264 |
5a4d4fba
|
Leszek Koltunski
|
int angle = bareAngle*(360/pre.getObject().getBasicAngle());
|
265 |
7f84a768
|
Leszek Koltunski
|
int numRot = Math.abs(bareAngle);
|
266 |
|
|
|
267 |
|
|
if( angle!=0 )
|
268 |
|
|
{
|
269 |
|
|
mCanRotate = false;
|
270 |
5a4d4fba
|
Leszek Koltunski
|
pre.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
|
271 |
7f84a768
|
Leszek Koltunski
|
}
|
272 |
|
|
else
|
273 |
|
|
{
|
274 |
|
|
android.util.Log.e("solution", "error: solution contains angle 0");
|
275 |
|
|
}
|
276 |
|
|
}
|
277 |
|
|
}
|
278 |
|
|
else
|
279 |
|
|
{
|
280 |
|
|
android.util.Log.e("solution", "failed to back move!");
|
281 |
|
|
}
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
285 |
|
|
|
286 |
373fa45f
|
Leszek Koltunski
|
void setupMoves(final RubikActivity act, String moves)
|
287 |
7f84a768
|
Leszek Koltunski
|
{
|
288 |
|
|
mCanRotate= true;
|
289 |
|
|
mCurrMove = 0;
|
290 |
373fa45f
|
Leszek Koltunski
|
mNumMoves = moves.length()/4;
|
291 |
7f84a768
|
Leszek Koltunski
|
mMoves = new int[mNumMoves][3];
|
292 |
|
|
|
293 |
373fa45f
|
Leszek Koltunski
|
RubikPattern.parseMoves(mMoves,mNumMoves,moves);
|
294 |
7f84a768
|
Leszek Koltunski
|
|
295 |
|
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
296 |
|
|
}
|
297 |
|
|
|
298 |
264af0ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299 |
|
|
|
300 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
301 |
|
|
{
|
302 |
4f36e418
|
Leszek Koltunski
|
|
303 |
264af0ad
|
Leszek Koltunski
|
}
|
304 |
|
|
|
305 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306 |
|
|
|
307 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
308 |
|
|
{
|
309 |
|
|
|
310 |
|
|
}
|
311 |
7f84a768
|
Leszek Koltunski
|
|
312 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
313 |
|
|
|
314 |
|
|
public void onActionFinished(final long effectID)
|
315 |
|
|
{
|
316 |
|
|
mCanRotate = true;
|
317 |
|
|
}
|
318 |
264af0ad
|
Leszek Koltunski
|
}
|