Project

General

Profile

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

magiccube / src / main / java / org / distorted / effect / BaseEffect.java @ efef689c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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;
21

    
22
import java.lang.reflect.InvocationTargetException;
23
import java.lang.reflect.Method;
24
import android.content.SharedPreferences;
25

    
26
import org.distorted.effect.scramble.ScrambleEffect;
27
import org.distorted.effect.sizechange.SizeChangeEffect;
28
import org.distorted.effect.solve.SolveEffect;
29
import org.distorted.effect.win.WinEffect;
30
import org.distorted.magic.R;
31
import org.distorted.magic.RubikRenderer;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class BaseEffect
36
  {
37
  public enum Type
38
    {
39
    SIZECHANGE  ( 20, 1, R.string.sizechange_effect , SizeChangeEffect.class),
40
    SOLVE       ( 20, 1, R.string.solve_effect      , SolveEffect.class     ),
41
    SCRAMBLE    ( 60, 1, R.string.scramble_effect   , ScrambleEffect.class  ),
42
    WIN         ( 20, 1, R.string.win_effect        , WinEffect.class       ),
43
    ;
44

    
45
    private final int mDefaultPos, mDefaultType;
46
    private final Class<? extends BaseEffect> mClass;
47
    private int mCurrentPos, mCurrentType;
48
    private int mText;
49

    
50
    Type(int dPos, int dType, int text, Class<? extends BaseEffect> clazz )
51
      {
52
      mDefaultPos  = mCurrentPos = dPos;
53
      mDefaultType = mCurrentType= dType;
54
      mText        = text;
55
      mClass       = clazz;
56
      }
57

    
58
    public static final int LENGTH = Type.values().length;
59
    private static final Type[] types;  // copy the values() to a local variable so that we
60
                                        // don't have to keep recreating the array every time
61
    static
62
      {
63
      int i=0;
64

    
65
      types= new Type[LENGTH];
66

    
67
      for(Type type: Type.values())
68
        {
69
        types[i] = type;
70
        i++;
71
        }
72
      }
73

    
74
  ////////////////////////////////////////////////////////////////////////////////
75

    
76
    public int getText()
77
      {
78
      return mText;
79
      }
80

    
81
  ////////////////////////////////////////////////////////////////////////////////
82

    
83
    public int getCurrentPos()
84
      {
85
      return mCurrentPos;
86
      }
87

    
88
  ////////////////////////////////////////////////////////////////////////////////
89

    
90
    public int getCurrentType()
91
      {
92
      return mCurrentType;
93
      }
94

    
95
  ////////////////////////////////////////////////////////////////////////////////
96

    
97
    public void setCurrentPos(int pos)
98
      {
99
      mCurrentPos = pos;
100
      }
101

    
102
  ////////////////////////////////////////////////////////////////////////////////
103

    
104
    public void setCurrentType(int type)
105
      {
106
      mCurrentType = type;
107
      }
108

    
109
  ////////////////////////////////////////////////////////////////////////////////
110

    
111
    public void savePreferences(SharedPreferences.Editor editor)
112
      {
113
      String name = name();
114

    
115
      editor.putInt(name+"_Pos" , mCurrentPos );
116
      editor.putInt(name+"_Type", mCurrentType);
117
      }
118

    
119
  ////////////////////////////////////////////////////////////////////////////////
120

    
121
    public void restorePreferences(SharedPreferences preferences)
122
      {
123
      String name = name();
124

    
125
      mCurrentPos  = preferences.getInt(name+"_Pos" , mDefaultPos );
126
      mCurrentType = preferences.getInt(name+"_Type", mDefaultType);
127
      }
128

    
129
  ////////////////////////////////////////////////////////////////////////////////
130

    
131
    public String[] getNames()
132
      {
133
      Method method;
134

    
135
      try
136
        {
137
        method = mClass.getDeclaredMethod("getNames");
138
        }
139
      catch(NoSuchMethodException ex)
140
        {
141
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
142
        method = null;
143
        }
144

    
145
      try
146
        {
147
        if( method!=null )
148
          {
149
          Object value = method.invoke(null);
150
          return (String[]) value;
151
          }
152
        }
153
      catch(Exception ex)
154
        {
155
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
156
        }
157

    
158
      return null;
159
      }
160

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

    
163
    public static Type getType(int ordinal)
164
      {
165
      return types[ordinal];
166
      }
167

    
168
  ////////////////////////////////////////////////////////////////////////////////
169

    
170
    public static void enableEffects()
171
      {
172
      Method method;
173

    
174
      for(Type type: values())
175
        {
176
        try
177
          {
178
          method = type.mClass.getDeclaredMethod("enableEffects");
179
          }
180
        catch(NoSuchMethodException ex)
181
          {
182
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
183
          method = null;
184
          }
185

    
186
        try
187
          {
188
          if( method!=null ) method.invoke(null);
189
          }
190
        catch(Exception ex)
191
          {
192
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
193
          }
194
        }
195
      }
196

    
197
  ////////////////////////////////////////////////////////////////////////////////
198

    
199
    public long startEffect(RubikRenderer renderer) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
200
      {
201
      Method method1 = mClass.getDeclaredMethod("create", int.class);
202

    
203
      Object value1 = method1.invoke(null,mCurrentType);
204
      BaseEffect baseEffect = (BaseEffect)value1;
205

    
206
      Method method2 = mClass.getDeclaredMethod("start", int.class, RubikRenderer.class);
207

    
208
      Integer translated = translatePos(mCurrentPos)+1;
209
      Object value2 = method2.invoke(baseEffect,translated,renderer);
210
      return (Long)value2;
211
      }
212

    
213
  ////////////////////////////////////////////////////////////////////////////////
214
  // map the (0..100) range of the SeekBar into (0..5000) milliseconds, in 100ms
215
  // increments.
216

    
217
    public static int translatePos(int pos)
218
      {
219
      return (pos/2)*100;
220
      }
221
    }
222

    
223
  // END ENUM ////////////////////////////////////////////////////////////////////
224
  }
    (1-1/1)