Project

General

Profile

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

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

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.ObjectList;
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 ObjectList mNextObject;
46
  private int mNextSize;
47
  private long mRotationFinishedID;
48
  private int mScreenWidth;
49
  private TwistyObject mOldObject, mNewObject;
50
  private int mAddRotationAxis, mAddRotationRowBitmap, mAddRotationAngle;
51
  private long mAddRotationDuration;
52
  private long mAddRotationID, mRemoveRotationID;
53
  private int mNearestAngle;
54
  private int mScrambleObjectNum;
55
  private final BlockController mBlockController;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

    
73
    mOldObject      = null;
74
    mNewObject      = null;
75

    
76
    mScreenWidth       = 0;
77
    mScrambleObjectNum = 0;
78

    
79
    mRemovePatternRotation= false;
80

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  private void createObjectNow(ObjectList object, int size)
89
    {
90
    if( mOldObject!=null ) mOldObject.releaseResources();
91
    mOldObject = mNewObject;
92

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

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

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void removePatternRotation()
124
    {
125
    mRemovePatternRotation = true;
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void removeRotationNow()
140
    {
141
    mRemoveRotation=false;
142
    mNewObject.removeRotationNow();
143

    
144
    boolean solved = mNewObject.isSolved();
145
    unblockEverything();
146
    if( solved && !mIsSolved ) doEffectNow( BaseEffect.Type.WIN );
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
    if( mAddRotationID==0 ) // failed to add effect - should never happen
167
      {
168
      unblockEverything();
169
      }
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void changeObjectNow()
189
    {
190
    mChangeObject = false;
191

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void setupObjectNow()
203
    {
204
    mSetupObject = false;
205

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  private void resetAllTextureMapsNow()
256
    {
257
    mResetAllTextureMaps = false;
258

    
259
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
//
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  void setScreenSize(int width)
275
    {
276
    if( mNewObject!=null )
277
      {
278
      mNewObject.createTexture();
279
      mNewObject.recomputeScaleFactor(width);
280
      }
281

    
282
    mScreenWidth  = width;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  void finishRotation(int nearestAngle)
288
    {
289
    mNearestAngle   = nearestAngle;
290
    mFinishRotation = true;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  void changeObject(ObjectList object, int size)
296
    {
297
    if( size>0 )
298
      {
299
      mChangeObject = true;
300
      mNextObject = object;
301
      mNextSize   = size;
302
      }
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  void setQuatOnNextRender()
308
    {
309
    mSetQuat = true;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  void preRender()
315
    {
316
    if( mSolve                 ) solveNow();
317
    if( mSetQuat               ) setQuatNow();
318
    if( mFinishRotation        ) finishRotationNow();
319
    if( mRemoveRotation        ) removeRotationNow();
320
    if( mChangeObject          ) changeObjectNow();
321
    if( mSetupObject           ) setupObjectNow();
322
    if( mSolveObject           ) solveObjectNow();
323
    if( mScrambleObject        ) scrambleObjectNow();
324
    if( mAddRotation           ) addRotationNow();
325
    if( mInitializeObject      ) initializeObjectNow();
326
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
327
    if( mRemovePatternRotation ) removePatternRotationNow();
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// PUBLIC API
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  public boolean isTouchBlocked()
335
    {
336
    return mTouchBlocked;
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  public boolean isUINotBlocked()
342
    {
343
    return !mUIBlocked;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public void blockEverything(int place)
349
    {
350
    mUIBlocked   = true;
351
    mTouchBlocked= true;
352
    mBlockController.touchBlocked(place);
353
    mBlockController.uiBlocked(place);
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  public void blockTouch(int place)
359
    {
360
    mTouchBlocked= true;
361
    mBlockController.touchBlocked(place);
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  public void unblockEverything()
367
    {
368
    mUIBlocked   = false;
369
    mTouchBlocked= false;
370
    mBlockController.touchUnblocked();
371
    mBlockController.uiUnblocked();
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public void unblockTouch()
377
    {
378
    mTouchBlocked= false;
379
    mBlockController.touchUnblocked();
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  public void unblockUI()
385
    {
386
    mUIBlocked= false;
387
    mBlockController.uiUnblocked();
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
393
    {
394
    mAddRotation = true;
395

    
396
    mAddActionListener    = listener;
397
    mAddRotationAxis      = axis;
398
    mAddRotationRowBitmap = rowBitmap;
399
    mAddRotationAngle     = angle;
400
    mAddRotationDuration  = duration;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  public int getNumScrambles()
406
    {
407
    return mScrambleObjectNum;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
// this starts the SolveEffect
412

    
413
  public void solveObject()
414
    {
415
    if( !mUIBlocked )
416
      {
417
      mSolveObject = true;
418
      }
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422
// this only solves the object
423

    
424
  public void solve()
425
    {
426
    mSolve = true;
427
    }
428

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

    
431
  public void scrambleObject(int num)
432
    {
433
    if( !mUIBlocked )
434
      {
435
      mScrambleObject = true;
436
      mScrambleObjectNum = num;
437
      }
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  public void resetAllTextureMaps()
443
    {
444
    mResetAllTextureMaps = true;
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  public TwistyObject getObject()
450
    {
451
    return mNewObject;
452
    }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
  public TwistyObject getOldObject()
457
    {
458
    return null;
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  public void effectFinished(final long effectID)
464
    {
465
    if( effectID == mRotationFinishedID )
466
      {
467
      mRotationFinishedID = 0;
468
      removeRotation();
469
      }
470
    else if( effectID == mAddRotationID )
471
      {
472
      mAddRotationID = 0;
473
      mRemoveRotationID = effectID;
474
      removePatternRotation();
475
      }
476
    else
477
      {
478
      unblockEverything();  // buggy? I think we shouldn't do it if the effect is of type 'WIN'
479
      }
480
    }
481
  }
(3-3/7)