1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 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.tutorial;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.content.res.Resources;
|
24
|
|
25
|
import org.distorted.effects.BaseEffect;
|
26
|
import org.distorted.effects.EffectController;
|
27
|
import org.distorted.objects.ObjectList;
|
28
|
import org.distorted.objects.TwistyObject;
|
29
|
import org.distorted.main.RubikPreRender.ActionFinishedListener;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
public class TutorialPreRender implements EffectController
|
34
|
{
|
35
|
private ActionFinishedListener mAddActionListener;
|
36
|
private TutorialSurfaceView mView;
|
37
|
private boolean mFinishRotation, mRemoveRotation, mAddRotation,
|
38
|
mSetQuat, mChangeObject, mSetupObject, mSolveObject,
|
39
|
mInitializeObject, mResetAllTextureMaps, mRemovePatternRotation;
|
40
|
private boolean mCanPlay;
|
41
|
private boolean mIsSolved;
|
42
|
private ObjectList mNextObject;
|
43
|
private int mNextSize;
|
44
|
private long mRotationFinishedID;
|
45
|
private int mScreenWidth;
|
46
|
private int[][] mNextMoves;
|
47
|
private TwistyObject mOldObject, mNewObject;
|
48
|
private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
|
49
|
private long mAddRotationDuration;
|
50
|
private long mAddRotationID, mRemoveRotationID;
|
51
|
private int mNearestAngle;
|
52
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
TutorialPreRender(TutorialSurfaceView view)
|
56
|
{
|
57
|
mView = view;
|
58
|
|
59
|
mFinishRotation = false;
|
60
|
mRemoveRotation = false;
|
61
|
mAddRotation = false;
|
62
|
mSetQuat = false;
|
63
|
mChangeObject = false;
|
64
|
mSetupObject = false;
|
65
|
mSolveObject = false;
|
66
|
mCanPlay = true;
|
67
|
mOldObject = null;
|
68
|
mNewObject = null;
|
69
|
mScreenWidth = 0;
|
70
|
|
71
|
mRemovePatternRotation= false;
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
private void createObjectNow(ObjectList object, int size, int[][] moves)
|
77
|
{
|
78
|
if( mOldObject!=null ) mOldObject.releaseResources();
|
79
|
mOldObject = mNewObject;
|
80
|
|
81
|
Context con = mView.getContext();
|
82
|
Resources res = con.getResources();
|
83
|
|
84
|
mNewObject = object.create(size, mView.getQuat(), moves, res, mScreenWidth);
|
85
|
|
86
|
if( mNewObject!=null )
|
87
|
{
|
88
|
mNewObject.createTexture();
|
89
|
mView.setMovement(object.getObjectMovementClass());
|
90
|
|
91
|
if( mScreenWidth!=0 )
|
92
|
{
|
93
|
mNewObject.recomputeScaleFactor(mScreenWidth);
|
94
|
}
|
95
|
|
96
|
mIsSolved = mNewObject.isSolved();
|
97
|
}
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
private void doEffectNow(BaseEffect.Type type)
|
103
|
{
|
104
|
try
|
105
|
{
|
106
|
type.startEffect(mView.getRenderer().getScreen(),this);
|
107
|
}
|
108
|
catch( Exception ex )
|
109
|
{
|
110
|
mCanPlay= true;
|
111
|
}
|
112
|
}
|
113
|
|
114
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
115
|
|
116
|
private void removePatternRotation()
|
117
|
{
|
118
|
mRemovePatternRotation = true;
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
private void removePatternRotationNow()
|
124
|
{
|
125
|
mRemovePatternRotation=false;
|
126
|
mNewObject.removeRotationNow();
|
127
|
mAddActionListener.onActionFinished(mRemoveRotationID);
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
private void removeRotationNow()
|
133
|
{
|
134
|
mRemoveRotation=false;
|
135
|
mNewObject.removeRotationNow();
|
136
|
|
137
|
boolean solved = mNewObject.isSolved();
|
138
|
|
139
|
if( solved && !mIsSolved )
|
140
|
{
|
141
|
doEffectNow( BaseEffect.Type.WIN );
|
142
|
}
|
143
|
else
|
144
|
{
|
145
|
mCanPlay = true;
|
146
|
}
|
147
|
|
148
|
mIsSolved = solved;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
private void removeRotation()
|
154
|
{
|
155
|
mRemoveRotation = true;
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
private void addRotationNow()
|
161
|
{
|
162
|
mAddRotation = false;
|
163
|
mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
|
164
|
mAddRotationAngle, mAddRotationDuration, this);
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
private void finishRotationNow()
|
170
|
{
|
171
|
mFinishRotation = false;
|
172
|
mCanPlay = false;
|
173
|
mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
|
174
|
|
175
|
if( mRotationFinishedID==0 ) // failed to add effect - should never happen
|
176
|
{
|
177
|
mCanPlay = true;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
private void changeObjectNow()
|
184
|
{
|
185
|
mChangeObject = false;
|
186
|
|
187
|
if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject || mNewObject.getNumLayers()!=mNextSize)
|
188
|
{
|
189
|
mCanPlay = false;
|
190
|
createObjectNow(mNextObject, mNextSize, null);
|
191
|
doEffectNow( BaseEffect.Type.SIZECHANGE );
|
192
|
}
|
193
|
}
|
194
|
|
195
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196
|
|
197
|
private void setupObjectNow()
|
198
|
{
|
199
|
mSetupObject = false;
|
200
|
|
201
|
if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject || mNewObject.getNumLayers()!=mNextSize)
|
202
|
{
|
203
|
mCanPlay = false;
|
204
|
createObjectNow(mNextObject, mNextSize, mNextMoves);
|
205
|
doEffectNow( BaseEffect.Type.SIZECHANGE );
|
206
|
}
|
207
|
else
|
208
|
{
|
209
|
mNewObject.initializeObject(mNextMoves);
|
210
|
}
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
|
215
|
private void solveObjectNow()
|
216
|
{
|
217
|
mSolveObject = false;
|
218
|
mCanPlay = false;
|
219
|
doEffectNow( BaseEffect.Type.SOLVE );
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
|
224
|
private void initializeObjectNow()
|
225
|
{
|
226
|
mInitializeObject = false;
|
227
|
mNewObject.initializeObject(mNextMoves);
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
|
232
|
private void resetAllTextureMapsNow()
|
233
|
{
|
234
|
mResetAllTextureMaps = false;
|
235
|
|
236
|
if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
|
237
|
}
|
238
|
|
239
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240
|
|
241
|
private void setQuatNow()
|
242
|
{
|
243
|
mSetQuat = false;
|
244
|
mView.setQuat();
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
//
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
void setScreenSize(int width)
|
252
|
{
|
253
|
if( mNewObject!=null )
|
254
|
{
|
255
|
mNewObject.createTexture();
|
256
|
mNewObject.recomputeScaleFactor(width);
|
257
|
}
|
258
|
|
259
|
mScreenWidth = width;
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
|
264
|
void finishRotation(int nearestAngle)
|
265
|
{
|
266
|
mNearestAngle = nearestAngle;
|
267
|
mFinishRotation = true;
|
268
|
}
|
269
|
|
270
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271
|
|
272
|
void changeObject(ObjectList object, int size)
|
273
|
{
|
274
|
if( size>0 )
|
275
|
{
|
276
|
mChangeObject = true;
|
277
|
mNextObject = object;
|
278
|
mNextSize = size;
|
279
|
}
|
280
|
}
|
281
|
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
void setQuatOnNextRender()
|
285
|
{
|
286
|
mSetQuat = true;
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
|
291
|
void preRender()
|
292
|
{
|
293
|
if( mSetQuat ) setQuatNow();
|
294
|
if( mFinishRotation ) finishRotationNow();
|
295
|
if( mRemoveRotation ) removeRotationNow();
|
296
|
if( mChangeObject ) changeObjectNow();
|
297
|
if( mSetupObject ) setupObjectNow();
|
298
|
if( mSolveObject ) solveObjectNow();
|
299
|
if( mAddRotation ) addRotationNow();
|
300
|
if( mInitializeObject ) initializeObjectNow();
|
301
|
if( mResetAllTextureMaps ) resetAllTextureMapsNow();
|
302
|
if( mRemovePatternRotation ) removePatternRotationNow();
|
303
|
}
|
304
|
|
305
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306
|
// PUBLIC API
|
307
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
308
|
|
309
|
public void addRotation(ActionFinishedListener listener, int axis, int rowBitmap, int angle, long duration)
|
310
|
{
|
311
|
mAddRotation = true;
|
312
|
|
313
|
mAddActionListener = listener;
|
314
|
mAddRotationAxis = axis;
|
315
|
mAddRotationRowBitmap = rowBitmap;
|
316
|
mAddRotationAngle = angle;
|
317
|
mAddRotationDuration = duration;
|
318
|
}
|
319
|
|
320
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
321
|
|
322
|
public void initializeObject(int[][] moves)
|
323
|
{
|
324
|
mInitializeObject = true;
|
325
|
mNextMoves = moves;
|
326
|
}
|
327
|
|
328
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
329
|
|
330
|
public int getNumScrambles()
|
331
|
{
|
332
|
return 0;
|
333
|
}
|
334
|
|
335
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
336
|
|
337
|
public void solveObject()
|
338
|
{
|
339
|
if( mCanPlay )
|
340
|
{
|
341
|
mSolveObject = true;
|
342
|
}
|
343
|
}
|
344
|
|
345
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
346
|
|
347
|
public void resetAllTextureMaps()
|
348
|
{
|
349
|
mResetAllTextureMaps = true;
|
350
|
}
|
351
|
|
352
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
353
|
|
354
|
public TwistyObject getObject()
|
355
|
{
|
356
|
return mNewObject;
|
357
|
}
|
358
|
|
359
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
360
|
|
361
|
public TwistyObject getOldObject()
|
362
|
{
|
363
|
return null;
|
364
|
}
|
365
|
|
366
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
367
|
|
368
|
public void effectFinished(final long effectID)
|
369
|
{
|
370
|
if( effectID == mRotationFinishedID )
|
371
|
{
|
372
|
mRotationFinishedID = 0;
|
373
|
removeRotation();
|
374
|
}
|
375
|
else if( effectID == mAddRotationID )
|
376
|
{
|
377
|
mAddRotationID = 0;
|
378
|
mRemoveRotationID = effectID;
|
379
|
removePatternRotation();
|
380
|
}
|
381
|
else
|
382
|
{
|
383
|
mCanPlay = true;
|
384
|
}
|
385
|
}
|
386
|
}
|