Project

General

Profile

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

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

1 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
23 faa3ff56 Leszek Koltunski
/**
24
 * Abstract Effect of any type.
25
 */
26 8eccf334 Leszek Koltunski
public abstract class Effect
27
  {
28 da9b3f07 Leszek Koltunski
  private final static int MAX_UNITY_DIM = 4;
29 c3651001 Leszek Koltunski
  private final static int NUM_EFFECTS = EffectName.LENGTH;
30 da9b3f07 Leszek Koltunski
31 b547aaba leszek
  private final long mID;
32 da9b3f07 Leszek Koltunski
  private final EffectType mType;
33
  private final EffectName mName;
34 b547aaba leszek
  private final int mDimension;
35
  private final boolean mSupportsR;
36
  private final boolean mSupportsC;
37 8eccf334 Leszek Koltunski
38
  private static long mNextID = 0;
39
40 da9b3f07 Leszek Koltunski
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
41
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
42 8eccf334 Leszek Koltunski
43 9af837e8 leszek
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
44
45
  static
46
    {
47
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
48
    }
49
50 15aa7d94 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 faa3ff56 Leszek Koltunski
  Effect(EffectName name)
53
    {
54
    mName      = name;
55
    mType      = name.getType();
56
    mDimension = name.getDimension();
57
    mSupportsC = name.supportsCenter();
58
    mSupportsR = name.supportsRegion();
59 15aa7d94 Leszek Koltunski
60 faa3ff56 Leszek Koltunski
    int n = name.ordinal();
61
    float[] u = name.getUnity();
62
    int l = name.getUnity().length;
63 8eccf334 Leszek Koltunski
64 faa3ff56 Leszek Koltunski
    for(int i=0; i<l; i++)
65
      {
66
      mUnity[n*MAX_UNITY_DIM+i] = u[i];
67
      }
68
69
    mUnityDim[n] = l;
70
71
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
72
    }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
/**
76
 * Only for use by the library itself.
77
 *
78
 * @y.exclude
79
 */
80 b547aaba leszek
  public static void onDestroy()
81 8eccf334 Leszek Koltunski
    {
82 b547aaba leszek
    mNextID = 0;
83 9af837e8 leszek
84
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
85 8eccf334 Leszek Koltunski
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88 faa3ff56 Leszek Koltunski
/**
89
 * Only for use by the library itself.
90
 *
91
 * @y.exclude
92
 */
93
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
94 8eccf334 Leszek Koltunski
95 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// PUBLIC API
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
/**
99
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
100
 * null Effect?
101
 */
102 da9b3f07 Leszek Koltunski
  public boolean isUnity(float[] buffer, int index)
103 8eccf334 Leszek Koltunski
    {
104 da9b3f07 Leszek Koltunski
    int name = mName.ordinal();
105
106
    switch(mUnityDim[name])
107
      {
108
      case 0: return true;
109
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
110
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
111
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
112
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
113
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
114
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
115
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
116
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
117
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
118
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
119
      }
120
121
    return false;
122 b547aaba leszek
    }
123 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124 faa3ff56 Leszek Koltunski
/**
125
 * Return the EffectType enum corresponding to this Effect.
126
 *
127
 * @see EffectType
128
 */
129 da9b3f07 Leszek Koltunski
  public EffectType getType()
130 b547aaba leszek
    {
131
    return mType;
132
    }
133 8eccf334 Leszek Koltunski
134 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135 faa3ff56 Leszek Koltunski
/**
136
 * Return the EffectName enum corresponding to this Effect.
137
 *
138
 * @see EffectName
139
 */
140 da9b3f07 Leszek Koltunski
  public EffectName getName()
141 6d62a900 Leszek Koltunski
    {
142
    return mName;
143
    }
144
145 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
146 faa3ff56 Leszek Koltunski
/**
147
 * Return the unique ID of this Effect.
148
 */
149 b547aaba leszek
  public long getID()
150
    {
151
    return mID;
152 8eccf334 Leszek Koltunski
    }
153
154 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155 faa3ff56 Leszek Koltunski
/**
156
 * Return a printable name of this Effect.
157
 */
158 6bb59aad Leszek Koltunski
  public String getString()
159
    {
160 da9b3f07 Leszek Koltunski
    return mName.name();
161 6bb59aad Leszek Koltunski
    }
162
163 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
164 faa3ff56 Leszek Koltunski
/**
165
 * Does this effect have a Center?
166
 */
167 b547aaba leszek
  public boolean supportsCenter()
168 8eccf334 Leszek Koltunski
    {
169 b547aaba leszek
    return mSupportsC;
170 8eccf334 Leszek Koltunski
    }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173 faa3ff56 Leszek Koltunski
/**
174
 * Does this effect support being masked by a Region?
175
 */
176 b547aaba leszek
  public boolean supportsRegion()
177 8eccf334 Leszek Koltunski
    {
178 b547aaba leszek
    return mSupportsR;
179 8eccf334 Leszek Koltunski
    }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182 faa3ff56 Leszek Koltunski
/**
183
 * Return the number of Uniforms needed to describe this effect.
184
 */
185 b547aaba leszek
  public int getDimension()
186 8eccf334 Leszek Koltunski
    {
187 b547aaba leszek
    return mDimension;
188 8eccf334 Leszek Koltunski
    }
189
  }