Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / DisappearEffect.java @ 4fd9efa1

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;
21

    
22
import org.distorted.library.effect.Effect;
23
import org.distorted.library.main.DistortedEffects;
24
import org.distorted.library.main.DistortedScreen;
25
import org.distorted.library.message.EffectListener;
26
import org.distorted.magic.RubikCube;
27

    
28
import java.lang.reflect.Method;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public abstract class DisappearEffect implements EffectListener
33
  {
34
  public enum Type
35
    {
36
    NONE          (DisappearEffectNone.class       ),
37
    TRANSPARENCY  (DisappearEffectTransparency.class),
38
    MOVE          (DisappearEffectMove.class        ),
39
    ROUND         (DisappearEffectRound.class       ),
40
    SCALE         (DisappearEffectScale.class       ),
41
    ;
42

    
43
    private final Class<? extends DisappearEffect> effectClass;
44

    
45
    Type(Class<? extends DisappearEffect> effectClass)
46
      {
47
      this.effectClass = effectClass;
48
      }
49
    }
50

    
51
  public static final int NUM_EFFECTS = Type.values().length;
52
  private static final Type[] types;
53

    
54
  static
55
    {
56
    int i=0;
57

    
58
    types = new Type[NUM_EFFECTS];
59

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

    
67
  private final int FAKE_EFFECT_ID = -2;
68

    
69
  private EffectListener mListener;
70
  private RubikCube mCube;
71
  private int mCubeEffectNumber, mNodeEffectNumber, mEffectFinished, mEffectReturned;
72

    
73
  DistortedScreen mScreen;
74
  Effect[] mCubeEffects;
75
  int[] mCubeEffectPosition;
76
  Effect[] mNodeEffects;
77
  int[] mNodeEffectPosition;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  DisappearEffect()
82
    {
83
    mEffectReturned = 0;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  abstract int createEffects(int duration);
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  public static Type getType(int ordinal)
93
    {
94
    return types[ordinal];
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public void effectFinished(final long effectID)
100
    {
101
    for(int i=0; i<mCubeEffectNumber; i++)
102
      {
103
      long id = mCubeEffects[i].getID();
104

    
105
      if( effectID == id )
106
        {
107
        effectReturned();
108
        mCube.remove(id);
109
        break;
110
        }
111
      }
112
    for(int i=0; i<mNodeEffectNumber; i++)
113
      {
114
      long id = mNodeEffects[i].getID();
115

    
116
      if( effectID == id )
117
        {
118
        effectReturned();
119
        mCube.getEffects().abortById(id);
120
        break;
121
        }
122
      }
123
    }
124

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

    
127
  private void effectReturned()
128
    {
129
    mEffectReturned++;
130

    
131
    if( mEffectReturned == mEffectFinished )
132
      {
133
      mListener.effectFinished(FAKE_EFFECT_ID);
134
      }
135

    
136
    if( mEffectReturned == mCubeEffectNumber+mNodeEffectNumber )
137
      {
138
      mScreen.detach(mCube);
139
      }
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public long start(int duration, DistortedScreen screen, RubikCube cube, EffectListener listener)
145
    {
146
    mScreen   = screen;
147
    mCube     = cube;
148
    mListener = listener;
149

    
150
    mEffectFinished = createEffects(duration);
151

    
152
    mCubeEffectNumber = ( mCubeEffects!=null ) ? mCubeEffects.length : 0;
153
    mNodeEffectNumber = ( mNodeEffects!=null ) ? mNodeEffects.length : 0;
154

    
155
    if( mCubeEffectNumber==0 && mNodeEffectNumber==0 )
156
      {
157
      throw new RuntimeException("Cube and Node Effects both not created!");
158
      }
159

    
160
    for(int i=0; i<mCubeEffectNumber; i++)
161
      {
162
      mCube.apply(mCubeEffects[i],mCubeEffectPosition[i]);
163
      mCubeEffects[i].notifyWhenFinished(this);
164
      }
165

    
166
    DistortedEffects nodeEffects = mCube.getEffects();
167

    
168
    for(int i=0; i<mNodeEffectNumber; i++)
169
      {
170
      nodeEffects.apply(mNodeEffects[i],mNodeEffectPosition[i]);
171
      mNodeEffects[i].notifyWhenFinished(this);
172
      }
173

    
174
    return FAKE_EFFECT_ID;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  public static DisappearEffect create(Type type) throws InstantiationException, IllegalAccessException
180
    {
181
    return type.effectClass.newInstance();
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public static void enableEffects()
187
    {
188
    Method method=null;
189

    
190
    for(Type type: Type.values())
191
      {
192
      Class<? extends DisappearEffect> cls = type.effectClass;
193

    
194
      try
195
        {
196
        method = cls.getMethod("enable");
197
        }
198
      catch(NoSuchMethodException ex)
199
        {
200
        android.util.Log.e("transitionEffect", "exception getting method: "+ex.getMessage());
201
        }
202

    
203
      try
204
        {
205
        method.invoke(null);
206
        }
207
      catch(Exception ex)
208
        {
209
        android.util.Log.e("transitionEffect", "exception invoking method: "+ex.getMessage());
210
        }
211
      }
212
    }
213
  }
(7-7/12)