Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectCategories.java @ 74636fa3

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.main.MainSettingsPopup.*;
13

    
14
import org.distorted.main.R;
15

    
16
import java.util.Arrays;
17

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

    
20
public class RubikObjectCategories
21
{
22
  public static final int[] DIFF_IDS =
23
    {
24
    R.id.configDifficulty0,
25
    R.id.configDifficulty1,
26
    R.id.configDifficulty2,
27
    R.id.configDifficulty3,
28
    R.id.configDifficulty4
29
    };
30

    
31
  public static final int[] DIFF_IMAGES =
32
    {
33
    R.drawable.difficulty1,
34
    R.drawable.difficulty2,
35
    R.drawable.difficulty3,
36
    R.drawable.difficulty4,
37
    R.drawable.difficulty5,
38
    };
39

    
40
  public static final int[] CATEGORY_IMAGES =
41
    {
42
    R.drawable.difficulty1,
43
    R.drawable.difficulty2,
44
    R.drawable.difficulty3,
45
    R.drawable.difficulty4,
46
    R.drawable.difficulty5,
47
    };
48

    
49
  private final int mNumCategories;
50
  private int[][] mObjectIndices;
51
  private int[] mIconIDs;
52
  private String[] mTitles;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  public RubikObjectCategories(int sortMode)
57
    {
58
    switch(sortMode)
59
      {
60
      case SORT_CATEGORY  : buildSortCategory(); break;
61
      case SORT_DIFFICULTY: buildSortDifficulty(); break;
62
      case SORT_AUTHOR    : buildSortAuthor(); break;
63
      case SORT_YEAR      : buildSortYear(); break;
64
      }
65

    
66
    mNumCategories = mObjectIndices.length;
67

    
68
    switch(sortMode)
69
      {
70
      case SORT_CATEGORY  : buildIconsCategory(); break;
71
      case SORT_DIFFICULTY: buildIconsDifficulty(); break;
72
      case SORT_AUTHOR    : buildTitleAuthor(); break;
73
      case SORT_YEAR      : buildTitleYear(); break;
74
      }
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private void buildSortStr(String[] data)
80
    {
81
    int numObjects = data.length;
82
    String[] sorted = new String[numObjects];
83
    System.arraycopy(data, 0, sorted, 0, numObjects);
84

    
85
    Arrays.sort(sorted);
86

    
87
    String[] categories = new String[numObjects];
88
    int numCategories = 0;
89

    
90
    for(int o=0; o<numObjects; o++)
91
      if( o==0 || !sorted[o-1].equals(sorted[o]) )
92
        {
93
        categories[numCategories] = sorted[o];
94
        numCategories++;
95
        }
96

    
97
    int lastChange = -1;
98
    int curr = 0;
99
    int[] numInCategory = new int[numCategories];
100

    
101
    for(int o=0; o<numObjects; o++)
102
      if( o==numObjects-1 || !sorted[o].equals(sorted[o+1]) )
103
        {
104
        numInCategory[curr] = o-lastChange;
105
        curr++;
106
        lastChange = o;
107
        }
108

    
109
    mObjectIndices = new int[numCategories][];
110

    
111
    for(int c=0; c<numCategories; c++)
112
      {
113
      mObjectIndices[c] = new int[numInCategory[c]];
114

    
115
      String cat = categories[c];
116
      int index = 0;
117

    
118
      for(int o=0; o<numObjects; o++)
119
        if( data[o].equals(cat) ) mObjectIndices[c][index++] = o;
120
      }
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private void buildSortInt(int[] data)
126
    {
127
    int numObjects = data.length;
128
    int[] sorted = new int[numObjects];
129
    System.arraycopy(data, 0, sorted, 0, numObjects);
130

    
131
    Arrays.sort(sorted);
132

    
133
    int[] categories = new int[numObjects];
134
    int numCategories = 0;
135

    
136
    for(int o=0; o<numObjects; o++)
137
      if( o==0 || sorted[o-1]!=sorted[o] )
138
        {
139
        categories[numCategories] = sorted[o];
140
        numCategories++;
141
        }
142

    
143
    int lastChange = -1;
144
    int curr = 0;
145
    int[] numInCategory = new int[numCategories];
146

    
147
    for(int o=0; o<numObjects; o++)
148
      if( o==numObjects-1 || sorted[o]!=sorted[o+1] )
149
        {
150
        numInCategory[curr] = o-lastChange;
151
        curr++;
152
        lastChange = o;
153
        }
154

    
155
    mObjectIndices = new int[numCategories][];
156

    
157
    for(int c=0; c<numCategories; c++)
158
      {
159
      mObjectIndices[c] = new int[numInCategory[c]];
160

    
161
      int cat = categories[c];
162
      int index = 0;
163

    
164
      for(int o=0; o<numObjects; o++)
165
        if( data[o]==cat ) mObjectIndices[c][index++] = o;
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  private void buildSortFloat(float[] data)
172
    {
173
    int numObjects = data.length;
174
    float[] sorted = new float[numObjects];
175
    System.arraycopy(data, 0, sorted, 0, numObjects);
176

    
177
    Arrays.sort(sorted);
178

    
179
    float[] categories = new float[numObjects];
180
    int numCategories = 0;
181

    
182
    for(int o=0; o<numObjects; o++)
183
      if( o==0 || sorted[o-1]!=sorted[o] )
184
        {
185
        categories[numCategories] = sorted[o];
186
        numCategories++;
187
        }
188

    
189
    int lastChange = -1;
190
    int curr = 0;
191
    int[] numInCategory = new int[numCategories];
192

    
193
    for(int o=0; o<numObjects; o++)
194
      if( o==numObjects-1 || sorted[o]!=sorted[o+1] )
195
        {
196
        numInCategory[curr] = o-lastChange;
197
        curr++;
198
        lastChange = o;
199
        }
200

    
201
    mObjectIndices = new int[numCategories][];
202

    
203
    for(int c=0; c<numCategories; c++)
204
      {
205
      mObjectIndices[c] = new int[numInCategory[c]];
206

    
207
      float cat = categories[c];
208
      int index = 0;
209

    
210
      for(int o=0; o<numObjects; o++)
211
        if( data[o]==cat ) mObjectIndices[c][index++] = o;
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  private void buildSortCategory()
218
    {
219
    int numObjects = RubikObjectList.getNumObjects();
220
    int[] cats = new int[numObjects];
221

    
222
    for(int o=0; o<numObjects; o++)
223
      {
224
      RubikObject obj = RubikObjectList.getObject(o);
225
      cats[o] = obj==null ? 0 : obj.getCategory();
226
      }
227

    
228
    buildSortInt(cats);
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  private void buildSortDifficulty()
234
    {
235
    int numObjects = RubikObjectList.getNumObjects();
236
    float[] diffs = new float[numObjects];
237

    
238
    for(int o=0; o<numObjects; o++)
239
      {
240
      RubikObject obj = RubikObjectList.getObject(o);
241
      diffs[o] = obj==null ? 0 : obj.getDifficulty();
242
      }
243

    
244
    buildSortFloat(diffs);
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  private void buildSortAuthor()
250
    {
251
    int numObjects = RubikObjectList.getNumObjects();
252
    String[] auths = new String[numObjects];
253

    
254
    for(int o=0; o<numObjects; o++)
255
      {
256
      RubikObject obj = RubikObjectList.getObject(o);
257
      auths[o] = obj==null ? "" : obj.getAuthor();
258
      }
259

    
260
    buildSortStr(auths);
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private void buildSortYear()
266
    {
267
    int numObjects = RubikObjectList.getNumObjects();
268
    int[] years = new int[numObjects];
269

    
270
    for(int o=0; o<numObjects; o++)
271
      {
272
      RubikObject obj = RubikObjectList.getObject(o);
273
      years[o] = obj==null ? 0 : obj.getYearOfInvention();
274
      }
275

    
276
    buildSortInt(years);
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  private void buildIconsCategory()
282
    {
283
    mIconIDs = new int[mNumCategories];
284

    
285
    for(int t=0; t<mNumCategories; t++)
286
      {
287
      int obj = mObjectIndices[t][0];
288
      RubikObject object = RubikObjectList.getObject(obj);
289
      int category = object==null ? 0 : object.getCategory();
290
      mIconIDs[t] = CATEGORY_IMAGES[category%5];  //TODO
291
      }
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  private void buildIconsDifficulty()
297
    {
298
    mIconIDs = new int[mNumCategories];
299

    
300
    for(int t=0; t<mNumCategories; t++)
301
      {
302
      int obj = mObjectIndices[t][0];
303
      RubikObject object = RubikObjectList.getObject(obj);
304
      int difficulty = object==null ? 0 : (int)object.getDifficulty();
305
      mIconIDs[t] = DIFF_IMAGES[difficulty];
306
      }
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  private void buildTitleAuthor()
312
    {
313
    mTitles = new String[mNumCategories];
314

    
315
    for(int t=0; t<mNumCategories; t++)
316
      {
317
      int obj = mObjectIndices[t][0];
318
      RubikObject object = RubikObjectList.getObject(obj);
319
      mTitles[t] = object==null ? "" : object.getAuthor();
320
      }
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  private void buildTitleYear()
326
    {
327
    mTitles = new String[mNumCategories];
328

    
329
    for(int t=0; t<mNumCategories; t++)
330
      {
331
      int obj = mObjectIndices[t][0];
332
      RubikObject object = RubikObjectList.getObject(obj);
333
      int year = object==null ? 0 : object.getYearOfInvention();
334
      mTitles[t] = String.valueOf(year);
335
      }
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
// PUBLIC API
340

    
341
  public int getNumCategories()
342
    {
343
    return mNumCategories;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public boolean hasIcons()
349
    {
350
    return mIconIDs!=null;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  public int getIconId(int category)
356
    {
357
    return ( mIconIDs!=null && category>=0 && category<mNumCategories ) ? mIconIDs[category] : -1;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  public String getTitle(int category)
363
    {
364
    return (mTitles!=null && category>=0 && category<mNumCategories) ? mTitles[category] : null;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  public int getNumObjects(int category)
370
    {
371
    return (category>=0 && category<mNumCategories) ? mObjectIndices[category].length : 0;
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public int getObjectIndex(int category, int index)
377
    {
378
    if( category>=0 && category<mNumCategories )
379
      {
380
      int[] objects = mObjectIndices[category];
381
      if( index>=0 && index<objects.length ) return objects[index];
382
      }
383

    
384
    return -1;
385
    }
386

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

    
389
  public int getCategory(int objectIndex)
390
    {
391
    int numC = mObjectIndices.length;
392

    
393
    for(int c=0; c<numC; c++)
394
      for(int object : mObjectIndices[c])
395
        if( object==objectIndex ) return c;
396

    
397
    return -1;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  public int getPosInCat(int category, int objectIndex)
403
    {
404
    int[] objects = mObjectIndices[category];
405
    int len = objects.length;
406

    
407
    for(int o=0; o<len; o++)
408
      if( objects[o]==objectIndex ) return o;
409

    
410
    return -1;
411
    }
412
}
(2-2/3)