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.Gravity;
|
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.patterns.RubikPattern;
|
37
|
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
|
40
|
public class RubikStateSolution extends RubikStateAbstract implements RubikPreRender.ActionFinishedListener
|
41
|
{
|
42
|
private static final int DURATION_MILLIS = 750;
|
43
|
|
44
|
private ImageButton mPrevButton, mNextButton, mBackButton;
|
45
|
private TextView mMovesText;
|
46
|
private int[][] mMoves;
|
47
|
private int mCurrMove, mNumMoves;
|
48
|
private boolean mCanRotate;
|
49
|
private float mButtonSize, mTitleSize;
|
50
|
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
void leaveState(RubikActivity act)
|
54
|
{
|
55
|
RubikObject object = act.getObject();
|
56
|
object.solve();
|
57
|
}
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
void enterState(final RubikActivity act)
|
62
|
{
|
63
|
float width = act.getScreenWidthInPixels();
|
64
|
mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
|
65
|
mTitleSize = width*RubikActivity.TITLE_TEXT_SIZE;
|
66
|
|
67
|
LayoutInflater inflater = act.getLayoutInflater();
|
68
|
|
69
|
// TOP ////////////////////////////
|
70
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
71
|
layoutTop.removeAllViews();
|
72
|
|
73
|
final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
|
74
|
text.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize);
|
75
|
text.setText(R.string.solution);
|
76
|
layoutTop.addView(text);
|
77
|
|
78
|
// BOT ////////////////////////////
|
79
|
LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
|
80
|
layoutBot.removeAllViews();
|
81
|
|
82
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
|
83
|
LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
|
84
|
LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
|
85
|
|
86
|
LinearLayout layoutLeft = new LinearLayout(act);
|
87
|
layoutLeft.setLayoutParams(paramsL);
|
88
|
LinearLayout layoutMid = new LinearLayout(act);
|
89
|
layoutMid.setLayoutParams(paramsM);
|
90
|
LinearLayout layoutRight = new LinearLayout(act);
|
91
|
layoutRight.setLayoutParams(paramsR);
|
92
|
|
93
|
setupPrevButton(act,width);
|
94
|
setupNextButton(act,width);
|
95
|
setupTextView(act,width);
|
96
|
|
97
|
layoutLeft.addView(mPrevButton);
|
98
|
layoutLeft.addView(mMovesText);
|
99
|
layoutLeft.addView(mNextButton);
|
100
|
|
101
|
setupBackButton(act,width);
|
102
|
|
103
|
layoutRight.addView(mBackButton);
|
104
|
|
105
|
layoutBot.addView(layoutLeft);
|
106
|
layoutBot.addView(layoutMid);
|
107
|
layoutBot.addView(layoutRight);
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
private void setupPrevButton(final RubikActivity act, final float width)
|
113
|
{
|
114
|
int padding = (int)(width*RubikActivity.PADDING);
|
115
|
int margin = (int)(width*RubikActivity.MARGIN);
|
116
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
|
117
|
|
118
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
119
|
params.topMargin = margin;
|
120
|
params.bottomMargin = margin;
|
121
|
params.leftMargin = margin;
|
122
|
params.rightMargin = margin;
|
123
|
|
124
|
mPrevButton = new ImageButton(act);
|
125
|
mPrevButton.setLayoutParams(params);
|
126
|
mPrevButton.setPadding(padding,0,padding,0);
|
127
|
mPrevButton.setImageResource(icon);
|
128
|
|
129
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
130
|
{
|
131
|
@Override
|
132
|
public void onClick(View v)
|
133
|
{
|
134
|
RubikPreRender pre = act.getPreRender();
|
135
|
backMove(pre);
|
136
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
137
|
}
|
138
|
});
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
private void setupNextButton(final RubikActivity act, final float width)
|
144
|
{
|
145
|
int padding = (int)(width*RubikActivity.PADDING);
|
146
|
int margin = (int)(width*RubikActivity.MARGIN);
|
147
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
|
148
|
|
149
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
150
|
params.topMargin = margin;
|
151
|
params.bottomMargin = margin;
|
152
|
params.leftMargin = margin;
|
153
|
params.rightMargin = margin;
|
154
|
|
155
|
mNextButton = new ImageButton(act);
|
156
|
mNextButton.setLayoutParams(params);
|
157
|
mNextButton.setPadding(padding,0,padding,0);
|
158
|
mNextButton.setImageResource(icon);
|
159
|
|
160
|
mNextButton.setOnClickListener( new View.OnClickListener()
|
161
|
{
|
162
|
@Override
|
163
|
public void onClick(View v)
|
164
|
{
|
165
|
RubikPreRender pre = act.getPreRender();
|
166
|
makeMove(pre);
|
167
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
168
|
}
|
169
|
});
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
private void setupTextView(final RubikActivity act, final float width)
|
175
|
{
|
176
|
int padding = (int)(width*RubikActivity.PADDING);
|
177
|
int margin = (int)(width*RubikActivity.MARGIN);
|
178
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
|
179
|
params.topMargin = margin;
|
180
|
params.bottomMargin = margin;
|
181
|
params.leftMargin = margin;
|
182
|
params.rightMargin = margin;
|
183
|
|
184
|
mMovesText = new TextView(act);
|
185
|
mMovesText.setTextSize(20);
|
186
|
mMovesText.setLayoutParams(params);
|
187
|
mMovesText.setPadding(padding,0,padding,0);
|
188
|
mMovesText.setGravity(Gravity.CENTER);
|
189
|
mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
|
190
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
private void setupBackButton(final RubikActivity act, final float width)
|
196
|
{
|
197
|
int padding = (int)(width*RubikActivity.PADDING);
|
198
|
int margin = (int)(width*RubikActivity.MARGIN);
|
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
|
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);
|
206
|
|
207
|
mBackButton = new ImageButton(act);
|
208
|
mBackButton.setLayoutParams(params);
|
209
|
mBackButton.setPadding(padding,0,padding,0);
|
210
|
mBackButton.setImageResource(icon);
|
211
|
|
212
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
213
|
{
|
214
|
@Override
|
215
|
public void onClick(View v)
|
216
|
{
|
217
|
RubikState.goBack(act);
|
218
|
}
|
219
|
});
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
private void makeMove(RubikPreRender pre)
|
225
|
{
|
226
|
if( mCanRotate )
|
227
|
{
|
228
|
mCurrMove++;
|
229
|
|
230
|
if( mCurrMove>mNumMoves )
|
231
|
{
|
232
|
mCurrMove= 0;
|
233
|
pre.initializeObject(null);
|
234
|
}
|
235
|
else
|
236
|
{
|
237
|
int axis = mMoves[mCurrMove-1][0];
|
238
|
int rowBitmap= mMoves[mCurrMove-1][1];
|
239
|
int bareAngle= mMoves[mCurrMove-1][2];
|
240
|
int angle = bareAngle*(360/pre.getObject().getBasicAngle());
|
241
|
int numRot = Math.abs(bareAngle);
|
242
|
|
243
|
if( angle!=0 )
|
244
|
{
|
245
|
mCanRotate = false;
|
246
|
pre.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
|
247
|
}
|
248
|
else
|
249
|
{
|
250
|
android.util.Log.e("solution", "error: solution contains angle 0");
|
251
|
}
|
252
|
}
|
253
|
}
|
254
|
else
|
255
|
{
|
256
|
android.util.Log.e("solution", "failed to make move!");
|
257
|
}
|
258
|
}
|
259
|
|
260
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261
|
|
262
|
private void backMove(RubikPreRender pre)
|
263
|
{
|
264
|
if( mCanRotate )
|
265
|
{
|
266
|
mCurrMove--;
|
267
|
|
268
|
if( mCurrMove<0 )
|
269
|
{
|
270
|
mCurrMove=mNumMoves;
|
271
|
pre.initializeObject(mMoves);
|
272
|
}
|
273
|
else
|
274
|
{
|
275
|
int axis = mMoves[mCurrMove][0];
|
276
|
int rowBitmap= mMoves[mCurrMove][1];
|
277
|
int bareAngle= mMoves[mCurrMove][2];
|
278
|
int angle = bareAngle*(360/pre.getObject().getBasicAngle());
|
279
|
int numRot = Math.abs(bareAngle);
|
280
|
|
281
|
if( angle!=0 )
|
282
|
{
|
283
|
mCanRotate = false;
|
284
|
pre.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
|
285
|
}
|
286
|
else
|
287
|
{
|
288
|
android.util.Log.e("solution", "error: solution contains angle 0");
|
289
|
}
|
290
|
}
|
291
|
}
|
292
|
else
|
293
|
{
|
294
|
android.util.Log.e("solution", "failed to back move!");
|
295
|
}
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
void setupMoves(final RubikActivity act, String moves)
|
301
|
{
|
302
|
mCanRotate= true;
|
303
|
mCurrMove = 0;
|
304
|
mNumMoves = moves.length()/4;
|
305
|
mMoves = new int[mNumMoves][3];
|
306
|
|
307
|
RubikPattern.parseMoves(mMoves,mNumMoves,moves);
|
308
|
|
309
|
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
|
310
|
}
|
311
|
|
312
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
313
|
|
314
|
public void savePreferences(SharedPreferences.Editor editor)
|
315
|
{
|
316
|
|
317
|
}
|
318
|
|
319
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
320
|
|
321
|
public void restorePreferences(SharedPreferences preferences)
|
322
|
{
|
323
|
|
324
|
}
|
325
|
|
326
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
327
|
|
328
|
public void onActionFinished(final long effectID)
|
329
|
{
|
330
|
mCanRotate = true;
|
331
|
}
|
332
|
}
|