Project

General

Profile

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

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

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.present.PresentEffect;
18
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
import org.distorted.objectlib.main.ObjectPreRender;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

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

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

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

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

    
59
      types= new Type[LENGTH];
60

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

    
68
  ////////////////////////////////////////////////////////////////////////////////
69

    
70
    public int getText()
71
      {
72
      return mText;
73
      }
74

    
75
  ////////////////////////////////////////////////////////////////////////////////
76

    
77
    public int getCurrentPos()
78
      {
79
      return mCurrentPos;
80
      }
81

    
82
  ////////////////////////////////////////////////////////////////////////////////
83

    
84
    public int getCurrentType()
85
      {
86
      return mCurrentType;
87
      }
88

    
89
  ////////////////////////////////////////////////////////////////////////////////
90

    
91
    public void setCurrentPos(int pos)
92
      {
93
      mCurrentPos = pos;
94
      }
95

    
96
  ////////////////////////////////////////////////////////////////////////////////
97

    
98
    public void setCurrentType(int type)
99
      {
100
      mCurrentType = type;
101
      }
102

    
103
  ////////////////////////////////////////////////////////////////////////////////
104

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

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

    
113
  ////////////////////////////////////////////////////////////////////////////////
114

    
115
    public void restorePreferences(SharedPreferences preferences)
116
      {
117
      String name = name();
118

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

    
123
  ////////////////////////////////////////////////////////////////////////////////
124

    
125
    public String[] getNames()
126
      {
127
      Method method;
128

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

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

    
152
      return null;
153
      }
154

    
155
  ////////////////////////////////////////////////////////////////////////////////
156

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

    
162
  ////////////////////////////////////////////////////////////////////////////////
163

    
164
    public static void enableEffects()
165
      {
166
      Method method;
167

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

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

    
191
  ////////////////////////////////////////////////////////////////////////////////
192

    
193
    public int getDuration()
194
      {
195
      return translatePos(mCurrentPos)+1;
196
      }
197

    
198
  ////////////////////////////////////////////////////////////////////////////////
199

    
200
    public long startEffect(ObjectPreRender pre, int duration) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
201
      {
202
      Method method1 = mClass.getDeclaredMethod("create", int.class);
203
      Object value1 = method1.invoke(null,mCurrentType);
204
      BaseEffect baseEffect = (BaseEffect)value1;
205
      Method method2 = mClass.getDeclaredMethod("start", int.class, ObjectPreRender.class);
206
      Object value2 = method2.invoke(baseEffect,duration,pre);
207
      return (Long)value2;
208
      }
209

    
210
  ////////////////////////////////////////////////////////////////////////////////
211
  // map the (0..100) range of the SeekBar into (0..5000) milliseconds, in 100ms
212
  // increments.
213

    
214
    public static int translatePos(int pos)
215
      {
216
      return (pos/2)*100;
217
      }
218
    }
219

    
220
  // END ENUM ////////////////////////////////////////////////////////////////////
221
  }
    (1-1/1)