Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 31a9f38d

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.objects;
21

    
22
import android.content.res.Resources;
23

    
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedTexture;
26
import org.distorted.library.mesh.MeshRectangles;
27
import org.distorted.library.type.Static4D;
28
import org.distorted.main.R;
29
import org.distorted.main.RubikActivity;
30

    
31
import java.lang.reflect.Field;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public enum RubikObjectList
36
  {
37
  CUBE (
38
         new int[][] {
39
                       {2 , 12, R.raw.cube2, R.drawable.ui_small_cube2, R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2} ,
40
                       {3 , 16, R.raw.cube3, R.drawable.ui_small_cube3, R.drawable.ui_medium_cube3, R.drawable.ui_big_cube3, R.drawable.ui_huge_cube3} ,
41
                       {4 , 20, R.raw.cube4, R.drawable.ui_small_cube4, R.drawable.ui_medium_cube4, R.drawable.ui_big_cube4, R.drawable.ui_huge_cube4} ,
42
                       {5 , 24, R.raw.cube5, R.drawable.ui_small_cube5, R.drawable.ui_medium_cube5, R.drawable.ui_big_cube5, R.drawable.ui_huge_cube5}
43
                     },
44
         RubikCube.class,
45
         new RubikCubeMovement()
46
       ),
47

    
48
  PYRA (
49
         new int[][] {
50
                       {3 , 10, R.raw.pyra3, R.drawable.ui_small_pyra3, R.drawable.ui_medium_pyra3, R.drawable.ui_big_pyra3, R.drawable.ui_huge_pyra3} ,
51
                       {4 , 15, R.raw.pyra4, R.drawable.ui_small_pyra4, R.drawable.ui_medium_pyra4, R.drawable.ui_big_pyra4, R.drawable.ui_huge_pyra4} ,
52
                       {5 , 20, R.raw.pyra5, R.drawable.ui_small_pyra5, R.drawable.ui_medium_pyra5, R.drawable.ui_big_pyra5, R.drawable.ui_huge_pyra5}
53
                     },
54
         RubikPyraminx.class,
55
         new RubikPyraminxMovement()
56
       ),
57
  ;
58

    
59
  public static final int NUM_OBJECTS = values().length;
60
  public static final int MAX_NUM_OBJECTS;
61
  public static final int MAX_LEVEL;
62
  public static final int MAX_OBJECT_SIZE;
63

    
64
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
65
  private final Class<? extends RubikObject> mObjectClass;
66
  private final RubikObjectMovement mObjectMovementClass;
67
  private static final RubikObjectList[] objects;
68
  private static int mNumAll;
69

    
70
  static
71
    {
72
    mNumAll = 0;
73
    int num, i = 0;
74
    objects = new RubikObjectList[NUM_OBJECTS];
75
    int maxNum  = Integer.MIN_VALUE;
76
    int maxLevel= Integer.MIN_VALUE;
77
    int maxSize = Integer.MIN_VALUE;
78

    
79
    for(RubikObjectList object: RubikObjectList.values())
80
      {
81
      objects[i] = object;
82
      i++;
83
      num = object.mObjectSizes.length;
84
      mNumAll += num;
85
      if( num> maxNum ) maxNum = num;
86

    
87
      for(int j=0; j<num; j++)
88
        {
89
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
90
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
91
        }
92
      }
93

    
94
    MAX_NUM_OBJECTS = maxNum;
95
    MAX_LEVEL       = maxLevel;
96
    MAX_OBJECT_SIZE = maxSize;
97
    }
98

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

    
101
  public static RubikObjectList getObject(int ordinal)
102
    {
103
    return objects[ordinal];
104
    }
105

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

    
108
  public static int pack(int object, int sizeIndex)
109
    {
110
    int ret = 0;
111
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
112

    
113
    return ret+sizeIndex;
114
    }
115

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

    
118
  public static int unpackSizeIndex(int number)
119
    {
120
    int num;
121

    
122
    for(int i=0; i<NUM_OBJECTS; i++)
123
      {
124
      num = objects[i].mObjectSizes.length;
125
      if( number<num ) return number;
126
      number -= num;
127
      }
128

    
129
    return -1;
130
    }
131

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

    
134
  public static int unpackObject(int number)
135
    {
136
    int num;
137

    
138
    for(int i=0; i<NUM_OBJECTS; i++)
139
      {
140
      num = objects[i].mObjectSizes.length;
141
      if( number<num ) return i;
142
      number -= num;
143
      }
144

    
145
    return -1;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public static int unpackObjectFromString(String obj)
151
    {
152
    int u = obj.indexOf('_');
153
    int l = obj.length();
154

    
155
    if( u>0 )
156
      {
157
      String name = obj.substring(0,u);
158
      int size = Integer.parseInt( obj.substring(u+1,l) );
159

    
160
      for(int i=0; i<NUM_OBJECTS; i++)
161
        {
162
        if( objects[i].name().equals(name) )
163
          {
164
          int sizeIndex = getSizeIndex(i,size);
165
          return pack(i,sizeIndex);
166
          }
167
        }
168
      }
169

    
170
    return -1;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  public static String getObjectList()
176
    {
177
    String name;
178
    StringBuilder list = new StringBuilder();
179
    int len;
180
    int[] sizes;
181

    
182
    for(int i=0; i<NUM_OBJECTS; i++)
183
      {
184
      sizes = objects[i].mObjectSizes;
185
      len   = sizes.length;
186
      name  = objects[i].name();
187

    
188
      for(int j=0; j<len; j++)
189
        {
190
        if( i>0 || j>0 ) list.append(',');
191
        list.append(name);
192
        list.append('_');
193
        list.append(sizes[j]);
194
        }
195
      }
196

    
197
    return list.toString();
198
    }
199

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

    
202
  public static int getTotal()
203
    {
204
    return mNumAll;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public static int getMaxLevel(int ordinal, int sizeIndex)
210
    {
211
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
212
      {
213
      int num = objects[ordinal].mObjectSizes.length;
214
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
215
      }
216

    
217
    return 0;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public static int getOrdinal(String name)
223
    {
224
    for(int i=0; i<NUM_OBJECTS; i++)
225
      {
226
      if(objects[i].name().equals(name)) return i;
227
      }
228

    
229
    return -1;
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public static int getSizeIndex(int ordinal, int size)
235
    {
236
    int[] sizes = objects[ordinal].getSizes();
237
    int len = sizes.length;
238

    
239
    for(int i=0; i<len; i++)
240
      {
241
      if( sizes[i]==size ) return i;
242
      }
243

    
244
    return -1;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public static int[] retFaceColors(RubikObjectList object)
250
    {
251
    Field field;
252
    int[] faceColors=null;
253

    
254
    try
255
      {
256
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
257
      field.setAccessible(true);
258
      Object obj = field.get(null);
259
      faceColors = (int[]) obj;
260
      }
261
    catch(NoSuchFieldException ex)
262
      {
263
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
264
      }
265
    catch(IllegalAccessException ex)
266
      {
267
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
268
      }
269

    
270
    return faceColors;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikObjectMovement movement)
276
    {
277
    int length = info.length;
278

    
279
    mObjectSizes  = new int[length];
280
    mMaxLevels    = new int[length];
281
    mResourceIDs  = new int[length];
282
    mSmallIconIDs = new int[length];
283
    mMediumIconIDs= new int[length];
284
    mBigIconIDs   = new int[length];
285
    mHugeIconIDs  = new int[length];
286

    
287
    for(int i=0; i<length; i++)
288
      {
289
      mObjectSizes[i]  = info[i][0];
290
      mMaxLevels[i]    = info[i][1];
291
      mResourceIDs[i]  = info[i][2];
292
      mSmallIconIDs[i] = info[i][3];
293
      mMediumIconIDs[i]= info[i][4];
294
      mBigIconIDs[i]   = info[i][5];
295
      mHugeIconIDs[i]  = info[i][6];
296
      }
297

    
298
    mObjectClass         = object;
299
    mObjectMovementClass = movement;
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public int[] getSizes()
305
    {
306
    return mObjectSizes;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  public int[] getMaxLevels()
312
    {
313
    return mMaxLevels;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  public int[] getIconIDs()
319
    {
320
    int size = RubikActivity.getDrawableSize();
321

    
322
    switch(size)
323
      {
324
      case 0 : return mSmallIconIDs;
325
      case 1 : return mMediumIconIDs;
326
      case 2 : return mBigIconIDs;
327
      default: return mHugeIconIDs;
328
      }
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  public int[] getResourceIDs()
334
    {
335
    return mResourceIDs;
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  public int getNumVariants()
341
    {
342
    return mObjectSizes.length;
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
348
    {
349
    DistortedTexture texture = new DistortedTexture();
350
    DistortedEffects effects = new DistortedEffects();
351
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
352

    
353
    switch(ordinal())
354
      {
355
      case 0: return new RubikCube    (size, quat, texture, mesh, effects, moves, res, scrWidth);
356
      case 1: return new RubikPyraminx(size, quat, texture, mesh, effects, moves, res, scrWidth);
357
      }
358

    
359
    return null;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  public RubikObjectMovement getObjectMovementClass()
365
    {
366
    return mObjectMovementClass;
367
    }
368
  }
(5-5/8)