Project

General

Profile

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

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

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 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
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  private int digestCategory(int cat)
73
    {
74
    int shape = (cat & 0x7);
75

    
76
    switch(shape)
77
      {
78
      case CATEGORY_SHAPE_HEX: int axis  = (cat & 0x1f) >> 3;
79
                               return (shape<<3) + axis;
80
      case CATEGORY_SHAPE_BAR:
81
      case CATEGORY_SHAPE_BAL: return (CATEGORY_SHAPE_OTH<<3);
82
      case CATEGORY_SHAPE_TET:
83
      case CATEGORY_SHAPE_OCT:
84
      case CATEGORY_SHAPE_DOD:
85
      case CATEGORY_SHAPE_ICO:
86
      case CATEGORY_SHAPE_OTH: return (cat & CATEGORY_CUBOID)!=0 ? CATEGORY_SHAPE_CUB : (shape<<3);
87
      }
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
    if( cat==CATEGORY_SHAPE_CUB ) return R.drawable.shape_cub;
99
    int shape = (cat>>3);
100

    
101
    switch(shape)
102
      {
103
      case CATEGORY_SHAPE_HEX: int axis = (cat & 0x3);
104
                                    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
      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
      case CATEGORY_SHAPE_BAL:
113
      case CATEGORY_SHAPE_OTH: return R.drawable.shape_oth;
114
      }
115

    
116
    android.util.Log.e("D", "getImage: unknown shape "+shape);
117
    return 0;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      int cat = obj==null ? 0 : obj.getCategory();
131
      cats[o] = digestCategory(cat);
132
      }
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
      int cat = object==null ? 0 : object.getCategory();
148
      int category = digestCategory(cat);
149
      mIconIDs[t] = getImage(category);
150
      }
151
    }
152
}
(5-5/7)