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.RubikObject;
|
34
|
import org.distorted.objects.RubikObjectList;
|
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
|
int padding = (int)(width*RubikActivity.PADDING);
|
139
|
int margin = (int)(width*RubikActivity.MARGIN);
|
140
|
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);
|
141
|
|
142
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
143
|
params.topMargin = margin;
|
144
|
params.bottomMargin = margin;
|
145
|
params.leftMargin = margin;
|
146
|
params.rightMargin = margin;
|
147
|
|
148
|
mBackButton = new ImageButton(act);
|
149
|
mBackButton.setLayoutParams(params);
|
150
|
mBackButton.setPadding(padding,0,padding,0);
|
151
|
mBackButton.setImageResource(icon);
|
152
|
|
153
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
154
|
{
|
155
|
@Override
|
156
|
public void onClick(View v)
|
157
|
{
|
158
|
RubikState.goBack(act);
|
159
|
}
|
160
|
});
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
private void setupLockButton(final RubikActivity act, final float width)
|
166
|
{
|
167
|
int padding = (int)(width*RubikActivity.PADDING);
|
168
|
int margin = (int)(width*RubikActivity.MARGIN);
|
169
|
|
170
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
|
171
|
params.topMargin = margin;
|
172
|
params.bottomMargin = margin;
|
173
|
params.leftMargin = margin;
|
174
|
params.rightMargin = margin;
|
175
|
|
176
|
mLockButton = new ImageButton(act);
|
177
|
mLockButton.setLayoutParams(params);
|
178
|
mLockButton.setPadding(padding,0,padding,0);
|
179
|
mLockButton.setImageResource(getLockIcon(act));
|
180
|
|
181
|
mLockButton.setOnClickListener( new View.OnClickListener()
|
182
|
{
|
183
|
@Override
|
184
|
public void onClick(View v)
|
185
|
{
|
186
|
toggleLock(act);
|
187
|
}
|
188
|
});
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
private void setupPrevButtom(final RubikActivity act, float width)
|
194
|
{
|
195
|
int padding = (int)(width*RubikActivity.PADDING);
|
196
|
int margin = (int)(width*RubikActivity.MARGIN);
|
197
|
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);
|
198
|
|
199
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
200
|
params.topMargin = margin;
|
201
|
params.bottomMargin = margin;
|
202
|
params.leftMargin = margin;
|
203
|
params.rightMargin = margin;
|
204
|
|
205
|
mPrevButton = new ImageButton(act);
|
206
|
mPrevButton.setLayoutParams(params);
|
207
|
mPrevButton.setPadding(padding,0,padding,0);
|
208
|
mPrevButton.setImageResource(icon);
|
209
|
|
210
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
211
|
{
|
212
|
@Override
|
213
|
public void onClick(View v)
|
214
|
{
|
215
|
RubikPreRender pre = act.getPreRender();
|
216
|
backMove(pre);
|
217
|
}
|
218
|
});
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
private void backMove(RubikPreRender pre)
|
224
|
{
|
225
|
if( mCanPrevMove )
|
226
|
{
|
227
|
int numMoves = mMoves.size();
|
228
|
|
229
|
if( numMoves>0 )
|
230
|
{
|
231
|
Move move = mMoves.remove(numMoves-1);
|
232
|
RubikObject object = pre.getObject();
|
233
|
|
234
|
int axis = move.mAxis;
|
235
|
int row = (1<<move.mRow);
|
236
|
int angle = move.mAngle;
|
237
|
int numRot= Math.abs(angle*object.getBasicAngle()/360);
|
238
|
|
239
|
if( angle!=0 )
|
240
|
{
|
241
|
mCanPrevMove = false;
|
242
|
pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
|
243
|
}
|
244
|
else
|
245
|
{
|
246
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
}
|
251
|
|
252
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253
|
|
254
|
private void toggleLock(RubikActivity act)
|
255
|
{
|
256
|
act.toggleLock();
|
257
|
mLockButton.setImageResource(getLockIcon(act));
|
258
|
}
|
259
|
|
260
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261
|
|
262
|
private int getLockIcon(RubikActivity act)
|
263
|
{
|
264
|
if( act.retLocked() )
|
265
|
{
|
266
|
return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
|
267
|
}
|
268
|
else
|
269
|
{
|
270
|
return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
|
271
|
}
|
272
|
}
|
273
|
|
274
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275
|
|
276
|
public void addMove(int axis, int row, int angle)
|
277
|
{
|
278
|
mMoves.add(new Move(axis,row,angle));
|
279
|
}
|
280
|
|
281
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
282
|
|
283
|
public void savePreferences(SharedPreferences.Editor editor)
|
284
|
{
|
285
|
mElapsed = System.currentTimeMillis()-mStartTime;
|
286
|
editor.putLong("stateSolving_elapsed" , mElapsed);
|
287
|
mScores.savePreferences(editor);
|
288
|
}
|
289
|
|
290
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
291
|
|
292
|
public void restorePreferences(SharedPreferences preferences)
|
293
|
{
|
294
|
mElapsed = preferences.getLong("stateSolving_elapsed" , 0 );
|
295
|
mScores.restorePreferences(preferences);
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
private void startCounting(final RubikActivity act)
|
301
|
{
|
302
|
if( !mRunning )
|
303
|
{
|
304
|
mRunning = true;
|
305
|
mStartTime = System.currentTimeMillis() - mElapsed;
|
306
|
mTimer = new Timer();
|
307
|
|
308
|
mTimer.scheduleAtFixedRate(new TimerTask()
|
309
|
{
|
310
|
@Override
|
311
|
public void run()
|
312
|
{
|
313
|
act.runOnUiThread(new Runnable()
|
314
|
{
|
315
|
@Override
|
316
|
public void run()
|
317
|
{
|
318
|
int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
|
319
|
mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
|
320
|
}
|
321
|
});
|
322
|
}
|
323
|
}, 0, 1000);
|
324
|
}
|
325
|
}
|
326
|
|
327
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
328
|
|
329
|
private void stopCounting()
|
330
|
{
|
331
|
if( mTimer!=null )
|
332
|
{
|
333
|
mTimer.cancel();
|
334
|
mTimer = null;
|
335
|
}
|
336
|
|
337
|
mRunning = false;
|
338
|
}
|
339
|
|
340
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
341
|
|
342
|
public long getRecord()
|
343
|
{
|
344
|
if( mRunning )
|
345
|
{
|
346
|
stopCounting();
|
347
|
|
348
|
mElapsed = System.currentTimeMillis()-mStartTime;
|
349
|
|
350
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
351
|
int object = play.getObject();
|
352
|
int size = play.getSize();
|
353
|
int level = play.getLevel();
|
354
|
int realSize= RubikObjectList.getSizeIndex(object,size);
|
355
|
|
356
|
boolean isNew = mScores.setRecord(object, realSize, level, mElapsed);
|
357
|
|
358
|
return isNew ? mElapsed : -mElapsed;
|
359
|
}
|
360
|
|
361
|
return 0;
|
362
|
}
|
363
|
|
364
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
365
|
|
366
|
public void resetElapsed()
|
367
|
{
|
368
|
mElapsed = 0;
|
369
|
}
|
370
|
|
371
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
372
|
|
373
|
public void onActionFinished(final long effectID)
|
374
|
{
|
375
|
mCanPrevMove = true;
|
376
|
}
|
377
|
}
|