Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueue.java @ 2e18813f

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 boolean removeByID(long id)
136
    {
137
    int i = getEffectIndex(id);
138
   
139
    if( i>=0 ) 
140
      {
141
      remove(i);
142
      return true;
143
      }
144
   
145
    return false; 
146
    }
147

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

    
150
  synchronized boolean removeByType(EffectNames effect)
151
    {
152
    boolean ret = false;  
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
        ret = true;
161
        }
162
      }
163
   
164
    return ret;
165
    }
166
  
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
  
169
  private synchronized int getEffectIndex(long id)
170
    {
171
    int index = mIDIndex[(int)(id%mMax[mMaxIndex])];
172
    return (index<mNumEffects && mID[index]==id ? index : -1);
173
    }
174

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

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

    
187
    mNumEffects= 0;
188

    
189
    return ret;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// this assumes 0<=effect<mNumEffects
194
  
195
  protected void remove(int effect)
196
    {
197
    mNumEffects--;     
198
    
199
    byte removedIndex = (byte)(mID[effect]%mMax[mMaxIndex]);
200
    byte removedPosition = mIDIndex[removedIndex];
201
    mFreeIndexes[mNumEffects] = removedIndex;
202
    
203
    long removedID = mID[effect];
204
    int removedType= mType[effect];
205
    
206
    for(int j=0; j<mMax[mMaxIndex]; j++)
207
      {
208
      if( mIDIndex[j] > removedPosition ) mIDIndex[j]--; 
209
      }
210
         
211
    for(int j=effect; j<mNumEffects; j++ ) 
212
      {
213
      mType[j]            = mType[j+1];
214
      mInterI[j]          = mInterI[j+1];
215
      mInterP[j]          = mInterP[j+1];
216
      mCurrentDuration[j] = mCurrentDuration[j+1];
217
      mID[j]              = mID[j+1];
218
    
219
      moveEffect(j);
220
      }
221
   
222
    mInterI[mNumEffects] = null;
223
    mInterP[mNumEffects] = null;
224
   
225
    for(int i=0; i<mNumListeners; i++) 
226
      EffectMessageSender.newMessage( mListeners.elementAt(i),
227
                                      EffectMessage.EFFECT_REMOVED, 
228
                                      (removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedType).type,
229
                                      removedType,
230
                                      mBitmapID);  
231
    }
232
  
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
  
235
  protected long addBase(EffectNames eln)
236
    {    
237
    mType[mNumEffects]  = eln.ordinal();  
238
    mCurrentDuration[mNumEffects] = 0;
239
    
240
    int index = mFreeIndexes[mNumEffects];
241
    long id = mTotalEffects*mMax[mMaxIndex] + index;
242
    mID[mNumEffects] = id;
243
    mIDIndex[index] = mNumEffects;  
244
   
245
    mNumEffects++; 
246
    mTotalEffects++;
247
   
248
    return (id<<EffectTypes.LENGTH)+eln.getType().type;
249
    }
250
    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
// used only for debugging
253
  
254
  protected String printEffects(int max)
255
    {
256
    long[] indexes = new long[mMax[mMaxIndex]];
257
   
258
    for(int g=0; g<mMax[mMaxIndex]; g++)
259
      {
260
      indexes[g] = -1;  
261
      }
262
   
263
    String ret="(";
264
    int f;
265
   
266
    for(int g=0; g<max; g++) 
267
      {
268
      f = getEffectIndex(g);
269
      if( f>=0 ) indexes[f] = g;
270
      }
271
   
272
    for(int g=0; g<mMax[mMaxIndex]; g++)
273
      {
274
      ret += (g>0 ? ",":"")+(indexes[g]>=0 ? indexes[g] : " ");   
275
      }
276
   
277
    ret += ")";
278
   
279
    return ret;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
// Only used for debugging
284
  
285
  protected boolean printByID(long id)
286
    {
287
    int index = getEffectIndex(id);
288
   
289
    if( index>=0 ) 
290
      {
291
      boolean interI = mInterI[index]==null; 
292
      boolean interP = mInterP[index]==null; 
293
      
294
      android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+" duration="+mCurrentDuration[index]+" interI null="+interI+" interP null="+interP);
295
      
296
      if( interI==false )
297
        {
298
        android.util.Log.e("EffectQueue","interI: "+mInterI[index].print());
299
        }
300
      if( interP==false )
301
        {
302
        android.util.Log.e("EffectQueue","interP: "+mInterP[index].print());
303
        }
304
     
305
      return true;
306
      }
307
   
308
    android.util.Log.e("EffectQueue", "effect id="+id+" not found");
309

    
310
    return false;  
311
    }
312
 
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  abstract void moveEffect(int index);
316
  }
317
///////////////////////////////////////////////////////////////////////////////////////////////////
(12-12/30)