Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.effects;
11

    
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import android.content.SharedPreferences;
15

    
16
import org.distorted.objectlib.effects.fastscramble.FastScrambleEffect;
17
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
18
import org.distorted.objectlib.effects.objectchange.ObjectChangeEffect;
19
import org.distorted.objectlib.effects.solve.SolveEffect;
20
import org.distorted.objectlib.effects.win.WinEffect;
21
import org.distorted.objectlib.R;
22
import org.distorted.objectlib.main.ObjectPreRender;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class BaseEffect
27
  {
28
  public enum Type
29
    {
30
    SIZECHANGE    ( 20, 1, R.string.objectchange_effect , ObjectChangeEffect.class),
31
    SOLVE         ( 20, 1, R.string.solve_effect        , SolveEffect.class       ),
32
    SCRAMBLE      ( 60, 1, R.string.scramble_effect     , ScrambleEffect.class    ),
33
    FAST_SCRAMBLE ( 20, 0, R.string.fast_scramble_effect, FastScrambleEffect.class),
34
    WIN           ( 20, 1, R.string.win_effect          , WinEffect.class         ),
35
    ;
36

    
37
    private final int mDefaultPos, mDefaultType;
38
    private final Class<? extends BaseEffect> mClass;
39
    private final int mText;
40
    private int mCurrentPos, mCurrentType;
41

    
42
    Type(int dPos, int dType, int text, Class<? extends BaseEffect> clazz )
43
      {
44
      mDefaultPos  = mCurrentPos = dPos;
45
      mDefaultType = mCurrentType= dType;
46
      mText        = text;
47
      mClass       = clazz;
48
      }
49

    
50
    public static final int LENGTH = Type.values().length;
51
    private static final Type[] types;  // copy the values() to a local variable so that we
52
                                        // don't have to keep recreating the array every time
53
    static
54
      {
55
      int i=0;
56

    
57
      types= new Type[LENGTH];
58

    
59
      for(Type type: Type.values())
60
        {
61
        types[i] = type;
62
        i++;
63
        }
64
      }
65

    
66
  ////////////////////////////////////////////////////////////////////////////////
67

    
68
    public int getText()
69
      {
70
      return mText;
71
      }
72

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

    
75
    public int getCurrentPos()
76
      {
77
      return mCurrentPos;
78
      }
79

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

    
82
    public int getCurrentType()
83
      {
84
      return mCurrentType;
85
      }
86

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

    
89
    public void setCurrentPos(int pos)
90
      {
91
      mCurrentPos = pos;
92
      }
93

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

    
96
    public void setCurrentType(int type)
97
      {
98
      mCurrentType = type;
99
      }
100

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

    
103
    public void savePreferences(SharedPreferences.Editor editor)
104
      {
105
      String name = name();
106

    
107
      editor.putInt(name+"_Pos" , mCurrentPos );
108
      editor.putInt(name+"_Type", mCurrentType);
109
      }
110

    
111
  ////////////////////////////////////////////////////////////////////////////////
112

    
113
    public void restorePreferences(SharedPreferences preferences)
114
      {
115
      String name = name();
116

    
117
      mCurrentPos  = preferences.getInt(name+"_Pos" , mDefaultPos );
118
      mCurrentType = preferences.getInt(name+"_Type", mDefaultType);
119
      }
120

    
121
  ////////////////////////////////////////////////////////////////////////////////
122

    
123
    public String[] getNames()
124
      {
125
      Method method;
126

    
127
      try
128
        {
129
        method = mClass.getDeclaredMethod("getNames");
130
        }
131
      catch(NoSuchMethodException ex)
132
        {
133
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
134
        method = null;
135
        }
136

    
137
      try
138
        {
139
        if( method!=null )
140
          {
141
          Object value = method.invoke(null);
142
          return (String[]) value;
143
          }
144
        }
145
      catch(Exception ex)
146
        {
147
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
148
        }
149

    
150
      return null;
151
      }
152

    
153
  ////////////////////////////////////////////////////////////////////////////////
154

    
155
    public static Type getType(int ordinal)
156
      {
157
      return types[ordinal];
158
      }
159

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

    
162
    public static void enableEffects()
163
      {
164
      Method method;
165

    
166
      for(Type type: values())
167
        {
168
        try
169
          {
170
          method = type.mClass.getDeclaredMethod("enableEffects");
171
          }
172
        catch(NoSuchMethodException ex)
173
          {
174
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
175
          method = null;
176
          }
177

    
178
        try
179
          {
180
          if( method!=null ) method.invoke(null);
181
          }
182
        catch(Exception ex)
183
          {
184
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
185
          }
186
        }
187
      }
188

    
189
  ////////////////////////////////////////////////////////////////////////////////
190

    
191
    public long startEffect(ObjectPreRender pre) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
192
      {
193
      Method method1 = mClass.getDeclaredMethod("create", int.class);
194

    
195
      Object value1 = method1.invoke(null,mCurrentType);
196
      BaseEffect baseEffect = (BaseEffect)value1;
197

    
198
      Method method2 = mClass.getDeclaredMethod("start", int.class, ObjectPreRender.class);
199

    
200
      Integer translated = translatePos(mCurrentPos)+1;
201
      Object value2 = method2.invoke(baseEffect,translated,pre);
202
      return (Long)value2;
203
      }
204

    
205
  ////////////////////////////////////////////////////////////////////////////////
206
  // map the (0..100) range of the SeekBar into (0..5000) milliseconds, in 100ms
207
  // increments.
208

    
209
    public static int translatePos(int pos)
210
      {
211
      return (pos/2)*100;
212
      }
213
    }
214

    
215
  // END ENUM ////////////////////////////////////////////////////////////////////
216
  }
    (1-1/1)