Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ 771f6dfa

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, mSolve;
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
    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(ObjectList object, int size)
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(size, mView.getQuat(), null, res, mScreenWidth);
96

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

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

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

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

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

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

    
153
    mIsSolved = solved;
154
    }
155

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

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

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

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

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

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

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

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
//
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

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

    
287
    mScreenWidth  = width;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

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

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336
// PUBLIC API
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

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

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

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

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

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

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

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

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

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

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

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

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

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

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

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

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

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

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416
// this starts the SolveEffect
417

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

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

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

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

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

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

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

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

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

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

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