Project

General

Profile

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

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

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.ObjectConstants;
39
import org.distorted.objectlib.main.ObjectControl;
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
import org.distorted.objects.RubikObject;
51
import org.distorted.objects.RubikObjectList;
52

    
53
import static android.view.View.inflate;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
public class RubikScreenPlay extends RubikScreenBase
58
  {
59
  public static final int NUM_COLUMNS  = 4;
60
  public static final int LEVELS_SHOWN = 10;
61
  public static final int DEF_OBJECT= ObjectConstants.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 int 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
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  void leaveScreen(RubikActivity act)
88
    {
89

    
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  void enterScreen(final RubikActivity act)
95
    {
96
    int numObjects = RubikObjectList.getNumObjects();
97
    float width = act.getScreenWidthInPixels();
98
    mUpperBarHeight = act.getHeightUpperBar();
99

    
100
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
101
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
102
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
103

    
104
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
105
    mColCount = NUM_COLUMNS;
106

    
107
    // TOP ////////////////////////////
108
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
109
    layoutTop.removeAllViews();
110

    
111
    setupObjectButton(act,width);
112
    layoutTop.addView(mObjButton);
113

    
114
    setupMenuButton(act,width);
115
    layoutTop.addView(mMenuButton);
116

    
117
    setupPlayButton(act,width);
118
    layoutTop.addView(mPlayButton);
119

    
120
    setupSolveButton(act);
121
    setupScrambleButton(act);
122
    createBottomPane(act,mSolveButton,mScrambleButton);
123
    }
124

    
125
//////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void setupObjectButton(final RubikActivity act, final float width)
128
    {
129
    final int margin  = (int)(width*RubikActivity.MARGIN);
130
    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);
131
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
132
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
133

    
134
    mObjButton.setOnClickListener( new View.OnClickListener()
135
      {
136
      @Override
137
      public void onClick(View view)
138
        {
139
        if( mObjectPopup==null )
140
          {
141
          float width = act.getScreenWidthInPixels();
142
          float height= act.getScreenHeightInPixels();
143
          setupObjectWindow(act,width,height);
144
          }
145

    
146
        if( act.getControl().isUINotBlocked())
147
          {
148
          int rowCount = Math.min(mMaxRowCount,mRowCount);
149
          View popupView = mObjectPopup.getContentView();
150
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
151
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount,margin,margin);
152
          }
153
        }
154
      });
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void setupPlayButton(final RubikActivity act, final float width)
160
    {
161
    final int margin = (int)(width*RubikActivity.MARGIN);
162

    
163
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
164

    
165
    mPlayButton.setOnClickListener( new View.OnClickListener()
166
      {
167
      @Override
168
      public void onClick(View view)
169
        {
170
         if( mPlayPopup==null )
171
          {
172
          float width = act.getScreenWidthInPixels();
173
          setupPlayWindow(act,width);
174
          }
175

    
176
        if( act.getControl().isUINotBlocked())
177
          {
178
          float height= act.getScreenHeightInPixels();
179
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
180
          View popupView = mPlayPopup.getContentView();
181
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
182
          final int dbLevel = RubikObjectList.getDBLevel(mObject);
183
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
184
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
185
          final int realHeight = Math.min(popupHeight,maxHeight);
186
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
187
          }
188
        }
189
      });
190
    }
191

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

    
194
  private void setupMenuButton(final RubikActivity act, final float width)
195
    {
196
    final int margin = (int)(width*RubikActivity.MARGIN);
197
    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);
198
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
199
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
200

    
201
    mMenuButton.setOnClickListener( new View.OnClickListener()
202
      {
203
      @Override
204
      public void onClick(View view)
205
        {
206
        if( mMenuPopup==null )
207
          {
208
          float width = act.getScreenWidthInPixels();
209
          setupMenuWindow(act,width);
210
          }
211

    
212
        if( act.getControl().isUINotBlocked())
213
          {
214
          View popupView = mMenuPopup.getContentView();
215
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
216
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
217
          }
218
        }
219
      });
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
225
    {
226
    int icon = RubikActivity.getDrawable(R.drawable.cube_2s,R.drawable.cube_2m, R.drawable.cube_2b, R.drawable.cube_2h);
227

    
228
    Resources res = act.getResources();
229
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(icon);
230
    int cubeWidth = bd.getIntrinsicWidth();
231
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
232
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
233
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
234

    
235
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
236
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
237

    
238
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
239
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
240

    
241
    objectGrid.setColumnCount(mColCount);
242
    objectGrid.setRowCount(mRowCount);
243

    
244
    LinearLayout bottomLayout = view.findViewById(R.id.bottomLayout);
245
    setupBottomLayout(act,bottomLayout,2*mObjectSize);
246

    
247
    mObjectPopup = new PopupWindow(act);
248
    mObjectPopup.setFocusable(true);
249
    mObjectPopup.setContentView(view);
250

    
251
    int[] nextInRow = new int[mRowCount];
252

    
253
    for(int row=0; row<mRowCount; row++)
254
      {
255
      rowSpecs[row] = GridLayout.spec(row);
256
      nextInRow[row]= 0;
257
      }
258
    for(int col=0; col<mColCount; col++)
259
      {
260
      colSpecs[col] = GridLayout.spec(col);
261
      }
262

    
263
    int numObjects = RubikObjectList.getNumObjects();
264

    
265
    for(int object=0; object<numObjects; object++)
266
      {
267
      final RubikObject robject = RubikObjectList.getObject(object);
268
      int iconSize = RubikActivity.getDrawableSize();
269
      int icons = robject==null ? 0 : robject.getIconID(iconSize);
270
      int row = object/NUM_COLUMNS;
271

    
272
      ImageButton button = new ImageButton(act);
273
      button.setBackgroundResource(icons);
274
      button.setOnClickListener( new View.OnClickListener()
275
        {
276
        @Override
277
        public void onClick(View v)
278
          {
279
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
280
            {
281
            mObject = robject==null ? 0 : robject.getOrdinal();
282
            act.changeObject(mObject, true);
283
            if( mPlayLayout!=null ) adjustLevels(act);
284
            mMovesController.clearMoves(act);
285
            }
286

    
287
          mObjectPopup.dismiss();
288
          }
289
        });
290

    
291
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
292
      params.bottomMargin = margin;
293
      params.topMargin    = margin;
294
      params.leftMargin   = margin;
295
      params.rightMargin  = margin;
296

    
297
      nextInRow[row]++;
298

    
299
      objectGrid.addView(button, params);
300
      }
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  private void setupBottomLayout(final RubikActivity act, final LinearLayout layout, int width)
306
    {
307
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
308
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
309

    
310
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
311
         Button buttonNul = layout.findViewById(R.id.buttonNul);
312
    ImageButton buttonDet = layout.findViewById(R.id.buttonDet);
313

    
314
    buttonTut.setImageResource(iconT);
315
    buttonDet.setImageResource(iconD);
316
    buttonNul.setVisibility(View.INVISIBLE);
317

    
318
    buttonNul.setWidth(width);
319

    
320
    TypedValue outValue = new TypedValue();
321
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
322
    buttonTut.setBackgroundResource(outValue.resourceId);
323
    buttonDet.setBackgroundResource(outValue.resourceId);
324

    
325
    buttonTut.setOnClickListener( new View.OnClickListener()
326
      {
327
      @Override
328
      public void onClick(View v)
329
        {
330
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
331
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
332
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
333
        }
334
      });
335

    
336
    buttonDet.setOnClickListener( new View.OnClickListener()
337
      {
338
      @Override
339
      public void onClick(View v)
340
        {
341
        act.switchConfig(mObject);
342
        }
343
      });
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  private void setupMenuWindow(final RubikActivity act, final float width)
349
    {
350
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
351
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
352
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
353

    
354
    mMenuPopup = new PopupWindow(act);
355
    mMenuPopup.setContentView(layout);
356
    mMenuPopup.setFocusable(true);
357
    int margin  = (int)(width*RubikActivity.MARGIN);
358
    int padding = (int)(width*RubikActivity.PADDING);
359

    
360
    mMenuLayoutWidth = (int)(width/2);
361
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
362

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

    
365
    for(int i=0; i<NUM_BUTTONS; i++)
366
      {
367
      final int but = i;
368
      Button button = new Button(act);
369
      button.setLayoutParams(p);
370
      button.setText(BUTTON_LABELS[i]);
371
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
372

    
373
      button.setOnClickListener( new View.OnClickListener()
374
        {
375
        @Override
376
        public void onClick(View v)
377
          {
378
          mMenuPopup.dismiss();
379
          MenuAction(act,but);
380
          }
381
        });
382

    
383
      menuLayout.addView(button);
384
      }
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  private void setupPlayWindow(final RubikActivity act, final float width)
390
    {
391
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
392
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
393
    mPlayLayout = layout.findViewById(R.id.playGrid);
394

    
395
    mPlayLayoutWidth = (int)(width*0.4f);
396

    
397
    mPlayPopup = new PopupWindow(act);
398
    mPlayPopup.setContentView(layout);
399
    mPlayPopup.setFocusable(true);
400

    
401
    adjustLevels(act);
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  private void MenuAction(RubikActivity act, int button)
407
    {
408
    switch(button)
409
      {
410
      case 0: Bundle sBundle = new Bundle();
411
              sBundle.putInt("tab", mObject );
412
              sBundle.putBoolean("submitting", false);
413
              RubikDialogScores scores = new RubikDialogScores();
414
              scores.setArguments(sBundle);
415
              scores.show(act.getSupportFragmentManager(), null);
416
              break;
417
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
418
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
419
              break;
420
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
421
              break;
422
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
423
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
424
              break;
425
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
426
              aDiag.show(act.getSupportFragmentManager(), null);
427
              break;
428
      }
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  void setupSolveButton(final RubikActivity act)
434
    {
435
    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);
436
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
437
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
438

    
439
    mSolveButton.setOnClickListener( new View.OnClickListener()
440
      {
441
      @Override
442
      public void onClick(View v)
443
        {
444
        act.getControl().solveObject();
445
        mMovesController.clearMoves(act);
446
        }
447
      });
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  private void setupScrambleButton(final RubikActivity act)
453
    {
454
    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);
455
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
456
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
457

    
458
    mScrambleButton.setOnClickListener( new View.OnClickListener()
459
      {
460
      @Override
461
      public void onClick(View v)
462
        {
463
        RubikObject object = RubikObjectList.getObject(mObject);
464
        int numScrambles = object==null ? 0 : object.getNumScramble();
465
        mShouldReactToEndOfScrambling = false;
466
        act.getControl().scrambleObject(numScrambles);
467
        }
468
      });
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

    
473
  public void savePreferences(SharedPreferences.Editor editor)
474
    {
475
    RubikObject object = RubikObjectList.getObject(mObject);
476

    
477
    if( object!=null )
478
      {
479
      editor.putString("statePlay_objName", object.getName() );
480
      }
481

    
482
    if( mObjectPopup!=null )
483
      {
484
      mObjectPopup.dismiss();
485
      mObjectPopup = null;
486
      }
487

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

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

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
  public void restorePreferences(SharedPreferences preferences)
504
    {
505
    RubikObject object = RubikObjectList.getObject(DEF_OBJECT);
506
    String defName = object==null ? "CUBE_3" : object.getName();
507
    String objName= preferences.getString("statePlay_objName",defName);
508
    mObject = RubikObjectList.getOrdinal(objName);
509

    
510
    if( mObject<0 || mObject>=RubikObjectList.getNumObjects() ) mObject = DEF_OBJECT;
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
  public boolean setObject(RubikActivity act, int ordinal)
516
    {
517
    if( mObject!=ordinal )
518
      {
519
      mObject = ordinal;
520
      if( mPlayLayout!=null ) adjustLevels(act);
521
      return true;
522
      }
523

    
524
    return false;
525
    }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
529

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

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

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

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

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

    
574
  public void adjustSolvedIcons()
575
    {
576
    int dbLevel = RubikObjectList.getDBLevel(mObject);
577
    int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
578
    RubikScores scores = RubikScores.getInstance();
579

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

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
  private void adjustLevels(final RubikActivity act)
592
    {
593
    int dbLevel = RubikObjectList.getDBLevel(mObject);
594
    RubikObject object = RubikObjectList.getObject(mObject);
595
    int numScrambles = object==null ? 0 : object.getNumScramble();
596
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
597
    String[] levels = new String[numLevel];
598

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

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

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

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

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

    
629
    mPlayLayout.removeAllViews();
630

    
631
    RubikScores scores = RubikScores.getInstance();
632

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

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

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

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

    
662
      mPlayLayout.addView(button);
663
      }
664
    }
665

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

    
668
  public int getLevel()
669
    {
670
    return mLevelValue;
671
    }
672

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674

    
675
  public boolean shouldReactToEndOfScrambling()
676
    {
677
    return mShouldReactToEndOfScrambling;
678
    }
679

    
680
///////////////////////////////////////////////////////////////////////////////////////////////////
681

    
682
  public int getObject()
683
    {
684
    return mObject;
685
    }
686
  }
(5-5/10)