Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 97a4ae23

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.content.res.Resources;
25
import android.graphics.drawable.BitmapDrawable;
26
import android.os.Build;
27
import android.os.Bundle;
28
import android.util.TypedValue;
29
import android.view.Gravity;
30
import android.view.LayoutInflater;
31
import android.view.View;
32
import android.widget.Button;
33
import android.widget.GridLayout;
34
import android.widget.ImageButton;
35
import android.widget.LinearLayout;
36
import android.widget.PopupWindow;
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 android.view.View.inflate;
52
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
53

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

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

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

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

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

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

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

    
96
    MAX_LEVEL = max;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  void leaveScreen(RubikActivity act)
102
    {
103

    
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

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

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

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

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

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

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

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

    
138
//////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  private void setupObjectButton(final RubikActivity act, final float width)
141
    {
142
    final int margin  = (int)(width*RubikActivity.MARGIN);
143
    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);
144
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
145
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
146

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private void setupMenuButton(final RubikActivity act, final float width)
208
    {
209
    final int margin = (int)(width*RubikActivity.MARGIN);
210
    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);
211
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
212
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
213

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

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
241
    Resources res = act.getResources();
242
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(icon);
243
    int cubeWidth = bd.getIntrinsicWidth();
244
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
245
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
246
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
247

    
248
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
249
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
250

    
251
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
252
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
253

    
254
    objectGrid.setColumnCount(mColCount);
255
    objectGrid.setRowCount(mRowCount);
256

    
257
    LinearLayout bottomLayout = view.findViewById(R.id.bottomLayout);
258
    setupBottomLayout(act,bottomLayout,2*mObjectSize,mObjectSize);
259

    
260
    mObjectPopup = new PopupWindow(act);
261
    mObjectPopup.setFocusable(true);
262
    mObjectPopup.setContentView(view);
263

    
264
    int[] nextInRow = new int[mRowCount];
265

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

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

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

    
298
          mObjectPopup.dismiss();
299
          }
300
        });
301

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

    
308
      nextInRow[row]++;
309

    
310
      objectGrid.addView(button, params);
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  private void setupBottomLayout(final RubikActivity act, final LinearLayout layout, int width, int height)
317
    {
318
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
319
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
320

    
321
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
322
         Button buttonNul = layout.findViewById(R.id.buttonNul);
323
    ImageButton buttonDet = layout.findViewById(R.id.buttonDet);
324

    
325
    buttonTut.setImageResource(iconT);
326
    buttonDet.setImageResource(iconD);
327
    buttonNul.setVisibility(View.INVISIBLE);
328

    
329
    buttonNul.setWidth(width);
330

    
331
    TypedValue outValue = new TypedValue();
332
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
333
    buttonTut.setBackgroundResource(outValue.resourceId);
334
    buttonDet.setBackgroundResource(outValue.resourceId);
335

    
336
    buttonTut.setOnClickListener( new View.OnClickListener()
337
      {
338
      @Override
339
      public void onClick(View v)
340
        {
341
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
342
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
343
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
344
        }
345
      });
346

    
347
    buttonDet.setOnClickListener( new View.OnClickListener()
348
      {
349
      @Override
350
      public void onClick(View v)
351
        {
352
        act.switchConfig(mObject);
353
        }
354
      });
355
    }
356

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

    
359
  private void setupMenuWindow(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_menu, null);
363
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
364

    
365
    mMenuPopup = new PopupWindow(act);
366
    mMenuPopup.setContentView(layout);
367
    mMenuPopup.setFocusable(true);
368
    int margin  = (int)(width*RubikActivity.MARGIN);
369
    int padding = (int)(width*RubikActivity.PADDING);
370

    
371
    mMenuLayoutWidth = (int)(width/2);
372
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
373

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

    
376
    for(int i=0; i<NUM_BUTTONS; i++)
377
      {
378
      final int but = i;
379
      Button button = new Button(act);
380
      button.setLayoutParams(p);
381
      button.setText(BUTTON_LABELS[i]);
382
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
383

    
384
      button.setOnClickListener( new View.OnClickListener()
385
        {
386
        @Override
387
        public void onClick(View v)
388
          {
389
          mMenuPopup.dismiss();
390
          MenuAction(act,but);
391
          }
392
        });
393

    
394
      menuLayout.addView(button);
395
      }
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  private void setupPlayWindow(final RubikActivity act, final float width)
401
    {
402
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
403
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
404
    mPlayLayout = layout.findViewById(R.id.playGrid);
405

    
406
    mPlayLayoutWidth = (int)(width*0.4f);
407

    
408
    mPlayPopup = new PopupWindow(act);
409
    mPlayPopup.setContentView(layout);
410
    mPlayPopup.setFocusable(true);
411

    
412
    adjustLevels(act);
413
    }
414

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

    
417
  private void MenuAction(RubikActivity act, int button)
418
    {
419
    switch(button)
420
      {
421
      case 0: Bundle sBundle = new Bundle();
422
              sBundle.putInt("tab", mObject.ordinal() );
423
              sBundle.putBoolean("submitting", false);
424
              RubikDialogScores scores = new RubikDialogScores();
425
              scores.setArguments(sBundle);
426
              scores.show(act.getSupportFragmentManager(), null);
427
              break;
428
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
429
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
430
              break;
431
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
432
              break;
433
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
434
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
435
              break;
436
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
437
              aDiag.show(act.getSupportFragmentManager(), null);
438
              break;
439
      }
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  void setupSolveButton(final RubikActivity act)
445
    {
446
    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);
447
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
448
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
449

    
450
    mSolveButton.setOnClickListener( new View.OnClickListener()
451
      {
452
      @Override
453
      public void onClick(View v)
454
        {
455
        act.getControl().solveObject();
456
        mMovesController.clearMoves(act);
457
        }
458
      });
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  private void setupScrambleButton(final RubikActivity act)
464
    {
465
    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);
466
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
467
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
468

    
469
    mScrambleButton.setOnClickListener( new View.OnClickListener()
470
      {
471
      @Override
472
      public void onClick(View v)
473
        {
474
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
475
        int numScrambles = play.getObject().getNumScramble();
476
        mShouldReactToEndOfScrambling = false;
477
        act.getControl().scrambleObject(numScrambles);
478
        }
479
      });
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public void savePreferences(SharedPreferences.Editor editor)
485
    {
486
    editor.putString("statePlay_objName", mObject.name() );
487

    
488
    if( mObjectPopup!=null )
489
      {
490
      mObjectPopup.dismiss();
491
      mObjectPopup = null;
492
      }
493

    
494
    if( mMenuPopup!=null )
495
      {
496
      mMenuPopup.dismiss();
497
      mMenuPopup = null;
498
      }
499

    
500
    if( mPlayPopup!=null )
501
      {
502
      mPlayPopup.dismiss();
503
      mPlayPopup = null;
504
      }
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
  public void restorePreferences(SharedPreferences preferences)
510
    {
511
    String objName= preferences.getString("statePlay_objName", DEF_OBJECT.name() );
512
    int ordinal = ObjectType.getOrdinal(objName);
513
    mObject = ordinal>=0 && ordinal<NUM_OBJECTS ? ObjectType.values()[ordinal] : DEF_OBJECT;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  public boolean setObject(RubikActivity act, ObjectType obj)
519
    {
520
    if( mObject!=obj )
521
      {
522
      mObject = obj;
523
      if( mPlayLayout!=null ) adjustLevels(act);
524
      return true;
525
      }
526

    
527
    return false;
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
532

    
533
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
534
    {
535
    View topLayout = act.findViewById(R.id.relativeLayout);
536
    boolean isFullScreen;
537

    
538
    if( topLayout!=null )
539
      {
540
      topLayout.getLocationOnScreen(mLocation);
541
      isFullScreen = (mLocation[1]==0);
542
      }
543
    else
544
      {
545
      isFullScreen = true;
546
      }
547

    
548
    try
549
      {
550
      // if on Android 11 or we are fullscreen
551
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
552
        {
553
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
554
        window.update(view, w, h);
555
        }
556
      else  // Android 10 or below in pop-up mode or split-screen mode
557
        {
558
        view.getLocationOnScreen(mLocation);
559
        int width  = view.getWidth();
560
        int height = view.getHeight();
561
        int x = mLocation[0]+(width-w)/2;
562
        int y = mLocation[1]+height+yoff;
563

    
564
        window.showAsDropDown(view);
565
        window.update(x,y,w,h);
566
        }
567
      }
568
    catch( IllegalArgumentException iae )
569
      {
570
      // ignore, this means window is 'not attached to window manager' -
571
      // which most probably is because we are already exiting the app.
572
      }
573
    }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

    
577
  public void adjustSolvedIcons()
578
    {
579
    int dbLevel = getDBLevel(mObject);
580
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
581
    RubikScores scores = RubikScores.getInstance();
582

    
583
    for(int i=0; i<numLevel; i++)
584
      {
585
      int level = i<numLevel-1 ? i+1 : dbLevel;
586
      Button button = (Button)mPlayLayout.getChildAt(i);
587
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
588
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
589
      }
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
  private void adjustLevels(final RubikActivity act)
595
    {
596
    int dbLevel = getDBLevel(mObject);
597
    int numScrambles = mObject.getNumScramble();
598
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
599
    String[] levels = new String[numLevel];
600

    
601
    for(int i=0; i<numLevel-1; i++)
602
      {
603
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
604
      }
605

    
606
    if( numLevel>0 )
607
      {
608
      levels[numLevel-1] = act.getString(R.string.level_full);
609
      }
610

    
611
    if( mLevelValue>dbLevel || mLevelValue<1 ||
612
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
613
      {
614
      mLevelValue=1;
615
      }
616

    
617
    float width  = act.getScreenWidthInPixels();
618
    int margin   = (int)(width*RubikActivity.MARGIN);
619
    int padding  = (int)(width*RubikActivity.PADDING);
620
    int butWidth = mPlayLayoutWidth - 2*padding;
621
    int butHeight= (int)mMenuItemSize;
622
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
623

    
624
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
625
    pM.setMargins(margin, 0, margin, margin);
626
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
627
    pT.setMargins(margin, margin, margin, margin);
628
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
629
    pB.setMargins(margin, margin, margin, 2*margin);
630

    
631
    mPlayLayout.removeAllViews();
632

    
633
    RubikScores scores = RubikScores.getInstance();
634

    
635
    for(int i=0; i<numLevel; i++)
636
      {
637
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
638
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
639
      Button button = new Button(act);
640
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
641
      button.setText(levels[i]);
642
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
643

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

    
647
      button.setOnClickListener( new View.OnClickListener()
648
        {
649
        @Override
650
        public void onClick(View v)
651
          {
652
          ObjectControl control = act.getControl();
653

    
654
          if(control.isUINotBlocked())
655
            {
656
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
657
            mLevelValue = level;
658
            mShouldReactToEndOfScrambling = true;
659
            control.scrambleObject(scrambles);
660
            }
661
          }
662
        });
663

    
664
      mPlayLayout.addView(button);
665
      }
666
    }
667

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

    
674
  public static int getDBLevel(ObjectType object)
675
    {
676
    switch(object)
677
      {
678
      case CUBE_3: return 16;
679
      case CUBE_4: return 20;
680
      case CUBE_5: return 24;
681
      case PYRA_4: return 15;
682
      case PYRA_5: return 20;
683
      case MEGA_5: return 35;
684
      case DIAM_2: return 10;
685
      case DIAM_3: return 18;
686
      case REDI_3: return 14;
687
      case HELI_3: return 18;
688
      case SKEW_3: return 17;
689
      case REX_3 : return 16;
690
      case MIRR_3: return 16;
691
      default    : return object.getNumScramble();
692
      }
693
    }
694

    
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696

    
697
  public int getLevel()
698
    {
699
    return mLevelValue;
700
    }
701

    
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703

    
704
  public boolean shouldReactToEndOfScrambling()
705
    {
706
    return mShouldReactToEndOfScrambling;
707
    }
708

    
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710

    
711
  public ObjectType getObject()
712
    {
713
    return mObject;
714
    }
715
  }
(5-5/10)