Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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 java.lang.reflect.Method;
23
import android.content.SharedPreferences;
24

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

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

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

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

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

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

    
64
      types= new Type[LENGTH];
65

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

    
73
  ////////////////////////////////////////////////////////////////////////////////
74

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

    
80
  ////////////////////////////////////////////////////////////////////////////////
81

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

    
87
  ////////////////////////////////////////////////////////////////////////////////
88

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

    
94
  ////////////////////////////////////////////////////////////////////////////////
95

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

    
101
  ////////////////////////////////////////////////////////////////////////////////
102

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

    
108
  ////////////////////////////////////////////////////////////////////////////////
109

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

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

    
118
  ////////////////////////////////////////////////////////////////////////////////
119

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

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

    
128
  ////////////////////////////////////////////////////////////////////////////////
129

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

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

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

    
157
      return null;
158
      }
159

    
160
  ////////////////////////////////////////////////////////////////////////////////
161

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

    
167
  ////////////////////////////////////////////////////////////////////////////////
168

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

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

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

    
196
  ////////////////////////////////////////////////////////////////////////////////
197

    
198
    public long startEffect(RubikRenderer renderer)
199
      {
200
      Method method1, method2;
201
      BaseEffect baseEffect=null;
202

    
203
      try
204
        {
205
        method1 = mClass.getDeclaredMethod("create", int.class);
206
        }
207
      catch(NoSuchMethodException ex)
208
        {
209
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 1 exception getting method: "+ex.getMessage());
210
        return -1;
211
        }
212

    
213
      try
214
        {
215
        if( method1!=null )
216
          {
217
          Object value = method1.invoke(null,mCurrentType);
218
          baseEffect = (BaseEffect)value;
219
          }
220
        }
221
      catch(Exception ex)
222
        {
223
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 1 exception invoking method: "+ex.getMessage());
224
        return -2;
225
        }
226

    
227
      try
228
        {
229
        method2 = mClass.getDeclaredMethod("start", int.class, RubikRenderer.class);
230
        }
231
      catch(NoSuchMethodException ex)
232
        {
233
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 2 exception getting method: "+ex.getMessage());
234
        return -3;
235
        }
236

    
237
      try
238
        {
239
        if( method2!=null )
240
          {
241
          Integer translated = translatePos(mCurrentPos)+1;
242
          Object value = method2.invoke(baseEffect,translated,renderer);
243
          return (Long)value;
244
          }
245
        }
246
      catch(Exception ex)
247
        {
248
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 2 exception invoking method: "+ex.getMessage());
249
        }
250

    
251
      return -4;
252
      }
253

    
254
  ////////////////////////////////////////////////////////////////////////////////
255
  // map the (0..100) range of the SeekBar into (0..5000) milliseconds, in 100ms
256
  // increments.
257

    
258
    public static int translatePos(int pos)
259
      {
260
      return (pos/2)*100;
261
      }
262
    }
263

    
264
  // END ENUM ////////////////////////////////////////////////////////////////////
265
  }
    (1-1/1)