Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 96e3b88a

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 2aeb75aa Leszek Koltunski
  int mAndAssociation;
52
  int mEquAssociation;
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 2aeb75aa Leszek Koltunski
    mAndAssociation = 0xffffffff;
72
    mEquAssociation = 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 96e3b88a Leszek Koltunski
  public void writeAssociations(int[] intUniforms, int index1, int index2)
146 bc208a9c Leszek Koltunski
    {
147 96e3b88a Leszek Koltunski
    intUniforms[index1] = mAndAssociation;
148
    intUniforms[index2] = mEquAssociation;
149 36d65d88 Leszek Koltunski
    }
150
151 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// PUBLIC API
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
/**
155
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
156
 * null Effect?
157
 */
158 da9b3f07 Leszek Koltunski
  public boolean isUnity(float[] buffer, int index)
159 8eccf334 Leszek Koltunski
    {
160 da9b3f07 Leszek Koltunski
    int name = mName.ordinal();
161
162
    switch(mUnityDim[name])
163
      {
164
      case 0: return true;
165
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
166
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
167
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
168
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
169
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
170
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
171
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
172
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
173
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
174
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
175
      }
176
177
    return false;
178 b547aaba leszek
    }
179 b82a9ac9 Leszek Koltunski
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
182
// but this shouldn't matter.
183
/**
184
 * Enable all effects of a given type.
185
 *
186
 * @param type EffectType to enable.
187
 */
188
  public static void enableEffects(EffectType type)
189
    {
190 7a1fcbeb Leszek Koltunski
    Method method;
191 b82a9ac9 Leszek Koltunski
192
    for(EffectName name: EffectName.values())
193
      {
194
      if( name.getType() == type )
195
        {
196
        Class<? extends Effect> cls = name.getEffectClass();
197
198
        try
199
          {
200 7a1fcbeb Leszek Koltunski
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
201
                                             // is public
202 b82a9ac9 Leszek Koltunski
          }
203
        catch(NoSuchMethodException ex)
204
          {
205
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
206 7a1fcbeb Leszek Koltunski
          method = null;
207 b82a9ac9 Leszek Koltunski
          }
208
209
        try
210
          {
211 7a1fcbeb Leszek Koltunski
          if( method!=null ) method.invoke(null);
212 b82a9ac9 Leszek Koltunski
          }
213
        catch(Exception ex)
214
          {
215
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
216
          }
217
        }
218
      }
219
    }
220
221 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
222 faa3ff56 Leszek Koltunski
/**
223
 * Return the EffectType enum corresponding to this Effect.
224
 *
225
 * @see EffectType
226
 */
227 da9b3f07 Leszek Koltunski
  public EffectType getType()
228 b547aaba leszek
    {
229
    return mType;
230
    }
231 8eccf334 Leszek Koltunski
232 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
233 faa3ff56 Leszek Koltunski
/**
234
 * Return the EffectName enum corresponding to this Effect.
235
 *
236
 * @see EffectName
237
 */
238 da9b3f07 Leszek Koltunski
  public EffectName getName()
239 6d62a900 Leszek Koltunski
    {
240
    return mName;
241
    }
242
243 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
244 faa3ff56 Leszek Koltunski
/**
245
 * Return the unique ID of this Effect.
246
 */
247 b547aaba leszek
  public long getID()
248
    {
249
    return mID;
250 8eccf334 Leszek Koltunski
    }
251
252 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253 faa3ff56 Leszek Koltunski
/**
254
 * Return a printable name of this Effect.
255
 */
256 6bb59aad Leszek Koltunski
  public String getString()
257
    {
258 da9b3f07 Leszek Koltunski
    return mName.name();
259 6bb59aad Leszek Koltunski
    }
260
261 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262 faa3ff56 Leszek Koltunski
/**
263 b24e4719 Leszek Koltunski
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
264 faa3ff56 Leszek Koltunski
 */
265 b24e4719 Leszek Koltunski
  public int getCenterDimension()
266 8eccf334 Leszek Koltunski
    {
267 b24e4719 Leszek Koltunski
    return mCenterDim;
268 8eccf334 Leszek Koltunski
    }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271 faa3ff56 Leszek Koltunski
/**
272 b24e4719 Leszek Koltunski
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
273 faa3ff56 Leszek Koltunski
 */
274 b24e4719 Leszek Koltunski
  public int getRegionDimension()
275 8eccf334 Leszek Koltunski
    {
276 b24e4719 Leszek Koltunski
    return mRegionDim;
277 8eccf334 Leszek Koltunski
    }
278
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280 faa3ff56 Leszek Koltunski
/**
281
 * Return the number of Uniforms needed to describe this effect.
282
 */
283 b24e4719 Leszek Koltunski
  public int getEffectDimension()
284 8eccf334 Leszek Koltunski
    {
285 b547aaba leszek
    return mDimension;
286 8eccf334 Leszek Koltunski
    }
287 20dbec0e Leszek Koltunski
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
/**
290
 * Adds the calling class to the list of Listeners that get notified when this Effect gets 'finished'
291
 * i.e. when the Dynamic inside this Effect reaches its final point and stops moving. This will be sent
292
 * only once, on the first time the Dynamic reaches its final point.
293
 *
294
 * If there's no Dynamic, ths message will never be sent.
295
 *
296
 * @param el A class implementing the EffectListener interface that wants to get notifications.
297
 */
298
  public void notifyWhenFinished(EffectListener el)
299
    {
300
    if( mListeners==null ) mListeners = new ArrayList<>();
301
302
    if( !mListeners.contains(el) )
303
      {
304
      mListeners.add(el);
305
      mNumListeners++;
306
      }
307
    }
308 8eccf334 Leszek Koltunski
  }