Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 7958d843

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