Project

General

Profile

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

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

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
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
68
    int n = name.ordinal();
69
    float[] u = name.getUnity();
70
    int l = u.length;
71

    
72
    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
  public static void onDestroy()
89
    {
90
    mNextID = 0;
91

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

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
/**
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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  public boolean isUnity(float[] buffer, int index)
144
    {
145
    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
    }
164

    
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
    Method method;
176

    
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
          method = cls.getMethod("enable");  // getMethod and NOT getDeclaredMethod because enable()
186
                                             // is public
187
          }
188
        catch(NoSuchMethodException ex)
189
          {
190
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
191
          method = null;
192
          }
193

    
194
        try
195
          {
196
          if( method!=null ) method.invoke(null);
197
          }
198
        catch(Exception ex)
199
          {
200
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
201
          }
202
        }
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
/**
208
 * Return the EffectType enum corresponding to this Effect.
209
 *
210
 * @see EffectType
211
 */
212
  public EffectType getType()
213
    {
214
    return mType;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
/**
219
 * Return the EffectName enum corresponding to this Effect.
220
 *
221
 * @see EffectName
222
 */
223
  public EffectName getName()
224
    {
225
    return mName;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
/**
230
 * Return the unique ID of this Effect.
231
 */
232
  public long getID()
233
    {
234
    return mID;
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
/**
239
 * Return a printable name of this Effect.
240
 */
241
  public String getString()
242
    {
243
    return mName.name();
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
/**
248
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
249
 */
250
  public int getCenterDimension()
251
    {
252
    return mCenterDim;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
/**
257
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
258
 */
259
  public int getRegionDimension()
260
    {
261
    return mRegionDim;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
/**
266
 * Return the number of Uniforms needed to describe this effect.
267
 */
268
  public int getEffectDimension()
269
    {
270
    return mDimension;
271
    }
272

    
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
  }
(1-1/26)