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