Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.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.RubikCube;
28
import org.distorted.magic.RubikRenderer;
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 RubikCube[] mCube;
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
    mCube               = new RubikCube[NUM_PHASES];
97
    }
98

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

    
101
  public static String[] getNames()
102
    {
103
    String[] names = new String[NUM_EFFECTS];
104

    
105
    for( int i=0; i<NUM_EFFECTS; i++)
106
      {
107
      names[i] = types[i].name();
108
      }
109

    
110
    return names;
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public static SizeChangeEffect create(int ordinal) throws InstantiationException, IllegalAccessException
116
    {
117
    return types[ordinal].effect.newInstance();
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  abstract int createEffectsPhase0(int duration);
123
  abstract int createEffectsPhase1(int duration);
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  public void effectFinished(final long effectID)
128
    {
129
    if( mPhaseActive[0] ) effectFinishedPhase(effectID,0);
130
    if( mPhaseActive[1] ) effectFinishedPhase(effectID,1);
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private void effectFinishedPhase(final long effectID, int phase)
136
    {
137
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
138
      {
139
      long id = mCubeEffects[phase][i].getID();
140

    
141
      if( effectID == id )
142
        {
143
        effectReturned(phase);
144
        mCube[phase].remove(id);
145
        return;
146
        }
147
      }
148
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
149
      {
150
      long id = mNodeEffects[phase][i].getID();
151

    
152
      if( effectID == id )
153
        {
154
        effectReturned(phase);
155
        mCube[phase].getEffects().abortById(id);
156
        return;
157
        }
158
      }
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void effectReturned(int phase)
164
    {
165
    mEffectReturned[phase]++;
166

    
167
    if( mEffectReturned[phase] == mEffectFinished[phase] )
168
      {
169
      switch(phase)
170
        {
171
        case 0: mPhaseActive[1] = true;
172
                mEffectFinished[1] = createEffectsPhase1(mDuration);
173
                assignEffects(1);
174
                mScreen.attach(mCube[1]);
175
                break;
176
        case 1: mListener.effectFinished(FAKE_EFFECT_ID);
177
                break;
178
        }
179
      }
180
    if( mEffectReturned[phase] == mCubeEffectNumber[phase]+mNodeEffectNumber[phase] )
181
      {
182
      switch(phase)
183
        {
184
        case 0: mPhaseActive[0] = false;
185
                mScreen.detach(mCube[0]);
186
                break;
187
        case 1: mPhaseActive[1] = false;
188
                break;
189
        }
190
      }
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public long start(int duration, RubikRenderer renderer)
196
    {
197
    mScreen   = renderer.getScreen();
198
    mCube[0]  = renderer.getOldCube();
199
    mCube[1]  = renderer.getCube();
200
    mListener = renderer;
201
    mDuration = duration;
202

    
203
    if( mCube[0]!=null )
204
      {
205
      mPhaseActive[0] = true;
206
      mEffectFinished[0] = createEffectsPhase0(mDuration);
207
      assignEffects(0);
208
      }
209
    else
210
      {
211
      mPhaseActive[1] = true;
212
      mEffectFinished[1] = createEffectsPhase1(mDuration);
213
      assignEffects(1);
214
      mScreen.attach(mCube[1]);
215
      }
216

    
217
    return FAKE_EFFECT_ID;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  private void assignEffects(int phase)
223
    {
224
    mCubeEffectNumber[phase] = ( mCubeEffects[phase]!=null ) ? mCubeEffects[phase].length : 0;
225
    mNodeEffectNumber[phase] = ( mNodeEffects[phase]!=null ) ? mNodeEffects[phase].length : 0;
226

    
227
    if( mCubeEffectNumber[phase]==0 && mNodeEffectNumber[phase]==0 )
228
      {
229
      throw new RuntimeException("Cube and Node Effects ("+phase+" phase) both not created!");
230
      }
231

    
232
    for(int i=0; i<mCubeEffectNumber[phase]; i++)
233
      {
234
      mCube[phase].apply(mCubeEffects[phase][i],mCubeEffectPosition[phase][i]);
235
      mCubeEffects[phase][i].notifyWhenFinished(this);
236
      }
237

    
238
    DistortedEffects nodeEffects = mCube[phase].getEffects();
239

    
240
    for(int i=0; i<mNodeEffectNumber[phase]; i++)
241
      {
242
      nodeEffects.apply(mNodeEffects[phase][i],mNodeEffectPosition[phase][i]);
243
      mNodeEffects[phase][i].notifyWhenFinished(this);
244
      }
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public static void enableEffects()
250
    {
251
    Method method;
252

    
253
    for(Type type: Type.values())
254
      {
255
      try
256
        {
257
        method = type.effect.getDeclaredMethod("enable");  // enable not public, thus getDeclaredMethod
258
        }
259
      catch(NoSuchMethodException ex)
260
        {
261
        android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception getting method: "+ex.getMessage());
262
        method = null;
263
        }
264

    
265
      try
266
        {
267
        if( method!=null ) method.invoke(null);
268
        }
269
      catch(Exception ex)
270
        {
271
        android.util.Log.e("SizeChangeEffect", type.effect.getSimpleName()+": exception invoking method: "+ex.getMessage());
272
        }
273
      }
274
    }
275
}
(1-1/6)