Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 36d65d88

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