Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ 4c49986e

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.effects.BaseEffect;
26
import org.distorted.effects.EffectController;
27
import org.distorted.helpers.BlockController;
28
import org.distorted.helpers.MovesFinished;
29
import org.distorted.helpers.TwistyPreRender;
30
import org.distorted.objects.ObjectList;
31
import org.distorted.objects.TwistyObject;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class TutorialPreRender implements EffectController, TwistyPreRender
36
  {
37
  private MovesFinished mAddActionListener;
38
  private final TutorialSurfaceView mView;
39
  private boolean mFinishRotation, mRemoveRotation, mAddRotation,
40
                  mSetQuat, mChangeObject, mSetupObject, mSolveObject, mScrambleObject,
41
                  mInitializeObject, mResetAllTextureMaps, mRemovePatternRotation;
42
  private boolean mUIBlocked, mTouchBlocked;
43
  private boolean mIsSolved;
44
  private ObjectList mNextObject;
45
  private int mNextSize;
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
    mScrambleObject = false;
70

    
71
    mOldObject      = null;
72
    mNewObject      = null;
73

    
74
    mScreenWidth       = 0;
75
    mScrambleObjectNum = 0;
76

    
77
    mRemovePatternRotation= false;
78

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

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

    
96
    if( mNewObject!=null )
97
      {
98
      mNewObject.createTexture();
99
      mView.setMovement(object.getObjectMovementClass());
100

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

    
106
      mIsSolved = mNewObject.isSolved();
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void removePatternRotation()
128
    {
129
    mRemovePatternRotation = true;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  private void removeRotationNow()
144
    {
145
    mRemoveRotation=false;
146
    mNewObject.removeRotationNow();
147

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

    
152
    mIsSolved = solved;
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private void removeRotation()
158
    {
159
    mRemoveRotation = true;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private void changeObjectNow()
193
    {
194
    mChangeObject = false;
195

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  private void setupObjectNow()
207
    {
208
    mSetupObject = false;
209

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  private void resetAllTextureMapsNow()
252
    {
253
    mResetAllTextureMaps = false;
254

    
255
    if( mNewObject!=null ) mNewObject.resetAllTextureMaps();
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

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

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
//
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

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

    
278
    mScreenWidth  = width;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  void finishRotation(int nearestAngle)
284
    {
285
    mNearestAngle   = nearestAngle;
286
    mFinishRotation = true;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  void changeObject(ObjectList object, int size)
292
    {
293
    if( size>0 )
294
      {
295
      mChangeObject = true;
296
      mNextObject = object;
297
      mNextSize   = size;
298
      }
299
    }
300

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

    
303
  void setQuatOnNextRender()
304
    {
305
    mSetQuat = true;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  void preRender()
311
    {
312
    if( mSetQuat               ) setQuatNow();
313
    if( mFinishRotation        ) finishRotationNow();
314
    if( mRemoveRotation        ) removeRotationNow();
315
    if( mChangeObject          ) changeObjectNow();
316
    if( mSetupObject           ) setupObjectNow();
317
    if( mSolveObject           ) solveObjectNow();
318
    if( mScrambleObject        ) scrambleObjectNow();
319
    if( mAddRotation           ) addRotationNow();
320
    if( mInitializeObject      ) initializeObjectNow();
321
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
322
    if( mRemovePatternRotation ) removePatternRotationNow();
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// PUBLIC API
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  public boolean isTouchBlocked()
330
    {
331
    return mTouchBlocked;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  public boolean isUINotBlocked()
337
    {
338
    return !mUIBlocked;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  public void blockEverything(int place)
344
    {
345
    mUIBlocked   = true;
346
    mTouchBlocked= true;
347
    mBlockController.touchBlocked(place);
348
    mBlockController.uiBlocked(place);
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  public void blockTouch(int place)
354
    {
355
    mTouchBlocked= true;
356
    mBlockController.touchBlocked(place);
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  public void unblockEverything()
362
    {
363
    mUIBlocked   = false;
364
    mTouchBlocked= false;
365
    mBlockController.touchUnblocked();
366
    mBlockController.uiUnblocked();
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  public void unblockTouch()
372
    {
373
    mTouchBlocked= false;
374
    mBlockController.touchUnblocked();
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  public void unblockUI()
380
    {
381
    mUIBlocked= false;
382
    mBlockController.uiUnblocked();
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
388
    {
389
    mAddRotation = true;
390

    
391
    mAddActionListener    = listener;
392
    mAddRotationAxis      = axis;
393
    mAddRotationRowBitmap = rowBitmap;
394
    mAddRotationAngle     = angle;
395
    mAddRotationDuration  = duration;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public int getNumScrambles()
401
    {
402
    return mScrambleObjectNum;
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  public void solveObject()
408
    {
409
    if( !mUIBlocked )
410
      {
411
      mSolveObject = true;
412
      }
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public void scrambleObject(int num)
418
    {
419
    if( !mUIBlocked )
420
      {
421
      mScrambleObject = true;
422
      mScrambleObjectNum = num;
423
      }
424
    }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

    
428
  public void resetAllTextureMaps()
429
    {
430
    mResetAllTextureMaps = true;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public TwistyObject getObject()
436
    {
437
    return mNewObject;
438
    }
439

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

    
442
  public TwistyObject getOldObject()
443
    {
444
    return null;
445
    }
446

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

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