Project

General

Profile

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

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

1 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 cc62b34a Leszek Koltunski
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3 8eccf334 Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 8eccf334 Leszek Koltunski
//                                                                                               //
6 cc62b34a Leszek Koltunski
// 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 8eccf334 Leszek Koltunski
//                                                                                               //
11 cc62b34a Leszek Koltunski
// This library 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 cc62b34a Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 8eccf334 Leszek Koltunski
//                                                                                               //
16 cc62b34a Leszek Koltunski
// 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 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.effect;
22
23 43814a57 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
24 3543a3cf Leszek Koltunski
import org.distorted.library.main.InternalStackFrameList;
25 20dbec0e Leszek Koltunski
import org.distorted.library.message.EffectListener;
26
27 b82a9ac9 Leszek Koltunski
import java.lang.reflect.Method;
28 20dbec0e Leszek Koltunski
import java.util.ArrayList;
29 b82a9ac9 Leszek Koltunski
30 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
31 faa3ff56 Leszek Koltunski
/**
32
 * Abstract Effect of any type.
33
 */
34 8eccf334 Leszek Koltunski
public abstract class Effect
35
  {
36 da9b3f07 Leszek Koltunski
  private final static int MAX_UNITY_DIM = 4;
37 c3651001 Leszek Koltunski
  private final static int NUM_EFFECTS = EffectName.LENGTH;
38 da9b3f07 Leszek Koltunski
39 b547aaba leszek
  private final long mID;
40 da9b3f07 Leszek Koltunski
  private final EffectType mType;
41
  private final EffectName mName;
42 b547aaba leszek
  private final int mDimension;
43 b24e4719 Leszek Koltunski
  private final int mRegionDim;
44
  private final int mCenterDim;
45 8eccf334 Leszek Koltunski
46 7a1fcbeb Leszek Koltunski
  private ArrayList<EffectListener> mListeners =null;
47
  private int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
48 20dbec0e Leszek Koltunski
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 3543a3cf Leszek Koltunski
    mID = (InternalStackFrameList.getNextEffectID()<<EffectType.LENGTH) + mType.ordinal();
89 faa3ff56 Leszek Koltunski
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
/**
93
 * Only for use by the library itself.
94
 *
95
 * @y.exclude
96
 */
97 8d98b65f Leszek Koltunski
  public static void onPause()
98 8eccf334 Leszek Koltunski
    {
99 9af837e8 leszek
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
100 2f1f7570 Leszek Koltunski
101
    MatrixEffect.destroyStatics();
102
    VertexEffect.destroyStatics();
103 143095f7 Leszek Koltunski
    FragmentEffect.destroyStatics();
104 2f1f7570 Leszek Koltunski
    PostprocessEffect.destroyStatics();
105 8eccf334 Leszek Koltunski
    }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108 faa3ff56 Leszek Koltunski
/**
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 8eccf334 Leszek Koltunski
115 20dbec0e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 bc208a9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
144
/**
145
 * Only for use by the library itself.
146
 *
147
 * @y.exclude
148
 */
149 96e3b88a Leszek Koltunski
  public void writeAssociations(int[] intUniforms, int index1, int index2)
150 bc208a9c Leszek Koltunski
    {
151 96e3b88a Leszek Koltunski
    intUniforms[index1] = mAndAssociation;
152
    intUniforms[index2] = mEquAssociation;
153 36d65d88 Leszek Koltunski
    }
154
155 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 da9b3f07 Leszek Koltunski
  public boolean isUnity(float[] buffer, int index)
163 8eccf334 Leszek Koltunski
    {
164 da9b3f07 Leszek Koltunski
    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 b547aaba leszek
    }
183 b82a9ac9 Leszek Koltunski
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 7a1fcbeb Leszek Koltunski
    Method method;
195 b82a9ac9 Leszek Koltunski
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 7a1fcbeb Leszek Koltunski
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
205
                                             // is public
206 b82a9ac9 Leszek Koltunski
          }
207
        catch(NoSuchMethodException ex)
208
          {
209
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
210 7a1fcbeb Leszek Koltunski
          method = null;
211 b82a9ac9 Leszek Koltunski
          }
212
213
        try
214
          {
215 7a1fcbeb Leszek Koltunski
          if( method!=null ) method.invoke(null);
216 b82a9ac9 Leszek Koltunski
          }
217
        catch(Exception ex)
218
          {
219
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
220
          }
221
        }
222
      }
223
    }
224
225 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
226 faa3ff56 Leszek Koltunski
/**
227
 * Return the EffectType enum corresponding to this Effect.
228
 *
229
 * @see EffectType
230
 */
231 da9b3f07 Leszek Koltunski
  public EffectType getType()
232 b547aaba leszek
    {
233
    return mType;
234
    }
235 8eccf334 Leszek Koltunski
236 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
237 faa3ff56 Leszek Koltunski
/**
238
 * Return the EffectName enum corresponding to this Effect.
239
 *
240
 * @see EffectName
241
 */
242 da9b3f07 Leszek Koltunski
  public EffectName getName()
243 6d62a900 Leszek Koltunski
    {
244
    return mName;
245
    }
246
247 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
248 faa3ff56 Leszek Koltunski
/**
249
 * Return the unique ID of this Effect.
250
 */
251 b547aaba leszek
  public long getID()
252
    {
253
    return mID;
254 8eccf334 Leszek Koltunski
    }
255
256 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
257 faa3ff56 Leszek Koltunski
/**
258
 * Return a printable name of this Effect.
259
 */
260 6bb59aad Leszek Koltunski
  public String getString()
261
    {
262 da9b3f07 Leszek Koltunski
    return mName.name();
263 6bb59aad Leszek Koltunski
    }
264
265 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
266 faa3ff56 Leszek Koltunski
/**
267 b24e4719 Leszek Koltunski
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
268 faa3ff56 Leszek Koltunski
 */
269 b24e4719 Leszek Koltunski
  public int getCenterDimension()
270 8eccf334 Leszek Koltunski
    {
271 b24e4719 Leszek Koltunski
    return mCenterDim;
272 8eccf334 Leszek Koltunski
    }
273
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275 faa3ff56 Leszek Koltunski
/**
276 b24e4719 Leszek Koltunski
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
277 faa3ff56 Leszek Koltunski
 */
278 b24e4719 Leszek Koltunski
  public int getRegionDimension()
279 8eccf334 Leszek Koltunski
    {
280 b24e4719 Leszek Koltunski
    return mRegionDim;
281 8eccf334 Leszek Koltunski
    }
282
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284 faa3ff56 Leszek Koltunski
/**
285
 * Return the number of Uniforms needed to describe this effect.
286
 */
287 b24e4719 Leszek Koltunski
  public int getEffectDimension()
288 8eccf334 Leszek Koltunski
    {
289 b547aaba leszek
    return mDimension;
290 8eccf334 Leszek Koltunski
    }
291 20dbec0e Leszek Koltunski
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 8eccf334 Leszek Koltunski
  }