Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectList.java @ 1e438fc7

1
package org.distorted.library;
2

    
3
import java.util.Vector;
4

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

    
7
abstract class EffectList
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 EffectList(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 void abortAll()
152
    {
153
    for(int i=0; i<mNumEffects; i++ )
154
      {
155
      mInterI[i] = null;
156
      mInterP[i] = null;
157
      } 
158
   
159
    mNumEffects= 0;  
160
    }
161

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

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

    
280
    return false;  
281
    }
282
 
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  abstract void moveEffect(int index);
286
  }
287
///////////////////////////////////////////////////////////////////////////////////////////////////
(8-8/30)