Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ dd874ae8

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.screens;
21

    
22
import android.content.Context;
23
import android.content.SharedPreferences;
24
import android.graphics.drawable.BitmapDrawable;
25
import android.os.Build;
26
import android.os.Bundle;
27
import android.util.TypedValue;
28
import android.view.Gravity;
29
import android.view.LayoutInflater;
30
import android.view.View;
31
import android.widget.Button;
32
import android.widget.GridLayout;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35
import android.widget.PopupWindow;
36
import android.widget.ScrollView;
37

    
38
import org.distorted.objectlib.main.ObjectControl;
39
import org.distorted.objectlib.main.ObjectType;
40

    
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.dialogs.RubikDialogAbout;
44
import org.distorted.dialogs.RubikDialogPattern;
45
import org.distorted.dialogs.RubikDialogScores;
46
import org.distorted.dialogs.RubikDialogTutorial;
47
import org.distorted.helpers.TransparentButton;
48
import org.distorted.helpers.TransparentImageButton;
49
import org.distorted.network.RubikScores;
50

    
51
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
public class RubikScreenPlay extends RubikScreenBase
56
  {
57
  public static final int NUM_COLUMNS  = 4;
58
  public static final int LEVELS_SHOWN = 10;
59
  public static int MAX_LEVEL;
60
  public static final ObjectType DEF_OBJECT= ObjectType.CUBE_3;
61

    
62
  private static final int[] BUTTON_LABELS = { R.string.scores,
63
                                               R.string.patterns,
64
                                               R.string.solver,
65
                                               R.string.tutorials,
66
                                               R.string.about };
67

    
68
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
69
  private static final float LAST_BUTTON = 1.5f;
70
  private static final int[] mLocation = new int[2];
71

    
72
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
73
  private Button mPlayButton;
74
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
75
  private ObjectType mObject = DEF_OBJECT;
76
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
77
  private int mLevelValue;
78
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
79
  private int mColCount, mRowCount, mMaxRowCount;
80
  private LinearLayout mPlayLayout;
81
  private int mUpperBarHeight;
82
  private boolean mShouldReactToEndOfScrambling;
83

    
84
  static
85
    {
86
    ObjectType[] types = ObjectType.values();
87
    int max = Integer.MIN_VALUE;
88

    
89
    for (ObjectType type : types)
90
      {
91
      int cur = getDBLevel(type);
92
      if( cur>max ) max = cur;
93
      }
94

    
95
    MAX_LEVEL = max;
96
    }
97

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

    
100
  void leaveScreen(RubikActivity act)
101
    {
102

    
103
    }
104

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

    
107
  void enterScreen(final RubikActivity act)
108
    {
109
    float width = act.getScreenWidthInPixels();
110
    mUpperBarHeight = act.getHeightUpperBar();
111

    
112
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
113
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
114
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
115

    
116
    mRowCount = (NUM_OBJECTS + NUM_COLUMNS-1) / NUM_COLUMNS;
117
    mColCount = NUM_COLUMNS;
118

    
119
    // TOP ////////////////////////////
120
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
121
    layoutTop.removeAllViews();
122

    
123
    setupObjectButton(act,width);
124
    layoutTop.addView(mObjButton);
125

    
126
    setupMenuButton(act,width);
127
    layoutTop.addView(mMenuButton);
128

    
129
    setupPlayButton(act,width);
130
    layoutTop.addView(mPlayButton);
131

    
132
    setupSolveButton(act);
133
    setupScrambleButton(act);
134
    createBottomPane(act,mSolveButton,mScrambleButton);
135
    }
136

    
137
//////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void setupObjectButton(final RubikActivity act, final float width)
140
    {
141
    final int margin  = (int)(width*RubikActivity.MARGIN);
142
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
143

    
144
    mObjButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_MIDDLE);
145

    
146
    mObjButton.setOnClickListener( new View.OnClickListener()
147
      {
148
      @Override
149
      public void onClick(View view)
150
        {
151
        if( mObjectPopup==null )
152
          {
153
          float width = act.getScreenWidthInPixels();
154
          float height= act.getScreenHeightInPixels();
155
          setupObjectWindow(act,width,height);
156
          }
157

    
158
        if( act.getControl().isUINotBlocked())
159
          {
160
          int rowCount = Math.min(mMaxRowCount,mRowCount);
161
          View popupView = mObjectPopup.getContentView();
162
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
163
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount,margin,margin);
164
          }
165
        }
166
      });
167
    }
168

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

    
171
  private void setupPlayButton(final RubikActivity act, final float width)
172
    {
173
    final int margin = (int)(width*RubikActivity.MARGIN);
174

    
175
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
176

    
177
    mPlayButton.setOnClickListener( new View.OnClickListener()
178
      {
179
      @Override
180
      public void onClick(View view)
181
        {
182
         if( mPlayPopup==null )
183
          {
184
          float width = act.getScreenWidthInPixels();
185
          setupPlayWindow(act,width);
186
          }
187

    
188
        if( act.getControl().isUINotBlocked())
189
          {
190
          float height= act.getScreenHeightInPixels();
191
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
192
          View popupView = mPlayPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194
          final int dbLevel = getDBLevel(mObject);
195
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
196
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
197
          final int realHeight = Math.min(popupHeight,maxHeight);
198
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
199
          }
200
        }
201
      });
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  private void setupMenuButton(final RubikActivity act, final float width)
207
    {
208
    final int margin = (int)(width*RubikActivity.MARGIN);
209
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_menu,R.drawable.ui_medium_menu, R.drawable.ui_big_menu, R.drawable.ui_huge_menu);
210

    
211
    mMenuButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_MIDDLE);
212

    
213
    mMenuButton.setOnClickListener( new View.OnClickListener()
214
      {
215
      @Override
216
      public void onClick(View view)
217
        {
218
        if( mMenuPopup==null )
219
          {
220
          float width = act.getScreenWidthInPixels();
221
          setupMenuWindow(act,width);
222
          }
223

    
224
        if( act.getControl().isUINotBlocked())
225
          {
226
          View popupView = mMenuPopup.getContentView();
227
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
228
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
229
          }
230
        }
231
      });
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
237
    {
238
    int icon = RubikActivity.getDrawable(R.drawable.cube_2s,R.drawable.cube_2m, R.drawable.cube_2b, R.drawable.cube_2h);
239

    
240
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
241
    int cubeWidth = bd.getIntrinsicWidth();
242
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
243
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
244
    mMaxRowCount = (int)(0.9f*(height-mUpperBarHeight)/mObjectSize);
245
    GridLayout objectGrid = new GridLayout(act);
246
    mObjectPopup = new PopupWindow(act);
247
    mObjectPopup.setFocusable(true);
248

    
249
    if( mMaxRowCount<mRowCount )
250
      {
251
      ScrollView scrollView = new ScrollView(act);
252
      scrollView.addView(objectGrid);
253
      mObjectPopup.setContentView(scrollView);
254
      }
255
    else
256
      {
257
      mObjectPopup.setContentView(objectGrid);
258
      }
259

    
260
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
261
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
262

    
263
    objectGrid.setColumnCount(mColCount);
264
    objectGrid.setRowCount(mRowCount);
265

    
266
    int[] nextInRow = new int[mRowCount];
267

    
268
    for(int row=0; row<mRowCount; row++)
269
      {
270
      rowSpecs[row] = GridLayout.spec(row);
271
      nextInRow[row]= 0;
272
      }
273
    for(int col=0; col<mColCount; col++)
274
      {
275
      colSpecs[col] = GridLayout.spec(col);
276
      }
277

    
278
    for(int object = 0; object< NUM_OBJECTS; object++)
279
      {
280
      final ObjectType type = ObjectType.getObject(object);
281
      int iconSize = RubikActivity.getDrawableSize();
282
      int icons = type.getIconID(iconSize);
283
      int row = object/NUM_COLUMNS;
284

    
285
      ImageButton button = new ImageButton(act);
286
      button.setBackgroundResource(icons);
287
      button.setOnClickListener( new View.OnClickListener()
288
        {
289
        @Override
290
        public void onClick(View v)
291
          {
292
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
293
            {
294
            mObject = type;
295
            act.changeObject(type, true);
296
            if( mPlayLayout!=null ) adjustLevels(act);
297
            mMovesController.clearMoves(act);
298
            }
299

    
300
          mObjectPopup.dismiss();
301
          }
302
        });
303

    
304
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
305
      params.bottomMargin = margin;
306
      params.topMargin    = margin;
307
      params.leftMargin   = margin;
308
      params.rightMargin  = margin;
309

    
310
      nextInRow[row]++;
311

    
312
      objectGrid.addView(button, params);
313
      }
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  private void setupMenuWindow(final RubikActivity act, final float width)
319
    {
320
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
321
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
322
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
323

    
324
    mMenuPopup = new PopupWindow(act);
325
    mMenuPopup.setContentView(layout);
326
    mMenuPopup.setFocusable(true);
327
    int margin  = (int)(width*RubikActivity.MARGIN);
328
    int padding = (int)(width*RubikActivity.PADDING);
329

    
330
    mMenuLayoutWidth = (int)(width/2);
331
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
332

    
333
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
334

    
335
    for(int i=0; i<NUM_BUTTONS; i++)
336
      {
337
      final int but = i;
338
      Button button = new Button(act);
339
      button.setLayoutParams(p);
340
      button.setText(BUTTON_LABELS[i]);
341
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
342

    
343
      button.setOnClickListener( new View.OnClickListener()
344
        {
345
        @Override
346
        public void onClick(View v)
347
          {
348
          mMenuPopup.dismiss();
349
          MenuAction(act,but);
350
          }
351
        });
352

    
353
      menuLayout.addView(button);
354
      }
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  private void setupPlayWindow(final RubikActivity act, final float width)
360
    {
361
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
362
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
363
    mPlayLayout = layout.findViewById(R.id.playGrid);
364

    
365
    mPlayLayoutWidth = (int)(width*0.4f);
366

    
367
    mPlayPopup = new PopupWindow(act);
368
    mPlayPopup.setContentView(layout);
369
    mPlayPopup.setFocusable(true);
370

    
371
    adjustLevels(act);
372
    }
373

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

    
376
  private void MenuAction(RubikActivity act, int button)
377
    {
378
    switch(button)
379
      {
380
      case 0: Bundle sBundle = new Bundle();
381
              sBundle.putInt("tab", mObject.ordinal() );
382
              sBundle.putBoolean("submitting", false);
383
              RubikDialogScores scores = new RubikDialogScores();
384
              scores.setArguments(sBundle);
385
              scores.show(act.getSupportFragmentManager(), null);
386
              break;
387
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
388
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
389
              break;
390
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
391
              break;
392
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
393
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
394
              break;
395
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
396
              aDiag.show(act.getSupportFragmentManager(), null);
397
              break;
398
      }
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
  void setupSolveButton(final RubikActivity act)
404
    {
405
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
406
    mSolveButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_END);
407

    
408
    mSolveButton.setOnClickListener( new View.OnClickListener()
409
      {
410
      @Override
411
      public void onClick(View v)
412
        {
413
        act.getControl().solveObject();
414
        mMovesController.clearMoves(act);
415
        }
416
      });
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  private void setupScrambleButton(final RubikActivity act)
422
    {
423
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble_new,R.drawable.ui_medium_cube_scramble_new, R.drawable.ui_big_cube_scramble_new, R.drawable.ui_huge_cube_scramble_new);
424
    mScrambleButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_START);
425

    
426
    mScrambleButton.setOnClickListener( new View.OnClickListener()
427
      {
428
      @Override
429
      public void onClick(View v)
430
        {
431
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
432
        int numScrambles = play.getObject().getNumScramble();
433
        mShouldReactToEndOfScrambling = false;
434
        act.getControl().scrambleObject(numScrambles);
435
        }
436
      });
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  public void savePreferences(SharedPreferences.Editor editor)
442
    {
443
    editor.putString("statePlay_objName", mObject.name() );
444

    
445
    if( mObjectPopup!=null )
446
      {
447
      mObjectPopup.dismiss();
448
      mObjectPopup = null;
449
      }
450

    
451
    if( mMenuPopup!=null )
452
      {
453
      mMenuPopup.dismiss();
454
      mMenuPopup = null;
455
      }
456

    
457
    if( mPlayPopup!=null )
458
      {
459
      mPlayPopup.dismiss();
460
      mPlayPopup = null;
461
      }
462
    }
463

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

    
466
  public void restorePreferences(SharedPreferences preferences)
467
    {
468
    String objName= preferences.getString("statePlay_objName", DEF_OBJECT.name() );
469
    int ordinal = ObjectType.getOrdinal(objName);
470
    mObject = ordinal>=0 && ordinal<NUM_OBJECTS ? ObjectType.values()[ordinal] : DEF_OBJECT;
471
    }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
  public boolean setObject(RubikActivity act, ObjectType obj)
476
    {
477
    if( mObject!=obj )
478
      {
479
      mObject = obj;
480
      if( mPlayLayout!=null ) adjustLevels(act);
481
      return true;
482
      }
483

    
484
    return false;
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
489

    
490
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
491
    {
492
    View topLayout = act.findViewById(R.id.relativeLayout);
493
    boolean isFullScreen;
494

    
495
    if( topLayout!=null )
496
      {
497
      topLayout.getLocationOnScreen(mLocation);
498
      isFullScreen = (mLocation[1]==0);
499
      }
500
    else
501
      {
502
      isFullScreen = true;
503
      }
504

    
505
    try
506
      {
507
      // if on Android 11 or we are fullscreen
508
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
509
        {
510
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
511
        window.update(view, w, h);
512
        }
513
      else  // Android 10 or below in pop-up mode or split-screen mode
514
        {
515
        view.getLocationOnScreen(mLocation);
516
        int width  = view.getWidth();
517
        int height = view.getHeight();
518
        int x = mLocation[0]+(width-w)/2;
519
        int y = mLocation[1]+height+yoff;
520

    
521
        window.showAsDropDown(view);
522
        window.update(x,y,w,h);
523
        }
524
      }
525
    catch( IllegalArgumentException iae )
526
      {
527
      // ignore, this means window is 'not attached to window manager' -
528
      // which most probably is because we are already exiting the app.
529
      }
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

    
534
  public void adjustSolvedIcons()
535
    {
536
    int dbLevel = getDBLevel(mObject);
537
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
538
    RubikScores scores = RubikScores.getInstance();
539

    
540
    for(int i=0; i<numLevel; i++)
541
      {
542
      int level = i<numLevel-1 ? i+1 : dbLevel;
543
      Button button = (Button)mPlayLayout.getChildAt(i);
544
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
545
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
546
      }
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
  private void adjustLevels(final RubikActivity act)
552
    {
553
    int dbLevel = getDBLevel(mObject);
554
    int numScrambles = mObject.getNumScramble();
555
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
556
    String[] levels = new String[numLevel];
557

    
558
    for(int i=0; i<numLevel-1; i++)
559
      {
560
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
561
      }
562

    
563
    if( numLevel>0 )
564
      {
565
      levels[numLevel-1] = act.getString(R.string.level_full);
566
      }
567

    
568
    if( mLevelValue>dbLevel || mLevelValue<1 ||
569
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
570
      {
571
      mLevelValue=1;
572
      }
573

    
574
    float width  = act.getScreenWidthInPixels();
575
    int margin   = (int)(width*RubikActivity.MARGIN);
576
    int padding  = (int)(width*RubikActivity.PADDING);
577
    int butWidth = mPlayLayoutWidth - 2*padding;
578
    int butHeight= (int)mMenuItemSize;
579
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
580

    
581
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
582
    pM.setMargins(margin, 0, margin, margin);
583
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
584
    pT.setMargins(margin, margin, margin, margin);
585
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
586
    pB.setMargins(margin, margin, margin, 2*margin);
587

    
588
    mPlayLayout.removeAllViews();
589

    
590
    RubikScores scores = RubikScores.getInstance();
591

    
592
    for(int i=0; i<numLevel; i++)
593
      {
594
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
595
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
596
      Button button = new Button(act);
597
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
598
      button.setText(levels[i]);
599
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
600

    
601
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
602
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
603

    
604
      button.setOnClickListener( new View.OnClickListener()
605
        {
606
        @Override
607
        public void onClick(View v)
608
          {
609
          ObjectControl control = act.getControl();
610

    
611
          if(control.isUINotBlocked())
612
            {
613
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
614
            mLevelValue = level;
615
            mShouldReactToEndOfScrambling = true;
616
            control.scrambleObject(scrambles);
617
            }
618
          }
619
        });
620

    
621
      mPlayLayout.addView(button);
622
      }
623
    }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
627
// those got remembered in the server-side DB already, so we need to keep using them. This function
628
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
629
// those two values the same.
630

    
631
  public static int getDBLevel(ObjectType object)
632
    {
633
    switch(object)
634
      {
635
      case CUBE_3: return 16;
636
      case CUBE_4: return 20;
637
      case CUBE_5: return 24;
638
      case PYRA_4: return 15;
639
      case PYRA_5: return 20;
640
      case MEGA_5: return 35;
641
      case DIAM_2: return 10;
642
      case DIAM_3: return 18;
643
      case REDI_3: return 14;
644
      case HELI_3: return 18;
645
      case SKEW_3: return 17;
646
      case REX_3 : return 16;
647
      case MIRR_3: return 16;
648
      default    : return object.getNumScramble();
649
      }
650
    }
651

    
652
///////////////////////////////////////////////////////////////////////////////////////////////////
653

    
654
  public int getLevel()
655
    {
656
    return mLevelValue;
657
    }
658

    
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660

    
661
  public boolean shouldReactToEndOfScrambling()
662
    {
663
    return mShouldReactToEndOfScrambling;
664
    }
665

    
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667

    
668
  public ObjectType getObject()
669
    {
670
    return mObject;
671
    }
672
  }
(5-5/10)