Project

General

Profile

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

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

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.resticker.RestickerEffect;
19
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
20
import org.distorted.objectlib.effects.objectchange.ObjectChangeEffect;
21
import org.distorted.objectlib.effects.solve.SolveEffect;
22
import org.distorted.objectlib.effects.win.WinEffect;
23
import org.distorted.objectlib.R;
24
import org.distorted.objectlib.main.ObjectPreRender;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class BaseEffect
29
  {
30
  public enum Type
31
    {
32
    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
    PRESENT       (100, 0, R.string.present_effect, PresentEffect.class),
37
    WIN           ( 20, 1, R.string.win_effect          , WinEffect.class         ),
38
    RESTICKER     ( 20, 0, R.string.resticker_effect          , RestickerEffect.class         ),
39
    ;
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
    public void savePreferences(SharedPreferences.Editor editor)
108
      {
109
      String name = name();
110

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

    
115
  ////////////////////////////////////////////////////////////////////////////////
116

    
117
    public void restorePreferences(SharedPreferences preferences)
118
      {
119
      String name = name();
120

    
121
      mCurrentPos  = preferences.getInt(name+"_Pos" , mDefaultPos );
122
      mCurrentType = preferences.getInt(name+"_Type", mDefaultType);
123
      }
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
    public int getDuration()
196
      {
197
      return translatePos(mCurrentPos)+1;
198
      }
199

    
200
  ////////////////////////////////////////////////////////////////////////////////
201

    
202
    public long startEffect(ObjectPreRender pre, int duration) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
203
      {
204
      Method method1 = mClass.getDeclaredMethod("create", int.class);
205
      Object value1 = method1.invoke(null,mCurrentType);
206
      BaseEffect baseEffect = (BaseEffect)value1;
207
      Method method2 = mClass.getDeclaredMethod("start", int.class, ObjectPreRender.class);
208
      Object value2 = method2.invoke(baseEffect,duration,pre);
209
      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
  }
    (1-1/1)