1 |
ad9e8bb3
|
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 |
ad9e8bb3
|
Leszek Koltunski
|
|
22 |
|
|
import android.content.SharedPreferences;
|
23 |
|
|
import android.util.DisplayMetrics;
|
24 |
|
|
import android.view.LayoutInflater;
|
25 |
a6d3b158
|
Leszek Koltunski
|
import android.view.View;
|
26 |
ad9e8bb3
|
Leszek Koltunski
|
import android.widget.Button;
|
27 |
0e5ad27c
|
Leszek Koltunski
|
import android.widget.ImageButton;
|
28 |
ad9e8bb3
|
Leszek Koltunski
|
import android.widget.LinearLayout;
|
29 |
|
|
import android.widget.TextView;
|
30 |
|
|
|
31 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.main.R;
|
32 |
|
|
import org.distorted.main.RubikActivity;
|
33 |
0e5ad27c
|
Leszek Koltunski
|
import org.distorted.main.RubikPostRender;
|
34 |
|
|
import org.distorted.objects.RubikObject;
|
35 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.objects.RubikObjectList;
|
36 |
f0e87514
|
Leszek Koltunski
|
import org.distorted.scores.RubikScores;
|
37 |
ad9e8bb3
|
Leszek Koltunski
|
|
38 |
0e5ad27c
|
Leszek Koltunski
|
import java.util.ArrayList;
|
39 |
0333d81e
|
Leszek Koltunski
|
import java.util.Timer;
|
40 |
|
|
import java.util.TimerTask;
|
41 |
|
|
|
42 |
ad9e8bb3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43 |
|
|
|
44 |
0e5ad27c
|
Leszek Koltunski
|
public class RubikStateSolving extends RubikStateAbstract implements RubikPostRender.ActionFinishedListener
|
45 |
ad9e8bb3
|
Leszek Koltunski
|
{
|
46 |
0e5ad27c
|
Leszek Koltunski
|
private static final int DURATION_MILLIS = 750;
|
47 |
|
|
|
48 |
0333d81e
|
Leszek Koltunski
|
private TextView mTime;
|
49 |
|
|
private Timer mTimer;
|
50 |
|
|
private long mStartTime;
|
51 |
|
|
private boolean mRunning;
|
52 |
f0e87514
|
Leszek Koltunski
|
private RubikScores mScores;
|
53 |
e41e7dc3
|
Leszek Koltunski
|
private Button mBack;
|
54 |
0e5ad27c
|
Leszek Koltunski
|
private ImageButton mPrevButton;
|
55 |
|
|
private boolean mCanPrevMove;
|
56 |
|
|
private ArrayList<Move> mMoves;
|
57 |
|
|
|
58 |
|
|
private static class Move
|
59 |
|
|
{
|
60 |
|
|
private int mAxis, mRow, mAngle;
|
61 |
|
|
|
62 |
|
|
Move(int axis, int row, int angle)
|
63 |
|
|
{
|
64 |
|
|
mAxis = axis;
|
65 |
|
|
mRow = row;
|
66 |
|
|
mAngle= angle;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
329c0aeb
|
Leszek Koltunski
|
|
70 |
0333d81e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71 |
|
|
|
72 |
329c0aeb
|
Leszek Koltunski
|
RubikStateSolving()
|
73 |
|
|
{
|
74 |
714292f1
|
Leszek Koltunski
|
mScores = RubikScores.getInstance();
|
75 |
329c0aeb
|
Leszek Koltunski
|
}
|
76 |
|
|
|
77 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78 |
|
|
|
79 |
|
|
void leaveState(RubikActivity act)
|
80 |
ad9e8bb3
|
Leszek Koltunski
|
{
|
81 |
e41e7dc3
|
Leszek Koltunski
|
stopCounting(act);
|
82 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
83 |
|
|
|
84 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
|
86 |
a6d3b158
|
Leszek Koltunski
|
void enterState(final RubikActivity act)
|
87 |
ad9e8bb3
|
Leszek Koltunski
|
{
|
88 |
0e5ad27c
|
Leszek Koltunski
|
mCanPrevMove = true;
|
89 |
|
|
|
90 |
|
|
if( mMoves==null ) mMoves = new ArrayList<>();
|
91 |
|
|
else mMoves.clear();
|
92 |
|
|
|
93 |
ad9e8bb3
|
Leszek Koltunski
|
LayoutInflater inflater = act.getLayoutInflater();
|
94 |
0e5ad27c
|
Leszek Koltunski
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
95 |
|
|
float scale = metrics.density;
|
96 |
ad9e8bb3
|
Leszek Koltunski
|
|
97 |
|
|
// TOP ////////////////////////////
|
98 |
7289fd6c
|
Leszek Koltunski
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
99 |
ad9e8bb3
|
Leszek Koltunski
|
layoutTop.removeAllViews();
|
100 |
0333d81e
|
Leszek Koltunski
|
mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
|
101 |
|
|
mTime.setText(R.string.ready);
|
102 |
|
|
layoutTop.addView(mTime);
|
103 |
ad9e8bb3
|
Leszek Koltunski
|
|
104 |
|
|
// BOT ////////////////////////////
|
105 |
4c0cd600
|
Leszek Koltunski
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
106 |
|
|
layoutLeft.removeAllViews();
|
107 |
0e5ad27c
|
Leszek Koltunski
|
|
108 |
|
|
if( mPrevButton==null ) setupPrevMoveButtom(act,scale);
|
109 |
|
|
layoutLeft.addView(mPrevButton);
|
110 |
|
|
|
111 |
4c0cd600
|
Leszek Koltunski
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
112 |
|
|
layoutRight.removeAllViews();
|
113 |
ad9e8bb3
|
Leszek Koltunski
|
|
114 |
|
|
int padding = (int)(5*scale + 0.5f);
|
115 |
4c0cd600
|
Leszek Koltunski
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
116 |
ad9e8bb3
|
Leszek Koltunski
|
|
117 |
e41e7dc3
|
Leszek Koltunski
|
mBack = new Button(act);
|
118 |
|
|
mBack.setLayoutParams(params);
|
119 |
|
|
mBack.setPadding(padding,0,padding,0);
|
120 |
|
|
mBack.setText(R.string.back);
|
121 |
a6d3b158
|
Leszek Koltunski
|
|
122 |
|
|
mBack.setOnClickListener( new View.OnClickListener()
|
123 |
|
|
{
|
124 |
|
|
@Override
|
125 |
|
|
public void onClick(View v)
|
126 |
|
|
{
|
127 |
|
|
RubikState.goBack(act);
|
128 |
|
|
}
|
129 |
|
|
});
|
130 |
|
|
|
131 |
4c0cd600
|
Leszek Koltunski
|
layoutRight.addView(mBack);
|
132 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
133 |
|
|
|
134 |
0e5ad27c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135 |
|
|
|
136 |
|
|
private void setupPrevMoveButtom(final RubikActivity act, float scale)
|
137 |
|
|
{
|
138 |
|
|
int padding = (int)( 3*scale + 0.5f);
|
139 |
|
|
int width = (int)(60*scale + 0.5f);
|
140 |
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT);
|
141 |
|
|
mPrevButton = new ImageButton(act);
|
142 |
|
|
mPrevButton.setLayoutParams(params);
|
143 |
|
|
mPrevButton.setPadding(padding,0,padding,0);
|
144 |
|
|
mPrevButton.setImageResource(R.drawable.left);
|
145 |
|
|
mPrevButton.setEnabled(false);
|
146 |
|
|
|
147 |
|
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
148 |
|
|
{
|
149 |
|
|
@Override
|
150 |
|
|
public void onClick(View v)
|
151 |
|
|
{
|
152 |
|
|
RubikPostRender post = act.getPostRender();
|
153 |
|
|
backMove(post);
|
154 |
|
|
}
|
155 |
|
|
});
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159 |
|
|
|
160 |
|
|
private void backMove(RubikPostRender post)
|
161 |
|
|
{
|
162 |
|
|
if( mCanPrevMove )
|
163 |
|
|
{
|
164 |
|
|
int numMoves = mMoves.size();
|
165 |
|
|
|
166 |
|
|
if( numMoves>0 )
|
167 |
|
|
{
|
168 |
|
|
Move move = mMoves.remove(numMoves-1);
|
169 |
|
|
RubikObject object = post.getObject();
|
170 |
|
|
|
171 |
|
|
int axis = move.mAxis;
|
172 |
|
|
int row = (1<<move.mRow);
|
173 |
|
|
int angle = move.mAngle;
|
174 |
|
|
int numRot= Math.abs(angle*object.getBasicAngle()/360);
|
175 |
|
|
|
176 |
|
|
if( angle!=0 )
|
177 |
|
|
{
|
178 |
|
|
mCanPrevMove = false;
|
179 |
|
|
post.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
|
180 |
|
|
|
181 |
|
|
if( numMoves==1 )
|
182 |
|
|
{
|
183 |
|
|
mPrevButton.setEnabled(false);
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
else
|
187 |
|
|
{
|
188 |
|
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
else
|
192 |
|
|
{
|
193 |
|
|
android.util.Log.e("solv", "error: no moves to back!");
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199 |
|
|
|
200 |
|
|
public void addMove(int axis, int row, int angle)
|
201 |
|
|
{
|
202 |
|
|
mMoves.add(new Move(axis,row,angle));
|
203 |
|
|
|
204 |
|
|
if( mMoves.size()==1 )
|
205 |
|
|
{
|
206 |
|
|
mPrevButton.setEnabled(true);
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
|
|
|
210 |
ad9e8bb3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211 |
|
|
|
212 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
213 |
|
|
{
|
214 |
f0e87514
|
Leszek Koltunski
|
mScores.savePreferences(editor);
|
215 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
216 |
|
|
|
217 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218 |
|
|
|
219 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
220 |
|
|
{
|
221 |
f0e87514
|
Leszek Koltunski
|
mScores.restorePreferences(preferences);
|
222 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
223 |
0333d81e
|
Leszek Koltunski
|
|
224 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
225 |
|
|
|
226 |
|
|
public void startCounting(final RubikActivity act)
|
227 |
|
|
{
|
228 |
|
|
if( !mRunning )
|
229 |
|
|
{
|
230 |
|
|
mRunning = true;
|
231 |
|
|
mStartTime = System.currentTimeMillis();
|
232 |
|
|
mTimer = new Timer();
|
233 |
|
|
|
234 |
|
|
mTimer.scheduleAtFixedRate(new TimerTask()
|
235 |
|
|
{
|
236 |
|
|
@Override
|
237 |
|
|
public void run()
|
238 |
|
|
{
|
239 |
|
|
act.runOnUiThread(new Runnable()
|
240 |
|
|
{
|
241 |
|
|
@Override
|
242 |
|
|
public void run()
|
243 |
|
|
{
|
244 |
|
|
int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
|
245 |
|
|
mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
|
246 |
|
|
}
|
247 |
|
|
});
|
248 |
|
|
}
|
249 |
|
|
}, 0, 1000);
|
250 |
|
|
}
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254 |
|
|
|
255 |
e41e7dc3
|
Leszek Koltunski
|
public long stopCounting(RubikActivity act)
|
256 |
0333d81e
|
Leszek Koltunski
|
{
|
257 |
|
|
if( mRunning )
|
258 |
|
|
{
|
259 |
e41e7dc3
|
Leszek Koltunski
|
act.runOnUiThread(new Runnable()
|
260 |
|
|
{
|
261 |
|
|
@Override
|
262 |
|
|
public void run()
|
263 |
|
|
{
|
264 |
|
|
mTime.setText(R.string.solved);
|
265 |
|
|
mBack.setClickable(false);
|
266 |
|
|
}
|
267 |
|
|
});
|
268 |
|
|
|
269 |
0333d81e
|
Leszek Koltunski
|
if( mTimer!=null )
|
270 |
|
|
{
|
271 |
|
|
mTimer.cancel();
|
272 |
|
|
mTimer = null;
|
273 |
|
|
}
|
274 |
|
|
mRunning = false;
|
275 |
|
|
|
276 |
329c0aeb
|
Leszek Koltunski
|
long timeTaken = System.currentTimeMillis()-mStartTime;
|
277 |
|
|
|
278 |
|
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
279 |
4888e97c
|
Leszek Koltunski
|
int object = play.getObject();
|
280 |
|
|
int size = play.getSize();
|
281 |
85b09df4
|
Leszek Koltunski
|
int level = play.getLevel();
|
282 |
53f23b64
|
Leszek Koltunski
|
int realSize= RubikObjectList.getSizeIndex(object,size);
|
283 |
329c0aeb
|
Leszek Koltunski
|
|
284 |
85b09df4
|
Leszek Koltunski
|
boolean isNew = mScores.setRecord(object, realSize, level, timeTaken);
|
285 |
e41e7dc3
|
Leszek Koltunski
|
|
286 |
|
|
return isNew ? timeTaken : -timeTaken;
|
287 |
0333d81e
|
Leszek Koltunski
|
}
|
288 |
|
|
|
289 |
|
|
return 0;
|
290 |
|
|
}
|
291 |
0e5ad27c
|
Leszek Koltunski
|
|
292 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293 |
|
|
|
294 |
|
|
public void onActionFinished(final long effectID)
|
295 |
|
|
{
|
296 |
|
|
mCanPrevMove = true;
|
297 |
|
|
}
|
298 |
ad9e8bb3
|
Leszek Koltunski
|
}
|