Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / TransitionEffect.java @ f291130e

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.DistortedScreen;
24
import org.distorted.library.message.EffectListener;
25
import org.distorted.library.message.EffectMessage;
26
import org.distorted.magic.RubikCube;
27

    
28
import java.lang.reflect.Method;
29

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

    
32
public abstract class TransitionEffect implements EffectListener
33
  {
34
  public enum Type
35
    {
36
    EMPTY      (TransitionEffectEmpty.class    ),
37
    DISAPPEAR  (TransitionEffectDisappear.class),
38
    MOVE       (TransitionEffectMove.class     ),
39
    ROUND      (TransitionEffectRound.class    ),
40
    SCALE      (TransitionEffectScale.class    ),
41
    ;
42

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

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

    
51
  private final int FAKE_EFFECT_ID = -1;
52

    
53
  private int mOldCubeEffectNumber  , mNewCubeEffectNumber;
54
  private int mOldCubeEffectReturned, mNewCubeEffectReturned;
55

    
56
  private EffectListener mListener;
57
  private DistortedScreen mScreen;
58
  private RubikCube mOldCube, mNewCube;
59

    
60
  Effect[] mOldCubeEffects, mNewCubeEffects;
61
  int[] mOldCubeEffectPosition, mNewCubeEffectPosition;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  TransitionEffect()
66
    {
67
    mOldCubeEffectReturned = 0;
68
    mNewCubeEffectReturned = 0;
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
74
    {
75
    //android.util.Log.e("transition", "transition effect "+System.currentTimeMillis());
76

    
77
    for(int i=0; i<mOldCubeEffectNumber; i++)
78
      {
79
      long id = mOldCubeEffects[i].getID();
80

    
81
      if( effectID == id )
82
        {
83
        //android.util.Log.e("transition", i+" old effect "+System.currentTimeMillis());
84

    
85
        mOldCubeEffectReturned++;
86
        mOldCube.remove(id);
87

    
88
        if( mOldCubeEffectReturned == mOldCubeEffectNumber )
89
          {
90
          mScreen.detach(mOldCube);
91
          mOldCube.deregisterForMessages(this);
92

    
93
          for(int j=0; j<mOldCubeEffectNumber; j++)
94
            {
95
            mNewCube.apply(mNewCubeEffects[j],mNewCubeEffectPosition[j]);
96
            }
97
          mScreen.attach(mNewCube);
98
          }
99
        break;
100
        }
101
      }
102

    
103
    for(int i=0; i<mNewCubeEffectNumber; i++)
104
      {
105
      long id = mNewCubeEffects[i].getID();
106

    
107
      if( effectID == id )
108
        {
109
        //android.util.Log.e("transition", i+" new effect "+System.currentTimeMillis());
110

    
111
        mNewCubeEffectReturned++;
112
        mNewCube.remove(id);
113

    
114
        if( mNewCubeEffectReturned == mNewCubeEffectNumber )
115
          {
116
          mNewCube.deregisterForMessages(this);
117
          mListener.effectMessage(null, FAKE_EFFECT_ID, 0);
118
          }
119
        break;
120
        }
121
      }
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public long start(DistortedScreen screen, RubikCube oldCube, RubikCube newCube,EffectListener listener)
127
    {
128
    mScreen   = screen;
129
    mNewCube  = newCube;
130
    mOldCube  = oldCube;
131
    mListener = listener;
132

    
133
    if( mOldCubeEffects!=null )
134
      {
135
      mOldCubeEffectNumber = mOldCubeEffects.length;
136
      }
137
    else
138
      {
139
      throw new RuntimeException("Old Cube Effects not created!");
140
      }
141

    
142
    if( mNewCubeEffects!=null )
143
      {
144
      mNewCubeEffectNumber = mNewCubeEffects.length;
145
      }
146
    else
147
      {
148
      throw new RuntimeException("New Cube Effects not created!");
149
      }
150

    
151
    for(int i=0; i<mOldCubeEffectNumber; i++)
152
      {
153
      mOldCube.apply(mOldCubeEffects[i],mOldCubeEffectPosition[i]);
154
      }
155

    
156
    mOldCube.registerForMessages(this);
157
    mNewCube.registerForMessages(this);
158

    
159
    return FAKE_EFFECT_ID;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public static TransitionEffect create(Type type) throws InstantiationException, IllegalAccessException
165
    {
166
    return type.effectClass.newInstance();
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public static void enableEffects()
172
    {
173
    Method method=null;
174

    
175
    for(Type type: Type.values())
176
      {
177
      Class<? extends TransitionEffect> cls = type.effectClass;
178

    
179
      try
180
        {
181
        method = cls.getMethod("enable");
182
        }
183
      catch(NoSuchMethodException ex)
184
        {
185
        android.util.Log.e("transitionEffect", "exception getting method: "+ex.getMessage());
186
        }
187

    
188
      try
189
        {
190
        method.invoke(null);
191
        }
192
      catch(Exception ex)
193
        {
194
        android.util.Log.e("transitionEffect", "exception invoking method: "+ex.getMessage());
195
        }
196
      }
197
    }
198
  }
(1-1/4)