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