Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ fa679111

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.MeshSquare;
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 RubikMovementCube(),
46
         0
47
       ),
48

    
49
  PYRA (
50
         new int[][] {
51
                       {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} ,
52
                       {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} ,
53
                       {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}
54
                     },
55
         RubikPyraminx.class,
56
         new RubikMovementPyraminx(),
57
         1
58
       ),
59

    
60
  DINO (
61
         new int[][] {
62
                       {3 , 10, R.raw.dino, R.drawable.ui_small_dino, R.drawable.ui_medium_dino, R.drawable.ui_big_dino, R.drawable.ui_huge_dino} ,
63
                     },
64
         RubikDino.class,
65
         new RubikMovementDino(),
66
         2
67
       ),
68
  ;
69

    
70
  public static final int NUM_OBJECTS = values().length;
71
  public static final int MAX_NUM_OBJECTS;
72
  public static final int MAX_LEVEL;
73
  public static final int MAX_OBJECT_SIZE;
74

    
75
  private final int[] mObjectSizes, mMaxLevels, mSmallIconIDs, mMediumIconIDs, mBigIconIDs, mHugeIconIDs, mResourceIDs;
76
  private final Class<? extends RubikObject> mObjectClass;
77
  private final RubikMovement mObjectMovementClass;
78
  private final int mColumn, mNumSizes;
79

    
80
  private static final RubikObjectList[] objects;
81
  private static int mNumAll;
82
  private static int[] mIndices;
83
  private static int mColCount, mRowCount;
84

    
85
  static
86
    {
87
    mNumAll = 0;
88
    int num, i = 0;
89
    objects = new RubikObjectList[NUM_OBJECTS];
90
    int maxNum  = Integer.MIN_VALUE;
91
    int maxLevel= Integer.MIN_VALUE;
92
    int maxSize = Integer.MIN_VALUE;
93

    
94
    for(RubikObjectList object: RubikObjectList.values())
95
      {
96
      objects[i] = object;
97
      i++;
98
      num = object.mObjectSizes.length;
99
      mNumAll += num;
100
      if( num> maxNum ) maxNum = num;
101

    
102
      for(int j=0; j<num; j++)
103
        {
104
        if( object.mMaxLevels[j] > maxLevel ) maxLevel = object.mMaxLevels[j];
105
        if( object.mObjectSizes[j] > maxSize) maxSize  = object.mObjectSizes[j];
106
        }
107
      }
108

    
109
    MAX_NUM_OBJECTS = maxNum;
110
    MAX_LEVEL       = maxLevel;
111
    MAX_OBJECT_SIZE = maxSize;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private static void setUpColAndRow()
117
    {
118
    mIndices = new int[NUM_OBJECTS];
119
    mColCount= 0;
120

    
121
    for(int obj=0; obj<NUM_OBJECTS; obj++)
122
      {
123
      mIndices[obj] = objects[obj].mColumn;
124
      if( mIndices[obj]>=mColCount ) mColCount = mIndices[obj]+1;
125
      }
126

    
127
    mRowCount = 0;
128

    
129
    for(int col=0; col<mColCount; col++)
130
      {
131
      int numObjects = computeNumObjectsInColumn(col);
132
      if( numObjects>mRowCount ) mRowCount = numObjects;
133
      }
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  private static int computeNumObjectsInColumn(int column)
139
    {
140
    int num=0;
141

    
142
    for(int object=0; object<NUM_OBJECTS; object++)
143
      {
144
      if( objects[object].mColumn == column )
145
        {
146
        num += objects[object].mNumSizes;
147
        }
148
      }
149

    
150
    return num;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  public static int getColumnCount()
156
    {
157
    if( mIndices==null ) setUpColAndRow();
158

    
159
    return mColCount;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public static int getRowCount()
165
    {
166
    if( mIndices==null ) setUpColAndRow();
167

    
168
    return mRowCount;
169
    }
170

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

    
173
  public static int[] getIndices()
174
    {
175
    if( mIndices==null ) setUpColAndRow();
176

    
177
    return mIndices;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public static RubikObjectList getObject(int ordinal)
183
    {
184
    return ordinal>=0 && ordinal<NUM_OBJECTS ? objects[ordinal] : CUBE;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public static int pack(int object, int sizeIndex)
190
    {
191
    int ret = 0;
192
    for(int i=0; i<object; i++) ret += objects[i].mObjectSizes.length;
193

    
194
    return ret+sizeIndex;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public static int unpackSizeIndex(int number)
200
    {
201
    int num;
202

    
203
    for(int i=0; i<NUM_OBJECTS; i++)
204
      {
205
      num = objects[i].mObjectSizes.length;
206
      if( number<num ) return number;
207
      number -= num;
208
      }
209

    
210
    return -1;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public static int unpackObject(int number)
216
    {
217
    int num;
218

    
219
    for(int i=0; i<NUM_OBJECTS; i++)
220
      {
221
      num = objects[i].mObjectSizes.length;
222
      if( number<num ) return i;
223
      number -= num;
224
      }
225

    
226
    return -1;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public static int unpackObjectFromString(String obj)
232
    {
233
    int u = obj.indexOf('_');
234
    int l = obj.length();
235

    
236
    if( u>0 )
237
      {
238
      String name = obj.substring(0,u);
239
      int size = Integer.parseInt( obj.substring(u+1,l) );
240

    
241
      for(int i=0; i<NUM_OBJECTS; i++)
242
        {
243
        if( objects[i].name().equals(name) )
244
          {
245
          int sizeIndex = getSizeIndex(i,size);
246
          return pack(i,sizeIndex);
247
          }
248
        }
249
      }
250

    
251
    return -1;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public static String getObjectList()
257
    {
258
    String name;
259
    StringBuilder list = new StringBuilder();
260
    int len;
261
    int[] sizes;
262

    
263
    for(int i=0; i<NUM_OBJECTS; i++)
264
      {
265
      sizes = objects[i].mObjectSizes;
266
      len   = sizes.length;
267
      name  = objects[i].name();
268

    
269
      for(int j=0; j<len; j++)
270
        {
271
        if( i>0 || j>0 ) list.append(',');
272
        list.append(name);
273
        list.append('_');
274
        list.append(sizes[j]);
275
        }
276
      }
277

    
278
    return list.toString();
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public static int getTotal()
284
    {
285
    return mNumAll;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public static int getMaxLevel(int ordinal, int sizeIndex)
291
    {
292
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
293
      {
294
      int num = objects[ordinal].mObjectSizes.length;
295
      return sizeIndex>=0 && sizeIndex<num ? objects[ordinal].mMaxLevels[sizeIndex] : 0;
296
      }
297

    
298
    return 0;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  public static int getOrdinal(String name)
304
    {
305
    for(int i=0; i<NUM_OBJECTS; i++)
306
      {
307
      if(objects[i].name().equals(name)) return i;
308
      }
309

    
310
    return -1;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  public static int getSizeIndex(int ordinal, int size)
316
    {
317
    if( ordinal>=0 && ordinal<NUM_OBJECTS )
318
      {
319
      int[] sizes = objects[ordinal].getSizes();
320
      int len = sizes.length;
321

    
322
      for(int i=0; i<len; i++)
323
        {
324
        if( sizes[i]==size ) return i;
325
        }
326
      }
327

    
328
    return -1;
329
    }
330

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

    
333
  public static int[] retFaceColors(RubikObjectList object)
334
    {
335
    Field field;
336
    int[] faceColors=null;
337

    
338
    try
339
      {
340
      field = object.mObjectClass.getDeclaredField("FACE_COLORS");
341
      field.setAccessible(true);
342
      Object obj = field.get(null);
343
      faceColors = (int[]) obj;
344
      }
345
    catch(NoSuchFieldException ex)
346
      {
347
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": no such field exception getting field: "+ex.getMessage());
348
      }
349
    catch(IllegalAccessException ex)
350
      {
351
      android.util.Log.e("RubikObjectList", object.mObjectClass.getSimpleName()+": illegal access exception getting field: "+ex.getMessage());
352
      }
353

    
354
    return faceColors;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  RubikObjectList(int[][] info, Class<? extends RubikObject> object , RubikMovement movement, int column)
360
    {
361
    mNumSizes = info.length;
362

    
363
    mObjectSizes  = new int[mNumSizes];
364
    mMaxLevels    = new int[mNumSizes];
365
    mResourceIDs  = new int[mNumSizes];
366
    mSmallIconIDs = new int[mNumSizes];
367
    mMediumIconIDs= new int[mNumSizes];
368
    mBigIconIDs   = new int[mNumSizes];
369
    mHugeIconIDs  = new int[mNumSizes];
370

    
371
    for(int i=0; i<mNumSizes; i++)
372
      {
373
      mObjectSizes[i]  = info[i][0];
374
      mMaxLevels[i]    = info[i][1];
375
      mResourceIDs[i]  = info[i][2];
376
      mSmallIconIDs[i] = info[i][3];
377
      mMediumIconIDs[i]= info[i][4];
378
      mBigIconIDs[i]   = info[i][5];
379
      mHugeIconIDs[i]  = info[i][6];
380
      }
381

    
382
    mObjectClass         = object;
383
    mObjectMovementClass = movement;
384
    mColumn              = column;
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  public int[] getSizes()
390
    {
391
    return mObjectSizes;
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
  public int[] getMaxLevels()
397
    {
398
    return mMaxLevels;
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
  public int[] getIconIDs()
404
    {
405
    int size = RubikActivity.getDrawableSize();
406

    
407
    switch(size)
408
      {
409
      case 0 : return mSmallIconIDs;
410
      case 1 : return mMediumIconIDs;
411
      case 2 : return mBigIconIDs;
412
      default: return mHugeIconIDs;
413
      }
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public int[] getResourceIDs()
419
    {
420
    return mResourceIDs;
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public int getNumVariants()
426
    {
427
    return mObjectSizes.length;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  public RubikObject create(int size, Static4D quat, int[][] moves, Resources res, int scrWidth)
433
    {
434
    DistortedTexture texture = new DistortedTexture();
435
    DistortedEffects effects = new DistortedEffects();
436
    MeshSquare mesh      = new MeshSquare(20,20);   // mesh of the node, not of the cubits
437

    
438
    switch(ordinal())
439
      {
440
      case 0: return new RubikCube    (size, quat, texture, mesh, effects, moves, res, scrWidth);
441
      case 1: return new RubikPyraminx(size, quat, texture, mesh, effects, moves, res, scrWidth);
442
      case 2: return new RubikDino    (size, quat, texture, mesh, effects, moves, res, scrWidth);
443
      }
444

    
445
    return null;
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
  public RubikMovement getObjectMovementClass()
451
    {
452
    return mObjectMovementClass;
453
    }
454
  }
(9-9/10)