Project

General

Profile

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

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

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