1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 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.bandaged;
|
21
|
|
22
|
import java.util.Random;
|
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.helpers.LockController;
|
30
|
import org.distorted.helpers.MovesController;
|
31
|
import org.distorted.helpers.TransparentImageButton;
|
32
|
import org.distorted.main.R;
|
33
|
import org.distorted.objectlib.main.ObjectControl;
|
34
|
import org.distorted.objectlib.main.TwistyObject;
|
35
|
import org.distorted.objectlib.scrambling.ObjectScrambler;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public class BandagedPlayScreen
|
40
|
{
|
41
|
public static final int ANIMATION_ON = 0;
|
42
|
public static final int ANIMATION_OFF = 1;
|
43
|
|
44
|
private static final int NUM_SCRAMBLES = 2;
|
45
|
|
46
|
private TransparentImageButton mBackButton, mScrambleButton, mSolveButton;
|
47
|
private final LockController mLockController;
|
48
|
private final MovesController mMovesController;
|
49
|
private String mKey;
|
50
|
private int[][] mMoves;
|
51
|
private Random mRnd;
|
52
|
|
53
|
private int mAnimationMode;
|
54
|
private int mScrambleDepth;
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
|
58
|
public BandagedPlayScreen()
|
59
|
{
|
60
|
mLockController = new LockController();
|
61
|
mMovesController= new MovesController();
|
62
|
|
63
|
mAnimationMode = ANIMATION_OFF;
|
64
|
mScrambleDepth = NUM_SCRAMBLES;
|
65
|
}
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
private void setupBackButton(final BandagedPlayActivity act)
|
70
|
{
|
71
|
int icon = BandagedPlayActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
|
72
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
73
|
mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
74
|
|
75
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
76
|
{
|
77
|
@Override
|
78
|
public void onClick(View v)
|
79
|
{
|
80
|
ObjectControl control = act.getControl();
|
81
|
if( control!=null ) control.unblockEverything();
|
82
|
act.finish();
|
83
|
}
|
84
|
});
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
private void setupSolveButton(final BandagedPlayActivity act)
|
90
|
{
|
91
|
int icon = BandagedPlayActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
|
92
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
93
|
mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
|
94
|
|
95
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
96
|
{
|
97
|
@Override
|
98
|
public void onClick(View v)
|
99
|
{
|
100
|
ObjectControl control = act.getControl();
|
101
|
if( mAnimationMode==ANIMATION_OFF ) control.solveOnly();
|
102
|
if( mAnimationMode==ANIMATION_ON ) control.solveObject();
|
103
|
mMovesController.clearMoves(act);
|
104
|
}
|
105
|
});
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
private void setupScrambleButton(final BandagedPlayActivity act)
|
111
|
{
|
112
|
int icon = BandagedPlayActivity.getDrawable(R.drawable.ui_small_cube_scramble,R.drawable.ui_medium_cube_scramble, R.drawable.ui_big_cube_scramble, R.drawable.ui_huge_cube_scramble);
|
113
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
114
|
mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
115
|
|
116
|
mScrambleButton.setOnClickListener( new View.OnClickListener()
|
117
|
{
|
118
|
@Override
|
119
|
public void onClick(View v)
|
120
|
{
|
121
|
ObjectControl control = act.getControl();
|
122
|
TwistyObject object = control.getObject();
|
123
|
ObjectScrambler.setSignature(object.getSignature());
|
124
|
|
125
|
if( mAnimationMode==ANIMATION_OFF )
|
126
|
{
|
127
|
if( mMoves==null || mMoves.length<mScrambleDepth ) mMoves = new int[mScrambleDepth][3];
|
128
|
if( mRnd==null ) mRnd = new Random();
|
129
|
|
130
|
for(int move=0; move<mScrambleDepth; move++)
|
131
|
{
|
132
|
object.randomizeNewScramble(mMoves, mRnd, move, mScrambleDepth);
|
133
|
}
|
134
|
for(int move=0; move<mScrambleDepth; move++)
|
135
|
{
|
136
|
int row = mMoves[move][1];
|
137
|
mMoves[move][1] = (1<<row);
|
138
|
}
|
139
|
|
140
|
control.initializeObject(mMoves);
|
141
|
}
|
142
|
|
143
|
if( mAnimationMode==ANIMATION_ON )
|
144
|
{
|
145
|
control.scrambleObject(mScrambleDepth);
|
146
|
}
|
147
|
}
|
148
|
});
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
void onAttachedToWindow(final BandagedPlayActivity act, String objectName)
|
154
|
{
|
155
|
mKey = "moveController_"+objectName;
|
156
|
|
157
|
int width = act.getScreenWidthInPixels();
|
158
|
|
159
|
LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
160
|
LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
|
161
|
LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
|
162
|
|
163
|
LinearLayout layoutLeft = new LinearLayout(act);
|
164
|
layoutLeft.setLayoutParams(paramsL);
|
165
|
LinearLayout layoutMid = new LinearLayout(act);
|
166
|
layoutMid.setLayoutParams(paramsM);
|
167
|
LinearLayout layoutRight= new LinearLayout(act);
|
168
|
layoutRight.setLayoutParams(paramsR);
|
169
|
|
170
|
setupBackButton(act);
|
171
|
ObjectControl control = act.getControl();
|
172
|
mMovesController.setupButton(act,control);
|
173
|
layoutLeft.addView(mMovesController.getButton());
|
174
|
mLockController.setupButton(act,control);
|
175
|
layoutMid.addView(mLockController.getButton());
|
176
|
|
177
|
layoutRight.addView(mBackButton);
|
178
|
|
179
|
LinearLayout layoutLower = act.findViewById(R.id.lowerBar);
|
180
|
layoutLower.removeAllViews();
|
181
|
layoutLower.addView(layoutLeft);
|
182
|
layoutLower.addView(layoutMid);
|
183
|
layoutLower.addView(layoutRight);
|
184
|
|
185
|
setupScrambleButton(act);
|
186
|
setupSolveButton(act);
|
187
|
|
188
|
LinearLayout layoutUpper = act.findViewById(R.id.upperBar);
|
189
|
|
190
|
LinearLayout layoutLeftU = new LinearLayout(act);
|
191
|
layoutLeftU.setLayoutParams(paramsL);
|
192
|
LinearLayout layoutMidU = new LinearLayout(act);
|
193
|
layoutMidU.setLayoutParams(paramsM);
|
194
|
LinearLayout layoutRightU= new LinearLayout(act);
|
195
|
layoutRightU.setLayoutParams(paramsR);
|
196
|
|
197
|
layoutLeftU.addView(mSolveButton);
|
198
|
layoutRightU.addView(mScrambleButton);
|
199
|
|
200
|
layoutUpper.removeAllViews();
|
201
|
layoutUpper.addView(layoutLeftU);
|
202
|
layoutUpper.addView(layoutMidU);
|
203
|
layoutUpper.addView(layoutRightU);
|
204
|
}
|
205
|
|
206
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207
|
|
208
|
public void setLockState(final BandagedPlayActivity act)
|
209
|
{
|
210
|
boolean locked = act.getControl().retLocked();
|
211
|
mLockController.setState(act,locked);
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
public void reddenLock(final BandagedPlayActivity act)
|
217
|
{
|
218
|
ObjectControl control = act.getControl();
|
219
|
mLockController.reddenLock(act,control);
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
public void addMove(Activity act, int axis, int row, int angle)
|
225
|
{
|
226
|
mMovesController.addMove(act,axis,row,angle);
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
|
231
|
public void savePreferences(BandagedPlayActivity act, SharedPreferences.Editor editor)
|
232
|
{
|
233
|
mMovesController.savePreferences(mKey,editor);
|
234
|
ObjectControl control = act.getControl();
|
235
|
control.savePreferences(editor);
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
public void restorePreferences(BandagedPlayActivity act, SharedPreferences preferences)
|
241
|
{
|
242
|
mMovesController.restorePreferences(act,mKey,preferences);
|
243
|
ObjectControl control = act.getControl();
|
244
|
control.restorePreferences(preferences);
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
public void setAnimationMode(int mode)
|
250
|
{
|
251
|
mAnimationMode = mode;
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
public void setScrambleDepth(int depth)
|
257
|
{
|
258
|
mScrambleDepth = depth;
|
259
|
}
|
260
|
}
|