Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectCategoriesShape.java @ 7f7fff03

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import static org.distorted.objectlib.metadata.Metadata.*;
13

    
14
import org.distorted.main.R;
15

    
16
import java.util.Arrays;
17

    
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
public class RubikObjectCategoriesShape extends RubikObjectCategories
21
{
22
  private void buildSort(int[] data)
23
    {
24
    int numObjects = data.length;
25
    int[] sorted = new int[numObjects];
26
    System.arraycopy(data, 0, sorted, 0, numObjects);
27

    
28
    Arrays.sort(sorted);
29

    
30
    int[] categories = new int[numObjects];
31
    int numCategories = 0;
32

    
33
    for(int o=0; o<numObjects; o++)
34
      if( o==0 || sorted[o-1]!=sorted[o] )
35
        {
36
        categories[numCategories] = sorted[o];
37
        numCategories++;
38
        }
39

    
40
    int lastChange = -1;
41
    int curr = 0;
42
    int[] numInCategory = new int[numCategories];
43

    
44
    for(int o=0; o<numObjects; o++)
45
      if( o==numObjects-1 || sorted[o]!=sorted[o+1] )
46
        {
47
        numInCategory[curr] = o-lastChange;
48
        curr++;
49
        lastChange = o;
50
        }
51

    
52
    mObjectIndices = new int[numCategories][];
53

    
54
    for(int c=0; c<numCategories; c++)
55
      {
56
      mObjectIndices[c] = new int[numInCategory[c]];
57

    
58
      int cat = categories[c];
59
      int index = 0;
60

    
61
      for(int o=0; o<numObjects; o++)
62
        if( data[o]==cat ) mObjectIndices[c][index++] = o;
63
      }
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  private int digestCategory(int cat)
69
    {
70
    int shape = (cat & 0x7);
71

    
72
    switch(shape)
73
      {
74
      case CATEGORY_SHAPE_HEX: int axis  = (cat & 0x1f) >> 3;
75
                               return (shape<<2) + axis;
76
      case CATEGORY_SHAPE_BAR:
77
      case CATEGORY_SHAPE_BAL: return (CATEGORY_SHAPE_OTH<<2);
78
      case CATEGORY_SHAPE_TET:
79
      case CATEGORY_SHAPE_OCT:
80
      case CATEGORY_SHAPE_DOD:
81
      case CATEGORY_SHAPE_ICO:
82
      case CATEGORY_SHAPE_OTH: return (shape<<2);
83
      }
84

    
85
    android.util.Log.e("D", "digestCategory: unknown shape "+shape);
86
    return 0;
87
    }
88

    
89

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

    
92
  private int getImage(int cat)
93
    {
94
    int shape = (cat & 0x1f) >> 2;
95

    
96
    switch(shape)
97
      {
98
      case CATEGORY_SHAPE_HEX: int axis = (cat & 0x3);
99
                                    if( axis==CATEGORY_AXIS_FACE>>3 ) return R.drawable.shape_hex_face;
100
                               else if( axis==CATEGORY_AXIS_CORN>>3 ) return R.drawable.shape_hex_corn;
101
                               else if( axis==CATEGORY_AXIS_EDGE>>3 ) return R.drawable.shape_hex_edge;
102
                               else                                   return R.drawable.shape_hex_other;
103
      case CATEGORY_SHAPE_TET: return R.drawable.shape_tet;
104
      case CATEGORY_SHAPE_OCT: return R.drawable.shape_oct;
105
      case CATEGORY_SHAPE_DOD: return R.drawable.shape_dod;
106
      case CATEGORY_SHAPE_ICO: return R.drawable.shape_ico;
107
      case CATEGORY_SHAPE_BAL:
108
      case CATEGORY_SHAPE_OTH: return R.drawable.shape_oth;
109
      }
110

    
111
    android.util.Log.e("D", "getImage: unknown shape "+shape);
112
    return 0;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  void buildIndices()
118
    {
119
    int numObjects = RubikObjectList.getNumObjects();
120
    int[] cats = new int[numObjects];
121

    
122
    for(int o=0; o<numObjects; o++)
123
      {
124
      RubikObject obj = RubikObjectList.getObject(o);
125
      int cat = obj==null ? 0 : obj.getCategory();
126
      cats[o] = digestCategory(cat);
127
      }
128

    
129
    buildSort(cats);
130
    }
131

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

    
134
  void buildTitle()
135
    {
136
    mIconIDs = new int[mNumCategories];
137

    
138
    for(int t=0; t<mNumCategories; t++)
139
      {
140
      int obj = mObjectIndices[t][0];
141
      RubikObject object = RubikObjectList.getObject(obj);
142
      int cat = object==null ? 0 : object.getCategory();
143
      int category = digestCategory(cat);
144
      mIconIDs[t] = getImage(category);
145
      }
146
    }
147
}
(5-5/7)