Project

General

Profile

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

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

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