Project

General

Profile

Download (9.37 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / effect / sizechange / SizeChangeEffect.java @ 8becce57

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.effect.sizechange;
21

    
22
import org.distorted.effect.BaseEffect;
23
import org.distorted.library.effect.Effect;
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedScreen;
26
import org.distorted.library.message.EffectListener;
27
import org.distorted.magic.RubikPostRender;
28
import org.distorted.object.RubikObject;
29

    
30
import java.lang.reflect.Method;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public abstract class SizeChangeEffect extends BaseEffect implements EffectListener
35
{
36
  public enum Type
37
    {
38
    NONE         (SizeChangeEffectNone.class        ),
39
    TRANSPARENCY (SizeChangeEffectTransparency.class),
40
    MOVE         (SizeChangeEffectMove.class        ),
41
    ROUND        (SizeChangeEffectRound.class       ),
42
    SCALE        (SizeChangeEffectScale.class       ),
43
    ;
44

    
45
    final Class<? extends SizeChangeEffect> effect;
46

    
47
    Type(Class<? extends SizeChangeEffect> effect)
48
      {
49
      this.effect= effect;
50
      }
51
    }
52

    
53
  private static int NUM_EFFECTS = Type.values().length;
54
  private static final int NUM_PHASES  = 2;
55
  private static final int FAKE_EFFECT_ID  = -1;
56
  private static final Type[] types;
57

    
58
  static
59
    {
60
    int i=0;
61
    types = new Type[NUM_EFFECTS];
62

    
63
    for(Type type: Type.values())
64
      {
65
      types[i++] = type;
66
      }
67
    }
68

    
69
  private EffectListener mListener;
70
  private int mDuration;
71
  private int[] mEffectReturned;
72
  private int[] mCubeEffectNumber, mNodeEffectNumber;
73
  private int[] mEffectFinished;
74
  private boolean[] mPhaseActive;
75
  private RubikObject[] mObject;
76

    
77
  DistortedScreen mScreen;
78
  Effect[][] mCubeEffects;
79
  int[][] mCubeEffectPosition;
80
  Effect[][] mNodeEffects;
81
  int[][] mNodeEffectPosition;
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  SizeChangeEffect()
86
    {
87
    mPhaseActive        = new boolean[NUM_PHASES];
88
    mEffectReturned     = new int[NUM_PHASES];
89
    mCubeEffectNumber   = new int[NUM_PHASES];
90
    mNodeEffectNumber   = new int[NUM_PHASES];
91
    mEffectFinished     = new int[NUM_PHASES];
92
    mCubeEffectPosition = new int[NUM_PHASES][];
93
    mNodeEffectPosition = new int[NUM_PHASES][];
94
    mCubeEffects        = new Effect[NUM_PHASES][];
95
    mNodeEffects        = new Effect[NUM_PHASES][];
96
    mObject             = new RubikObject[NUM_PHASES];
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  abstract int createEffectsPhase0(int duration);
102
  abstract int createEffectsPhase1(int duration);
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  private void effectFinishedPhase(final long effectID, int phase)
107
    {
108
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
109
      {
110
      long id = mCubeEffects[phase][i].getID();
111

    
112
      if( effectID == id )
113
        {
114
        effectReturned(phase);
115
        mObject[phase].remove(id);
116
        return;
117
        }
118
      }
119
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
120
      {
121
      long id = mNodeEffects[phase][i].getID();
122

    
123
      if( effectID == id )
124
        {
125
        effectReturned(phase);
126
        mObject[phase].getEffects().abortById(id);
127
        return;
128
        }
129
      }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private void effectReturned(int phase)
135
    {
136
    mEffectReturned[phase]++;
137

    
138
    if( mEffectReturned[phase] == mEffectFinished[phase] )
139
      {
140
      switch(phase)
141
        {
142
        case 0: mPhaseActive[1] = true;
143
                mEffectFinished[1] = createEffectsPhase1(mDuration);
144
                assignEffects(1);
145
                mScreen.attach(mObject[1]);
146
                break;
147
        case 1: mListener.effectFinished(FAKE_EFFECT_ID);
148
                break;
149
        }
150
      }
151
    if( mEffectReturned[phase] == mCubeEffectNumber[phase]+mNodeEffectNumber[phase] )
152
      {
153
      switch(phase)
154
        {
155
        case 0: mPhaseActive[0] = false;
156
                mScreen.detach(mObject[0]);
157
                break;
158
        case 1: mPhaseActive[1] = false;
159
                break;
160
        }
161
      }
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void assignEffects(int phase)
167
    {
168
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
169
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
170

    
171
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
172
      {
173
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
174
      }
175

    
176
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
177
      {
178
      mObject[phase].apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
179
      mCubeEffects[phase][i].notifyWhenFinished(this);
180
      }
181

    
182
    DistortedEffects nodeEffects = mObject[phase].getEffects();
183

    
184
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
185
      {
186
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
187
      mNodeEffects[phase][i].notifyWhenFinished(this);
188
      }
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
// PUBLIC API
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public void effectFinished(final long effectID)
196
    {
197
    if( mPhaseActive[0] ) effectFinishedPhase(effectID,0);
198
    if( mPhaseActive[1] ) effectFinishedPhase(effectID,1);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  @SuppressWarnings("unused")
204
  public static String[] getNames()
205
    {
206
    String[] names = new String[NUM_EFFECTS];
207

    
208
    for( int i=0; i<NUM_EFFECTS; i++)
209
      {
210
      names[i] = types[i].name();
211
      }
212

    
213
    return names;
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  @SuppressWarnings("unused")
219
  public static SizeChangeEffect create(int ordinal) throws InstantiationException, IllegalAccessException
220
    {
221
    return types[ordinal].effect.newInstance();
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  @SuppressWarnings("unused")
227
  public long start(int duration, DistortedScreen screen, RubikPostRender post)
228
    {
229
    mScreen   = screen;
230
    mObject[0]= post.getOldObject();
231
    mObject[1]= post.getObject();
232
    mListener = post;
233
    mDuration = duration;
234

    
235
    if( mObject[0]!=null )
236
      {
237
      mPhaseActive[0] = true;
238
      mEffectFinished[0] = createEffectsPhase0(mDuration);
239
      assignEffects(0);
240
      }
241
    else
242
      {
243
      mPhaseActive[1] = true;
244
      mEffectFinished[1] = createEffectsPhase1(mDuration);
245
      assignEffects(1);
246
      mScreen.attach(mObject[1]);
247
      }
248

    
249
    return FAKE_EFFECT_ID;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  @SuppressWarnings("unused")
255
  public static void enableEffects()
256
    {
257
    Method method;
258

    
259
    for(Type type: Type.values())
260
      {
261
      try
262
        {
263
        method = type.effect.getDeclaredMethod("enable");  // enable not public, thus getDeclaredMethod
264
        }
265
      catch(NoSuchMethodException ex)
266
        {
267
        android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
268
        method = null;
269
        }
270

    
271
      try
272
        {
273
        if( method!=null ) method.invoke(null);
274
        }
275
      catch(Exception ex)
276
        {
277
        android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
278
        }
279
      }
280
    }
281
}
(1-1/6)