Project

General

Profile

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

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

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.objects.ObjectList;
28
import org.distorted.objects.TwistyObject;
29
import org.distorted.main.RubikPreRender.ActionFinishedListener;
30
import org.distorted.scores.RubikScores;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class TutorialPreRender implements EffectController
35
  {
36
  private ActionFinishedListener mAddActionListener;
37
  private TutorialSurfaceView mView;
38
  private boolean mFinishRotation, mRemoveRotation, mAddRotation,
39
                  mSetQuat, mChangeObject, mSetupObject, mSolveObject, mScrambleObject,
40
                  mInitializeObject, mResetAllTextureMaps, mRemovePatternRotation;
41
  private boolean mCanPlay;
42
  private boolean mIsSolved;
43
  private ObjectList mNextObject;
44
  private int mNextSize;
45
  private long mRotationFinishedID;
46
  private int mScreenWidth;
47
  private int[][] mNextMoves;
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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  TutorialPreRender(TutorialSurfaceView view)
58
    {
59
    mView = view;
60

    
61
    mFinishRotation = false;
62
    mRemoveRotation = false;
63
    mAddRotation    = false;
64
    mSetQuat        = false;
65
    mChangeObject   = false;
66
    mSetupObject    = false;
67
    mSolveObject    = false;
68
    mScrambleObject = false;
69

    
70
    mCanPlay        = true;
71
    mOldObject      = null;
72
    mNewObject      = null;
73

    
74
    mScreenWidth       = 0;
75
    mScrambleObjectNum = 0;
76

    
77
    mRemovePatternRotation= false;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private void createObjectNow(ObjectList object, int size, int[][] moves)
83
    {
84
    if( mOldObject!=null ) mOldObject.releaseResources();
85
    mOldObject = mNewObject;
86

    
87
    Context con = mView.getContext();
88
    Resources res = con.getResources();
89

    
90
    mNewObject = object.create(size, mView.getQuat(), moves, res, mScreenWidth);
91

    
92
    if( mNewObject!=null )
93
      {
94
      mNewObject.createTexture();
95
      mView.setMovement(object.getObjectMovementClass());
96

    
97
      if( mScreenWidth!=0 )
98
        {
99
        mNewObject.recomputeScaleFactor(mScreenWidth);
100
        }
101

    
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
      mCanPlay= true;
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

    
145
    if( solved && !mIsSolved )
146
      {
147
      doEffectNow( BaseEffect.Type.WIN );
148
      }
149
    else
150
      {
151
      mCanPlay = true;
152
      }
153

    
154
    mIsSolved = solved;
155
    }
156

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

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private void finishRotationNow()
181
    {
182
    mFinishRotation = false;
183
    mCanPlay        = false;
184
    mRotationFinishedID = mNewObject.finishRotationNow(this, mNearestAngle);
185

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  private void solveObjectNow()
227
    {
228
    mSolveObject = false;
229
    mCanPlay     = false;
230
    doEffectNow( BaseEffect.Type.SOLVE );
231
    }
232

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

    
235
  private void scrambleObjectNow()
236
    {
237
    mScrambleObject = false;
238
    mCanPlay        = false;
239
    mIsSolved       = false;
240
    RubikScores.getInstance().incrementNumPlays();
241
    doEffectNow( BaseEffect.Type.SCRAMBLE );
242
    }
243

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

    
246
  private void initializeObjectNow()
247
    {
248
    mInitializeObject = false;
249
    mNewObject.initializeObject(mNextMoves);
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(ObjectList object, int size)
295
    {
296
    if( size>0 )
297
      {
298
      mChangeObject = true;
299
      mNextObject = object;
300
      mNextSize   = size;
301
      }
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  void setQuatOnNextRender()
307
    {
308
    mSetQuat = true;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  void preRender()
314
    {
315
    if( mSetQuat               ) setQuatNow();
316
    if( mFinishRotation        ) finishRotationNow();
317
    if( mRemoveRotation        ) removeRotationNow();
318
    if( mChangeObject          ) changeObjectNow();
319
    if( mSetupObject           ) setupObjectNow();
320
    if( mSolveObject           ) solveObjectNow();
321
    if( mScrambleObject        ) scrambleObjectNow();
322
    if( mAddRotation           ) addRotationNow();
323
    if( mInitializeObject      ) initializeObjectNow();
324
    if( mResetAllTextureMaps   ) resetAllTextureMapsNow();
325
    if( mRemovePatternRotation ) removePatternRotationNow();
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
// PUBLIC API
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  public void addRotation(ActionFinishedListener listener, int axis, int rowBitmap, int angle, long duration)
333
    {
334
    mAddRotation = true;
335

    
336
    mAddActionListener    = listener;
337
    mAddRotationAxis      = axis;
338
    mAddRotationRowBitmap = rowBitmap;
339
    mAddRotationAngle     = angle;
340
    mAddRotationDuration  = duration;
341
    }
342

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

    
345
  public void initializeObject(int[][] moves)
346
    {
347
    mInitializeObject = true;
348
    mNextMoves = moves;
349
    }
350

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

    
353
  public int getNumScrambles()
354
    {
355
    return mScrambleObjectNum;
356
    }
357

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

    
360
  public void solveObject()
361
    {
362
    if( mCanPlay )
363
      {
364
      mSolveObject = true;
365
      }
366
    }
367

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

    
370
  public void scrambleObject(int num)
371
    {
372
    if( mCanPlay )
373
      {
374
      mScrambleObject = true;
375
      mScrambleObjectNum = num;
376
      }
377
    }
378

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

    
381
  public void resetAllTextureMaps()
382
    {
383
    mResetAllTextureMaps = true;
384
    }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  public TwistyObject getObject()
389
    {
390
    return mNewObject;
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  public TwistyObject getOldObject()
396
    {
397
    return null;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  public void effectFinished(final long effectID)
403
    {
404
    if( effectID == mRotationFinishedID )
405
      {
406
      mRotationFinishedID = 0;
407
      removeRotation();
408
      }
409
    else if( effectID == mAddRotationID )
410
      {
411
      mAddRotationID = 0;
412
      mRemoveRotationID = effectID;
413
      removePatternRotation();
414
      }
415
    else
416
      {
417
      mCanPlay   = true;
418
      }
419
    }
420
  }
(3-3/7)