Project

General

Profile

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

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

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 mAssociation;
52

    
53
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
54

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  Effect(EffectName name)
63
    {
64
    mName      = name;
65
    mType      = name.getType();
66
    mDimension = name.getEffectDimension();
67
    mCenterDim = name.getCenterDimension();
68
    mRegionDim = name.getRegionDimension();
69

    
70
    mAssociation = 0xffffffff;
71

    
72
    int n = name.ordinal();
73
    float[] u = name.getUnity();
74
    int l = u.length;
75

    
76
    System.arraycopy(u, 0, mUnity, MAX_UNITY_DIM*n, l);
77

    
78
    mUnityDim[n] = l;
79

    
80
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
/**
85
 * Only for use by the library itself.
86
 *
87
 * @y.exclude
88
 */
89
  public static void onDestroy()
90
    {
91
    mNextID = 0;
92

    
93
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
94

    
95
    MatrixEffect.destroyStatics();
96
    VertexEffect.destroyStatics();
97
    FragmentEffect.destroyStatics();
98
    PostprocessEffect.destroyStatics();
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
/**
103
 * Only for use by the library itself.
104
 *
105
 * @y.exclude
106
 */
107
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
/**
111
 * Only for use by the library itself.
112
 *
113
 * @y.exclude
114
 */
115
  public int getNumListeners()
116
    {
117
    return mNumListeners;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
/**
122
 * Only for use by the library itself.
123
 *
124
 * @y.exclude
125
 */
126
  public EffectListener removeFirstListener()
127
    {
128
    if( mNumListeners>0 )
129
      {
130
      mNumListeners--;
131
      return mListeners.remove(0);
132
      }
133

    
134
    return null;
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
/**
139
 * Only for use by the library itself.
140
 *
141
 * @y.exclude
142
 */
143
  public int getAssociation()
144
    {
145
    return mAssociation;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// PUBLIC API
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
153
 * null Effect?
154
 */
155
  public boolean isUnity(float[] buffer, int index)
156
    {
157
    int name = mName.ordinal();
158

    
159
    switch(mUnityDim[name])
160
      {
161
      case 0: return true;
162
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
163
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
164
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
165
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
166
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
167
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
168
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
169
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
170
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
171
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
172
      }
173

    
174
    return false;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
179
// but this shouldn't matter.
180
/**
181
 * Enable all effects of a given type.
182
 *
183
 * @param type EffectType to enable.
184
 */
185
  public static void enableEffects(EffectType type)
186
    {
187
    Method method;
188

    
189
    for(EffectName name: EffectName.values())
190
      {
191
      if( name.getType() == type )
192
        {
193
        Class<? extends Effect> cls = name.getEffectClass();
194

    
195
        try
196
          {
197
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
198
                                             // is public
199
          }
200
        catch(NoSuchMethodException ex)
201
          {
202
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
203
          method = null;
204
          }
205

    
206
        try
207
          {
208
          if( method!=null ) method.invoke(null);
209
          }
210
        catch(Exception ex)
211
          {
212
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
213
          }
214
        }
215
      }
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
/**
220
 * Return the EffectType enum corresponding to this Effect.
221
 *
222
 * @see EffectType
223
 */
224
  public EffectType getType()
225
    {
226
    return mType;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
/**
231
 * Return the EffectName enum corresponding to this Effect.
232
 *
233
 * @see EffectName
234
 */
235
  public EffectName getName()
236
    {
237
    return mName;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
/**
242
 * Return the unique ID of this Effect.
243
 */
244
  public long getID()
245
    {
246
    return mID;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
/**
251
 * Return a printable name of this Effect.
252
 */
253
  public String getString()
254
    {
255
    return mName.name();
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
261
 */
262
  public int getCenterDimension()
263
    {
264
    return mCenterDim;
265
    }
266

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
/**
278
 * Return the number of Uniforms needed to describe this effect.
279
 */
280
  public int getEffectDimension()
281
    {
282
    return mDimension;
283
    }
284

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

    
299
    if( !mListeners.contains(el) )
300
      {
301
      mListeners.add(el);
302
      mNumListeners++;
303
      }
304
    }
305
  }
(1-1/31)