Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ e5f796bc

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effect;
22

    
23
import org.distorted.library.effectqueue.EffectQueue;
24
import org.distorted.library.main.InternalStackFrameList;
25
import org.distorted.library.message.EffectListener;
26

    
27
import java.lang.reflect.Method;
28
import java.util.ArrayList;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
/**
32
 * Abstract Effect of any type.
33
 */
34
public abstract class Effect
35
  {
36
  private final static int MAX_UNITY_DIM = 4;
37
  private final static int NUM_EFFECTS = EffectName.LENGTH;
38

    
39
  private final long mID;
40
  private final EffectType mType;
41
  private final EffectName mName;
42
  private final int mDimension;
43
  private final int mRegionDim;
44
  private final int mCenterDim;
45

    
46
  private ArrayList<EffectListener> mListeners =null;
47
  private int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
48

    
49
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
50
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
51

    
52
  int mAndAssociation;
53
  int mEquAssociation;
54

    
55
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
56

    
57
  static
58
    {
59
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public abstract void addQueue(EffectQueue queue);
65
  public abstract void remQueue(EffectQueue queue);
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  Effect(EffectName name)
70
    {
71
    mName      = name;
72
    mType      = name.getType();
73
    mDimension = name.getEffectDimension();
74
    mCenterDim = name.getCenterDimension();
75
    mRegionDim = name.getRegionDimension();
76

    
77
    mAndAssociation = 0xffffffff;
78
    mEquAssociation = 0;
79

    
80
    int n = name.ordinal();
81
    float[] u = name.getUnity();
82
    int l = u.length;
83

    
84
    System.arraycopy(u, 0, mUnity, MAX_UNITY_DIM*n, l);
85

    
86
    mUnityDim[n] = l;
87

    
88
    mID = (InternalStackFrameList.getNextEffectID()<<EffectType.LENGTH) + mType.ordinal();
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
/**
93
 * Only for use by the library itself.
94
 *
95
 * @y.exclude
96
 */
97
  public static void onPause()
98
    {
99
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
100

    
101
    MatrixEffect.destroyStatics();
102
    VertexEffect.destroyStatics();
103
    FragmentEffect.destroyStatics();
104
    PostprocessEffect.destroyStatics();
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
/**
109
 * Only for use by the library itself.
110
 *
111
 * @y.exclude
112
 */
113
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
/**
117
 * Only for use by the library itself.
118
 *
119
 * @y.exclude
120
 */
121
  public int getNumListeners()
122
    {
123
    return mNumListeners;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
/**
128
 * Only for use by the library itself.
129
 *
130
 * @y.exclude
131
 */
132
  public EffectListener removeFirstListener()
133
    {
134
    if( mNumListeners>0 )
135
      {
136
      mNumListeners--;
137
      return mListeners.remove(0);
138
      }
139

    
140
    return null;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
/**
145
 * Only for use by the library itself.
146
 *
147
 * @y.exclude
148
 */
149
  public void writeAssociations(int[] intUniforms, int index1, int index2)
150
    {
151
    intUniforms[index1] = mAndAssociation;
152
    intUniforms[index2] = mEquAssociation;
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// PUBLIC API
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
/**
159
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
160
 * null Effect?
161
 */
162
  public boolean isUnity(float[] buffer, int index)
163
    {
164
    int name = mName.ordinal();
165

    
166
    switch(mUnityDim[name])
167
      {
168
      case 0: return true;
169
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
170
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
171
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
172
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
173
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
174
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
175
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
176
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
177
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
178
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
179
      }
180

    
181
    return false;
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
186
// but this shouldn't matter.
187
/**
188
 * Enable all effects of a given type.
189
 *
190
 * @param type EffectType to enable.
191
 */
192
  public static void enableEffects(EffectType type)
193
    {
194
    Method method;
195

    
196
    for(EffectName name: EffectName.values())
197
      {
198
      if( name.getType() == type )
199
        {
200
        Class<? extends Effect> cls = name.getEffectClass();
201

    
202
        try
203
          {
204
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
205
                                             // is public
206
          }
207
        catch(NoSuchMethodException ex)
208
          {
209
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
210
          method = null;
211
          }
212

    
213
        try
214
          {
215
          if( method!=null ) method.invoke(null);
216
          }
217
        catch(Exception ex)
218
          {
219
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
220
          }
221
        }
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
/**
227
 * Return the EffectType enum corresponding to this Effect.
228
 *
229
 * @see EffectType
230
 */
231
  public EffectType getType()
232
    {
233
    return mType;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
/**
238
 * Return the EffectName enum corresponding to this Effect.
239
 *
240
 * @see EffectName
241
 */
242
  public EffectName getName()
243
    {
244
    return mName;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
/**
249
 * Return the unique ID of this Effect.
250
 */
251
  public long getID()
252
    {
253
    return mID;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
/**
258
 * Return a printable name of this Effect.
259
 */
260
  public String getString()
261
    {
262
    return mName.name();
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
/**
267
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
268
 */
269
  public int getCenterDimension()
270
    {
271
    return mCenterDim;
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
/**
276
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
277
 */
278
  public int getRegionDimension()
279
    {
280
    return mRegionDim;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
/**
285
 * Return the number of Uniforms needed to describe this effect.
286
 */
287
  public int getEffectDimension()
288
    {
289
    return mDimension;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293
/**
294
 * Adds the calling class to the list of Listeners that get notified when this Effect gets 'finished'
295
 * i.e. when the Dynamic inside this Effect reaches its final point and stops moving. This will be sent
296
 * only once, on the first time the Dynamic reaches its final point.
297
 *
298
 * If there's no Dynamic, ths message will never be sent.
299
 *
300
 * @param el A class implementing the EffectListener interface that wants to get notifications.
301
 */
302
  public void notifyWhenFinished(EffectListener el)
303
    {
304
    if( mListeners==null ) mListeners = new ArrayList<>();
305

    
306
    if( !mListeners.contains(el) )
307
      {
308
      mListeners.add(el);
309
      mNumListeners++;
310
      }
311
    }
312
  }
(1-1/34)