1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 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.helpers;
|
21
|
|
22
|
import java.util.ArrayList;
|
23
|
|
24
|
import android.app.Activity;
|
25
|
import android.content.SharedPreferences;
|
26
|
import android.view.View;
|
27
|
import android.widget.LinearLayout;
|
28
|
|
29
|
import org.distorted.objectlib.helpers.MovesFinished;
|
30
|
import org.distorted.objectlib.main.ObjectControl;
|
31
|
|
32
|
import org.distorted.main.R;
|
33
|
import org.distorted.main.RubikActivity;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
|
37
|
public class MovesController implements MovesFinished
|
38
|
{
|
39
|
private static final int MOVES_PLACE_0 = 100;
|
40
|
private static final int MILLIS_PER_DEGREE = 6;
|
41
|
|
42
|
private static class Move
|
43
|
{
|
44
|
private final int mAxis, mRow, mAngle;
|
45
|
|
46
|
Move(int axis, int row, int angle)
|
47
|
{
|
48
|
mAxis = axis;
|
49
|
mRow = row;
|
50
|
mAngle= angle;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
private final ArrayList<Move> mMoves;
|
55
|
private boolean mCanPrevMove;
|
56
|
private ObjectControl mControl;
|
57
|
private TransparentImageButton mPrevButton;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
// PUBLIC API
|
61
|
|
62
|
public MovesController()
|
63
|
{
|
64
|
mCanPrevMove = true;
|
65
|
mMoves = new ArrayList<>();
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
private int getPrevIcon(boolean on)
|
71
|
{
|
72
|
if( on )
|
73
|
{
|
74
|
return RubikActivity.getDrawable(R.drawable.ui_small_cube_back,
|
75
|
R.drawable.ui_medium_cube_back,
|
76
|
R.drawable.ui_big_cube_back,
|
77
|
R.drawable.ui_huge_cube_back);
|
78
|
}
|
79
|
else
|
80
|
{
|
81
|
return RubikActivity.getDrawable(R.drawable.ui_small_cube_grey,
|
82
|
R.drawable.ui_medium_cube_grey,
|
83
|
R.drawable.ui_big_cube_grey,
|
84
|
R.drawable.ui_huge_cube_grey);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
public void backMove(Activity act, ObjectControl control)
|
91
|
{
|
92
|
if( mCanPrevMove )
|
93
|
{
|
94
|
int numMoves = mMoves.size();
|
95
|
|
96
|
if( numMoves>0 )
|
97
|
{
|
98
|
Move move = mMoves.remove(numMoves-1);
|
99
|
int axis = move.mAxis;
|
100
|
int angle = move.mAngle;
|
101
|
|
102
|
if( angle!=0 )
|
103
|
{
|
104
|
mCanPrevMove = false;
|
105
|
mControl = control;
|
106
|
mControl.blockTouch(MOVES_PLACE_0);
|
107
|
mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
|
108
|
}
|
109
|
else
|
110
|
{
|
111
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
112
|
}
|
113
|
|
114
|
if( numMoves==1 ) changeBackMove(act, false);
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
private void changeBackMove(Activity act, final boolean on)
|
122
|
{
|
123
|
act.runOnUiThread(new Runnable()
|
124
|
{
|
125
|
@Override
|
126
|
public void run()
|
127
|
{
|
128
|
if( mPrevButton!=null )
|
129
|
mPrevButton.setIconResource(getPrevIcon(on));
|
130
|
}
|
131
|
});
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
public void addMove(Activity act, int axis, int row, int angle)
|
137
|
{
|
138
|
if( mMoves.isEmpty() ) changeBackMove(act,true);
|
139
|
mMoves.add(new Move(axis,row,angle));
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
public void onActionFinished(final long effectID)
|
145
|
{
|
146
|
mCanPrevMove = true;
|
147
|
mControl.unblockTouch();
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
public void clearMoves(final Activity act)
|
153
|
{
|
154
|
mMoves.clear();
|
155
|
changeBackMove(act,false);
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public int getNumMoves()
|
161
|
{
|
162
|
return mMoves.size();
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public void setupButton(final Activity act, ObjectControl control)
|
168
|
{
|
169
|
final int icon = getPrevIcon( !mMoves.isEmpty() );
|
170
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
171
|
mPrevButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
172
|
|
173
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
174
|
{
|
175
|
@Override
|
176
|
public void onClick(View v)
|
177
|
{
|
178
|
backMove(act,control);
|
179
|
}
|
180
|
});
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
public void savePreferences(SharedPreferences.Editor editor)
|
186
|
{
|
187
|
StringBuilder moves = new StringBuilder();
|
188
|
int numMoves = getNumMoves();
|
189
|
|
190
|
for(int m=0; m<numMoves; m++)
|
191
|
{
|
192
|
Move move = mMoves.get(m);
|
193
|
|
194
|
if( m>0 ) moves.append(' ');
|
195
|
moves.append(move.mAxis);
|
196
|
moves.append(' ');
|
197
|
moves.append(move.mRow);
|
198
|
moves.append(' ');
|
199
|
moves.append(move.mAngle);
|
200
|
}
|
201
|
|
202
|
editor.putString("movesController", moves.toString() );
|
203
|
editor.apply();
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
public void restorePreferences(Activity act, SharedPreferences preferences)
|
209
|
{
|
210
|
String objects = preferences.getString("movesController","");
|
211
|
|
212
|
if( objects.length()>0 )
|
213
|
{
|
214
|
String[] tokens = objects.split(" ");
|
215
|
int length = tokens.length;
|
216
|
|
217
|
for(int m=0; m<length/3; m++)
|
218
|
{
|
219
|
String axis = tokens[3*m ];
|
220
|
String row = tokens[3*m+1];
|
221
|
String angle = tokens[3*m+2];
|
222
|
|
223
|
try
|
224
|
{
|
225
|
int axisI = Integer.parseInt(axis);
|
226
|
int rowI = Integer.parseInt(row);
|
227
|
int angleI= Integer.parseInt(angle);
|
228
|
|
229
|
addMove(act,axisI,rowI,angleI);
|
230
|
}
|
231
|
catch(NumberFormatException ex)
|
232
|
{
|
233
|
// ignore
|
234
|
android.util.Log.e("D", "exception: "+ex.getMessage());
|
235
|
}
|
236
|
}
|
237
|
}
|
238
|
}
|
239
|
|
240
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241
|
|
242
|
public TransparentImageButton getButton()
|
243
|
{
|
244
|
return mPrevButton;
|
245
|
}
|
246
|
}
|