Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectCategoriesShape.java @ f96ceb84

1 48e61813 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2024 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objects;
11
12 121f1ba9 leszek
import static org.distorted.objectlib.metadata.Metadata.*;
13
14
import org.distorted.main.R;
15
16 48e61813 leszek
import java.util.Arrays;
17
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 881697b3 leszek
public class RubikObjectCategoriesShape extends RubikObjectCategories
21 48e61813 leszek
{
22 f96ceb84 leszek
  private static final int CATEGORY_SHAPE_CUB = 12;  // more than (CATEGORY_SHAPE_HEX<<3) + AXIS_OTHE
23
                                                     // but less than (CATEGORY_SHAPE_OCT<<3)
24
                                                     // so that CUBOIDS come right after hexes.
25
26 48e61813 leszek
  private void buildSort(int[] data)
27
    {
28
    int numObjects = data.length;
29
    int[] sorted = new int[numObjects];
30
    System.arraycopy(data, 0, sorted, 0, numObjects);
31
32
    Arrays.sort(sorted);
33
34
    int[] categories = new int[numObjects];
35
    int numCategories = 0;
36
37
    for(int o=0; o<numObjects; o++)
38
      if( o==0 || sorted[o-1]!=sorted[o] )
39
        {
40
        categories[numCategories] = sorted[o];
41
        numCategories++;
42
        }
43
44
    int lastChange = -1;
45
    int curr = 0;
46
    int[] numInCategory = new int[numCategories];
47
48
    for(int o=0; o<numObjects; o++)
49
      if( o==numObjects-1 || sorted[o]!=sorted[o+1] )
50
        {
51
        numInCategory[curr] = o-lastChange;
52
        curr++;
53
        lastChange = o;
54
        }
55
56
    mObjectIndices = new int[numCategories][];
57
58
    for(int c=0; c<numCategories; c++)
59
      {
60
      mObjectIndices[c] = new int[numInCategory[c]];
61
62
      int cat = categories[c];
63
      int index = 0;
64
65
      for(int o=0; o<numObjects; o++)
66
        if( data[o]==cat ) mObjectIndices[c][index++] = o;
67
      }
68
    }
69
70 881697b3 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72
  private int digestCategory(int cat)
73
    {
74
    int shape = (cat & 0x7);
75 121f1ba9 leszek
76
    switch(shape)
77
      {
78
      case CATEGORY_SHAPE_HEX: int axis  = (cat & 0x1f) >> 3;
79 f96ceb84 leszek
                               return (shape<<3) + axis;
80 c7ebb6f4 leszek
      case CATEGORY_SHAPE_BAR:
81 f96ceb84 leszek
      case CATEGORY_SHAPE_BAL: return (CATEGORY_SHAPE_OTH<<3);
82 121f1ba9 leszek
      case CATEGORY_SHAPE_TET:
83
      case CATEGORY_SHAPE_OCT:
84
      case CATEGORY_SHAPE_DOD:
85
      case CATEGORY_SHAPE_ICO:
86 f96ceb84 leszek
      case CATEGORY_SHAPE_OTH: return (cat & CATEGORY_CUBOID)!=0 ? CATEGORY_SHAPE_CUB : (shape<<3);
87 121f1ba9 leszek
      }
88
89
    android.util.Log.e("D", "digestCategory: unknown shape "+shape);
90
    return 0;
91
    }
92
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
  private int getImage(int cat)
97
    {
98 f96ceb84 leszek
    if( cat==CATEGORY_SHAPE_CUB ) return R.drawable.shape_cub;
99
    int shape = (cat>>3);
100 121f1ba9 leszek
101
    switch(shape)
102
      {
103 7f7fff03 leszek
      case CATEGORY_SHAPE_HEX: int axis = (cat & 0x3);
104 121f1ba9 leszek
                                    if( axis==CATEGORY_AXIS_FACE>>3 ) return R.drawable.shape_hex_face;
105
                               else if( axis==CATEGORY_AXIS_CORN>>3 ) return R.drawable.shape_hex_corn;
106
                               else if( axis==CATEGORY_AXIS_EDGE>>3 ) return R.drawable.shape_hex_edge;
107
                               else                                   return R.drawable.shape_hex_other;
108 567a1955 leszek
      case CATEGORY_SHAPE_TET: return R.drawable.shape_tet;
109
      case CATEGORY_SHAPE_OCT: return R.drawable.shape_oct;
110
      case CATEGORY_SHAPE_DOD: return R.drawable.shape_dod;
111
      case CATEGORY_SHAPE_ICO: return R.drawable.shape_ico;
112 121f1ba9 leszek
      case CATEGORY_SHAPE_BAL:
113 567a1955 leszek
      case CATEGORY_SHAPE_OTH: return R.drawable.shape_oth;
114 121f1ba9 leszek
      }
115
116
    android.util.Log.e("D", "getImage: unknown shape "+shape);
117
    return 0;
118 881697b3 leszek
    }
119
120 48e61813 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
  void buildIndices()
123
    {
124
    int numObjects = RubikObjectList.getNumObjects();
125
    int[] cats = new int[numObjects];
126
127
    for(int o=0; o<numObjects; o++)
128
      {
129
      RubikObject obj = RubikObjectList.getObject(o);
130 881697b3 leszek
      int cat = obj==null ? 0 : obj.getCategory();
131
      cats[o] = digestCategory(cat);
132 48e61813 leszek
      }
133
134
    buildSort(cats);
135
    }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
  void buildTitle()
140
    {
141
    mIconIDs = new int[mNumCategories];
142
143
    for(int t=0; t<mNumCategories; t++)
144
      {
145
      int obj = mObjectIndices[t][0];
146
      RubikObject object = RubikObjectList.getObject(obj);
147 881697b3 leszek
      int cat = object==null ? 0 : object.getCategory();
148
      int category = digestCategory(cat);
149 121f1ba9 leszek
      mIconIDs[t] = getImage(category);
150 48e61813 leszek
      }
151
    }
152
}