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