Project

General

Profile

Download (8.06 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / effect / BaseEffect.java @ 64975793

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.effect;
21

    
22
import java.lang.reflect.Method;
23
import android.content.SharedPreferences;
24

    
25
import org.distorted.effect.scramble.ScrambleEffect;
26
import org.distorted.effect.sizechange.SizeChangeEffect;
27
import org.distorted.effect.solve.SolveEffect;
28
import org.distorted.magic.R;
29
import org.distorted.magic.RubikRenderer;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class BaseEffect
34
  {
35
  public enum Type
36
    {
37
    SIZECHANGE  ( 20, 1, R.string.sizechange_effect , SizeChangeEffect.class),
38
    SOLVE       ( 20, 1, R.string.solve_effect      , SolveEffect.class     ),
39
    SCRAMBLE    ( 20, 1, R.string.scramble_effect   , ScrambleEffect.class  ),
40
    ;
41

    
42
    private final int mDefaultPos, mDefaultType;
43
    private final Class mClass;
44
    private int mCurrentPos, mCurrentType;
45
    private int mText;
46

    
47
    Type(int dPos, int dType, int text, Class clazz )
48
      {
49
      mDefaultPos  = mCurrentPos = dPos;
50
      mDefaultType = mCurrentType= dType;
51
      mText        = text;
52
      mClass       = clazz;
53
      }
54

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

    
62
      types= new Type[LENGTH];
63

    
64
      for(Type type: Type.values())
65
        {
66
        types[i] = type;
67
        i++;
68
        }
69
      }
70

    
71
  ////////////////////////////////////////////////////////////////////////////////
72

    
73
    public int getText()
74
      {
75
      return mText;
76
      }
77

    
78
  ////////////////////////////////////////////////////////////////////////////////
79

    
80
    public int getCurrentPos()
81
      {
82
      return mCurrentPos;
83
      }
84

    
85
  ////////////////////////////////////////////////////////////////////////////////
86

    
87
    public int getCurrentType()
88
      {
89
      return mCurrentType;
90
      }
91

    
92
  ////////////////////////////////////////////////////////////////////////////////
93

    
94
    public void setCurrentPos(int pos)
95
      {
96
      mCurrentPos = pos;
97
      }
98

    
99
  ////////////////////////////////////////////////////////////////////////////////
100

    
101
    public void setCurrentType(int type)
102
      {
103
      mCurrentType = type;
104
      }
105

    
106
  ////////////////////////////////////////////////////////////////////////////////
107

    
108
    public void savePreferences(SharedPreferences.Editor editor)
109
      {
110
      String name = name();
111

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

    
116
  ////////////////////////////////////////////////////////////////////////////////
117

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

    
122
      mCurrentPos  = preferences.getInt(name+"_Pos" , mDefaultPos );
123
      mCurrentType = preferences.getInt(name+"_Type", mDefaultType);
124
      }
125

    
126
  ////////////////////////////////////////////////////////////////////////////////
127

    
128
    public String[] getNames()
129
      {
130
      Method method;
131

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

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

    
155
      return null;
156
      }
157

    
158
  ////////////////////////////////////////////////////////////////////////////////
159

    
160
    public static Type getType(int ordinal)
161
      {
162
      return types[ordinal];
163
      }
164

    
165
  ////////////////////////////////////////////////////////////////////////////////
166

    
167
    public static void enableEffects()
168
      {
169
      Method method;
170

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

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

    
194
  ////////////////////////////////////////////////////////////////////////////////
195

    
196
    public long startEffect(RubikRenderer renderer)
197
      {
198
      Method method1, method2;
199
      BaseEffect baseEffect=null;
200

    
201
      try
202
        {
203
        method1 = mClass.getDeclaredMethod("create", int.class);
204
        }
205
      catch(NoSuchMethodException ex)
206
        {
207
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 1 exception getting method: "+ex.getMessage());
208
        return -1;
209
        }
210

    
211
      try
212
        {
213
        if( method1!=null )
214
          {
215
          Object value = method1.invoke(null,mCurrentType);
216
          baseEffect = (BaseEffect)value;
217
          }
218
        }
219
      catch(Exception ex)
220
        {
221
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 1 exception invoking method: "+ex.getMessage());
222
        return -2;
223
        }
224

    
225
      try
226
        {
227
        method2 = mClass.getDeclaredMethod("start", int.class, RubikRenderer.class);
228
        }
229
      catch(NoSuchMethodException ex)
230
        {
231
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 2 exception getting method: "+ex.getMessage());
232
        return -3;
233
        }
234

    
235
      try
236
        {
237
        if( method2!=null )
238
          {
239
          Integer translated = translatePos(mCurrentPos)+1;
240
          Object value = method2.invoke(baseEffect,translated,renderer);
241
          return (Long)value;
242
          }
243
        }
244
      catch(Exception ex)
245
        {
246
        android.util.Log.e("BaseEffect", mClass.getSimpleName()+": 2 exception invoking method: "+ex.getMessage());
247
        }
248

    
249
      return -4;
250
      }
251

    
252
  ////////////////////////////////////////////////////////////////////////////////
253

    
254
    public static int translatePos(int pos)
255
      {
256
      return (pos/2)*100;
257
      }
258
    }
259
  }
    (1-1/1)