Revision 48e61813
Added by Leszek Koltunski 4 months ago
src/main/java/org/distorted/objects/RubikObjectCategories.java | ||
---|---|---|
13 | 13 |
|
14 | 14 |
import org.distorted.main.R; |
15 | 15 |
|
16 |
import java.util.Arrays; |
|
17 |
|
|
18 | 16 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
19 | 17 |
|
20 |
public class RubikObjectCategories |
|
18 |
public abstract class RubikObjectCategories
|
|
21 | 19 |
{ |
22 | 20 |
public static final int[] DIFF_IDS = |
23 | 21 |
{ |
... | ... | |
46 | 44 |
R.drawable.difficulty5, |
47 | 45 |
}; |
48 | 46 |
|
49 |
private final int mNumCategories;
|
|
50 |
private int[][] mObjectIndices;
|
|
51 |
private int[] mIconIDs;
|
|
52 |
private String[] mTitles;
|
|
47 |
protected final int mNumCategories;
|
|
48 |
protected int[] mIconIDs;
|
|
49 |
protected String[] mTitles;
|
|
50 |
protected int[][] mObjectIndices;
|
|
53 | 51 |
|
54 | 52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
55 | 53 |
|
56 |
public RubikObjectCategories(int sortMode)
|
|
54 |
public RubikObjectCategories() |
|
57 | 55 |
{ |
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 |
|
|
56 |
buildIndices(); |
|
66 | 57 |
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); |
|
58 |
buildTitle(); |
|
277 | 59 |
} |
278 | 60 |
|
279 | 61 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
280 | 62 |
|
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 |
} |
|
63 |
abstract void buildIndices(); |
|
64 |
abstract void buildTitle(); |
|
293 | 65 |
|
294 | 66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
295 | 67 |
|
296 |
private void buildIconsDifficulty()
|
|
68 |
public static RubikObjectCategories create(int sortMode)
|
|
297 | 69 |
{ |
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++) |
|
70 |
switch(sortMode) |
|
316 | 71 |
{ |
317 |
int obj = mObjectIndices[t][0]; |
|
318 |
RubikObject object = RubikObjectList.getObject(obj); |
|
319 |
mTitles[t] = object==null ? "" : object.getAuthor(); |
|
72 |
case SORT_CATEGORY : return new RubikObjectCategoriesCat(); |
|
73 |
case SORT_DIFFICULTY: return new RubikObjectCategoriesDiff(); |
|
74 |
case SORT_AUTHOR : return new RubikObjectCategoriesAuthor(); |
|
75 |
case SORT_YEAR : return new RubikObjectCategoriesYear(); |
|
320 | 76 |
} |
321 |
} |
|
322 | 77 |
|
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] = year==0 ? "Unknown" : String.valueOf(year); |
|
335 |
} |
|
78 |
android.util.Log.e("D", "sortMode = "+sortMode+" not implemented!"); |
|
79 |
return null; |
|
336 | 80 |
} |
337 | 81 |
|
338 | 82 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
394 | 138 |
for(int object : mObjectIndices[c]) |
395 | 139 |
if( object==objectIndex ) return c; |
396 | 140 |
|
141 |
android.util.Log.e("D", "returning category -1: index: "+objectIndex); |
|
142 |
|
|
397 | 143 |
return -1; |
398 | 144 |
} |
399 | 145 |
|
Also available in: Unified diff
progress