Project

General

Profile

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

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

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