Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 20dbec0e

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
  ArrayList<EffectListener> mListeners =null;
44
  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=null;
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");
186
          }
187
        catch(NoSuchMethodException ex)
188
          {
189
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
190
          }
191

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

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

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

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

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

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

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

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

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272
/**
273
 * Adds the calling class to the list of Listeners that get notified when this Effect gets 'finished'
274
 * i.e. when the Dynamic inside this Effect reaches its final point and stops moving. This will be sent
275
 * only once, on the first time the Dynamic reaches its final point.
276
 *
277
 * If there's no Dynamic, ths message will never be sent.
278
 *
279
 * @param el A class implementing the EffectListener interface that wants to get notifications.
280
 */
281
  public void notifyWhenFinished(EffectListener el)
282
    {
283
    if( mListeners==null ) mListeners = new ArrayList<>();
284

    
285
    if( !mListeners.contains(el) )
286
      {
287
      mListeners.add(el);
288
      mNumListeners++;
289
      }
290
    }
291
  }
(1-1/26)