Project

General

Profile

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

magiccube / src / main / java / org / distorted / patterns / RubikPattern.java @ b498f3f6

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.magic.RubikPostRender;
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 = 800;
34

    
35
  private int[] numCategories   = new int[NUM_OBJECTS];
36
  private int[] currentCategory = new int[NUM_OBJECTS];
37
  private int[] currentScrollPos= new int[NUM_OBJECTS];
38

    
39
  private List<List<Category>> mCategories;
40
  private static RubikPattern mThis;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  private static class Category
45
    {
46
    private String[] mLines;
47
    private int numPatterns;
48
    private ArrayList<Pattern> mPatterns;
49
    private boolean mInitialized;
50

    
51
  /////////////////////////////////////////////////////////////
52

    
53
    Category(String[] lines)
54
      {
55
      mLines       = lines;
56
      numPatterns  = lines.length-1;
57
      mInitialized = false;
58
      }
59

    
60
  /////////////////////////////////////////////////////////////
61

    
62
    void initialize()
63
      {
64
      int colon;
65
      String moves, name;
66
      Pattern pattern;
67

    
68
      mPatterns = new ArrayList<>();
69

    
70
      for(int i=0; i<numPatterns; i++)
71
        {
72
        colon = mLines[i+1].indexOf(":");
73

    
74
        if( colon!=-1 )
75
          {
76
          moves   = mLines[i+1].substring(colon+1);
77
          name    = mLines[i+1].substring(0,colon);
78
          pattern = new Pattern(name,moves);
79

    
80
          mPatterns.add(pattern);
81
          }
82
        else
83
          {
84
          numPatterns--;
85
          }
86
        }
87

    
88
      mInitialized = true;
89
      }
90

    
91
  /////////////////////////////////////////////////////////////
92

    
93
    int getNumPatterns()
94
      {
95
      return numPatterns;
96
      }
97

    
98
  /////////////////////////////////////////////////////////////
99

    
100
    String getName()
101
      {
102
      return mLines[0];
103
      }
104

    
105
  /////////////////////////////////////////////////////////////
106

    
107
    String getPatternName(int pattern)
108
      {
109
      if( !mInitialized ) initialize();
110

    
111
      if( pattern>=0 && pattern<numPatterns )
112
        {
113
        Pattern p = mPatterns.get(pattern);
114
        return  p!=null ? p.getName():"";
115
        }
116

    
117
      return "";
118
      }
119

    
120
  /////////////////////////////////////////////////////////////
121

    
122
    int getPatternCurMove(int pattern)
123
      {
124
      if( !mInitialized ) initialize();
125

    
126
      if( pattern>=0 && pattern<numPatterns )
127
        {
128
        Pattern p = mPatterns.get(pattern);
129
        return  p!=null ? p.getCurMove():-1;
130
        }
131

    
132
      return -1;
133
      }
134

    
135
  /////////////////////////////////////////////////////////////
136

    
137
    int getPatternNumMove(int pattern)
138
      {
139
      if( !mInitialized ) initialize();
140

    
141
      if( pattern>=0 && pattern<numPatterns )
142
        {
143
        Pattern p = mPatterns.get(pattern);
144
        return  p!=null ? p.getNumMove():-1;
145
        }
146

    
147
      return -1;
148
      }
149

    
150
  /////////////////////////////////////////////////////////////
151

    
152
    void makeMove(RubikPostRender post, int pattern)
153
      {
154
      if( !mInitialized ) initialize();
155

    
156
      if( pattern>=0 && pattern<numPatterns )
157
        {
158
        Pattern p = mPatterns.get(pattern);
159
        if( p!=null ) p.makeMove(post);
160
        }
161
      }
162

    
163
  /////////////////////////////////////////////////////////////
164

    
165
    void backMove(RubikPostRender post, int pattern)
166
      {
167
      if( !mInitialized ) initialize();
168

    
169
      if( pattern>=0 && pattern<numPatterns )
170
        {
171
        Pattern p = mPatterns.get(pattern);
172
        if( p!=null ) p.backMove(post);
173
        }
174
      }
175

    
176
  /////////////////////////////////////////////////////////////
177

    
178
    int[][] reInitialize(int pattern)
179
      {
180
      if( !mInitialized ) initialize();
181

    
182
      if( pattern>=0 && pattern<numPatterns )
183
        {
184
        Pattern p = mPatterns.get(pattern);
185
        if( p!=null ) return p.reInitialize();
186
        }
187

    
188
      return null;
189
      }
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private static class Pattern implements RubikPostRender.ActionFinishedListener
195
    {
196
    private String nameStr, moveStr;
197
    private int[][] moves;
198
    private int curMove;
199
    private int numMove;
200
    private boolean mCanRotate;
201
    private boolean mInitialized;
202

    
203
  /////////////////////////////////////////////////////////////
204

    
205
    Pattern(String name, String moves)
206
      {
207
      nameStr      = name;
208
      moveStr      = moves;
209
      mCanRotate   = true;
210
      mInitialized = false;
211
      }
212

    
213
  /////////////////////////////////////////////////////////////
214

    
215
    private void initialize()
216
      {
217
      numMove = moveStr.length()/4;
218
      moves   = new int[numMove][3];
219
      curMove = numMove;
220

    
221
      int digit0, digit1, digit2;
222

    
223
      for(int i=0; i<numMove; i++)
224
        {
225
        digit0 = moveStr.charAt(4*i+1)-'0';
226
        digit1 = moveStr.charAt(4*i+2)-'0';
227
        digit2 = moveStr.charAt(4*i+3)-'0';
228

    
229
        moves[i][0] = (10*digit0+digit1)/32;
230
        moves[i][1] = (10*digit0+digit1)%32;
231
        moves[i][2] = 2-digit2;
232
        }
233

    
234
      moveStr = null;
235
      mInitialized = true;
236
      }
237

    
238
  /////////////////////////////////////////////////////////////
239

    
240
    String getName()
241
      {
242
      return nameStr;
243
      }
244

    
245
  /////////////////////////////////////////////////////////////
246

    
247
    int getNumMove()
248
      {
249
      if( !mInitialized ) initialize();
250

    
251
      return numMove;
252
      }
253

    
254
  /////////////////////////////////////////////////////////////
255

    
256
    int getCurMove()
257
      {
258
      if( !mInitialized ) initialize();
259

    
260
      return curMove;
261
      }
262

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

    
265
    void makeMove(RubikPostRender post)
266
      {
267
      if( !mInitialized ) initialize();
268

    
269
      curMove++;
270

    
271
      if( mCanRotate )
272
        {
273
        if( curMove>numMove )
274
          {
275
          curMove= 0;
276
          post.initializeObject(null);
277
          }
278
        else
279
          {
280
          int axis     = moves[curMove-1][0];
281
		      int rowBitmap= moves[curMove-1][1];
282
		      int bareAngle= moves[curMove-1][2];
283
          int angle    = bareAngle*(360/post.getObject().getBasicAngle());
284
          int numRot   = Math.abs(bareAngle);
285

    
286
          if( angle!=0 )
287
            {
288
            mCanRotate = false;
289
            post.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
290
            }
291
          else
292
            {
293
            android.util.Log.e("pattern", "error: pattern "+nameStr+" move "+(curMove-1)+" angle 0");
294
            }
295
          }
296
        }
297
      else
298
        {
299
        android.util.Log.e("pattern", "failed to make Move!");
300
        curMove--;
301
        }
302
      }
303

    
304
  /////////////////////////////////////////////////////////////
305

    
306
    void backMove(RubikPostRender post)
307
      {
308
      if( !mInitialized ) initialize();
309

    
310
      curMove--;
311

    
312
      if( mCanRotate )
313
        {
314
        if( curMove<0 )
315
          {
316
          curMove=numMove;
317
          post.initializeObject(moves);
318
          }
319
        else
320
          {
321
          int axis     = moves[curMove][0];
322
		      int rowBitmap= moves[curMove][1];
323
		      int bareAngle= moves[curMove][2];
324
          int angle    = bareAngle*(360/post.getObject().getBasicAngle());
325
          int numRot   = Math.abs(bareAngle);
326

    
327
          if( angle!=0 )
328
            {
329
            mCanRotate = false;
330
            post.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
331
            }
332
          else
333
            {
334
            android.util.Log.e("pattern", "error: pattern "+nameStr+" move "+curMove+" angle 0");
335
            }
336
          }
337
        }
338
      else
339
        {
340
        android.util.Log.e("pattern", "failed to back Move!");
341
        curMove++;
342
        }
343
      }
344

    
345
  /////////////////////////////////////////////////////////////
346

    
347
    int[][] reInitialize()
348
      {
349
      if( !mInitialized ) initialize();
350

    
351
      mCanRotate = true;
352
      curMove = numMove;
353
      return moves;
354
      }
355

    
356
  /////////////////////////////////////////////////////////////
357

    
358
    public void onActionFinished(final long effectID)
359
      {
360
      mCanRotate = true;
361
      }
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  private RubikPattern()
367
    {
368
    mCategories = new ArrayList<>();
369

    
370
    for(int i=0; i<NUM_OBJECTS; i++)
371
      {
372
      List<Category> list = new ArrayList<>();
373
      String[][] patStrings = RubikPatternList.getPatterns(i);
374

    
375
      for(String[] lines: patStrings) list.add(new Category(lines));
376

    
377
      mCategories.add(list);
378
      numCategories[i]=patStrings.length;
379
      }
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  private Category getCategory(int tab, int cat)
385
    {
386
    if( tab>=0 && tab<NUM_OBJECTS && cat>=0 && cat<numCategories[tab] )
387
      {
388
      return mCategories.get(tab).get(cat);
389
      }
390

    
391
    return null;
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// PUBLIC API
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public static RubikPattern getInstance()
399
    {
400
    if( mThis==null )
401
      {
402
      mThis = new RubikPattern();
403
      }
404

    
405
    return mThis;
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  public int getNumCategories(int tab)
411
    {
412
    return tab>=0 && tab<NUM_OBJECTS ? numCategories[tab] : 0;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public void rememberState(int tab, int cat, int scrollPos)
418
    {
419
    if( tab>=0 && tab<NUM_OBJECTS )
420
      {
421
      currentCategory[tab] = cat;
422
      currentScrollPos[tab]= scrollPos;
423
      }
424
    }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

    
428
  public int recallCategory(int tab)
429
    {
430
    return tab>=0 && tab<NUM_OBJECTS ? currentCategory[tab] : 0;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public int recallScrollPos(int tab)
436
    {
437
    return tab>=0 && tab<NUM_OBJECTS ? currentScrollPos[tab] : 0;
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  public String getCategoryName(int tab, int cat)
443
    {
444
    Category c = getCategory(tab,cat);
445
    return c!=null ? c.getName() : null;
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
  public String getPatternName(int tab, int cat, int pat)
451
    {
452
    Category c = getCategory(tab,cat);
453
    return c!=null ? c.getPatternName(pat) : null;
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  public int getNumPatterns(int tab, int cat)
459
    {
460
    Category c = getCategory(tab,cat);
461
    return c!=null ? c.getNumPatterns() : 0;
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  public int getCurMove(int tab, int cat, int pat)
467
    {
468
    Category c = getCategory(tab,cat);
469
    return c!=null ? c.getPatternCurMove(pat) : 0;
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public int getNumMoves(int tab, int cat, int pat)
475
    {
476
    Category c = getCategory(tab,cat);
477
    return c!=null ? c.getPatternNumMove(pat) : 0;
478
    }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
  public void makeMove(RubikPostRender post, int tab, int cat, int pat)
483
    {
484
    Category c = getCategory(tab,cat);
485
    if( c!=null ) c.makeMove(post,pat);
486
    }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
  public void backMove(RubikPostRender post, int tab, int cat, int pat)
491
    {
492
    Category c = getCategory(tab,cat);
493
    if( c!=null ) c.backMove(post,pat);
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public int[][] reInitialize(int tab, int cat, int pat)
499
    {
500
    Category c = getCategory(tab,cat);
501
    return c!=null ? c.reInitialize(pat) : null;
502
    }
503
}
(1-1/6)