Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ 318c0a7d

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
      mNewObject.createTexture();
100
      mView.setMovement(mNewObject.getMovement());
101
      mIsSolved = mNewObject.isSolved();
102
      }
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

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

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

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

    
147
    mIsSolved = solved;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

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

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270
//
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

    
281
    mScreenWidth  = width;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  void changeObject(ObjectType object)
295
    {
296
    mChangeObject = true;
297
    mNextObject = object;
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

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

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
  void preRender()
310
    {
311
    if( mSolve                 ) solveNow();
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
// this starts the SolveEffect
407

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
// this only solves the object
418

    
419
  public void solve()
420
    {
421
    mSolve = true;
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public void scrambleObject(int num)
427
    {
428
    if( !mUIBlocked )
429
      {
430
      mScrambleObject = true;
431
      mScrambleObjectNum = num;
432
      }
433
    }
434

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

    
437
  public TwistyObject getObject()
438
    {
439
    return mNewObject;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  public TwistyObject getOldObject()
445
    {
446
    return null;
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

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