Project

General

Profile

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

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

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

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

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

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

    
146
    mIsSolved = solved;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

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

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

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

    
253
  private void setQuatNow()
254
    {
255
    mSetQuat = false;
256
    mView.setQuat();
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
//
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  void setScreenSize(int width)
264
    {
265
    if( mNewObject!=null ) mNewObject.recomputeScaleFactor(width);
266
    mScreenWidth = width;
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  void finishRotation(int nearestAngle)
272
    {
273
    mNearestAngle   = nearestAngle;
274
    mFinishRotation = true;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  void changeObject(ObjectType object)
280
    {
281
    mChangeObject = true;
282
    mNextObject = object;
283
    }
284

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

    
287
  void setQuatOnNextRender()
288
    {
289
    mSetQuat = true;
290
    }
291

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

    
294
  void preRender()
295
    {
296
    if( mSolve                 ) solveNow();
297
    if( mSetQuat               ) setQuatNow();
298
    if( mFinishRotation        ) finishRotationNow();
299
    if( mRemoveRotation        ) removeRotationNow();
300
    if( mChangeObject          ) changeObjectNow();
301
    if( mSetupObject           ) setupObjectNow();
302
    if( mSolveObject           ) solveObjectNow();
303
    if( mScrambleObject        ) scrambleObjectNow();
304
    if( mAddRotation           ) addRotationNow();
305
    if( mInitializeObject      ) initializeObjectNow();
306
    if( mRemovePatternRotation ) removePatternRotationNow();
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310
// PUBLIC API
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public boolean isTouchBlocked()
314
    {
315
    return mTouchBlocked;
316
    }
317

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

    
320
  public boolean isUINotBlocked()
321
    {
322
    return !mUIBlocked;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public void blockEverything(int place)
328
    {
329
    mUIBlocked   = true;
330
    mTouchBlocked= true;
331
    mBlockController.touchBlocked(place);
332
    mBlockController.uiBlocked(place);
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public void blockTouch(int place)
338
    {
339
    mTouchBlocked= true;
340
    mBlockController.touchBlocked(place);
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  public void unblockEverything()
346
    {
347
    mUIBlocked   = false;
348
    mTouchBlocked= false;
349
    mBlockController.touchUnblocked();
350
    mBlockController.uiUnblocked();
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  public void unblockTouch()
356
    {
357
    mTouchBlocked= false;
358
    mBlockController.touchUnblocked();
359
    }
360

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

    
363
  public void unblockUI()
364
    {
365
    mUIBlocked= false;
366
    mBlockController.uiUnblocked();
367
    }
368

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

    
371
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
372
    {
373
    mAddRotation = true;
374

    
375
    mAddActionListener    = listener;
376
    mAddRotationAxis      = axis;
377
    mAddRotationRowBitmap = rowBitmap;
378
    mAddRotationAngle     = angle;
379
    mAddRotationDuration  = duration;
380
    }
381

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

    
384
  public int getNumScrambles()
385
    {
386
    return mScrambleObjectNum;
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
// this starts the SolveEffect
391

    
392
  public void solveObject()
393
    {
394
    if( !mUIBlocked )
395
      {
396
      mSolveObject = true;
397
      }
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
// this only solves the object
402

    
403
  public void solve()
404
    {
405
    mSolve = true;
406
    }
407

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

    
410
  public void scrambleObject(int num)
411
    {
412
    if( !mUIBlocked )
413
      {
414
      mScrambleObject = true;
415
      mScrambleObjectNum = num;
416
      }
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public TwistyObject getObject()
422
    {
423
    return mNewObject;
424
    }
425

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

    
428
  public TwistyObject getOldObject()
429
    {
430
    return null;
431
    }
432

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

    
435
  public void effectFinished(final long effectID)
436
    {
437
    if( effectID == mRotationFinishedID )
438
      {
439
      mRotationFinishedID = 0;
440
      removeRotation();
441
      }
442
    else if( effectID == mAddRotationID )
443
      {
444
      mAddRotationID = 0;
445
      mRemoveRotationID = effectID;
446
      removePatternRotation();
447
      }
448
    else
449
      {
450
      unblockEverything();  // buggy? I think we shouldn't do it if the effect is of type 'WIN'
451
      }
452
    }
453
  }
(3-3/7)