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.view.View;
|
25
|
import android.widget.ImageButton;
|
26
|
import android.widget.LinearLayout;
|
27
|
|
28
|
import org.distorted.objectlib.helpers.BlockController;
|
29
|
import org.distorted.objectlib.helpers.MovesFinished;
|
30
|
import org.distorted.objectlib.helpers.TwistyActivity;
|
31
|
import org.distorted.objectlib.main.ObjectControl;
|
32
|
|
33
|
import org.distorted.main.R;
|
34
|
import org.distorted.main.RubikActivity;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
|
38
|
public class MovesController implements MovesFinished
|
39
|
{
|
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 ImageButton 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(TwistyActivity act)
|
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 row = (1<<move.mRow);
|
101
|
int angle = move.mAngle;
|
102
|
int duration= Math.abs(angle)*MILLIS_PER_DEGREE;
|
103
|
|
104
|
if( angle!=0 )
|
105
|
{
|
106
|
mCanPrevMove = false;
|
107
|
mControl = act.getControl();
|
108
|
mControl.blockTouch(BlockController.MOVES_PLACE_0);
|
109
|
mControl.addRotation(this, axis, row, -angle, duration);
|
110
|
}
|
111
|
else
|
112
|
{
|
113
|
android.util.Log.e("solution", "error: trying to back move of angle 0");
|
114
|
}
|
115
|
|
116
|
if( numMoves==1 ) changeBackMove(act, false);
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
private void changeBackMove(TwistyActivity act, final boolean on)
|
124
|
{
|
125
|
act.runOnUiThread(new Runnable()
|
126
|
{
|
127
|
@Override
|
128
|
public void run()
|
129
|
{
|
130
|
if( mPrevButton!=null )
|
131
|
mPrevButton.setImageResource(getPrevIcon(on));
|
132
|
}
|
133
|
});
|
134
|
}
|
135
|
|
136
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137
|
|
138
|
public void addMove(TwistyActivity act, int axis, int row, int angle)
|
139
|
{
|
140
|
if( mMoves.isEmpty() ) changeBackMove(act,true);
|
141
|
mMoves.add(new Move(axis,row,angle));
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
public void onActionFinished(final long effectID)
|
147
|
{
|
148
|
mCanPrevMove = true;
|
149
|
mControl.unblockTouch();
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
public void clearMoves(final TwistyActivity act)
|
155
|
{
|
156
|
mMoves.clear();
|
157
|
changeBackMove(act,false);
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
|
162
|
public int getNumMoves()
|
163
|
{
|
164
|
return mMoves.size();
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
public void setupButton(final TwistyActivity act, final float width)
|
170
|
{
|
171
|
final int icon = getPrevIcon( !mMoves.isEmpty() );
|
172
|
mPrevButton = new TransparentImageButton(act, icon, width, LinearLayout.LayoutParams.MATCH_PARENT);
|
173
|
|
174
|
mPrevButton.setOnClickListener( new View.OnClickListener()
|
175
|
{
|
176
|
@Override
|
177
|
public void onClick(View v)
|
178
|
{
|
179
|
backMove(act);
|
180
|
}
|
181
|
});
|
182
|
}
|
183
|
|
184
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185
|
|
186
|
public ImageButton getButton()
|
187
|
{
|
188
|
return mPrevButton;
|
189
|
}
|
190
|
}
|