Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ e8205412

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.tutorials;
21

    
22
import android.content.Context;
23
import android.content.res.Resources;
24

    
25
import org.distorted.objectlib.main.ObjectType;
26
import org.distorted.objectlib.main.TwistyObject;
27

    
28
import org.distorted.effects.BaseEffect;
29
import org.distorted.effects.EffectController;
30
import org.distorted.helpers.BlockController;
31
import org.distorted.helpers.MovesFinished;
32
import org.distorted.helpers.TwistyPreRender;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class TutorialPreRender implements EffectController, TwistyPreRender
37
  {
38
  private MovesFinished mAddActionListener;
39
  private final TutorialSurfaceView mView;
40
  private boolean mFinishRotation, mRemoveRotation, mAddRotation,
41
                  mSetQuat, mChangeObject, mSetupObject, mSolveObject, mScrambleObject,
42
                  mInitializeObject, mResetAllTextureMaps, mRemovePatternRotation, mSolve;
43
  private boolean mUIBlocked, mTouchBlocked;
44
  private boolean mIsSolved;
45
  private ObjectType mNextObject;
46
  private long mRotationFinishedID;
47
  private int mScreenWidth;
48
  private TwistyObject mOldObject, mNewObject;
49
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
50
  private long mAddRotationDuration;
51
  private long mAddRotationID, mRemoveRotationID;
52
  private int mNearestAngle;
53
  private int mScrambleObjectNum;
54
  private final BlockController mBlockController;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  TutorialPreRender(TutorialSurfaceView view)
59
    {
60
    mView = view;
61

    
62
    mFinishRotation = false;
63
    mRemoveRotation = false;
64
    mAddRotation    = false;
65
    mSetQuat        = false;
66
    mChangeObject   = false;
67
    mSetupObject    = false;
68
    mSolveObject    = false;
69
    mSolve          = false;
70
    mScrambleObject = false;
71

    
72
    mOldObject      = null;
73
    mNewObject      = null;
74

    
75
    mScreenWidth       = 0;
76
    mScrambleObjectNum = 0;
77

    
78
    mRemovePatternRotation= false;
79

    
80
    TutorialActivity act = (TutorialActivity)mView.getContext();
81
    mBlockController = new BlockController(act);
82
    unblockEverything();
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private void createObjectNow(ObjectType object)
88
    {
89
    if( mOldObject!=null ) mOldObject.releaseResources();
90
    mOldObject = mNewObject;
91

    
92
    Context con = mView.getContext();
93
    Resources res = con.getResources();
94

    
95
    mNewObject = object.create(mView.getQuat(), null, res, mScreenWidth);
96

    
97
    if( mNewObject!=null )
98
      {
99
      mView.setMovement(mNewObject.getMovement());
100
      mIsSolved = mNewObject.isSolved();
101
      }
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  private void doEffectNow(BaseEffect.Type type)
107
    {
108
    try
109
      {
110
      type.startEffect(mView.getRenderer().getScreen(),this);
111
      }
112
    catch( Exception ex )
113
      {
114
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
115
      unblockEverything();
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  private void removePatternRotation()
122
    {
123
    mRemovePatternRotation = true;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void removePatternRotationNow()
129
    {
130
    mRemovePatternRotation=false;
131
    mNewObject.removeRotationNow();
132
    mAddActionListener.onActionFinished(mRemoveRotationID);
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void removeRotationNow()
138
    {
139
    mRemoveRotation=false;
140
    mNewObject.removeRotationNow();
141

    
142
    boolean solved = mNewObject.isSolved();
143
    unblockEverything();
144
    if( solved && !mIsSolved ) doEffectNow( BaseEffect.Type.WIN );
145

    
146
    mIsSolved = solved;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private void removeRotation()
152
    {
153
    mRemoveRotation = true;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private void addRotationNow()
159
    {
160
    mAddRotation = false;
161
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
162
                                                mAddRotationAngle, mAddRotationDuration, this);
163

    
164
    if( mAddRotationID==0 ) // failed to add effect - should never happen
165
      {
166
      unblockEverything();
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void finishRotationNow()
173
    {
174
    mFinishRotation = false;
175
    blockEverything(BlockController.TUTORIAL_PLACE_0);
176
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
177

    
178
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
179
      {
180
      unblockEverything();
181
      }
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private void changeObjectNow()
187
    {
188
    mChangeObject = false;
189

    
190
    if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject)
191
      {
192
      blockEverything(BlockController.TUTORIAL_PLACE_1);
193
      createObjectNow(mNextObject);
194
      doEffectNow( BaseEffect.Type.SIZECHANGE );
195
      }
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void setupObjectNow()
201
    {
202
    mSetupObject = false;
203

    
204
    if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject)
205
      {
206
      blockEverything(BlockController.TUTORIAL_PLACE_2);
207
      createObjectNow(mNextObject);
208
      doEffectNow( BaseEffect.Type.SIZECHANGE );
209
      }
210
    else
211
      {
212
      mNewObject.initializeObject(null);
213
      }
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private void scrambleObjectNow()
219
    {
220
    mScrambleObject = false;
221
    mIsSolved       = false;
222
    blockEverything(BlockController.TUTORIAL_PLACE_3);
223
    doEffectNow( BaseEffect.Type.SCRAMBLE );
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  private void solveObjectNow()
229
    {
230
    mSolveObject = false;
231
    blockEverything(BlockController.TUTORIAL_PLACE_4);
232
    doEffectNow( BaseEffect.Type.SOLVE );
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  private void solveNow()
238
    {
239
    mSolve = false;
240
    mNewObject.solve();
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  private void initializeObjectNow()
246
    {
247
    mInitializeObject = false;
248
    mNewObject.initializeObject(null);
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private void resetAllTextureMapsNow()
254
    {
255
    mResetAllTextureMaps = false;
256

    
257
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private void setQuatNow()
263
    {
264
    mSetQuat = false;
265
    mView.setQuat();
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
//
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  void setScreenSize(int width)
273
    {
274
    if( mNewObject!=null ) mNewObject.recomputeScaleFactor(width);
275
    mScreenWidth = width;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  void finishRotation(int nearestAngle)
281
    {
282
    mNearestAngle   = nearestAngle;
283
    mFinishRotation = true;
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  void changeObject(ObjectType object)
289
    {
290
    mChangeObject = true;
291
    mNextObject = object;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  void setQuatOnNextRender()
297
    {
298
    mSetQuat = true;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  void preRender()
304
    {
305
    if( mSolve                 ) solveNow();
306
    if( mSetQuat               ) setQuatNow();
307
    if( mFinishRotation        ) finishRotationNow();
308
    if( mRemoveRotation        ) removeRotationNow();
309
    if( mChangeObject          ) changeObjectNow();
310
    if( mSetupObject           ) setupObjectNow();
311
    if( mSolveObject           ) solveObjectNow();
312
    if( mScrambleObject        ) scrambleObjectNow();
313
    if( mAddRotation           ) addRotationNow();
314
    if( mInitializeObject      ) initializeObjectNow();
315
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
316
    if( mRemovePatternRotation ) removePatternRotationNow();
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
// PUBLIC API
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  public boolean isTouchBlocked()
324
    {
325
    return mTouchBlocked;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  public boolean isUINotBlocked()
331
    {
332
    return !mUIBlocked;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public void blockEverything(int place)
338
    {
339
    mUIBlocked   = true;
340
    mTouchBlocked= true;
341
    mBlockController.touchBlocked(place);
342
    mBlockController.uiBlocked(place);
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  public void blockTouch(int place)
348
    {
349
    mTouchBlocked= true;
350
    mBlockController.touchBlocked(place);
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  public void unblockEverything()
356
    {
357
    mUIBlocked   = false;
358
    mTouchBlocked= false;
359
    mBlockController.touchUnblocked();
360
    mBlockController.uiUnblocked();
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  public void unblockTouch()
366
    {
367
    mTouchBlocked= false;
368
    mBlockController.touchUnblocked();
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  public void unblockUI()
374
    {
375
    mUIBlocked= false;
376
    mBlockController.uiUnblocked();
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
382
    {
383
    mAddRotation = true;
384

    
385
    mAddActionListener    = listener;
386
    mAddRotationAxis      = axis;
387
    mAddRotationRowBitmap = rowBitmap;
388
    mAddRotationAngle     = angle;
389
    mAddRotationDuration  = duration;
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  public int getNumScrambles()
395
    {
396
    return mScrambleObjectNum;
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400
// this starts the SolveEffect
401

    
402
  public void solveObject()
403
    {
404
    if( !mUIBlocked )
405
      {
406
      mSolveObject = true;
407
      }
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
// this only solves the object
412

    
413
  public void solve()
414
    {
415
    mSolve = true;
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public void scrambleObject(int num)
421
    {
422
    if( !mUIBlocked )
423
      {
424
      mScrambleObject = true;
425
      mScrambleObjectNum = num;
426
      }
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  public TwistyObject getObject()
432
    {
433
    return mNewObject;
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

    
438
  public TwistyObject getOldObject()
439
    {
440
    return null;
441
    }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
  public void effectFinished(final long effectID)
446
    {
447
    if( effectID == mRotationFinishedID )
448
      {
449
      mRotationFinishedID = 0;
450
      removeRotation();
451
      }
452
    else if( effectID == mAddRotationID )
453
      {
454
      mAddRotationID = 0;
455
      mRemoveRotationID = effectID;
456
      removePatternRotation();
457
      }
458
    else
459
      {
460
      unblockEverything();  // buggy? I think we shouldn't do it if the effect is of type 'WIN'
461
      }
462
    }
463
  }
(3-3/7)