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 android.view.View;
|
23
|
import android.widget.ImageButton;
|
24
|
import android.widget.LinearLayout;
|
25
|
|
26
|
import org.distorted.main.R;
|
27
|
import org.distorted.main.RubikActivity;
|
28
|
|
29
|
import java.util.ArrayList;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
public class MovesAndLockController implements MovesFinished
|
34
|
{
|
35
|
private static final int MILLIS_PER_DEGREE = 6;
|
36
|
|
37
|
private static class Move
|
38
|
{
|
39
|
private final int mAxis, mRow, mAngle;
|
40
|
|
41
|
Move(int axis, int row, int angle)
|
42
|
{
|
43
|
mAxis = axis;
|
44
|
mRow = row;
|
45
|
mAngle= angle;
|
46
|
}
|
47
|
}
|
48
|
|
49
|
private ArrayList<Move> mMoves;
|
50
|
private boolean mCanPrevMove;
|
51
|
private TwistyPreRender mPre;
|
52
|
private ImageButton mPrevButton, mLockButton;
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
// PUBLIC API
|
56
|
|
57
|
public MovesAndLockController()
|
58
|
{
|
59
|
mCanPrevMove = true;
|
60
|
|
61
|
if( mMoves==null ) mMoves = new ArrayList<>();
|
62
|
else mMoves.clear();
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
public void toggleLock(TwistyActivity act)
|
68
|
{
|
69
|
act.toggleLock();
|
70
|
mLockButton.setImageResource(getLockIcon(act));
|
71
|
}
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
public int getLockIcon(TwistyActivity act)
|
76
|
{
|
77
|
if( act.retLocked() )
|
78
|
{
|
79
|
return RubikActivity.getDrawable(R.drawable.ui_small_locked,R.drawable.ui_medium_locked, R.drawable.ui_big_locked, R.drawable.ui_huge_locked);
|
80
|
}
|
81
|
else
|
82
|
{
|
83
|
return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,R.drawable.ui_medium_unlocked, R.drawable.ui_big_unlocked, R.drawable.ui_huge_unlocked);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
public void backMove(TwistyPreRender pre)
|
90
|
{
|
91
|
if( mCanPrevMove )
|
92
|
{
|
93
|
int numMoves = mMoves.size();
|
94
|
|
95
|
if( numMoves>0 )
|
96
|
{
|
97
|
Move move = mMoves.remove(numMoves-1);
|
98
|
int axis = move.mAxis;
|
99
|
int row = (1<<move.mRow);
|
100
|
int angle = move.mAngle;
|
101
|
int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
|
102
|
|
103
|
if( angle!=0 )
|
104
|
{
|
105
|
mCanPrevMove = false;
|
106
|
mPre = pre;
|
107
|
pre.blockTouch();
|
108
|
pre.addRotation(this, axis, row, -angle, duration);
|
109
|
}
|
110
|
else
|
111
|
{
|
112
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
public void addMove(int axis, int row, int angle)
|
121
|
{
|
122
|
mMoves.add(new Move(axis,row,angle));
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
public void onActionFinished(final long effectID)
|
128
|
{
|
129
|
mCanPrevMove = true;
|
130
|
mPre.unblockTouch();
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
public void clearMoves()
|
136
|
{
|
137
|
mMoves.clear();
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
public void setupPrevButton(final TwistyActivity act, final float width)
|
143
|
{
|
144
|
int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_back,R.drawable.ui_medium_cube_back, R.drawable.ui_big_cube_back, R.drawable.ui_huge_cube_back);
|
145
|
mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
|
146
|
|
147
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
148
|
{
|
149
|
@Override
|
150
|
public void onClick(View v)
|
151
|
{
|
152
|
TwistyPreRender pre = act.getTwistyPreRender();
|
153
|
backMove(pre);
|
154
|
}
|
155
|
});
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void setupLockButton(final TwistyActivity act, final float width)
|
161
|
{
|
162
|
final int icon = getLockIcon(act);
|
163
|
mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
|
164
|
|
165
|
mLockButton.setOnClickListener( new View.OnClickListener()
|
166
|
{
|
167
|
@Override
|
168
|
public void onClick(View v)
|
169
|
{
|
170
|
toggleLock(act);
|
171
|
}
|
172
|
});
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
|
177
|
public void setLockState(final TwistyActivity act)
|
178
|
{
|
179
|
act.runOnUiThread(new Runnable()
|
180
|
{
|
181
|
@Override
|
182
|
public void run()
|
183
|
{
|
184
|
if( mLockButton!=null )
|
185
|
mLockButton.setImageResource(getLockIcon(act));
|
186
|
}
|
187
|
});
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
public ImageButton getPrevButton()
|
193
|
{
|
194
|
return mPrevButton;
|
195
|
}
|
196
|
|
197
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198
|
|
199
|
public ImageButton getLockButton()
|
200
|
{
|
201
|
return mLockButton;
|
202
|
}
|
203
|
}
|