Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 8c57d77b

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