Project

General

Profile

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

magiccube / src / main / java / org / distorted / effects / BaseEffect.java @ 3f7a4363

1 64975793 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 64975793 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 64975793 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 64975793 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 64975793 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.effects;
21 64975793 Leszek Koltunski
22 beb325a0 Leszek Koltunski
import java.lang.reflect.InvocationTargetException;
23 64975793 Leszek Koltunski
import java.lang.reflect.Method;
24
import android.content.SharedPreferences;
25
26 3f7a4363 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
27
28 1f9772f3 Leszek Koltunski
import org.distorted.effects.scramble.ScrambleEffect;
29 f61fc32c Leszek Koltunski
import org.distorted.effects.objectchange.ObjectChangeEffect;
30 1f9772f3 Leszek Koltunski
import org.distorted.effects.solve.SolveEffect;
31
import org.distorted.effects.win.WinEffect;
32
import org.distorted.main.R;
33 64975793 Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36
public class BaseEffect
37
  {
38
  public enum Type
39
    {
40 f61fc32c Leszek Koltunski
    SIZECHANGE  ( 20, 1, R.string.objectchange_effect, ObjectChangeEffect.class),
41 64975793 Leszek Koltunski
    SOLVE       ( 20, 1, R.string.solve_effect      , SolveEffect.class     ),
42 e4db5995 Leszek Koltunski
    SCRAMBLE    ( 60, 1, R.string.scramble_effect   , ScrambleEffect.class  ),
43 ebb64a1d Leszek Koltunski
    WIN         ( 20, 1, R.string.win_effect        , WinEffect.class       ),
44 64975793 Leszek Koltunski
    ;
45
46
    private final int mDefaultPos, mDefaultType;
47 ebb64a1d Leszek Koltunski
    private final Class<? extends BaseEffect> mClass;
48 64975793 Leszek Koltunski
    private int mCurrentPos, mCurrentType;
49
    private int mText;
50
51 ebb64a1d Leszek Koltunski
    Type(int dPos, int dType, int text, Class<? extends BaseEffect> clazz )
52 64975793 Leszek Koltunski
      {
53
      mDefaultPos  = mCurrentPos = dPos;
54
      mDefaultType = mCurrentType= dType;
55
      mText        = text;
56
      mClass       = clazz;
57
      }
58
59
    public static final int LENGTH = Type.values().length;
60
    private static final Type[] types;  // copy the values() to a local variable so that we
61
                                        // don't have to keep recreating the array every time
62
    static
63
      {
64
      int i=0;
65
66
      types= new Type[LENGTH];
67
68
      for(Type type: Type.values())
69
        {
70
        types[i] = type;
71
        i++;
72
        }
73
      }
74
75
  ////////////////////////////////////////////////////////////////////////////////
76
77
    public int getText()
78
      {
79
      return mText;
80
      }
81
82
  ////////////////////////////////////////////////////////////////////////////////
83
84
    public int getCurrentPos()
85
      {
86
      return mCurrentPos;
87
      }
88
89
  ////////////////////////////////////////////////////////////////////////////////
90
91
    public int getCurrentType()
92
      {
93
      return mCurrentType;
94
      }
95
96
  ////////////////////////////////////////////////////////////////////////////////
97
98
    public void setCurrentPos(int pos)
99
      {
100
      mCurrentPos = pos;
101
      }
102
103
  ////////////////////////////////////////////////////////////////////////////////
104
105
    public void setCurrentType(int type)
106
      {
107
      mCurrentType = type;
108
      }
109
110
  ////////////////////////////////////////////////////////////////////////////////
111
112
    public void savePreferences(SharedPreferences.Editor editor)
113
      {
114
      String name = name();
115
116
      editor.putInt(name+"_Pos" , mCurrentPos );
117
      editor.putInt(name+"_Type", mCurrentType);
118
      }
119
120
  ////////////////////////////////////////////////////////////////////////////////
121
122
    public void restorePreferences(SharedPreferences preferences)
123
      {
124
      String name = name();
125
126
      mCurrentPos  = preferences.getInt(name+"_Pos" , mDefaultPos );
127
      mCurrentType = preferences.getInt(name+"_Type", mDefaultType);
128
      }
129
130
  ////////////////////////////////////////////////////////////////////////////////
131
132
    public String[] getNames()
133
      {
134
      Method method;
135
136
      try
137
        {
138
        method = mClass.getDeclaredMethod("getNames");
139
        }
140
      catch(NoSuchMethodException ex)
141
        {
142
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
143
        method = null;
144
        }
145
146
      try
147
        {
148
        if( method!=null )
149
          {
150
          Object value = method.invoke(null);
151
          return (String[]) value;
152
          }
153
        }
154
      catch(Exception ex)
155
        {
156
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
157
        }
158
159
      return null;
160
      }
161
162
  ////////////////////////////////////////////////////////////////////////////////
163
164
    public static Type getType(int ordinal)
165
      {
166
      return types[ordinal];
167
      }
168
169
  ////////////////////////////////////////////////////////////////////////////////
170
171
    public static void enableEffects()
172
      {
173
      Method method;
174
175
      for(Type type: values())
176
        {
177
        try
178
          {
179
          method = type.mClass.getDeclaredMethod("enableEffects");
180
          }
181
        catch(NoSuchMethodException ex)
182
          {
183
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
184
          method = null;
185
          }
186
187
        try
188
          {
189
          if( method!=null ) method.invoke(null);
190
          }
191
        catch(Exception ex)
192
          {
193
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
194
          }
195
        }
196
      }
197
198
  ////////////////////////////////////////////////////////////////////////////////
199
200 af88bf2e Leszek Koltunski
    public long startEffect(DistortedScreen screen, EffectController cont) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
201 64975793 Leszek Koltunski
      {
202 beb325a0 Leszek Koltunski
      Method method1 = mClass.getDeclaredMethod("create", int.class);
203 64975793 Leszek Koltunski
204 beb325a0 Leszek Koltunski
      Object value1 = method1.invoke(null,mCurrentType);
205
      BaseEffect baseEffect = (BaseEffect)value1;
206 64975793 Leszek Koltunski
207 af88bf2e Leszek Koltunski
      Method method2 = mClass.getDeclaredMethod("start", int.class, DistortedScreen.class, EffectController.class);
208 64975793 Leszek Koltunski
209 beb325a0 Leszek Koltunski
      Integer translated = translatePos(mCurrentPos)+1;
210 af88bf2e Leszek Koltunski
      Object value2 = method2.invoke(baseEffect,translated,screen,cont);
211 beb325a0 Leszek Koltunski
      return (Long)value2;
212 64975793 Leszek Koltunski
      }
213
214
  ////////////////////////////////////////////////////////////////////////////////
215 bee1d997 Leszek Koltunski
  // map the (0..100) range of the SeekBar into (0..5000) milliseconds, in 100ms
216
  // increments.
217 64975793 Leszek Koltunski
218
    public static int translatePos(int pos)
219
      {
220
      return (pos/2)*100;
221
      }
222
    }
223 ebb64a1d Leszek Koltunski
224
  // END ENUM ////////////////////////////////////////////////////////////////////
225 64975793 Leszek Koltunski
  }