1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted 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
|
// Distorted 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 Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.effect;
|
21
|
|
22
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
|
24
|
import org.distorted.library.effect.Effect;
|
25
|
import org.distorted.library.main.DistortedEffects;
|
26
|
import org.distorted.library.main.DistortedScreen;
|
27
|
import org.distorted.library.message.EffectListener;
|
28
|
import org.distorted.magic.RubikCube;
|
29
|
|
30
|
import java.lang.reflect.Method;
|
31
|
|
32
|
public abstract class SizeChangeEffect implements EffectListener
|
33
|
{
|
34
|
public enum Type
|
35
|
{
|
36
|
NONE (SizeChangeEffectNone.class ),
|
37
|
TRANSPARENCY (SizeChangeEffectTransparency.class),
|
38
|
MOVE (SizeChangeEffectMove.class ),
|
39
|
ROUND (SizeChangeEffectRound.class ),
|
40
|
SCALE (SizeChangeEffectScale.class ),
|
41
|
;
|
42
|
|
43
|
final Class<? extends SizeChangeEffect> effect;
|
44
|
|
45
|
Type(Class<? extends SizeChangeEffect> effect)
|
46
|
{
|
47
|
this.effect= effect;
|
48
|
}
|
49
|
}
|
50
|
|
51
|
private static int NUM_EFFECTS = Type.values().length;
|
52
|
private static final int NUM_PHASES = 2;
|
53
|
private static final int FAKE_EFFECT_ID = -1;
|
54
|
private static final Type[] types;
|
55
|
|
56
|
static
|
57
|
{
|
58
|
int i=0;
|
59
|
types = new Type[NUM_EFFECTS];
|
60
|
|
61
|
for(Type type: Type.values())
|
62
|
{
|
63
|
types[i++] = type;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
private EffectListener mListener;
|
68
|
private int mDuration;
|
69
|
private int[] mEffectReturned;
|
70
|
private int[] mCubeEffectNumber, mNodeEffectNumber;
|
71
|
private int[] mEffectFinished;
|
72
|
private boolean[] mPhaseActive;
|
73
|
private RubikCube[] mCube;
|
74
|
|
75
|
DistortedScreen mScreen;
|
76
|
Effect[][] mCubeEffects;
|
77
|
int[][] mCubeEffectPosition;
|
78
|
Effect[][] mNodeEffects;
|
79
|
int[][] mNodeEffectPosition;
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
SizeChangeEffect()
|
84
|
{
|
85
|
mPhaseActive = new boolean[NUM_PHASES];
|
86
|
mEffectReturned = new int[NUM_PHASES];
|
87
|
mCubeEffectNumber = new int[NUM_PHASES];
|
88
|
mNodeEffectNumber = new int[NUM_PHASES];
|
89
|
mEffectFinished = new int[NUM_PHASES];
|
90
|
mCubeEffectPosition = new int[NUM_PHASES][];
|
91
|
mNodeEffectPosition = new int[NUM_PHASES][];
|
92
|
mCubeEffects = new Effect[NUM_PHASES][];
|
93
|
mNodeEffects = new Effect[NUM_PHASES][];
|
94
|
mCube = new RubikCube[NUM_PHASES];
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public static String[] getNames()
|
100
|
{
|
101
|
String[] names = new String[NUM_EFFECTS];
|
102
|
|
103
|
for( int i=0; i<NUM_EFFECTS; i++)
|
104
|
{
|
105
|
names[i] = types[i].name();
|
106
|
}
|
107
|
|
108
|
return names;
|
109
|
}
|
110
|
|
111
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112
|
|
113
|
public static SizeChangeEffect create(int ordinal) throws InstantiationException, IllegalAccessException
|
114
|
{
|
115
|
return types[ordinal].effect.newInstance();
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
abstract int createEffectsPhase0(int duration);
|
121
|
abstract int createEffectsPhase1(int duration);
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void effectFinished(final long effectID)
|
126
|
{
|
127
|
if( mPhaseActive[0] ) effectFinishedPhase(effectID,0);
|
128
|
if( mPhaseActive[1] ) effectFinishedPhase(effectID,1);
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
|
133
|
private void effectFinishedPhase(final long effectID, int phase)
|
134
|
{
|
135
|
for(int i=0; i<mCubeEffectNumber[phase]; i++)
|
136
|
{
|
137
|
long id = mCubeEffects[phase][i].getID();
|
138
|
|
139
|
if( effectID == id )
|
140
|
{
|
141
|
effectReturned(phase);
|
142
|
mCube[phase].remove(id);
|
143
|
return;
|
144
|
}
|
145
|
}
|
146
|
for(int i=0; i<mNodeEffectNumber[phase]; i++)
|
147
|
{
|
148
|
long id = mNodeEffects[phase][i].getID();
|
149
|
|
150
|
if( effectID == id )
|
151
|
{
|
152
|
effectReturned(phase);
|
153
|
mCube[phase].getEffects().abortById(id);
|
154
|
return;
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160
|
|
161
|
private void effectReturned(int phase)
|
162
|
{
|
163
|
mEffectReturned[phase]++;
|
164
|
|
165
|
if( mEffectReturned[phase] == mEffectFinished[phase] )
|
166
|
{
|
167
|
switch(phase)
|
168
|
{
|
169
|
case 0: mPhaseActive[1] = true;
|
170
|
mEffectFinished[1] = createEffectsPhase1(mDuration);
|
171
|
assignEffects(1);
|
172
|
mScreen.attach(mCube[1]);
|
173
|
break;
|
174
|
case 1: mListener.effectFinished(FAKE_EFFECT_ID);
|
175
|
break;
|
176
|
}
|
177
|
}
|
178
|
if( mEffectReturned[phase] == mCubeEffectNumber[phase]+mNodeEffectNumber[phase] )
|
179
|
{
|
180
|
switch(phase)
|
181
|
{
|
182
|
case 0: mPhaseActive[0] = false;
|
183
|
mScreen.detach(mCube[0]);
|
184
|
break;
|
185
|
case 1: mPhaseActive[1] = false;
|
186
|
break;
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
public long start(int duration, DistortedScreen screen, RubikCube oldcube, RubikCube newcube, EffectListener listener)
|
194
|
{
|
195
|
mScreen = screen;
|
196
|
mCube[0] = oldcube;
|
197
|
mCube[1] = newcube;
|
198
|
mListener = listener;
|
199
|
mDuration = duration;
|
200
|
|
201
|
if( oldcube!=null )
|
202
|
{
|
203
|
mPhaseActive[0] = true;
|
204
|
mEffectFinished[0] = createEffectsPhase0(mDuration);
|
205
|
assignEffects(0);
|
206
|
}
|
207
|
else
|
208
|
{
|
209
|
mPhaseActive[1] = true;
|
210
|
mEffectFinished[1] = createEffectsPhase1(mDuration);
|
211
|
assignEffects(1);
|
212
|
mScreen.attach(mCube[1]);
|
213
|
}
|
214
|
|
215
|
return FAKE_EFFECT_ID;
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
private void assignEffects(int phase)
|
221
|
{
|
222
|
mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
|
223
|
mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
|
224
|
|
225
|
if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
|
226
|
{
|
227
|
throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
|
228
|
}
|
229
|
|
230
|
for(int i=0; i<mCubeEffectNumber[phase]; i++)
|
231
|
{
|
232
|
mCube[phase].apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
|
233
|
mCubeEffects[phase][i].notifyWhenFinished(this);
|
234
|
}
|
235
|
|
236
|
DistortedEffects nodeEffects = mCube[phase].getEffects();
|
237
|
|
238
|
for(int i=0; i<mNodeEffectNumber[phase]; i++)
|
239
|
{
|
240
|
nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
|
241
|
mNodeEffects[phase][i].notifyWhenFinished(this);
|
242
|
}
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
public static void enableEffects()
|
248
|
{
|
249
|
Method method;
|
250
|
|
251
|
for(Type type: Type.values())
|
252
|
{
|
253
|
try
|
254
|
{
|
255
|
method = type.effect.getDeclaredMethod("enable"); // enable not public, thus getDeclaredMethod
|
256
|
}
|
257
|
catch(NoSuchMethodException ex)
|
258
|
{
|
259
|
android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
|
260
|
method = null;
|
261
|
}
|
262
|
|
263
|
try
|
264
|
{
|
265
|
if( method!=null ) method.invoke(null);
|
266
|
}
|
267
|
catch(Exception ex)
|
268
|
{
|
269
|
android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
|
270
|
}
|
271
|
}
|
272
|
}
|
273
|
}
|