Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ 88a3e972

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
import org.distorted.objectlib.helpers.BlockController;
28
import org.distorted.objectlib.helpers.MovesFinished;
29
import org.distorted.objectlib.helpers.TwistyPreRender;
30

    
31
import org.distorted.effects.BaseEffect;
32
import org.distorted.effects.EffectController;
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 )
266
      {
267
      mNewObject.createTexture();
268
      mNewObject.recomputeScaleFactor(width);
269
      }
270

    
271
    mScreenWidth = width;
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  void finishRotation(int nearestAngle)
277
    {
278
    mNearestAngle   = nearestAngle;
279
    mFinishRotation = true;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  void changeObject(ObjectType object)
285
    {
286
    mChangeObject = true;
287
    mNextObject = object;
288
    }
289

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

    
292
  void setQuatOnNextRender()
293
    {
294
    mSetQuat = true;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

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

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
// PUBLIC API
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  public boolean isTouchBlocked()
319
    {
320
    return mTouchBlocked;
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  public boolean isUINotBlocked()
326
    {
327
    return !mUIBlocked;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  public void blockEverything(int place)
333
    {
334
    mUIBlocked   = true;
335
    mTouchBlocked= true;
336
    mBlockController.touchBlocked(place);
337
    mBlockController.uiBlocked(place);
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

    
342
  public void blockTouch(int place)
343
    {
344
    mTouchBlocked= true;
345
    mBlockController.touchBlocked(place);
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
  public void unblockEverything()
351
    {
352
    mUIBlocked   = false;
353
    mTouchBlocked= false;
354
    mBlockController.touchUnblocked();
355
    mBlockController.uiUnblocked();
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
  public void unblockTouch()
361
    {
362
    mTouchBlocked= false;
363
    mBlockController.touchUnblocked();
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  public void unblockUI()
369
    {
370
    mUIBlocked= false;
371
    mBlockController.uiUnblocked();
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration)
377
    {
378
    mAddRotation = true;
379

    
380
    mAddActionListener    = listener;
381
    mAddRotationAxis      = axis;
382
    mAddRotationRowBitmap = rowBitmap;
383
    mAddRotationAngle     = angle;
384
    mAddRotationDuration  = duration;
385
    }
386

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

    
389
  public int getNumScrambles()
390
    {
391
    return mScrambleObjectNum;
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// this starts the SolveEffect
396

    
397
  public void solveObject()
398
    {
399
    if( !mUIBlocked )
400
      {
401
      mSolveObject = true;
402
      }
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
// this only solves the object
407

    
408
  public void solve()
409
    {
410
    mSolve = true;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

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

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

    
426
  public TwistyObject getObject()
427
    {
428
    return mNewObject;
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  public TwistyObject getOldObject()
434
    {
435
    return null;
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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