Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 6d4d56cb

1 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 27a70eae Leszek Koltunski
22 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
23
24 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedTexture;
26 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
27 27a70eae Leszek Koltunski
import org.distorted.library.type.Static4D;
28 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
29 f6a9c538 Leszek Koltunski
import org.distorted.main.RubikActivity;
30 27a70eae Leszek Koltunski
31 20931cf6 Leszek Koltunski
import java.lang.reflect.Field;
32
33 27a70eae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35
public enum RubikObjectList
36
  {
37 4c0cd600 Leszek Koltunski
  CUBE (
38
         new int[][] {
39 31a9f38d Leszek Koltunski
                       {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 4c0cd600 Leszek Koltunski
                     },
44 20931cf6 Leszek Koltunski
         RubikCube.class,
45 4c0cd600 Leszek Koltunski
         new RubikCubeMovement()
46
       ),
47
48
  PYRA (
49
         new int[][] {
50 31a9f38d Leszek Koltunski
                       {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 4c0cd600 Leszek Koltunski
                     },
54 20931cf6 Leszek Koltunski
         RubikPyraminx.class,
55 4c0cd600 Leszek Koltunski
         new RubikPyraminxMovement()
56
       ),
57 27a70eae Leszek Koltunski
  ;
58
59 4888e97c Leszek Koltunski
  public static final int NUM_OBJECTS = values().length;
60 8cccfb10 Leszek Koltunski
  public static final int MAX_NUM_OBJECTS;
61 8e3898c8 Leszek Koltunski
  public static final int MAX_LEVEL;
62 8cccfb10 Leszek Koltunski
  public static final int MAX_OBJECT_SIZE;
63 4888e97c Leszek Koltunski
64 31a9f38d Leszek Koltunski
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
65 20931cf6 Leszek Koltunski
  private final Class<? extends RubikObject> mObjectClass;
66
  private final RubikObjectMovement mObjectMovementClass;
67 27a70eae Leszek Koltunski
  private static final RubikObjectList[] objects;
68 4888e97c Leszek Koltunski
  private static int mNumAll;
69 27a70eae Leszek Koltunski
70
  static
71
    {
72 4888e97c Leszek Koltunski
    mNumAll = 0;
73 8cccfb10 Leszek Koltunski
    int num, i = 0;
74 4888e97c Leszek Koltunski
    objects = new RubikObjectList[NUM_OBJECTS];
75 8cccfb10 Leszek Koltunski
    int maxNum  = Integer.MIN_VALUE;
76
    int maxLevel= Integer.MIN_VALUE;
77
    int maxSize = Integer.MIN_VALUE;
78 27a70eae Leszek Koltunski
79 4888e97c Leszek Koltunski
    for(RubikObjectList object: RubikObjectList.values())
80 27a70eae Leszek Koltunski
      {
81 4888e97c Leszek Koltunski
      objects[i] = object;
82 27a70eae Leszek Koltunski
      i++;
83 8cccfb10 Leszek Koltunski
      num = object.mObjectSizes.length;
84
      mNumAll += num;
85
      if( num> maxNum ) maxNum = num;
86 8e3898c8 Leszek Koltunski
87 8cccfb10 Leszek Koltunski
      for(int j=0; j<num; j++)
88 8e3898c8 Leszek Koltunski
        {
89
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
90 8cccfb10 Leszek Koltunski
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
91 8e3898c8 Leszek Koltunski
        }
92 27a70eae Leszek Koltunski
      }
93 4888e97c Leszek Koltunski
94 8cccfb10 Leszek Koltunski
    MAX_NUM_OBJECTS = maxNum;
95
    MAX_LEVEL       = maxLevel;
96
    MAX_OBJECT_SIZE = maxSize;
97 27a70eae Leszek Koltunski
    }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
  public static RubikObjectList getObject(int ordinal)
102
    {
103
    return objects[ordinal];
104
    }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 b498f3f6 Leszek Koltunski
  public static int pack(int object, int sizeIndex)
109 4888e97c Leszek Koltunski
    {
110
    int ret = 0;
111
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
112
113 b498f3f6 Leszek Koltunski
    return ret+sizeIndex;
114 4888e97c Leszek Koltunski
    }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118 8e3898c8 Leszek Koltunski
  public static int unpackSizeIndex(int number)
119 4888e97c Leszek Koltunski
    {
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 27a70eae Leszek Koltunski
    {
136 4888e97c Leszek Koltunski
    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 286d73ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 53f23b64 Leszek Koltunski
          int sizeIndex = getSizeIndex(i,size);
165
          return pack(i,sizeIndex);
166 286d73ae Leszek Koltunski
          }
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 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  public static int getTotal()
203
    {
204
    return mNumAll;
205
    }
206
207 8e3898c8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c86f9f1f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 53f23b64 Leszek Koltunski
  public static int getSizeIndex(int ordinal, int size)
235 c86f9f1f Leszek Koltunski
    {
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 4888e97c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249 20931cf6 Leszek Koltunski
  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 4888e97c Leszek Koltunski
    {
277 ea6ee91b Leszek Koltunski
    int length = info.length;
278
279 f6a9c538 Leszek Koltunski
    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 31a9f38d Leszek Koltunski
    mHugeIconIDs  = new int[length];
286 ea6ee91b Leszek Koltunski
287
    for(int i=0; i<length; i++)
288
      {
289 f6a9c538 Leszek Koltunski
      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 31a9f38d Leszek Koltunski
      mHugeIconIDs[i]  = info[i][6];
296 ea6ee91b Leszek Koltunski
      }
297
298 20931cf6 Leszek Koltunski
    mObjectClass         = object;
299 27a70eae Leszek Koltunski
    mObjectMovementClass = movement;
300
    }
301
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303
304 8e3898c8 Leszek Koltunski
  public int[] getSizes()
305 27a70eae Leszek Koltunski
    {
306 8e3898c8 Leszek Koltunski
    return mObjectSizes;
307 27a70eae Leszek Koltunski
    }
308
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310
311 8e3898c8 Leszek Koltunski
  public int[] getMaxLevels()
312 4888e97c Leszek Koltunski
    {
313 8e3898c8 Leszek Koltunski
    return mMaxLevels;
314
    }
315
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318
  public int[] getIconIDs()
319
    {
320 f6a9c538 Leszek Koltunski
    int size = RubikActivity.getDrawableSize();
321
322
    switch(size)
323
      {
324
      case 0 : return mSmallIconIDs;
325
      case 1 : return mMediumIconIDs;
326 31a9f38d Leszek Koltunski
      case 2 : return mBigIconIDs;
327
      default: return mHugeIconIDs;
328 f6a9c538 Leszek Koltunski
      }
329 4888e97c Leszek Koltunski
    }
330
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333 ccf9fec5 Leszek Koltunski
  public int[] getResourceIDs()
334
    {
335
    return mResourceIDs;
336
    }
337
338 fbca0033 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
  public int getNumVariants()
341
    {
342
    return mObjectSizes.length;
343
    }
344
345 ccf9fec5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347 5b893eee Leszek Koltunski
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
348 27a70eae Leszek Koltunski
    {
349 5974d2ae Leszek Koltunski
    DistortedTexture texture = new DistortedTexture();
350 36273130 Leszek Koltunski
    DistortedEffects effects = new DistortedEffects();
351 b32444ee Leszek Koltunski
    MeshRectangles mesh      = new MeshRectangles(20,20);   // mesh of the node, not of the cubits
352 74686c71 Leszek Koltunski
353 4888e97c Leszek Koltunski
    switch(ordinal())
354
      {
355 5b893eee Leszek Koltunski
      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 4888e97c Leszek Koltunski
      }
358
359
    return null;
360 27a70eae Leszek Koltunski
    }
361
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
364
  public RubikObjectMovement getObjectMovementClass()
365
    {
366 4888e97c Leszek Koltunski
    return mObjectMovementClass;
367 27a70eae Leszek Koltunski
    }
368
  }