Project

General

Profile

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

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

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