Project

General

Profile

Download (7.65 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / effects / BaseEffect.java @ a5a52e8d

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.objectlib.effects;
21

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

    
26
import org.distorted.library.main.DistortedScreen;
27

    
28
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
29
import org.distorted.objectlib.effects.objectchange.ObjectChangeEffect;
30
import org.distorted.objectlib.effects.solve.SolveEffect;
31
import org.distorted.objectlib.effects.win.WinEffect;
32
import org.distorted.objectlib.R;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

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

    
51
    Type(int dPos, int dType, int text, Class<? extends BaseEffect> clazz )
52
      {
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
    public long startEffect(DistortedScreen screen, EffectController cont) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
201
      {
202
      Method method1 = mClass.getDeclaredMethod("create", int.class);
203

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

    
207
      Method method2 = mClass.getDeclaredMethod("start", int.class, DistortedScreen.class, EffectController.class);
208

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

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

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

    
224
  // END ENUM ////////////////////////////////////////////////////////////////////
225
  }
(1-1/2)