Project

General

Profile

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

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

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.objectlib.effects.scramble.ScrambleEffect;
29
import org.distorted.objectlib.effects.objectchange.ObjectChangeEffect;
30
import org.distorted.objectlib.effects.solve.SolveEffect;
31
import org.distorted.objectlib.effects.win.WinEffect;
32
import org.distorted.objectlib.R;
33
import org.distorted.objectlib.main.ObjectPreRender;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

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

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

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

    
67
      types= new Type[LENGTH];
68

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

    
76
  ////////////////////////////////////////////////////////////////////////////////
77

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

    
83
  ////////////////////////////////////////////////////////////////////////////////
84

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

    
90
  ////////////////////////////////////////////////////////////////////////////////
91

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

    
97
  ////////////////////////////////////////////////////////////////////////////////
98

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

    
104
  ////////////////////////////////////////////////////////////////////////////////
105

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

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

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

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

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

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

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

    
131
  ////////////////////////////////////////////////////////////////////////////////
132

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

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

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

    
160
      return null;
161
      }
162

    
163
  ////////////////////////////////////////////////////////////////////////////////
164

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

    
170
  ////////////////////////////////////////////////////////////////////////////////
171

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

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

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

    
199
  ////////////////////////////////////////////////////////////////////////////////
200

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

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

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

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

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

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

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