Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 43814a57

1 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 8eccf334 Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 8eccf334 Leszek Koltunski
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 8eccf334 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.effect;
21
22 43814a57 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
23 20dbec0e Leszek Koltunski
import org.distorted.library.message.EffectListener;
24
25 b82a9ac9 Leszek Koltunski
import java.lang.reflect.Method;
26 20dbec0e Leszek Koltunski
import java.util.ArrayList;
27 b82a9ac9 Leszek Koltunski
28 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
29 faa3ff56 Leszek Koltunski
/**
30
 * Abstract Effect of any type.
31
 */
32 8eccf334 Leszek Koltunski
public abstract class Effect
33
  {
34 da9b3f07 Leszek Koltunski
  private final static int MAX_UNITY_DIM = 4;
35 c3651001 Leszek Koltunski
  private final static int NUM_EFFECTS = EffectName.LENGTH;
36 da9b3f07 Leszek Koltunski
37 b547aaba leszek
  private final long mID;
38 da9b3f07 Leszek Koltunski
  private final EffectType mType;
39
  private final EffectName mName;
40 b547aaba leszek
  private final int mDimension;
41 b24e4719 Leszek Koltunski
  private final int mRegionDim;
42
  private final int mCenterDim;
43 8eccf334 Leszek Koltunski
44 7a1fcbeb Leszek Koltunski
  private ArrayList<EffectListener> mListeners =null;
45
  private int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
46 20dbec0e Leszek Koltunski
47 8eccf334 Leszek Koltunski
  private static long mNextID = 0;
48
49 da9b3f07 Leszek Koltunski
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
50
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
51 8eccf334 Leszek Koltunski
52 2aeb75aa Leszek Koltunski
  int mAndAssociation;
53
  int mEquAssociation;
54 bc208a9c Leszek Koltunski
55 9af837e8 leszek
  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 43814a57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
  public abstract void addQueue(EffectQueue queue);
65
  public abstract void remQueue(EffectQueue queue);
66
67 15aa7d94 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 faa3ff56 Leszek Koltunski
  Effect(EffectName name)
70
    {
71
    mName      = name;
72
    mType      = name.getType();
73 b24e4719 Leszek Koltunski
    mDimension = name.getEffectDimension();
74
    mCenterDim = name.getCenterDimension();
75
    mRegionDim = name.getRegionDimension();
76 15aa7d94 Leszek Koltunski
77 2aeb75aa Leszek Koltunski
    mAndAssociation = 0xffffffff;
78
    mEquAssociation = 0;
79 bc208a9c Leszek Koltunski
80 faa3ff56 Leszek Koltunski
    int n = name.ordinal();
81
    float[] u = name.getUnity();
82 2f1f7570 Leszek Koltunski
    int l = u.length;
83 8eccf334 Leszek Koltunski
84 f046b159 Leszek Koltunski
    System.arraycopy(u, 0, mUnity, MAX_UNITY_DIM*n, l);
85 faa3ff56 Leszek Koltunski
86
    mUnityDim[n] = l;
87
88
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
89
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
/**
93
 * Only for use by the library itself.
94
 *
95
 * @y.exclude
96
 */
97 b547aaba leszek
  public static void onDestroy()
98 8eccf334 Leszek Koltunski
    {
99 b547aaba leszek
    mNextID = 0;
100 9af837e8 leszek
101
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
102 2f1f7570 Leszek Koltunski
103
    MatrixEffect.destroyStatics();
104
    VertexEffect.destroyStatics();
105 143095f7 Leszek Koltunski
    FragmentEffect.destroyStatics();
106 2f1f7570 Leszek Koltunski
    PostprocessEffect.destroyStatics();
107 8eccf334 Leszek Koltunski
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110 faa3ff56 Leszek Koltunski
/**
111
 * Only for use by the library itself.
112
 *
113
 * @y.exclude
114
 */
115
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
116 8eccf334 Leszek Koltunski
117 20dbec0e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
/**
119
 * Only for use by the library itself.
120
 *
121
 * @y.exclude
122
 */
123
  public int getNumListeners()
124
    {
125
    return mNumListeners;
126
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
/**
130
 * Only for use by the library itself.
131
 *
132
 * @y.exclude
133
 */
134
  public EffectListener removeFirstListener()
135
    {
136
    if( mNumListeners>0 )
137
      {
138
      mNumListeners--;
139
      return mListeners.remove(0);
140
      }
141
142
    return null;
143
    }
144
145 bc208a9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
146
/**
147
 * Only for use by the library itself.
148
 *
149
 * @y.exclude
150
 */
151 96e3b88a Leszek Koltunski
  public void writeAssociations(int[] intUniforms, int index1, int index2)
152 bc208a9c Leszek Koltunski
    {
153 96e3b88a Leszek Koltunski
    intUniforms[index1] = mAndAssociation;
154
    intUniforms[index2] = mEquAssociation;
155 36d65d88 Leszek Koltunski
    }
156
157 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// PUBLIC API
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
162
 * null Effect?
163
 */
164 da9b3f07 Leszek Koltunski
  public boolean isUnity(float[] buffer, int index)
165 8eccf334 Leszek Koltunski
    {
166 da9b3f07 Leszek Koltunski
    int name = mName.ordinal();
167
168
    switch(mUnityDim[name])
169
      {
170
      case 0: return true;
171
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
172
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
173
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
174
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
175
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
176
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
177
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
178
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
179
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
180
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
181
      }
182
183
    return false;
184 b547aaba leszek
    }
185 b82a9ac9 Leszek Koltunski
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
188
// but this shouldn't matter.
189
/**
190
 * Enable all effects of a given type.
191
 *
192
 * @param type EffectType to enable.
193
 */
194
  public static void enableEffects(EffectType type)
195
    {
196 7a1fcbeb Leszek Koltunski
    Method method;
197 b82a9ac9 Leszek Koltunski
198
    for(EffectName name: EffectName.values())
199
      {
200
      if( name.getType() == type )
201
        {
202
        Class<? extends Effect> cls = name.getEffectClass();
203
204
        try
205
          {
206 7a1fcbeb Leszek Koltunski
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
207
                                             // is public
208 b82a9ac9 Leszek Koltunski
          }
209
        catch(NoSuchMethodException ex)
210
          {
211
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
212 7a1fcbeb Leszek Koltunski
          method = null;
213 b82a9ac9 Leszek Koltunski
          }
214
215
        try
216
          {
217 7a1fcbeb Leszek Koltunski
          if( method!=null ) method.invoke(null);
218 b82a9ac9 Leszek Koltunski
          }
219
        catch(Exception ex)
220
          {
221
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
222
          }
223
        }
224
      }
225
    }
226
227 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
228 faa3ff56 Leszek Koltunski
/**
229
 * Return the EffectType enum corresponding to this Effect.
230
 *
231
 * @see EffectType
232
 */
233 da9b3f07 Leszek Koltunski
  public EffectType getType()
234 b547aaba leszek
    {
235
    return mType;
236
    }
237 8eccf334 Leszek Koltunski
238 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239 faa3ff56 Leszek Koltunski
/**
240
 * Return the EffectName enum corresponding to this Effect.
241
 *
242
 * @see EffectName
243
 */
244 da9b3f07 Leszek Koltunski
  public EffectName getName()
245 6d62a900 Leszek Koltunski
    {
246
    return mName;
247
    }
248
249 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
250 faa3ff56 Leszek Koltunski
/**
251
 * Return the unique ID of this Effect.
252
 */
253 b547aaba leszek
  public long getID()
254
    {
255
    return mID;
256 8eccf334 Leszek Koltunski
    }
257
258 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
259 faa3ff56 Leszek Koltunski
/**
260
 * Return a printable name of this Effect.
261
 */
262 6bb59aad Leszek Koltunski
  public String getString()
263
    {
264 da9b3f07 Leszek Koltunski
    return mName.name();
265 6bb59aad Leszek Koltunski
    }
266
267 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
268 faa3ff56 Leszek Koltunski
/**
269 b24e4719 Leszek Koltunski
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
270 faa3ff56 Leszek Koltunski
 */
271 b24e4719 Leszek Koltunski
  public int getCenterDimension()
272 8eccf334 Leszek Koltunski
    {
273 b24e4719 Leszek Koltunski
    return mCenterDim;
274 8eccf334 Leszek Koltunski
    }
275
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277 faa3ff56 Leszek Koltunski
/**
278 b24e4719 Leszek Koltunski
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
279 faa3ff56 Leszek Koltunski
 */
280 b24e4719 Leszek Koltunski
  public int getRegionDimension()
281 8eccf334 Leszek Koltunski
    {
282 b24e4719 Leszek Koltunski
    return mRegionDim;
283 8eccf334 Leszek Koltunski
    }
284
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286 faa3ff56 Leszek Koltunski
/**
287
 * Return the number of Uniforms needed to describe this effect.
288
 */
289 b24e4719 Leszek Koltunski
  public int getEffectDimension()
290 8eccf334 Leszek Koltunski
    {
291 b547aaba leszek
    return mDimension;
292 8eccf334 Leszek Koltunski
    }
293 20dbec0e Leszek Koltunski
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
/**
296
 * Adds the calling class to the list of Listeners that get notified when this Effect gets 'finished'
297
 * i.e. when the Dynamic inside this Effect reaches its final point and stops moving. This will be sent
298
 * only once, on the first time the Dynamic reaches its final point.
299
 *
300
 * If there's no Dynamic, ths message will never be sent.
301
 *
302
 * @param el A class implementing the EffectListener interface that wants to get notifications.
303
 */
304
  public void notifyWhenFinished(EffectListener el)
305
    {
306
    if( mListeners==null ) mListeners = new ArrayList<>();
307
308
    if( !mListeners.contains(el) )
309
      {
310
      mListeners.add(el);
311
      mNumListeners++;
312
      }
313
    }
314 8eccf334 Leszek Koltunski
  }