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.util.TypedValue;
|
24
|
import android.view.LayoutInflater;
|
25
|
import android.view.View;
|
26
|
import android.widget.ImageButton;
|
27
|
import android.widget.LinearLayout;
|
28
|
import android.widget.TextView;
|
29
|
|
30
|
import org.distorted.main.R;
|
31
|
import org.distorted.main.RubikActivity;
|
32
|
import org.distorted.main.RubikPreRender;
|
33
|
import org.distorted.objects.TwistyObject;
|
34
|
import org.distorted.objects.ObjectList;
|
35
|
import org.distorted.scores.RubikScores;
|
36
|
|
37
|
import java.util.ArrayList;
|
38
|
import java.util.Timer;
|
39
|
import java.util.TimerTask;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class RubikStateSolving extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
|
44
|
{
|
45
|
private static final int DURATION_MILLIS = 750;
|
46
|
|
47
|
private TextView mTime;
|
48
|
private Timer mTimer;
|
49
|
private long mStartTime;
|
50
|
private boolean mRunning;
|
51
|
private RubikScores mScores;
|
52
|
private ImageButton mPrevButton, mLockButton, mBackButton;
|
53
|
private boolean mCanPrevMove;
|
54
|
private ArrayList<Move> mMoves;
|
55
|
private long mElapsed;
|
56
|
|
57
|
private static class Move
|
58
|
{
|
59
|
private int mAxis, mRow, mAngle;
|
60
|
|
61
|
Move(int axis, int row, int angle)
|
62
|
{
|
63
|
mAxis = axis;
|
64
|
mRow = row;
|
65
|
mAngle= angle;
|
66
|
}
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
RubikStateSolving()
|
72
|
{
|
73
|
mScores = RubikScores.getInstance();
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
void leaveState(RubikActivity act)
|
79
|
{
|
80
|
stopCounting();
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
void enterState(final RubikActivity act)
|
86
|
{
|
87
|
float width = act.getScreenWidthInPixels();
|
88
|
float titleSize = width*RubikActivity.TITLE_TEXT_SIZE;
|
89
|
|
90
|
mCanPrevMove = true;
|
91
|
|
92
|
startCounting(act);
|
93
|
|
94
|
if( mMoves==null ) mMoves = new ArrayList<>();
|
95
|
else mMoves.clear();
|
96
|
|
97
|
LayoutInflater inflater = act.getLayoutInflater();
|
98
|
|
99
|
// TOP ////////////////////////////
|
100
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
101
|
layoutTop.removeAllViews();
|
102
|
mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
|
103
|
int elapsed = (int)mElapsed/1000;
|
104
|
mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
|
105
|
mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
|
106
|
layoutTop.addView(mTime);
|
107
|
|
108
|
// BOT ////////////////////////////
|
109
|
LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
|
110
|
layoutBot.removeAllViews();
|
111
|
|
112
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
|
113
|
|
114
|
LinearLayout layoutLeft = new LinearLayout(act);
|
115
|
layoutLeft.setLayoutParams(paramsL);
|
116
|
LinearLayout layoutMid = new LinearLayout(act);
|
117
|
layoutMid.setLayoutParams(paramsL);
|
118
|
LinearLayout layoutRight = new LinearLayout(act);
|
119
|
layoutRight.setLayoutParams(paramsL);
|
120
|
|
121
|
setupPrevButtom(act,width);
|
122
|
layoutLeft.addView(mPrevButton);
|
123
|
setupLockButton(act,width);
|
124
|
layoutMid.addView(mLockButton);
|
125
|
setupBackButtom(act,width);
|
126
|
layoutRight.addView(mBackButton);
|
127
|
|
128
|
layoutBot.addView(layoutLeft);
|
129
|
layoutBot.addView(layoutMid);
|
130
|
layoutBot.addView(layoutRight);
|
131
|
}
|
132
|
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
private void setupBackButtom(final RubikActivity act, float width)
|
137
|
{
|
138
|
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);
|
139
|
mBackButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
|
140
|
|
141
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
142
|
{
|
143
|
@Override
|
144
|
public void onClick(View v)
|
145
|
{
|
146
|
StateList.goBack(act);
|
147
|
}
|
148
|
});
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
private void setupLockButton(final RubikActivity act, final float width)
|
154
|
{
|
155
|
final int icon = getLockIcon(act);
|
156
|
mLockButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
|
157
|
|
158
|
mLockButton.setOnClickListener( new View.OnClickListener()
|
159
|
{
|
160
|
@Override
|
161
|
public void onClick(View v)
|
162
|
{
|
163
|
toggleLock(act);
|
164
|
}
|
165
|
});
|
166
|
}
|
167
|
|
168
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
169
|
|
170
|
private void setupPrevButtom(final RubikActivity act, float width)
|
171
|
{
|
172
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_back,R.drawable.ui_medium_cube_back, R.drawable.ui_big_cube_back, R.drawable.ui_huge_cube_back);
|
173
|
mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
|
174
|
|
175
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
176
|
{
|
177
|
@Override
|
178
|
public void onClick(View v)
|
179
|
{
|
180
|
RubikPreRender pre = act.getPreRender();
|
181
|
backMove(pre);
|
182
|
}
|
183
|
});
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
private void backMove(RubikPreRender pre)
|
189
|
{
|
190
|
if( mCanPrevMove )
|
191
|
{
|
192
|
int numMoves = mMoves.size();
|
193
|
|
194
|
if( numMoves>0 )
|
195
|
{
|
196
|
Move move = mMoves.remove(numMoves-1);
|
197
|
TwistyObject object = pre.getObject();
|
198
|
|
199
|
int axis = move.mAxis;
|
200
|
int row = (1<<move.mRow);
|
201
|
int angle = move.mAngle;
|
202
|
int numRot= Math.abs(angle*object.getBasicAngle()/360);
|
203
|
|
204
|
if( angle!=0 )
|
205
|
{
|
206
|
mCanPrevMove = false;
|
207
|
pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
|
208
|
}
|
209
|
else
|
210
|
{
|
211
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
|
219
|
private void toggleLock(RubikActivity act)
|
220
|
{
|
221
|
act.toggleLock();
|
222
|
mLockButton.setImageResource(getLockIcon(act));
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
private int getLockIcon(RubikActivity act)
|
228
|
{
|
229
|
if( act.retLocked() )
|
230
|
{
|
231
|
return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
|
232
|
}
|
233
|
else
|
234
|
{
|
235
|
return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
|
236
|
}
|
237
|
}
|
238
|
|
239
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240
|
|
241
|
public void addMove(int axis, int row, int angle)
|
242
|
{
|
243
|
mMoves.add(new Move(axis,row,angle));
|
244
|
}
|
245
|
|
246
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
247
|
|
248
|
public void savePreferences(SharedPreferences.Editor editor)
|
249
|
{
|
250
|
mElapsed = System.currentTimeMillis()-mStartTime;
|
251
|
editor.putLong("stateSolving_elapsed" , mElapsed);
|
252
|
mScores.savePreferences(editor);
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
|
257
|
public void restorePreferences(SharedPreferences preferences)
|
258
|
{
|
259
|
mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
|
260
|
mScores.restorePreferences(preferences);
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
private void startCounting(final RubikActivity act)
|
266
|
{
|
267
|
if( !mRunning )
|
268
|
{
|
269
|
mRunning = true;
|
270
|
mStartTime = System.currentTimeMillis() - mElapsed;
|
271
|
mTimer = new Timer();
|
272
|
|
273
|
mTimer.scheduleAtFixedRate(new TimerTask()
|
274
|
{
|
275
|
@Override
|
276
|
public void run()
|
277
|
{
|
278
|
act.runOnUiThread(new Runnable()
|
279
|
{
|
280
|
@Override
|
281
|
public void run()
|
282
|
{
|
283
|
int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
|
284
|
mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
|
285
|
}
|
286
|
});
|
287
|
}
|
288
|
}, 0, 1000);
|
289
|
}
|
290
|
}
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
private void stopCounting()
|
295
|
{
|
296
|
if( mTimer!=null )
|
297
|
{
|
298
|
mTimer.cancel();
|
299
|
mTimer = null;
|
300
|
}
|
301
|
|
302
|
mRunning = false;
|
303
|
}
|
304
|
|
305
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306
|
|
307
|
public long getRecord()
|
308
|
{
|
309
|
if( mRunning )
|
310
|
{
|
311
|
stopCounting();
|
312
|
|
313
|
mElapsed = System.currentTimeMillis()-mStartTime;
|
314
|
|
315
|
RubikStatePlay play = (RubikStatePlay) StateList.PLAY.getStateClass();
|
316
|
int object = play.getObject();
|
317
|
int size = play.getSize();
|
318
|
int level = play.getLevel();
|
319
|
int realSize= ObjectList.getSizeIndex(object,size);
|
320
|
|
321
|
boolean isNew = mScores.setRecord(object, realSize, level, mElapsed);
|
322
|
|
323
|
return isNew ? mElapsed : -mElapsed;
|
324
|
}
|
325
|
|
326
|
return 0;
|
327
|
}
|
328
|
|
329
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
330
|
|
331
|
public void resetElapsed()
|
332
|
{
|
333
|
mElapsed = 0;
|
334
|
}
|
335
|
|
336
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
337
|
|
338
|
public void onActionFinished(final long effectID)
|
339
|
{
|
340
|
mCanPrevMove = true;
|
341
|
}
|
342
|
}
|