Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 43814a57

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.effectqueue.EffectQueue;
23
import org.distorted.library.message.EffectListener;
24

    
25
import java.lang.reflect.Method;
26
import java.util.ArrayList;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Abstract Effect of any type.
31
 */
32
public abstract class Effect
33
  {
34
  private final static int MAX_UNITY_DIM = 4;
35
  private final static int NUM_EFFECTS = EffectName.LENGTH;
36

    
37
  private final long mID;
38
  private final EffectType mType;
39
  private final EffectName mName;
40
  private final int mDimension;
41
  private final int mRegionDim;
42
  private final int mCenterDim;
43

    
44
  private ArrayList<EffectListener> mListeners =null;
45
  private int mNumListeners=0;  // ==mListeners.length(), but we only create mListeners if the first one gets added
46

    
47
  private static long mNextID = 0;
48

    
49
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
50
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
51

    
52
  int mAndAssociation;
53
  int mEquAssociation;
54

    
55
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
56

    
57
  static
58
    {
59
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public abstract void addQueue(EffectQueue queue);
65
  public abstract void remQueue(EffectQueue queue);
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  Effect(EffectName name)
70
    {
71
    mName      = name;
72
    mType      = name.getType();
73
    mDimension = name.getEffectDimension();
74
    mCenterDim = name.getCenterDimension();
75
    mRegionDim = name.getRegionDimension();
76

    
77
    mAndAssociation = 0xffffffff;
78
    mEquAssociation = 0;
79

    
80
    int n = name.ordinal();
81
    float[] u = name.getUnity();
82
    int l = u.length;
83

    
84
    System.arraycopy(u, 0, mUnity, MAX_UNITY_DIM*n, l);
85

    
86
    mUnityDim[n] = l;
87

    
88
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
/**
93
 * Only for use by the library itself.
94
 *
95
 * @y.exclude
96
 */
97
  public static void onDestroy()
98
    {
99
    mNextID = 0;
100

    
101
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
102

    
103
    MatrixEffect.destroyStatics();
104
    VertexEffect.destroyStatics();
105
    FragmentEffect.destroyStatics();
106
    PostprocessEffect.destroyStatics();
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
/**
111
 * Only for use by the library itself.
112
 *
113
 * @y.exclude
114
 */
115
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
/**
119
 * Only for use by the library itself.
120
 *
121
 * @y.exclude
122
 */
123
  public int getNumListeners()
124
    {
125
    return mNumListeners;
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
/**
130
 * Only for use by the library itself.
131
 *
132
 * @y.exclude
133
 */
134
  public EffectListener removeFirstListener()
135
    {
136
    if( mNumListeners>0 )
137
      {
138
      mNumListeners--;
139
      return mListeners.remove(0);
140
      }
141

    
142
    return null;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
/**
147
 * Only for use by the library itself.
148
 *
149
 * @y.exclude
150
 */
151
  public void writeAssociations(int[] intUniforms, int index1, int index2)
152
    {
153
    intUniforms[index1] = mAndAssociation;
154
    intUniforms[index2] = mEquAssociation;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// PUBLIC API
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
162
 * null Effect?
163
 */
164
  public boolean isUnity(float[] buffer, int index)
165
    {
166
    int name = mName.ordinal();
167

    
168
    switch(mUnityDim[name])
169
      {
170
      case 0: return true;
171
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
172
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
173
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
174
      case 3: 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
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
178
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
179
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
180
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
181
      }
182

    
183
    return false;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
188
// but this shouldn't matter.
189
/**
190
 * Enable all effects of a given type.
191
 *
192
 * @param type EffectType to enable.
193
 */
194
  public static void enableEffects(EffectType type)
195
    {
196
    Method method;
197

    
198
    for(EffectName name: EffectName.values())
199
      {
200
      if( name.getType() == type )
201
        {
202
        Class<? extends Effect> cls = name.getEffectClass();
203

    
204
        try
205
          {
206
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
207
                                             // is public
208
          }
209
        catch(NoSuchMethodException ex)
210
          {
211
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
212
          method = null;
213
          }
214

    
215
        try
216
          {
217
          if( method!=null ) method.invoke(null);
218
          }
219
        catch(Exception ex)
220
          {
221
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
222
          }
223
        }
224
      }
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
/**
229
 * Return the EffectType enum corresponding to this Effect.
230
 *
231
 * @see EffectType
232
 */
233
  public EffectType getType()
234
    {
235
    return mType;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Return the EffectName enum corresponding to this Effect.
241
 *
242
 * @see EffectName
243
 */
244
  public EffectName getName()
245
    {
246
    return mName;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
/**
251
 * Return the unique ID of this Effect.
252
 */
253
  public long getID()
254
    {
255
    return mID;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Return a printable name of this Effect.
261
 */
262
  public String getString()
263
    {
264
    return mName.name();
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
/**
269
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
270
 */
271
  public int getCenterDimension()
272
    {
273
    return mCenterDim;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
/**
278
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
279
 */
280
  public int getRegionDimension()
281
    {
282
    return mRegionDim;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
/**
287
 * Return the number of Uniforms needed to describe this effect.
288
 */
289
  public int getEffectDimension()
290
    {
291
    return mDimension;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
/**
296
 * Adds the calling class to the list of Listeners that get notified when this Effect gets 'finished'
297
 * i.e. when the Dynamic inside this Effect reaches its final point and stops moving. This will be sent
298
 * only once, on the first time the Dynamic reaches its final point.
299
 *
300
 * If there's no Dynamic, ths message will never be sent.
301
 *
302
 * @param el A class implementing the EffectListener interface that wants to get notifications.
303
 */
304
  public void notifyWhenFinished(EffectListener el)
305
    {
306
    if( mListeners==null ) mListeners = new ArrayList<>();
307

    
308
    if( !mListeners.contains(el) )
309
      {
310
      mListeners.add(el);
311
      mNumListeners++;
312
      }
313
    }
314
  }
(1-1/32)