Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueue.java @ d07f2950

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
  protected static boolean mCreated;
27
 
28
  protected Vector<EffectListener> mListeners =null;
29
  protected int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
30
  protected long mBitmapID;
31
  
32
  static
33
    {
34
    reset();
35
    }
36
  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
   
39
  public EffectQueue(DistortedObject obj, int numUniforms, int index)
40
    {
41
    mNumEffects   = 0;
42
    mTotalEffects = 0;
43
    mMaxIndex     = index;
44

    
45
    if( obj!=null )
46
      {
47
      mObjHalfX = obj.getWidth() / 2.0f;
48
      mObjHalfY = obj.getHeight() / 2.0f;
49
      mObjHalfZ = obj.getDepth() / 2.0f;
50

    
51
      mBitmapID = obj.getID();
52
      }
53

    
54
    if( mMax[mMaxIndex]>0 )
55
      {
56
      mType            = new int[mMax[mMaxIndex]];
57
      mUniforms        = new float[numUniforms*mMax[mMaxIndex]];
58
      mInterI          = new Interpolator[mMax[mMaxIndex]];
59
      mInterP          = new Interpolator2D[mMax[mMaxIndex]];
60
      mCurrentDuration = new long[mMax[mMaxIndex]];
61
      mID              = new long[mMax[mMaxIndex]];
62
      mIDIndex         = new byte[mMax[mMaxIndex]];
63
      mFreeIndexes     = new byte[mMax[mMaxIndex]];
64
     
65
      for(byte i=0; i<mMax[mMaxIndex]; i++) mFreeIndexes[i] = i;
66
      }
67
   
68
    mCreated = true;  
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  int getNumEffects()
74
    {
75
    return mNumEffects;  
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  void addListener(EffectListener el)
81
    {
82
    if( mListeners==null ) mListeners = new Vector<>(2,2);
83
   
84
    mListeners.add(el);
85
    mNumListeners++;
86
    }
87
 
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  void removeListener(EffectListener el)
91
    {
92
    if( mNumListeners>0 )  
93
      {
94
      mListeners.remove(el);
95
      mNumListeners--;
96
      }
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  static void reset()
102
    {
103
    EffectTypes.reset(mMax);
104
    mCreated = false;  
105
    }
106
 
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  synchronized boolean removeByID(long id)
110
    {
111
    int i = getEffectIndex(id);
112
   
113
    if( i>=0 ) 
114
      {
115
      remove(i);
116
      return true;
117
      }
118
   
119
    return false; 
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  synchronized boolean removeByType(EffectNames effect)
125
    {
126
    boolean ret = false;  
127
    int ord = effect.ordinal();  
128
     
129
    for(int i=0; i<mNumEffects; i++)
130
      {
131
      if( mType[i]==ord )
132
        {
133
        remove(i);
134
        ret = true;
135
        }
136
      }
137
   
138
    return ret;
139
    }
140
  
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
  
143
  protected synchronized int getEffectIndex(long id)
144
    {
145
    int index = mIDIndex[(int)(id%mMax[mMaxIndex])];
146
    return (index<mNumEffects && mID[index]==id ? index : -1);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
  
151
  synchronized int abortAll()
152
    {
153
    int ret = mNumEffects;
154

    
155
    for(int i=0; i<ret; i++ )
156
      {
157
      mInterI[i] = null;
158
      mInterP[i] = null;
159
      }
160

    
161
    mNumEffects= 0;
162

    
163
    return ret;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// this assumes 0<=effect<mNumEffects
168
  
169
  protected void remove(int effect)
170
    {
171
    mNumEffects--;     
172
    
173
    byte removedIndex = (byte)(mID[effect]%mMax[mMaxIndex]);
174
    byte removedPosition = mIDIndex[removedIndex];
175
    mFreeIndexes[mNumEffects] = removedIndex;
176
    
177
    long removedID = mID[effect];
178
    int removedType= mType[effect];
179
    
180
    for(int j=0; j<mMax[mMaxIndex]; j++)
181
      {
182
      if( mIDIndex[j] > removedPosition ) mIDIndex[j]--; 
183
      }
184
         
185
    for(int j=effect; j<mNumEffects; j++ ) 
186
      {
187
      mType[j]            = mType[j+1];
188
      mInterI[j]          = mInterI[j+1];
189
      mInterP[j]          = mInterP[j+1];
190
      mCurrentDuration[j] = mCurrentDuration[j+1];
191
      mID[j]              = mID[j+1];
192
    
193
      moveEffect(j);
194
      }
195
   
196
    mInterI[mNumEffects] = null;
197
    mInterP[mNumEffects] = null;
198
   
199
    for(int i=0; i<mNumListeners; i++) 
200
      EffectMessageSender.newMessage( mListeners.elementAt(i),
201
                                      EffectMessage.EFFECT_REMOVED, 
202
                                      (removedID<<EffectTypes.LENGTH)+EffectNames.getType(removedType).type,
203
                                      removedType,
204
                                      mBitmapID);  
205
    }
206
  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
  
209
  protected long addBase(EffectNames eln)
210
    {    
211
    mType[mNumEffects]  = eln.ordinal();  
212
    mCurrentDuration[mNumEffects] = 0;
213
    
214
    int index = mFreeIndexes[mNumEffects];
215
    long id = mTotalEffects*mMax[mMaxIndex] + index;
216
    mID[mNumEffects] = id;
217
    mIDIndex[index] = mNumEffects;  
218
   
219
    mNumEffects++; 
220
    mTotalEffects++;
221
   
222
    return (id<<EffectTypes.LENGTH)+eln.getType().type;
223
    }
224
    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
// used only for debugging
227
  
228
  protected String printEffects(int max)
229
    {
230
    long[] indexes = new long[mMax[mMaxIndex]];
231
   
232
    for(int g=0; g<mMax[mMaxIndex]; g++)
233
      {
234
      indexes[g] = -1;  
235
      }
236
   
237
    String ret="(";
238
    int f;
239
   
240
    for(int g=0; g<max; g++) 
241
      {
242
      f = getEffectIndex(g);
243
      if( f>=0 ) indexes[f] = g;
244
      }
245
   
246
    for(int g=0; g<mMax[mMaxIndex]; g++)
247
      {
248
      ret += (g>0 ? ",":"")+(indexes[g]>=0 ? indexes[g] : " ");   
249
      }
250
   
251
    ret += ")";
252
   
253
    return ret;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
// Only used for debugging
258
  
259
  protected boolean printByID(long id)
260
    {
261
    int index = getEffectIndex(id);
262
   
263
    if( index>=0 ) 
264
      {
265
      boolean interI = mInterI[index]==null; 
266
      boolean interP = mInterP[index]==null; 
267
      
268
      android.util.Log.e("EffectQueue", "numEffects="+mNumEffects+" effect id="+id+" index="+index+" duration="+mCurrentDuration[index]+" interI null="+interI+" interP null="+interP);
269
      
270
      if( interI==false )
271
        {
272
        android.util.Log.e("EffectQueue","interI: "+mInterI[index].print());
273
        }
274
      if( interP==false )
275
        {
276
        android.util.Log.e("EffectQueue","interP: "+mInterP[index].print());
277
        }
278
     
279
      return true;
280
      }
281
   
282
    android.util.Log.e("EffectQueue", "effect id="+id+" not found");
283

    
284
    return false;  
285
    }
286
 
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  abstract void moveEffect(int index);
290
  }
291
///////////////////////////////////////////////////////////////////////////////////////////////////
(12-12/30)