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 TextView mTime;
|
49
|
private Timer mTimer;
|
50
|
private long mStartTime;
|
51
|
private boolean mRunning;
|
52
|
private RubikScores mScores;
|
53
|
private Button mBack;
|
54
|
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
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
RubikStateSolving()
|
73
|
{
|
74
|
mScores = RubikScores.getInstance();
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
void leaveState(RubikActivity act)
|
80
|
{
|
81
|
stopCounting(act);
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
void enterState(final RubikActivity act)
|
87
|
{
|
88
|
mCanPrevMove = true;
|
89
|
|
90
|
if( mMoves==null ) mMoves = new ArrayList<>();
|
91
|
else mMoves.clear();
|
92
|
|
93
|
LayoutInflater inflater = act.getLayoutInflater();
|
94
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
95
|
float scale = metrics.density;
|
96
|
|
97
|
// TOP ////////////////////////////
|
98
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
99
|
layoutTop.removeAllViews();
|
100
|
mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
|
101
|
mTime.setText(R.string.ready);
|
102
|
layoutTop.addView(mTime);
|
103
|
|
104
|
// BOT ////////////////////////////
|
105
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
106
|
layoutLeft.removeAllViews();
|
107
|
|
108
|
if( mPrevButton==null ) setupPrevMoveButtom(act,scale);
|
109
|
layoutLeft.addView(mPrevButton);
|
110
|
|
111
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
112
|
layoutRight.removeAllViews();
|
113
|
|
114
|
int padding = (int)(5*scale + 0.5f);
|
115
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
116
|
|
117
|
mBack = new Button(act);
|
118
|
mBack.setLayoutParams(params);
|
119
|
mBack.setPadding(padding,0,padding,0);
|
120
|
mBack.setText(R.string.back);
|
121
|
|
122
|
mBack.setOnClickListener( new View.OnClickListener()
|
123
|
{
|
124
|
@Override
|
125
|
public void onClick(View v)
|
126
|
{
|
127
|
RubikState.goBack(act);
|
128
|
}
|
129
|
});
|
130
|
|
131
|
layoutRight.addView(mBack);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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.setVisibility(View.INVISIBLE);
|
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.setVisibility(View.INVISIBLE);
|
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.setVisibility(View.VISIBLE);
|
207
|
}
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
public void savePreferences(SharedPreferences.Editor editor)
|
213
|
{
|
214
|
mPrevButton = null;
|
215
|
|
216
|
mScores.savePreferences(editor);
|
217
|
}
|
218
|
|
219
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220
|
|
221
|
public void restorePreferences(SharedPreferences preferences)
|
222
|
{
|
223
|
mScores.restorePreferences(preferences);
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
|
228
|
public void startCounting(final RubikActivity act)
|
229
|
{
|
230
|
if( !mRunning )
|
231
|
{
|
232
|
mRunning = true;
|
233
|
mStartTime = System.currentTimeMillis();
|
234
|
mTimer = new Timer();
|
235
|
|
236
|
mTimer.scheduleAtFixedRate(new TimerTask()
|
237
|
{
|
238
|
@Override
|
239
|
public void run()
|
240
|
{
|
241
|
act.runOnUiThread(new Runnable()
|
242
|
{
|
243
|
@Override
|
244
|
public void run()
|
245
|
{
|
246
|
int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
|
247
|
mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
|
248
|
}
|
249
|
});
|
250
|
}
|
251
|
}, 0, 1000);
|
252
|
}
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
|
257
|
public long stopCounting(RubikActivity act)
|
258
|
{
|
259
|
if( mRunning )
|
260
|
{
|
261
|
act.runOnUiThread(new Runnable()
|
262
|
{
|
263
|
@Override
|
264
|
public void run()
|
265
|
{
|
266
|
mTime.setText(R.string.solved);
|
267
|
mBack.setClickable(false);
|
268
|
mPrevButton.setVisibility(View.INVISIBLE);
|
269
|
}
|
270
|
});
|
271
|
|
272
|
if( mTimer!=null )
|
273
|
{
|
274
|
mTimer.cancel();
|
275
|
mTimer = null;
|
276
|
}
|
277
|
mRunning = false;
|
278
|
|
279
|
long timeTaken = System.currentTimeMillis()-mStartTime;
|
280
|
|
281
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
282
|
int object = play.getObject();
|
283
|
int size = play.getSize();
|
284
|
int level = play.getLevel();
|
285
|
int realSize= RubikObjectList.getSizeIndex(object,size);
|
286
|
|
287
|
boolean isNew = mScores.setRecord(object, realSize, level, timeTaken);
|
288
|
|
289
|
return isNew ? timeTaken : -timeTaken;
|
290
|
}
|
291
|
|
292
|
return 0;
|
293
|
}
|
294
|
|
295
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
296
|
|
297
|
public void onActionFinished(final long effectID)
|
298
|
{
|
299
|
mCanPrevMove = true;
|
300
|
}
|
301
|
}
|