1
|
package org.distorted.library;
|
2
|
|
3
|
import java.util.Vector;
|
4
|
|
5
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
6
|
|
7
|
public abstract class EffectList
|
8
|
{
|
9
|
protected static final int DEFAULT_NUM_EFFECTS = 5;
|
10
|
|
11
|
protected static final int MATRIX =0;
|
12
|
protected static final int VERTEX =1;
|
13
|
protected static final int FRAGMENT=2;
|
14
|
|
15
|
protected byte mNumEffects; // number of effects at the moment
|
16
|
protected long mTotalEffects; // total number of effects ever created
|
17
|
|
18
|
protected int[] mType;
|
19
|
protected float[] mUniforms;
|
20
|
protected Interpolator[] mInterP; // center of the effect
|
21
|
protected Interpolator[] mInterI; // all other interpolated values
|
22
|
protected long[] mCurrentDuration;
|
23
|
protected byte[] mFreeIndexes;
|
24
|
protected byte[] mIDIndex;
|
25
|
protected long[] mID;
|
26
|
|
27
|
protected long mTime=0;
|
28
|
protected float mObjHalfX, mObjHalfY, mObjHalfZ;
|
29
|
|
30
|
protected static int[] mMax = new int[3];
|
31
|
protected int mMaxIndex;
|
32
|
protected static boolean mCreated = false;
|
33
|
|
34
|
protected Vector<EffectListener> mListeners =null;
|
35
|
protected int mNumListeners=0; // ==mListeners.length(), but we only create mListeners if the first one gets added
|
36
|
protected long mBitmapID;
|
37
|
|
38
|
static
|
39
|
{
|
40
|
mMax[MATRIX] = DEFAULT_NUM_EFFECTS;
|
41
|
mMax[VERTEX] = DEFAULT_NUM_EFFECTS;
|
42
|
mMax[FRAGMENT] = DEFAULT_NUM_EFFECTS;
|
43
|
}
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
public EffectList(DistortedObject obj, int numUniforms, int index)
|
48
|
{
|
49
|
mNumEffects = 0;
|
50
|
mTotalEffects = 0;
|
51
|
mMaxIndex = index;
|
52
|
|
53
|
mObjHalfX = obj.getWidth() /2.0f;
|
54
|
mObjHalfY = obj.getHeight()/2.0f;
|
55
|
mObjHalfZ = obj.getDepth() /2.0f;
|
56
|
|
57
|
mBitmapID = obj.getID();
|
58
|
|
59
|
if( mMax[mMaxIndex]>0 )
|
60
|
{
|
61
|
mType = new int[mMax[mMaxIndex]];
|
62
|
mUniforms = new float[numUniforms*mMax[mMaxIndex]];
|
63
|
mInterI = new Interpolator[mMax[mMaxIndex]];
|
64
|
mInterP = new Interpolator2D[mMax[mMaxIndex]];
|
65
|
mCurrentDuration = new long[mMax[mMaxIndex]];
|
66
|
mID = new long[mMax[mMaxIndex]];
|
67
|
mIDIndex = new byte[mMax[mMaxIndex]];
|
68
|
mFreeIndexes = new byte[mMax[mMaxIndex]];
|
69
|
|
70
|
for(byte i=0; i<mMax[mMaxIndex]; i++) mFreeIndexes[i] = i;
|
71
|
}
|
72
|
|
73
|
mCreated = true;
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
int getNumEffects()
|
79
|
{
|
80
|
return mNumEffects;
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
void addListener(EffectListener el)
|
86
|
{
|
87
|
if( mNumListeners==0 ) mListeners = new Vector<EffectListener>(2,2);
|
88
|
|
89
|
mListeners.add(el);
|
90
|
mNumListeners++;
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
void removeListener(EffectListener el)
|
96
|
{
|
97
|
if( mNumListeners>0 )
|
98
|
{
|
99
|
mListeners.remove(el);
|
100
|
mNumListeners--;
|
101
|
}
|
102
|
}
|
103
|
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
|
106
|
static void reset()
|
107
|
{
|
108
|
mMax[MATRIX] = DEFAULT_NUM_EFFECTS;
|
109
|
mMax[VERTEX] = DEFAULT_NUM_EFFECTS;
|
110
|
mMax[FRAGMENT] = DEFAULT_NUM_EFFECTS;
|
111
|
|
112
|
mCreated = false;
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
synchronized boolean removeByID(long id)
|
118
|
{
|
119
|
int i = getEffectIndex(id);
|
120
|
|
121
|
if( i>=0 )
|
122
|
{
|
123
|
remove(i);
|
124
|
return true;
|
125
|
}
|
126
|
|
127
|
return false;
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
synchronized boolean removeByType(EffectNames effect)
|
133
|
{
|
134
|
boolean ret = false;
|
135
|
int ord = effect.ordinal();
|
136
|
|
137
|
for(int i=0; i<mNumEffects; i++)
|
138
|
{
|
139
|
if( mType[i]==ord )
|
140
|
{
|
141
|
remove(i);
|
142
|
ret = true;
|
143
|
}
|
144
|
}
|
145
|
|
146
|
return ret;
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
protected synchronized int getEffectIndex(long id)
|
152
|
{
|
153
|
int index = mIDIndex[(int)(id%mMax[mMaxIndex])];
|
154
|
return (index<mNumEffects && mID[index]==id ? index : -1);
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
synchronized void abortAll()
|
160
|
{
|
161
|
for(int i=0; i<mNumEffects; i++ )
|
162
|
{
|
163
|
mInterI[i] = null;
|
164
|
mInterP[i] = null;
|
165
|
}
|
166
|
|
167
|
mNumEffects= 0;
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
// this assumes 0<=effect<mNumEffects
|
172
|
|
173
|
protected void remove(int effect)
|
174
|
{
|
175
|
mNumEffects--;
|
176
|
|
177
|
byte removedIndex = (byte)(mID[effect]%mMax[mMaxIndex]);
|
178
|
byte removedPosition = mIDIndex[removedIndex];
|
179
|
mFreeIndexes[mNumEffects] = removedIndex;
|
180
|
|
181
|
long removedID = mID[effect];
|
182
|
int removedType= mType[effect];
|
183
|
|
184
|
for(int j=0; j<mMax[mMaxIndex]; j++)
|
185
|
{
|
186
|
if( mIDIndex[j] > removedPosition ) mIDIndex[j]--;
|
187
|
}
|
188
|
|
189
|
for(int j=effect; j<mNumEffects; j++ )
|
190
|
{
|
191
|
mType[j] = mType[j+1];
|
192
|
mInterI[j] = mInterI[j+1];
|
193
|
mInterP[j] = mInterP[j+1];
|
194
|
mCurrentDuration[j] = mCurrentDuration[j+1];
|
195
|
mID[j] = mID[j+1];
|
196
|
|
197
|
moveEffect(j);
|
198
|
}
|
199
|
|
200
|
mInterI[mNumEffects] = null;
|
201
|
mInterP[mNumEffects] = null;
|
202
|
|
203
|
for(int i=0; i<mNumListeners; i++)
|
204
|
EffectMessageSender.newMessage( mListeners.elementAt(i),
|
205
|
EffectMessage.EFFECT_REMOVED,
|
206
|
(removedID<<DistortedObject.TYPE_NUM)+EffectNames.getType(removedType),
|
207
|
removedType,
|
208
|
mBitmapID);
|
209
|
}
|
210
|
|
211
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212
|
|
213
|
protected long addBase(EffectNames eln)
|
214
|
{
|
215
|
mType[mNumEffects] = eln.ordinal();
|
216
|
mCurrentDuration[mNumEffects] = 0;
|
217
|
|
218
|
int index = mFreeIndexes[mNumEffects];
|
219
|
long id = mTotalEffects*mMax[mMaxIndex] + index;
|
220
|
mID[mNumEffects] = id;
|
221
|
mIDIndex[index] = mNumEffects;
|
222
|
|
223
|
mNumEffects++;
|
224
|
mTotalEffects++;
|
225
|
|
226
|
return (id<<DistortedObject.TYPE_NUM)+eln.getType();
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
// used only for debugging
|
231
|
|
232
|
protected String printEffects(int max)
|
233
|
{
|
234
|
long[] indexes = new long[mMax[mMaxIndex]];
|
235
|
|
236
|
for(int g=0; g<mMax[mMaxIndex]; g++)
|
237
|
{
|
238
|
indexes[g] = -1;
|
239
|
}
|
240
|
|
241
|
String ret= new String();
|
242
|
ret = "(";
|
243
|
|
244
|
int f;
|
245
|
|
246
|
for(int g=0; g<max; g++)
|
247
|
{
|
248
|
f = getEffectIndex(g);
|
249
|
if( f>=0 ) indexes[f] = g;
|
250
|
}
|
251
|
|
252
|
for(int g=0; g<mMax[mMaxIndex]; g++)
|
253
|
{
|
254
|
ret += (g>0 ? ",":"")+(indexes[g]>=0 ? indexes[g] : " ");
|
255
|
}
|
256
|
|
257
|
ret += ")";
|
258
|
|
259
|
return ret;
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
// Only used for debugging
|
264
|
|
265
|
protected boolean printByID(long id)
|
266
|
{
|
267
|
int index = getEffectIndex(id);
|
268
|
|
269
|
if( index>=0 )
|
270
|
{
|
271
|
boolean interI = mInterI[index]==null;
|
272
|
boolean interP = mInterP[index]==null;
|
273
|
|
274
|
android.util.Log.e("EffectList", "numEffects="+mNumEffects+" effect id="+id+" index="+index+" duration="+mCurrentDuration[index]+" interI null="+interI+" interP null="+interP);
|
275
|
|
276
|
if( interI==false )
|
277
|
{
|
278
|
android.util.Log.e("EffectList","interI: "+mInterI[index].print());
|
279
|
}
|
280
|
if( interP==false )
|
281
|
{
|
282
|
android.util.Log.e("EffectList","interP: "+mInterP[index].print());
|
283
|
}
|
284
|
|
285
|
return true;
|
286
|
}
|
287
|
|
288
|
android.util.Log.e("EffectList", "effect id="+id+" not found");
|
289
|
|
290
|
return false;
|
291
|
}
|
292
|
|
293
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294
|
|
295
|
abstract void moveEffect(int index);
|
296
|
}
|
297
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|