1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.helpers;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
|
14
|
import android.app.Activity;
|
15
|
import android.content.SharedPreferences;
|
16
|
import android.view.View;
|
17
|
import android.widget.LinearLayout;
|
18
|
|
19
|
import org.distorted.objectlib.helpers.MovesFinished;
|
20
|
import org.distorted.objectlib.main.ObjectControl;
|
21
|
|
22
|
import org.distorted.main.R;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
|
26
|
public class MovesController implements MovesFinished
|
27
|
{
|
28
|
private static final int MOVES_PLACE_0 = 100;
|
29
|
private static final int MILLIS_PER_DEGREE = 6;
|
30
|
|
31
|
private static class Move
|
32
|
{
|
33
|
private final int mAxis, mRow, mAngle;
|
34
|
|
35
|
Move(int axis, int row, int angle)
|
36
|
{
|
37
|
mAxis = axis;
|
38
|
mRow = row;
|
39
|
mAngle= angle;
|
40
|
}
|
41
|
}
|
42
|
|
43
|
private final ArrayList<Move> mMoves;
|
44
|
private boolean mCanPrevMove;
|
45
|
private ObjectControl mControl;
|
46
|
private TransparentImageButton mPrevButton;
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
// PUBLIC API
|
50
|
|
51
|
public MovesController()
|
52
|
{
|
53
|
mCanPrevMove = true;
|
54
|
mMoves = new ArrayList<>();
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
private int getPrevIcon(boolean on)
|
60
|
{
|
61
|
return on ? R.drawable.ui_cube_back : R.drawable.ui_cube_grey;
|
62
|
}
|
63
|
|
64
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
public void backMove(Activity act, ObjectControl control)
|
67
|
{
|
68
|
if( mCanPrevMove )
|
69
|
{
|
70
|
int numMoves = mMoves.size();
|
71
|
|
72
|
if( numMoves>0 )
|
73
|
{
|
74
|
Move move = mMoves.remove(numMoves-1);
|
75
|
int axis = move.mAxis;
|
76
|
int angle = move.mAngle;
|
77
|
|
78
|
if( angle!=0 )
|
79
|
{
|
80
|
mCanPrevMove = false;
|
81
|
mControl = control;
|
82
|
mControl.blockTouch(MOVES_PLACE_0);
|
83
|
mControl.addRotation(this, axis, (1<<move.mRow), -angle, MILLIS_PER_DEGREE);
|
84
|
}
|
85
|
else
|
86
|
{
|
87
|
android.util.Log.e("backMove", "error: trying to back move of angle 0");
|
88
|
}
|
89
|
|
90
|
if( numMoves==1 ) changeBackMove(act, false);
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
private void changeBackMove(Activity act, final boolean on)
|
98
|
{
|
99
|
act.runOnUiThread(new Runnable()
|
100
|
{
|
101
|
@Override
|
102
|
public void run()
|
103
|
{
|
104
|
if( mPrevButton!=null )
|
105
|
mPrevButton.setImageResource(getPrevIcon(on));
|
106
|
}
|
107
|
});
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
public void addMove(Activity act, int axis, int row, int angle)
|
113
|
{
|
114
|
if( mMoves.isEmpty() ) changeBackMove(act,true);
|
115
|
mMoves.add(new Move(axis,row,angle));
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
public void onActionFinished(final long effectID)
|
121
|
{
|
122
|
mCanPrevMove = true;
|
123
|
mControl.unblockRotation();
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
public void clearMoves(final Activity act)
|
129
|
{
|
130
|
mMoves.clear();
|
131
|
changeBackMove(act,false);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
public int getNumMoves()
|
137
|
{
|
138
|
return mMoves.size();
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
public void setupButton(final Activity act, ObjectControl control)
|
144
|
{
|
145
|
final int icon = getPrevIcon( !mMoves.isEmpty() );
|
146
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
147
|
mPrevButton = new TransparentImageButton(act,icon,params);
|
148
|
|
149
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
150
|
{
|
151
|
@Override
|
152
|
public void onClick(View v)
|
153
|
{
|
154
|
if( control.isScramblingAndSolvingNotBlocked() ) backMove(act,control);
|
155
|
}
|
156
|
});
|
157
|
}
|
158
|
|
159
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160
|
|
161
|
public void savePreferences(String key, SharedPreferences.Editor editor)
|
162
|
{
|
163
|
StringBuilder moves = new StringBuilder();
|
164
|
int numMoves = getNumMoves();
|
165
|
|
166
|
for(int m=0; m<numMoves; m++)
|
167
|
{
|
168
|
Move move = mMoves.get(m);
|
169
|
|
170
|
if( m>0 ) moves.append(' ');
|
171
|
moves.append(move.mAxis);
|
172
|
moves.append(' ');
|
173
|
moves.append(move.mRow);
|
174
|
moves.append(' ');
|
175
|
moves.append(move.mAngle);
|
176
|
}
|
177
|
|
178
|
editor.putString( key, moves.toString() );
|
179
|
editor.apply();
|
180
|
}
|
181
|
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
|
184
|
public void restorePreferences(Activity act, String key, SharedPreferences preferences)
|
185
|
{
|
186
|
String objects = preferences.getString( key,"");
|
187
|
|
188
|
if( objects.length()>0 )
|
189
|
{
|
190
|
String[] tokens = objects.split(" ");
|
191
|
int length = tokens.length/3;
|
192
|
|
193
|
for(int m=0; m<length; m++)
|
194
|
{
|
195
|
String axis = tokens[3*m ];
|
196
|
String row = tokens[3*m+1];
|
197
|
String angle = tokens[3*m+2];
|
198
|
|
199
|
try
|
200
|
{
|
201
|
int axisI = Integer.parseInt(axis);
|
202
|
int rowI = Integer.parseInt(row);
|
203
|
int angleI= Integer.parseInt(angle);
|
204
|
|
205
|
addMove(act,axisI,rowI,angleI);
|
206
|
}
|
207
|
catch(NumberFormatException ex)
|
208
|
{
|
209
|
// ignore
|
210
|
android.util.Log.e("D", "exception: "+ex.getMessage());
|
211
|
}
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
|
216
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
217
|
|
218
|
public TransparentImageButton getButton()
|
219
|
{
|
220
|
return mPrevButton;
|
221
|
}
|
222
|
}
|