Project

General

Profile

Download (8.47 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / EffectQueue.java @ 476bbc81

1
package org.distorted.library;
2

    
3
import java.util.Vector;
4

    
5
///////////////////////////////////////////////////////////////////////////////////////////////////
6

    
7
abstract class EffectQueue
8
  {
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
  protected static int[] mMax = new int[EffectTypes.LENGTH];
25
  protected int mMaxIndex;
26

    
27
  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

    
31
  private static boolean mCreated;
32

    
33
  static
34
    {
35
    reset();
36
    }
37
  
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
   
40
  public EffectQueue(DistortedObject obj, int numUniforms, int index)
41
    {
42
    mNumEffects   = 0;
43
    mTotalEffects = 0;
44
    mMaxIndex     = index;
45

    
46
    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
    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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  void addListener(EffectListener el)
107
    {
108
    if( mListeners==null ) mListeners = new Vector<>(2,2);
109
   
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
    EffectTypes.reset(mMax);
130
    mCreated = false;  
131
    }
132
 
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  synchronized int removeByID(long id)
136
    {
137
    int i = getEffectIndex(id);
138
   
139
    if( i>=0 ) 
140
      {
141
      remove(i);
142
      return 1;
143
      }
144
   
145
    return 0;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  synchronized int removeByType(EffectNames effect)
151
    {
152
    int ret = 0;
153
    int ord = effect.ordinal();  
154
     
155
    for(int i=0; i<mNumEffects; i++)
156
      {
157
      if( mType[i]==ord )
158
        {
159
        remove(i);
160
        i--;
161
        ret++;
162
        }
163
      }
164
   
165
    return ret;
166
    }
167
  
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
  
170
  private synchronized int getEffectIndex(long id)
171
    {
172
    int index = mIDIndex[(int)(id%mMax[mMaxIndex])];
173
    return (index<mNumEffects && mID[index]==id ? index : -1);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
  
178
  synchronized int abortAll()
179
    {
180
    int ret = mNumEffects;
181

    
182
    for(int i=0; i<ret; i++ )
183
      {
184
      mInterI[i] = null;
185
      mInterP[i] = null;
186
      }
187

    
188
    mNumEffects= 0;
189

    
190
    return ret;
191
    }
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
                                      (removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedType).type,
230
                                      removedType,
231
                                      mBitmapID,
232
                                      null);
233
    }
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
    return (id<<EffectTypes.LENGTH)+eln.getType().type;
251
    }
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
    String ret="(";
266
    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
      android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+" duration="+mCurrentDuration[index]+" interI null="+interI+" interP null="+interP);
297
      
298
      if( interI==false )
299
        {
300
        android.util.Log.e("EffectQueue","interI: "+mInterI[index].print());
301
        }
302
      if( interP==false )
303
        {
304
        android.util.Log.e("EffectQueue","interP: "+mInterP[index].print());
305
        }
306
     
307
      return true;
308
      }
309
   
310
    android.util.Log.e("EffectQueue", "effect id="+id+" not found");
311

    
312
    return false;  
313
    }
314
 
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  abstract void moveEffect(int index);
318
  }
319
///////////////////////////////////////////////////////////////////////////////////////////////////
(12-12/30)