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 |
|
|
import android.util.DisplayMetrics;
|
24 |
|
|
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 |
7f84a768
|
Leszek Koltunski
|
import org.distorted.main.RubikPostRender;
|
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 |
7f84a768
|
Leszek Koltunski
|
public class RubikStateSolution extends RubikStateAbstract implements RubikPostRender.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 |
264af0ad
|
Leszek Koltunski
|
|
51 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
|
|
void leaveState(RubikActivity act)
|
54 |
|
|
{
|
55 |
1f9772f3
|
Leszek Koltunski
|
RubikObject object = act.getObject();
|
56 |
|
|
object.solve();
|
57 |
264af0ad
|
Leszek Koltunski
|
}
|
58 |
|
|
|
59 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60 |
|
|
|
61 |
|
|
void enterState(final RubikActivity act)
|
62 |
|
|
{
|
63 |
|
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
64 |
|
|
final float scale = metrics.density;
|
65 |
|
|
LayoutInflater inflater = act.getLayoutInflater();
|
66 |
|
|
|
67 |
|
|
// TOP ////////////////////////////
|
68 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
69 |
|
|
layoutTop.removeAllViews();
|
70 |
|
|
|
71 |
|
|
final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
|
72 |
|
|
text.setText(R.string.solution);
|
73 |
|
|
layoutTop.addView(text);
|
74 |
|
|
|
75 |
|
|
// BOT ////////////////////////////
|
76 |
|
|
if( mPrevButton==null ) setupPrevButton(act,scale);
|
77 |
|
|
if( mNextButton==null ) setupNextButton(act,scale);
|
78 |
|
|
if( mMovesText ==null ) setupTextView(act,scale);
|
79 |
|
|
|
80 |
|
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
81 |
|
|
layoutLeft.removeAllViews();
|
82 |
|
|
layoutLeft.addView(mPrevButton);
|
83 |
|
|
layoutLeft.addView(mMovesText);
|
84 |
|
|
layoutLeft.addView(mNextButton);
|
85 |
|
|
|
86 |
|
|
if( mBackButton==null ) setupBackButton(act,scale);
|
87 |
|
|
|
88 |
|
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
89 |
|
|
layoutRight.removeAllViews();
|
90 |
|
|
layoutRight.addView(mBackButton);
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
|
95 |
|
|
private void setupPrevButton(final RubikActivity act, final float scale)
|
96 |
|
|
{
|
97 |
|
|
int padding = (int)(3*scale + 0.5f);
|
98 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
99 |
|
|
mPrevButton = new ImageButton(act);
|
100 |
|
|
mPrevButton.setLayoutParams(params);
|
101 |
|
|
mPrevButton.setPadding(padding,0,padding,0);
|
102 |
|
|
mPrevButton.setImageResource(R.drawable.left);
|
103 |
|
|
|
104 |
|
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
105 |
|
|
{
|
106 |
|
|
@Override
|
107 |
|
|
public void onClick(View v)
|
108 |
|
|
{
|
109 |
7f84a768
|
Leszek Koltunski
|
RubikPostRender post = act.getPostRender();
|
110 |
|
|
backMove(post);
|
111 |
|
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
112 |
264af0ad
|
Leszek Koltunski
|
}
|
113 |
|
|
});
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117 |
|
|
|
118 |
|
|
private void setupNextButton(final RubikActivity act, final float scale)
|
119 |
|
|
{
|
120 |
|
|
int padding = (int)( 3*scale + 0.5f);
|
121 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
122 |
|
|
mNextButton = new ImageButton(act);
|
123 |
|
|
mNextButton.setLayoutParams(params);
|
124 |
|
|
mNextButton.setPadding(padding,0,padding,0);
|
125 |
|
|
mNextButton.setImageResource(R.drawable.right);
|
126 |
|
|
|
127 |
|
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
128 |
|
|
{
|
129 |
|
|
@Override
|
130 |
|
|
public void onClick(View v)
|
131 |
|
|
{
|
132 |
7f84a768
|
Leszek Koltunski
|
RubikPostRender post = act.getPostRender();
|
133 |
|
|
makeMove(post);
|
134 |
|
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
135 |
264af0ad
|
Leszek Koltunski
|
}
|
136 |
|
|
});
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140 |
|
|
|
141 |
|
|
private void setupTextView(final RubikActivity act, final float scale)
|
142 |
|
|
{
|
143 |
|
|
int padding = (int)( 3*scale + 0.5f);
|
144 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
|
145 |
|
|
|
146 |
|
|
mMovesText = new TextView(act);
|
147 |
|
|
mMovesText.setTextSize(20);
|
148 |
|
|
mMovesText.setLayoutParams(params);
|
149 |
|
|
mMovesText.setPadding(padding,0,padding,0);
|
150 |
|
|
mMovesText.setGravity(Gravity.CENTER);
|
151 |
7f84a768
|
Leszek Koltunski
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
152 |
264af0ad
|
Leszek Koltunski
|
}
|
153 |
|
|
|
154 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155 |
|
|
|
156 |
|
|
private void setupBackButton(final RubikActivity act, final float scale)
|
157 |
|
|
{
|
158 |
|
|
int padding = (int)(3*scale + 0.5f);
|
159 |
|
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
160 |
|
|
mBackButton = new Button(act);
|
161 |
|
|
mBackButton.setLayoutParams(backParams);
|
162 |
|
|
mBackButton.setPadding(padding,0,padding,0);
|
163 |
|
|
mBackButton.setText(R.string.back);
|
164 |
|
|
|
165 |
|
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
166 |
|
|
{
|
167 |
|
|
@Override
|
168 |
|
|
public void onClick(View v)
|
169 |
|
|
{
|
170 |
|
|
RubikState.goBack(act);
|
171 |
|
|
}
|
172 |
|
|
});
|
173 |
|
|
}
|
174 |
|
|
|
175 |
7f84a768
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176 |
|
|
|
177 |
|
|
private void makeMove(RubikPostRender post)
|
178 |
|
|
{
|
179 |
|
|
if( mCanRotate )
|
180 |
|
|
{
|
181 |
|
|
mCurrMove++;
|
182 |
|
|
|
183 |
|
|
if( mCurrMove>mNumMoves )
|
184 |
|
|
{
|
185 |
|
|
mCurrMove= 0;
|
186 |
|
|
post.initializeObject(null);
|
187 |
|
|
}
|
188 |
|
|
else
|
189 |
|
|
{
|
190 |
|
|
int axis = mMoves[mCurrMove-1][0];
|
191 |
|
|
int rowBitmap= mMoves[mCurrMove-1][1];
|
192 |
|
|
int bareAngle= mMoves[mCurrMove-1][2];
|
193 |
|
|
int angle = bareAngle*(360/post.getObject().getBasicAngle());
|
194 |
|
|
int numRot = Math.abs(bareAngle);
|
195 |
|
|
|
196 |
|
|
if( angle!=0 )
|
197 |
|
|
{
|
198 |
|
|
mCanRotate = false;
|
199 |
|
|
post.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
|
200 |
|
|
}
|
201 |
|
|
else
|
202 |
|
|
{
|
203 |
|
|
android.util.Log.e("solution", "error: solution contains angle 0");
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
else
|
208 |
|
|
{
|
209 |
|
|
android.util.Log.e("solution", "failed to make move!");
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214 |
|
|
|
215 |
|
|
private void backMove(RubikPostRender post)
|
216 |
|
|
{
|
217 |
|
|
if( mCanRotate )
|
218 |
|
|
{
|
219 |
|
|
mCurrMove--;
|
220 |
|
|
|
221 |
|
|
if( mCurrMove<0 )
|
222 |
|
|
{
|
223 |
|
|
mCurrMove=mNumMoves;
|
224 |
|
|
post.initializeObject(mMoves);
|
225 |
|
|
}
|
226 |
|
|
else
|
227 |
|
|
{
|
228 |
|
|
int axis = mMoves[mCurrMove][0];
|
229 |
|
|
int rowBitmap= mMoves[mCurrMove][1];
|
230 |
|
|
int bareAngle= mMoves[mCurrMove][2];
|
231 |
|
|
int angle = bareAngle*(360/post.getObject().getBasicAngle());
|
232 |
|
|
int numRot = Math.abs(bareAngle);
|
233 |
|
|
|
234 |
|
|
if( angle!=0 )
|
235 |
|
|
{
|
236 |
|
|
mCanRotate = false;
|
237 |
|
|
post.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
|
238 |
|
|
}
|
239 |
|
|
else
|
240 |
|
|
{
|
241 |
|
|
android.util.Log.e("solution", "error: solution contains angle 0");
|
242 |
|
|
}
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
|
|
else
|
246 |
|
|
{
|
247 |
|
|
android.util.Log.e("solution", "failed to back move!");
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252 |
|
|
|
253 |
373fa45f
|
Leszek Koltunski
|
void setupMoves(final RubikActivity act, String moves)
|
254 |
7f84a768
|
Leszek Koltunski
|
{
|
255 |
|
|
mCanRotate= true;
|
256 |
|
|
mCurrMove = 0;
|
257 |
373fa45f
|
Leszek Koltunski
|
mNumMoves = moves.length()/4;
|
258 |
7f84a768
|
Leszek Koltunski
|
mMoves = new int[mNumMoves][3];
|
259 |
|
|
|
260 |
373fa45f
|
Leszek Koltunski
|
RubikPattern.parseMoves(mMoves,mNumMoves,moves);
|
261 |
7f84a768
|
Leszek Koltunski
|
|
262 |
|
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
263 |
|
|
}
|
264 |
|
|
|
265 |
264af0ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266 |
|
|
|
267 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
268 |
|
|
{
|
269 |
811b0446
|
Leszek Koltunski
|
mBackButton= null;
|
270 |
|
|
mPrevButton= null;
|
271 |
|
|
mNextButton= null;
|
272 |
|
|
mMovesText = null;
|
273 |
264af0ad
|
Leszek Koltunski
|
}
|
274 |
|
|
|
275 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
276 |
|
|
|
277 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
278 |
|
|
{
|
279 |
|
|
|
280 |
|
|
}
|
281 |
7f84a768
|
Leszek Koltunski
|
|
282 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283 |
|
|
|
284 |
|
|
public void onActionFinished(final long effectID)
|
285 |
|
|
{
|
286 |
|
|
mCanRotate = true;
|
287 |
|
|
}
|
288 |
264af0ad
|
Leszek Koltunski
|
}
|