1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.patterns;
|
21
|
|
22
|
import org.distorted.main.RubikPreRender;
|
23
|
|
24
|
import java.util.ArrayList;
|
25
|
import java.util.List;
|
26
|
|
27
|
import static org.distorted.patterns.RubikPatternList.NUM_OBJECTS;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
public class RubikPattern
|
32
|
{
|
33
|
private static final int DURATION_MILLIS = 750;
|
34
|
|
35
|
private int[] numCategories = new int[NUM_OBJECTS];
|
36
|
private int[] currentCategory = new int[NUM_OBJECTS];
|
37
|
private int[] currentVisiblePos= new int[NUM_OBJECTS];
|
38
|
private int[] currentExpanded = new int[NUM_OBJECTS];
|
39
|
|
40
|
private List<List<Category>> mCategories;
|
41
|
private static RubikPattern mThis;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
private static class Category
|
46
|
{
|
47
|
private String[] mLines;
|
48
|
private int numPatterns;
|
49
|
private ArrayList<Pattern> mPatterns;
|
50
|
private boolean mInitialized;
|
51
|
|
52
|
/////////////////////////////////////////////////////////////
|
53
|
|
54
|
Category(String[] lines)
|
55
|
{
|
56
|
mLines = lines;
|
57
|
numPatterns = lines.length-1;
|
58
|
mInitialized = false;
|
59
|
}
|
60
|
|
61
|
/////////////////////////////////////////////////////////////
|
62
|
|
63
|
void initialize()
|
64
|
{
|
65
|
int colon;
|
66
|
String moves, name;
|
67
|
Pattern pattern;
|
68
|
|
69
|
mPatterns = new ArrayList<>();
|
70
|
|
71
|
for(int i=0; i<numPatterns; i++)
|
72
|
{
|
73
|
colon = mLines[i+1].indexOf(":");
|
74
|
|
75
|
if( colon!=-1 )
|
76
|
{
|
77
|
moves = mLines[i+1].substring(colon+1);
|
78
|
name = mLines[i+1].substring(0,colon);
|
79
|
pattern = new Pattern(name,moves);
|
80
|
|
81
|
mPatterns.add(pattern);
|
82
|
}
|
83
|
else
|
84
|
{
|
85
|
numPatterns--;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
mInitialized = true;
|
90
|
}
|
91
|
|
92
|
/////////////////////////////////////////////////////////////
|
93
|
|
94
|
int getNumPatterns()
|
95
|
{
|
96
|
return numPatterns;
|
97
|
}
|
98
|
|
99
|
/////////////////////////////////////////////////////////////
|
100
|
|
101
|
String getName()
|
102
|
{
|
103
|
return mLines[0];
|
104
|
}
|
105
|
|
106
|
/////////////////////////////////////////////////////////////
|
107
|
|
108
|
String getPatternName(int pattern)
|
109
|
{
|
110
|
if( !mInitialized ) initialize();
|
111
|
|
112
|
if( pattern>=0 && pattern<numPatterns )
|
113
|
{
|
114
|
Pattern p = mPatterns.get(pattern);
|
115
|
return p!=null ? p.getName():"";
|
116
|
}
|
117
|
|
118
|
return "";
|
119
|
}
|
120
|
|
121
|
/////////////////////////////////////////////////////////////
|
122
|
|
123
|
int getPatternCurMove(int pattern)
|
124
|
{
|
125
|
if( !mInitialized ) initialize();
|
126
|
|
127
|
if( pattern>=0 && pattern<numPatterns )
|
128
|
{
|
129
|
Pattern p = mPatterns.get(pattern);
|
130
|
return p!=null ? p.getCurMove():-1;
|
131
|
}
|
132
|
|
133
|
return -1;
|
134
|
}
|
135
|
|
136
|
/////////////////////////////////////////////////////////////
|
137
|
|
138
|
int getPatternNumMove(int pattern)
|
139
|
{
|
140
|
if( !mInitialized ) initialize();
|
141
|
|
142
|
if( pattern>=0 && pattern<numPatterns )
|
143
|
{
|
144
|
Pattern p = mPatterns.get(pattern);
|
145
|
return p!=null ? p.getNumMove():-1;
|
146
|
}
|
147
|
|
148
|
return -1;
|
149
|
}
|
150
|
|
151
|
/////////////////////////////////////////////////////////////
|
152
|
|
153
|
void makeMove(RubikPreRender pre, int pattern)
|
154
|
{
|
155
|
if( !mInitialized ) initialize();
|
156
|
|
157
|
if( pattern>=0 && pattern<numPatterns )
|
158
|
{
|
159
|
Pattern p = mPatterns.get(pattern);
|
160
|
if( p!=null ) p.makeMove(pre);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
/////////////////////////////////////////////////////////////
|
165
|
|
166
|
void backMove(RubikPreRender pre, int pattern)
|
167
|
{
|
168
|
if( !mInitialized ) initialize();
|
169
|
|
170
|
if( pattern>=0 && pattern<numPatterns )
|
171
|
{
|
172
|
Pattern p = mPatterns.get(pattern);
|
173
|
if( p!=null ) p.backMove(pre);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/////////////////////////////////////////////////////////////
|
178
|
|
179
|
int[][] reInitialize(int pattern)
|
180
|
{
|
181
|
if( !mInitialized ) initialize();
|
182
|
|
183
|
if( pattern>=0 && pattern<numPatterns )
|
184
|
{
|
185
|
Pattern p = mPatterns.get(pattern);
|
186
|
if( p!=null ) return p.reInitialize();
|
187
|
}
|
188
|
|
189
|
return null;
|
190
|
}
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
private static class Pattern implements RubikPreRender.ActionFinishedListener
|
196
|
{
|
197
|
private String nameStr, moveStr;
|
198
|
private int[][] moves;
|
199
|
private int curMove;
|
200
|
private int numMove;
|
201
|
private boolean mCanRotate;
|
202
|
private boolean mInitialized;
|
203
|
|
204
|
/////////////////////////////////////////////////////////////
|
205
|
|
206
|
Pattern(String name, String moves)
|
207
|
{
|
208
|
nameStr = name;
|
209
|
moveStr = moves;
|
210
|
mCanRotate = true;
|
211
|
mInitialized = false;
|
212
|
}
|
213
|
|
214
|
/////////////////////////////////////////////////////////////
|
215
|
|
216
|
private void initialize()
|
217
|
{
|
218
|
numMove = moveStr.length()/4;
|
219
|
moves = new int[numMove][3];
|
220
|
curMove = numMove;
|
221
|
parseMoves(moves,numMove,moveStr);
|
222
|
moveStr = null;
|
223
|
mInitialized = true;
|
224
|
}
|
225
|
|
226
|
/////////////////////////////////////////////////////////////
|
227
|
|
228
|
String getName()
|
229
|
{
|
230
|
return nameStr;
|
231
|
}
|
232
|
|
233
|
/////////////////////////////////////////////////////////////
|
234
|
|
235
|
int getNumMove()
|
236
|
{
|
237
|
if( !mInitialized ) initialize();
|
238
|
|
239
|
return numMove;
|
240
|
}
|
241
|
|
242
|
/////////////////////////////////////////////////////////////
|
243
|
|
244
|
int getCurMove()
|
245
|
{
|
246
|
if( !mInitialized ) initialize();
|
247
|
|
248
|
return curMove;
|
249
|
}
|
250
|
|
251
|
/////////////////////////////////////////////////////////////
|
252
|
|
253
|
void makeMove(RubikPreRender pre)
|
254
|
{
|
255
|
if( !mInitialized ) initialize();
|
256
|
|
257
|
if( mCanRotate )
|
258
|
{
|
259
|
curMove++;
|
260
|
|
261
|
if( curMove>numMove )
|
262
|
{
|
263
|
curMove= 0;
|
264
|
pre.initializeObject(null);
|
265
|
}
|
266
|
else
|
267
|
{
|
268
|
int axis = moves[curMove-1][0];
|
269
|
int rowBitmap= moves[curMove-1][1];
|
270
|
int bareAngle= moves[curMove-1][2];
|
271
|
int angle = bareAngle*(360/pre.getObject().getBasicAngle());
|
272
|
int numRot = Math.abs(bareAngle);
|
273
|
|
274
|
if( angle!=0 )
|
275
|
{
|
276
|
mCanRotate = false;
|
277
|
pre.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
|
278
|
}
|
279
|
else
|
280
|
{
|
281
|
android.util.Log.e("pattern", "error: pattern "+nameStr+" move "+(curMove-1)+" angle 0");
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
else
|
286
|
{
|
287
|
android.util.Log.e("pattern", "failed to make Move!");
|
288
|
}
|
289
|
}
|
290
|
|
291
|
/////////////////////////////////////////////////////////////
|
292
|
|
293
|
void backMove(RubikPreRender pre)
|
294
|
{
|
295
|
if( !mInitialized ) initialize();
|
296
|
|
297
|
if( mCanRotate )
|
298
|
{
|
299
|
curMove--;
|
300
|
|
301
|
if( curMove<0 )
|
302
|
{
|
303
|
curMove=numMove;
|
304
|
pre.initializeObject(moves);
|
305
|
}
|
306
|
else
|
307
|
{
|
308
|
int axis = moves[curMove][0];
|
309
|
int rowBitmap= moves[curMove][1];
|
310
|
int bareAngle= moves[curMove][2];
|
311
|
int angle = bareAngle*(360/pre.getObject().getBasicAngle());
|
312
|
int numRot = Math.abs(bareAngle);
|
313
|
|
314
|
if( angle!=0 )
|
315
|
{
|
316
|
mCanRotate = false;
|
317
|
pre.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
|
318
|
}
|
319
|
else
|
320
|
{
|
321
|
android.util.Log.e("pattern", "error: pattern "+nameStr+" move "+curMove+" angle 0");
|
322
|
}
|
323
|
}
|
324
|
}
|
325
|
else
|
326
|
{
|
327
|
android.util.Log.e("pattern", "failed to back Move!");
|
328
|
}
|
329
|
}
|
330
|
|
331
|
/////////////////////////////////////////////////////////////
|
332
|
|
333
|
int[][] reInitialize()
|
334
|
{
|
335
|
if( !mInitialized ) initialize();
|
336
|
|
337
|
mCanRotate = true;
|
338
|
curMove = numMove;
|
339
|
return moves;
|
340
|
}
|
341
|
|
342
|
/////////////////////////////////////////////////////////////
|
343
|
|
344
|
public void onActionFinished(final long effectID)
|
345
|
{
|
346
|
mCanRotate = true;
|
347
|
}
|
348
|
}
|
349
|
|
350
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
351
|
|
352
|
private RubikPattern()
|
353
|
{
|
354
|
mCategories = new ArrayList<>();
|
355
|
|
356
|
for(int i=0; i<NUM_OBJECTS; i++)
|
357
|
{
|
358
|
List<Category> list = new ArrayList<>();
|
359
|
String[][] patStrings = RubikPatternList.getPatterns(i);
|
360
|
|
361
|
for(String[] lines: patStrings) list.add(new Category(lines));
|
362
|
|
363
|
mCategories.add(list);
|
364
|
numCategories[i]=patStrings.length;
|
365
|
|
366
|
currentExpanded[i] = -1;
|
367
|
}
|
368
|
}
|
369
|
|
370
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
371
|
|
372
|
private Category getCategory(int tab, int cat)
|
373
|
{
|
374
|
if( tab>=0 && tab<NUM_OBJECTS && cat>=0 && cat<numCategories[tab] )
|
375
|
{
|
376
|
return mCategories.get(tab).get(cat);
|
377
|
}
|
378
|
|
379
|
return null;
|
380
|
}
|
381
|
|
382
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
383
|
// PUBLIC API
|
384
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
385
|
|
386
|
public static RubikPattern getInstance()
|
387
|
{
|
388
|
if( mThis==null )
|
389
|
{
|
390
|
mThis = new RubikPattern();
|
391
|
}
|
392
|
|
393
|
return mThis;
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
public static void parseMoves(int[][] result, int numMoves, String moves)
|
399
|
{
|
400
|
int digit0, digit1, digit2, number;
|
401
|
|
402
|
for(int i=0; i<numMoves; i++)
|
403
|
{
|
404
|
digit0 = moves.charAt(4*i+1)-'0';
|
405
|
digit1 = moves.charAt(4*i+2)-'0';
|
406
|
digit2 = moves.charAt(4*i+3)-'0';
|
407
|
number = 100*digit0+10*digit1+digit2;
|
408
|
|
409
|
result[i][0] = (number/32)%4; // axis
|
410
|
result[i][1] = (number%32) ; // bitmap
|
411
|
result[i][2] =2-(number/32)/4; // angle
|
412
|
}
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
|
417
|
public int getNumCategories(int tab)
|
418
|
{
|
419
|
return tab>=0 && tab<NUM_OBJECTS ? numCategories[tab] : 0;
|
420
|
}
|
421
|
|
422
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
423
|
|
424
|
public void rememberState(int tab, int cat, int scrollPos, int expanded)
|
425
|
{
|
426
|
if( tab>=0 && tab<NUM_OBJECTS )
|
427
|
{
|
428
|
currentCategory[tab] = cat;
|
429
|
currentVisiblePos[tab]= scrollPos;
|
430
|
currentExpanded[tab] = expanded;
|
431
|
}
|
432
|
}
|
433
|
|
434
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435
|
|
436
|
public int recallCategory(int tab)
|
437
|
{
|
438
|
return tab>=0 && tab<NUM_OBJECTS ? currentCategory[tab] : 0;
|
439
|
}
|
440
|
|
441
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
442
|
|
443
|
public int recallVisiblePos(int tab)
|
444
|
{
|
445
|
return tab>=0 && tab<NUM_OBJECTS ? currentVisiblePos[tab] : 0;
|
446
|
}
|
447
|
|
448
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
449
|
|
450
|
public int recallExpanded(int tab)
|
451
|
{
|
452
|
return tab>=0 && tab<NUM_OBJECTS ? currentExpanded[tab] : -1;
|
453
|
}
|
454
|
|
455
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
456
|
|
457
|
public String getCategoryName(int tab, int cat)
|
458
|
{
|
459
|
Category c = getCategory(tab,cat);
|
460
|
return c!=null ? c.getName() : null;
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
|
465
|
public String getPatternName(int tab, int cat, int pat)
|
466
|
{
|
467
|
Category c = getCategory(tab,cat);
|
468
|
return c!=null ? c.getPatternName(pat) : null;
|
469
|
}
|
470
|
|
471
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
472
|
|
473
|
public int getNumPatterns(int tab, int cat)
|
474
|
{
|
475
|
Category c = getCategory(tab,cat);
|
476
|
return c!=null ? c.getNumPatterns() : 0;
|
477
|
}
|
478
|
|
479
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
480
|
|
481
|
public int getCurMove(int tab, int cat, int pat)
|
482
|
{
|
483
|
Category c = getCategory(tab,cat);
|
484
|
return c!=null ? c.getPatternCurMove(pat) : 0;
|
485
|
}
|
486
|
|
487
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
488
|
|
489
|
public int getNumMoves(int tab, int cat, int pat)
|
490
|
{
|
491
|
Category c = getCategory(tab,cat);
|
492
|
return c!=null ? c.getPatternNumMove(pat) : 0;
|
493
|
}
|
494
|
|
495
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
496
|
|
497
|
public void makeMove(RubikPreRender pre, int tab, int cat, int pat)
|
498
|
{
|
499
|
Category c = getCategory(tab,cat);
|
500
|
if( c!=null ) c.makeMove(pre,pat);
|
501
|
}
|
502
|
|
503
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
504
|
|
505
|
public void backMove(RubikPreRender pre, int tab, int cat, int pat)
|
506
|
{
|
507
|
Category c = getCategory(tab,cat);
|
508
|
if( c!=null ) c.backMove(pre,pat);
|
509
|
}
|
510
|
|
511
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
512
|
|
513
|
public int[][] reInitialize(int tab, int cat, int pat)
|
514
|
{
|
515
|
Category c = getCategory(tab,cat);
|
516
|
return c!=null ? c.reInitialize(pat) : null;
|
517
|
}
|
518
|
}
|