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