1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 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.magic;
|
21
|
|
22
|
import android.content.SharedPreferences;
|
23
|
import android.opengl.GLSurfaceView;
|
24
|
import android.os.Bundle;
|
25
|
|
26
|
import org.distorted.dialog.RubikDialogNewRecord;
|
27
|
import org.distorted.dialog.RubikDialogSolved;
|
28
|
import org.distorted.effect.BaseEffect;
|
29
|
import org.distorted.library.effect.VertexEffectSink;
|
30
|
import org.distorted.library.main.DistortedLibrary;
|
31
|
import org.distorted.library.main.DistortedScreen;
|
32
|
import org.distorted.library.message.EffectListener;
|
33
|
import org.distorted.object.RubikObject;
|
34
|
import org.distorted.object.RubikObjectList;
|
35
|
import org.distorted.scores.RubikScores;
|
36
|
import org.distorted.uistate.RubikState;
|
37
|
import org.distorted.uistate.RubikStateSolving;
|
38
|
|
39
|
import javax.microedition.khronos.egl.EGLConfig;
|
40
|
import javax.microedition.khronos.opengles.GL10;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
|
45
|
{
|
46
|
public static final int NODE_FBO_SIZE = 600;
|
47
|
|
48
|
private RubikSurfaceView mView;
|
49
|
private DistortedScreen mScreen;
|
50
|
private RubikObjectList mNextObject;
|
51
|
private int mNextSize;
|
52
|
private int mScrambleObjectNum;
|
53
|
private long mRotationFinishedID;
|
54
|
private long[] mEffectID;
|
55
|
private boolean mFinishRotation, mRemoveRotation, mSetQuatCurrent, mSetQuatAccumulated;
|
56
|
private boolean mChangeObject, mSolveObject, mScrambleObject;
|
57
|
private boolean mCanRotate, mCanDrag, mCanUI;
|
58
|
private boolean mIsSolved;
|
59
|
private boolean mIsNewRecord;
|
60
|
private long mNewRecord;
|
61
|
private RubikObject mOldObject, mNewObject;
|
62
|
private int mScreenWidth, mScreenHeight;
|
63
|
private SharedPreferences mPreferences;
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
RubikRenderer(RubikSurfaceView v)
|
68
|
{
|
69
|
mView = v;
|
70
|
mScreen = new DistortedScreen();
|
71
|
|
72
|
mOldObject = null;
|
73
|
mNewObject = null;
|
74
|
|
75
|
mScreenWidth = mScreenHeight = 0;
|
76
|
mScrambleObjectNum = 0;
|
77
|
|
78
|
mFinishRotation = false;
|
79
|
mRemoveRotation = false;
|
80
|
mSetQuatCurrent = false;
|
81
|
mSetQuatAccumulated = false;
|
82
|
mChangeObject = false;
|
83
|
mSolveObject = false;
|
84
|
mScrambleObject = false;
|
85
|
|
86
|
mCanRotate = true;
|
87
|
mCanDrag = true;
|
88
|
mCanUI = true;
|
89
|
|
90
|
mEffectID = new long[BaseEffect.Type.LENGTH];
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
private void createObjectNow(RubikObjectList object, int size)
|
96
|
{
|
97
|
boolean firstTime = (mNewObject==null);
|
98
|
|
99
|
if( mOldObject!=null ) mOldObject.releaseResources();
|
100
|
mOldObject = mNewObject;
|
101
|
|
102
|
mNewObject = object.create(size, mView.getQuatCurrent(), mView.getQuatAccumulated());
|
103
|
mNewObject.createTexture();
|
104
|
mView.setMovement(object.getObjectMovementClass());
|
105
|
|
106
|
if( firstTime ) mNewObject.restorePreferences(mPreferences);
|
107
|
|
108
|
if( mScreenWidth!=0 )
|
109
|
{
|
110
|
mNewObject.recomputeScaleFactor(mScreenWidth, mScreenHeight);
|
111
|
}
|
112
|
|
113
|
mIsSolved = true;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
// do all 'adjustable' effects (SizeChange, Solve, Scramble)
|
118
|
|
119
|
private void doEffectNow(BaseEffect.Type type)
|
120
|
{
|
121
|
int index = type.ordinal();
|
122
|
|
123
|
try
|
124
|
{
|
125
|
mEffectID[index] = type.startEffect(this);
|
126
|
}
|
127
|
catch( Exception ex )
|
128
|
{
|
129
|
android.util.Log.e("renderer", "exception starting effect: "+ex.getMessage());
|
130
|
|
131
|
mCanUI = true;
|
132
|
mCanRotate = true;
|
133
|
mCanDrag = true;
|
134
|
}
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
void savePreferences(SharedPreferences.Editor editor)
|
140
|
{
|
141
|
if( mNewObject!=null )
|
142
|
{
|
143
|
mNewObject.savePreferences(editor);
|
144
|
}
|
145
|
}
|
146
|
|
147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148
|
|
149
|
void restorePreferences(SharedPreferences preferences)
|
150
|
{
|
151
|
mPreferences = preferences;
|
152
|
}
|
153
|
|
154
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155
|
// no this will not race with onDrawFrame
|
156
|
|
157
|
void finishRotation()
|
158
|
{
|
159
|
mFinishRotation = true;
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
boolean createObject(RubikObjectList object, int size)
|
165
|
{
|
166
|
if( mCanDrag && mCanRotate && (object!=mNextObject || mNextSize!=size) && size>0 )
|
167
|
{
|
168
|
mChangeObject = true;
|
169
|
mNextObject = object;
|
170
|
mNextSize = size;
|
171
|
return true;
|
172
|
}
|
173
|
|
174
|
return false;
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
void scrambleObject(int num)
|
180
|
{
|
181
|
if( mCanUI )
|
182
|
{
|
183
|
mScrambleObject = true;
|
184
|
mScrambleObjectNum = num;
|
185
|
}
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
void solveObject()
|
191
|
{
|
192
|
if( mCanUI )
|
193
|
{
|
194
|
mSolveObject = true;
|
195
|
}
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
boolean canRotate()
|
201
|
{
|
202
|
return mCanRotate;
|
203
|
}
|
204
|
|
205
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
boolean canDrag()
|
208
|
{
|
209
|
return mCanDrag;
|
210
|
}
|
211
|
|
212
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
213
|
|
214
|
void setQuatCurrentOnNextRender()
|
215
|
{
|
216
|
mSetQuatCurrent = true;
|
217
|
}
|
218
|
|
219
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220
|
|
221
|
void setQuatAccumulatedOnNextRender()
|
222
|
{
|
223
|
mSetQuatAccumulated = true;
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
// various things are done here delayed, 'after the next render' as not to be done mid-render and
|
228
|
// cause artifacts.
|
229
|
|
230
|
@Override
|
231
|
public void onDrawFrame(GL10 glUnused)
|
232
|
{
|
233
|
mScreen.render( System.currentTimeMillis() );
|
234
|
|
235
|
if( mSetQuatCurrent )
|
236
|
{
|
237
|
mSetQuatCurrent = false;
|
238
|
mView.setQuatCurrent();
|
239
|
}
|
240
|
|
241
|
if( mSetQuatAccumulated )
|
242
|
{
|
243
|
mSetQuatAccumulated = false;
|
244
|
mView.setQuatAccumulated();
|
245
|
}
|
246
|
|
247
|
if( mFinishRotation )
|
248
|
{
|
249
|
mFinishRotation = false;
|
250
|
mCanRotate = false;
|
251
|
mCanUI = false;
|
252
|
mRotationFinishedID = mNewObject.finishRotationNow(this);
|
253
|
|
254
|
if( mRotationFinishedID==0 ) // failed to add effect - should never happen
|
255
|
{
|
256
|
mCanRotate = true;
|
257
|
mCanUI = true;
|
258
|
}
|
259
|
}
|
260
|
|
261
|
if( mRemoveRotation )
|
262
|
{
|
263
|
mRemoveRotation=false;
|
264
|
mNewObject.removeRotationNow();
|
265
|
|
266
|
boolean solved = mNewObject.isSolved();
|
267
|
|
268
|
if( solved && !mIsSolved )
|
269
|
{
|
270
|
if( RubikState.getCurrentState()==RubikState.SOLV )
|
271
|
{
|
272
|
RubikStateSolving solving = (RubikStateSolving)RubikState.SOLV.getStateClass();
|
273
|
mNewRecord = solving.stopCounting((RubikActivity)mView.getContext());
|
274
|
|
275
|
if( mNewRecord< 0 )
|
276
|
{
|
277
|
mNewRecord = -mNewRecord;
|
278
|
mIsNewRecord = false;
|
279
|
}
|
280
|
else
|
281
|
{
|
282
|
mIsNewRecord = true;
|
283
|
}
|
284
|
}
|
285
|
|
286
|
mCanDrag = true;
|
287
|
mCanRotate = false;
|
288
|
mCanUI = false;
|
289
|
doEffectNow( BaseEffect.Type.WIN );
|
290
|
}
|
291
|
else
|
292
|
{
|
293
|
mCanRotate = true;
|
294
|
mCanUI = true;
|
295
|
}
|
296
|
|
297
|
mIsSolved = solved;
|
298
|
}
|
299
|
|
300
|
if( mChangeObject )
|
301
|
{
|
302
|
mChangeObject = false;
|
303
|
mCanDrag = false;
|
304
|
mCanRotate = false;
|
305
|
mCanUI = false;
|
306
|
createObjectNow(mNextObject, mNextSize);
|
307
|
doEffectNow( BaseEffect.Type.SIZECHANGE );
|
308
|
}
|
309
|
|
310
|
if( mSolveObject )
|
311
|
{
|
312
|
mSolveObject = false;
|
313
|
mCanDrag = false;
|
314
|
mCanRotate = false;
|
315
|
mCanUI = false;
|
316
|
doEffectNow( BaseEffect.Type.SOLVE );
|
317
|
}
|
318
|
|
319
|
if( mScrambleObject )
|
320
|
{
|
321
|
mScrambleObject = false;
|
322
|
mCanDrag = false;
|
323
|
mCanRotate = false;
|
324
|
mCanUI = false;
|
325
|
mIsSolved = false;
|
326
|
RubikScores.getInstance().incrementNumPlays();
|
327
|
doEffectNow( BaseEffect.Type.SCRAMBLE );
|
328
|
}
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
|
333
|
@Override
|
334
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
335
|
{
|
336
|
if( mNewObject!=null ) mNewObject.createTexture();
|
337
|
|
338
|
mScreen.resize(width, height);
|
339
|
mView.setScreenSize(width,height);
|
340
|
|
341
|
if( mNewObject!=null )
|
342
|
{
|
343
|
mNewObject.recomputeScaleFactor(width,height);
|
344
|
}
|
345
|
|
346
|
mScreenHeight = height;
|
347
|
mScreenWidth = width;
|
348
|
}
|
349
|
|
350
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
351
|
|
352
|
@Override
|
353
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
354
|
{
|
355
|
VertexEffectSink.enable();
|
356
|
BaseEffect.Type.enableEffects();
|
357
|
|
358
|
try
|
359
|
{
|
360
|
DistortedLibrary.onCreate(mView.getContext());
|
361
|
}
|
362
|
catch(Exception ex)
|
363
|
{
|
364
|
android.util.Log.e("Rubik", ex.getMessage() );
|
365
|
}
|
366
|
}
|
367
|
|
368
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
369
|
// PUBLIC API
|
370
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
371
|
|
372
|
public void effectFinished(final long effectID)
|
373
|
{
|
374
|
if( effectID == mRotationFinishedID )
|
375
|
{
|
376
|
mRemoveRotation = true;
|
377
|
}
|
378
|
else
|
379
|
{
|
380
|
for(int i=0; i<BaseEffect.Type.LENGTH; i++)
|
381
|
{
|
382
|
if( effectID == mEffectID[i] )
|
383
|
{
|
384
|
mCanRotate = true;
|
385
|
mCanDrag = true;
|
386
|
mCanUI = true;
|
387
|
|
388
|
if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
|
389
|
{
|
390
|
final RubikActivity act = (RubikActivity)mView.getContext();
|
391
|
|
392
|
act.runOnUiThread(new Runnable()
|
393
|
{
|
394
|
@Override
|
395
|
public void run()
|
396
|
{
|
397
|
RubikState.switchState( act, RubikState.SOLV);
|
398
|
}
|
399
|
});
|
400
|
}
|
401
|
|
402
|
if( i==BaseEffect.Type.WIN.ordinal() )
|
403
|
{
|
404
|
if( RubikState.getCurrentState()==RubikState.SOLV )
|
405
|
{
|
406
|
final RubikActivity act = (RubikActivity)mView.getContext();
|
407
|
Bundle bundle = new Bundle();
|
408
|
bundle.putLong("time", mNewRecord );
|
409
|
|
410
|
if( mIsNewRecord )
|
411
|
{
|
412
|
RubikDialogNewRecord dialog = new RubikDialogNewRecord();
|
413
|
dialog.setArguments(bundle);
|
414
|
dialog.show( act.getSupportFragmentManager(), null);
|
415
|
}
|
416
|
else
|
417
|
{
|
418
|
RubikDialogSolved dialog = new RubikDialogSolved();
|
419
|
dialog.setArguments(bundle);
|
420
|
dialog.show( act.getSupportFragmentManager(), null);
|
421
|
}
|
422
|
}
|
423
|
}
|
424
|
|
425
|
break;
|
426
|
}
|
427
|
}
|
428
|
}
|
429
|
}
|
430
|
|
431
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
432
|
|
433
|
public RubikObject getObject()
|
434
|
{
|
435
|
return mNewObject;
|
436
|
}
|
437
|
|
438
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439
|
|
440
|
public RubikObject getOldObject()
|
441
|
{
|
442
|
return mOldObject;
|
443
|
}
|
444
|
|
445
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
446
|
|
447
|
public DistortedScreen getScreen()
|
448
|
{
|
449
|
return mScreen;
|
450
|
}
|
451
|
|
452
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
453
|
|
454
|
public int getNumScrambles()
|
455
|
{
|
456
|
return mScrambleObjectNum;
|
457
|
}
|
458
|
}
|