Project

General

Profile

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

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

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.DistortedFramebuffer;
27

    
28
import org.distorted.library.main.DistortedNode;
29
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
30
import org.distorted.objectlib.effects.objectchange.ObjectChangeEffect;
31
import org.distorted.objectlib.effects.solve.SolveEffect;
32
import org.distorted.objectlib.effects.win.WinEffect;
33
import org.distorted.objectlib.R;
34
import org.distorted.objectlib.main.ObjectPreRender;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

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

    
53
    Type(int dPos, int dType, int text, Class<? extends BaseEffect> clazz )
54
      {
55
      mDefaultPos  = mCurrentPos = dPos;
56
      mDefaultType = mCurrentType= dType;
57
      mText        = text;
58
      mClass       = clazz;
59
      }
60

    
61
    public static final int LENGTH = Type.values().length;
62
    private static final Type[] types;  // copy the values() to a local variable so that we
63
                                        // don't have to keep recreating the array every time
64
    static
65
      {
66
      int i=0;
67

    
68
      types= new Type[LENGTH];
69

    
70
      for(Type type: Type.values())
71
        {
72
        types[i] = type;
73
        i++;
74
        }
75
      }
76

    
77
  ////////////////////////////////////////////////////////////////////////////////
78

    
79
    public int getText()
80
      {
81
      return mText;
82
      }
83

    
84
  ////////////////////////////////////////////////////////////////////////////////
85

    
86
    public int getCurrentPos()
87
      {
88
      return mCurrentPos;
89
      }
90

    
91
  ////////////////////////////////////////////////////////////////////////////////
92

    
93
    public int getCurrentType()
94
      {
95
      return mCurrentType;
96
      }
97

    
98
  ////////////////////////////////////////////////////////////////////////////////
99

    
100
    public void setCurrentPos(int pos)
101
      {
102
      mCurrentPos = pos;
103
      }
104

    
105
  ////////////////////////////////////////////////////////////////////////////////
106

    
107
    public void setCurrentType(int type)
108
      {
109
      mCurrentType = type;
110
      }
111

    
112
  ////////////////////////////////////////////////////////////////////////////////
113

    
114
    public void savePreferences(SharedPreferences.Editor editor)
115
      {
116
      String name = name();
117

    
118
      editor.putInt(name+"_Pos" , mCurrentPos );
119
      editor.putInt(name+"_Type", mCurrentType);
120
      }
121

    
122
  ////////////////////////////////////////////////////////////////////////////////
123

    
124
    public void restorePreferences(SharedPreferences preferences)
125
      {
126
      String name = name();
127

    
128
      mCurrentPos  = preferences.getInt(name+"_Pos" , mDefaultPos );
129
      mCurrentType = preferences.getInt(name+"_Type", mDefaultType);
130
      }
131

    
132
  ////////////////////////////////////////////////////////////////////////////////
133

    
134
    public String[] getNames()
135
      {
136
      Method method;
137

    
138
      try
139
        {
140
        method = mClass.getDeclaredMethod("getNames");
141
        }
142
      catch(NoSuchMethodException ex)
143
        {
144
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
145
        method = null;
146
        }
147

    
148
      try
149
        {
150
        if( method!=null )
151
          {
152
          Object value = method.invoke(null);
153
          return (String[]) value;
154
          }
155
        }
156
      catch(Exception ex)
157
        {
158
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
159
        }
160

    
161
      return null;
162
      }
163

    
164
  ////////////////////////////////////////////////////////////////////////////////
165

    
166
    public static Type getType(int ordinal)
167
      {
168
      return types[ordinal];
169
      }
170

    
171
  ////////////////////////////////////////////////////////////////////////////////
172

    
173
    public static void enableEffects()
174
      {
175
      Method method;
176

    
177
      for(Type type: values())
178
        {
179
        try
180
          {
181
          method = type.mClass.getDeclaredMethod("enableEffects");
182
          }
183
        catch(NoSuchMethodException ex)
184
          {
185
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception getting method: "+ex.getMessage());
186
          method = null;
187
          }
188

    
189
        try
190
          {
191
          if( method!=null ) method.invoke(null);
192
          }
193
        catch(Exception ex)
194
          {
195
          android.util.Log.e("BaseEffect", type.mClass.getSimpleName()+": exception invoking method: "+ex.getMessage());
196
          }
197
        }
198
      }
199

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

    
202
    public long startEffect(ObjectPreRender pre) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
203
      {
204
      Method method1 = mClass.getDeclaredMethod("create", int.class);
205

    
206
      Object value1 = method1.invoke(null,mCurrentType);
207
      BaseEffect baseEffect = (BaseEffect)value1;
208

    
209
      Method method2 = mClass.getDeclaredMethod("start", int.class, ObjectPreRender.class);
210

    
211
      Integer translated = translatePos(mCurrentPos)+1;
212
      Object value2 = method2.invoke(baseEffect,translated,pre);
213
      return (Long)value2;
214
      }
215

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

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

    
226
  // END ENUM ////////////////////////////////////////////////////////////////////
227
  }
    (1-1/1)