Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ 3f7a4363

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

    
103
      if( mScreenWidth!=0 )
104
        {
105
        mNewObject.recomputeScaleFactor(mScreenWidth);
106
        }
107

    
108
      mIsSolved = mNewObject.isSolved();
109
      }
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  private void doEffectNow(BaseEffect.Type type)
115
    {
116
    try
117
      {
118
      type.startEffect(mView.getRenderer().getScreen(),this);
119
      }
120
    catch( Exception ex )
121
      {
122
      android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
123
      unblockEverything();
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  private void removePatternRotation()
130
    {
131
    mRemovePatternRotation = true;
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  private void removePatternRotationNow()
137
    {
138
    mRemovePatternRotation=false;
139
    mNewObject.removeRotationNow();
140
    mAddActionListener.onActionFinished(mRemoveRotationID);
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private void removeRotationNow()
146
    {
147
    mRemoveRotation=false;
148
    mNewObject.removeRotationNow();
149

    
150
    boolean solved = mNewObject.isSolved();
151
    unblockEverything();
152
    if( solved && !mIsSolved ) doEffectNow( BaseEffect.Type.WIN );
153

    
154
    mIsSolved = solved;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void removeRotation()
160
    {
161
    mRemoveRotation = true;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void addRotationNow()
167
    {
168
    mAddRotation = false;
169
    mAddRotationID = mNewObject.addNewRotation( mAddRotationAxis, mAddRotationRowBitmap,
170
                                                mAddRotationAngle, mAddRotationDuration, this);
171

    
172
    if( mAddRotationID==0 ) // failed to add effect - should never happen
173
      {
174
      unblockEverything();
175
      }
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private void finishRotationNow()
181
    {
182
    mFinishRotation = false;
183
    blockEverything(BlockController.TUTORIAL_PLACE_0);
184
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
185

    
186
    if( mRotationFinishedID==0 ) // failed to add effect - should never happen
187
      {
188
      unblockEverything();
189
      }
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private void changeObjectNow()
195
    {
196
    mChangeObject = false;
197

    
198
    if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject || mNewObject.getNumLayers()!=mNextSize)
199
      {
200
      blockEverything(BlockController.TUTORIAL_PLACE_1);
201
      createObjectNow(mNextObject, mNextSize);
202
      doEffectNow( BaseEffect.Type.SIZECHANGE );
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  private void setupObjectNow()
209
    {
210
    mSetupObject = false;
211

    
212
    if ( mNewObject==null || mNewObject.getObjectList()!=mNextObject || mNewObject.getNumLayers()!=mNextSize)
213
      {
214
      blockEverything(BlockController.TUTORIAL_PLACE_2);
215
      createObjectNow(mNextObject, mNextSize);
216
      doEffectNow( BaseEffect.Type.SIZECHANGE );
217
      }
218
    else
219
      {
220
      mNewObject.initializeObject(null);
221
      }
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  private void scrambleObjectNow()
227
    {
228
    mScrambleObject = false;
229
    mIsSolved       = false;
230
    blockEverything(BlockController.TUTORIAL_PLACE_3);
231
    doEffectNow( BaseEffect.Type.SCRAMBLE );
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  private void solveObjectNow()
237
    {
238
    mSolveObject = false;
239
    blockEverything(BlockController.TUTORIAL_PLACE_4);
240
    doEffectNow( BaseEffect.Type.SOLVE );
241
    }
242

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

    
245
  private void solveNow()
246
    {
247
    mSolve = false;
248
    mNewObject.solve();
249
    }
250

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

    
253
  private void initializeObjectNow()
254
    {
255
    mInitializeObject = false;
256
    mNewObject.initializeObject(null);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  private void resetAllTextureMapsNow()
262
    {
263
    mResetAllTextureMaps = false;
264

    
265
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private void setQuatNow()
271
    {
272
    mSetQuat = false;
273
    mView.setQuat();
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
//
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  void setScreenSize(int width)
281
    {
282
    if( mNewObject!=null )
283
      {
284
      mNewObject.createTexture();
285
      mNewObject.recomputeScaleFactor(width);
286
      }
287

    
288
    mScreenWidth  = width;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  void finishRotation(int nearestAngle)
294
    {
295
    mNearestAngle   = nearestAngle;
296
    mFinishRotation = true;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  void changeObject(ObjectList object, int size)
302
    {
303
    if( size>0 )
304
      {
305
      mChangeObject = true;
306
      mNextObject = object;
307
      mNextSize   = size;
308
      }
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  void setQuatOnNextRender()
314
    {
315
    mSetQuat = true;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  void preRender()
321
    {
322
    if( mSolve                 ) solveNow();
323
    if( mSetQuat               ) setQuatNow();
324
    if( mFinishRotation        ) finishRotationNow();
325
    if( mRemoveRotation        ) removeRotationNow();
326
    if( mChangeObject          ) changeObjectNow();
327
    if( mSetupObject           ) setupObjectNow();
328
    if( mSolveObject           ) solveObjectNow();
329
    if( mScrambleObject        ) scrambleObjectNow();
330
    if( mAddRotation           ) addRotationNow();
331
    if( mInitializeObject      ) initializeObjectNow();
332
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
333
    if( mRemovePatternRotation ) removePatternRotationNow();
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
// PUBLIC API
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  public boolean isTouchBlocked()
341
    {
342
    return mTouchBlocked;
343
    }
344

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

    
347
  public boolean isUINotBlocked()
348
    {
349
    return !mUIBlocked;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  public void blockEverything(int place)
355
    {
356
    mUIBlocked   = true;
357
    mTouchBlocked= true;
358
    mBlockController.touchBlocked(place);
359
    mBlockController.uiBlocked(place);
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  public void blockTouch(int place)
365
    {
366
    mTouchBlocked= true;
367
    mBlockController.touchBlocked(place);
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
  public void unblockEverything()
373
    {
374
    mUIBlocked   = false;
375
    mTouchBlocked= false;
376
    mBlockController.touchUnblocked();
377
    mBlockController.uiUnblocked();
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  public void unblockTouch()
383
    {
384
    mTouchBlocked= false;
385
    mBlockController.touchUnblocked();
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
  public void unblockUI()
391
    {
392
    mUIBlocked= false;
393
    mBlockController.uiUnblocked();
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
399
    {
400
    mAddRotation = true;
401

    
402
    mAddActionListener    = listener;
403
    mAddRotationAxis      = axis;
404
    mAddRotationRowBitmap = rowBitmap;
405
    mAddRotationAngle     = angle;
406
    mAddRotationDuration  = duration;
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public int getNumScrambles()
412
    {
413
    return mScrambleObjectNum;
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
// this starts the SolveEffect
418

    
419
  public void solveObject()
420
    {
421
    if( !mUIBlocked )
422
      {
423
      mSolveObject = true;
424
      }
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428
// this only solves the object
429

    
430
  public void solve()
431
    {
432
    mSolve = true;
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  public void scrambleObject(int num)
438
    {
439
    if( !mUIBlocked )
440
      {
441
      mScrambleObject = true;
442
      mScrambleObjectNum = num;
443
      }
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  public void resetAllTextureMaps()
449
    {
450
    mResetAllTextureMaps = true;
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  public TwistyObject getObject()
456
    {
457
    return mNewObject;
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  public TwistyObject getOldObject()
463
    {
464
    return null;
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

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