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