Project

General

Profile

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

magiccube / src / main / java / org / distorted / tutorials / TutorialPreRender.java @ 1cd323dd

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.helpers.ObjectStateActioner;
26
import org.distorted.objectlib.main.ObjectType;
27
import org.distorted.objectlib.main.TwistyObject;
28
import org.distorted.objectlib.helpers.BlockController;
29
import org.distorted.objectlib.helpers.MovesFinished;
30
import org.distorted.objectlib.helpers.TwistyPreRender;
31
import org.distorted.objectlib.effects.BaseEffect;
32
import org.distorted.objectlib.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
  private final ObjectStateActioner mActioner;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  TutorialPreRender(TutorialSurfaceView view, ObjectStateActioner actioner)
60
    {
61
    mView = view;
62
    mActioner = actioner;
63

    
64
    mFinishRotation = false;
65
    mRemoveRotation = false;
66
    mAddRotation    = false;
67
    mSetQuat        = false;
68
    mChangeObject   = false;
69
    mSetupObject    = false;
70
    mSolveObject    = false;
71
    mSolve          = false;
72
    mScrambleObject = false;
73

    
74
    mOldObject      = null;
75
    mNewObject      = null;
76

    
77
    mScreenWidth       = 0;
78
    mScrambleObjectNum = 0;
79

    
80
    mRemovePatternRotation= false;
81

    
82
    TutorialActivity act = (TutorialActivity)mView.getContext();
83
    mBlockController = new BlockController(act);
84
    unblockEverything();
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  private void createObjectNow(ObjectType object)
90
    {
91
    if( mOldObject!=null ) mOldObject.releaseResources();
92
    mOldObject = mNewObject;
93

    
94
    Context con = mView.getContext();
95
    Resources res = con.getResources();
96

    
97
    mNewObject = object.create(mView.getQuat(), null, res, mScreenWidth);
98

    
99
    if( mNewObject!=null )
100
      {
101
      mView.setMovement(mNewObject.getMovement());
102
      mIsSolved = mNewObject.isSolved();
103
      }
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

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

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

    
148
    mIsSolved = solved;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

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

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
//
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  void setScreenSize(int width)
266
    {
267
    if( mNewObject!=null )
268
      {
269
      mNewObject.createTexture();
270
      mNewObject.recomputeScaleFactor(width);
271
      }
272

    
273
    mScreenWidth = width;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

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

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

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

    
294
  void setQuatOnNextRender()
295
    {
296
    mSetQuat = true;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317
// PUBLIC API
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public boolean isTouchBlocked()
321
    {
322
    return mTouchBlocked;
323
    }
324

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

    
327
  public boolean isUINotBlocked()
328
    {
329
    return !mUIBlocked;
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

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

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

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

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

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

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

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

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

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  public int getNumScrambles()
392
    {
393
    return mScrambleObjectNum;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397
// this starts the SolveEffect
398

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

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
// this only solves the object
409

    
410
  public void solve()
411
    {
412
    mSolve = true;
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 TwistyObject getObject()
429
    {
430
    return mNewObject;
431
    }
432

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

    
435
  public TwistyObject getOldObject()
436
    {
437
    return null;
438
    }
439

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

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