Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 0b162619

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.view.ViewGroup;
33
import android.widget.Button;
34
import android.widget.GridLayout;
35
import android.widget.ImageButton;
36
import android.widget.LinearLayout;
37
import android.widget.PopupWindow;
38

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

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

    
52
import static android.view.View.inflate;
53
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
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 int MAX_LEVEL;
62
  public static final ObjectType DEF_OBJECT= ObjectType.CUBE_3;
63

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

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

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

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

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

    
97
    MAX_LEVEL = max;
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  void leaveScreen(RubikActivity act)
103
    {
104

    
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

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

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

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

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

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

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

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

    
139
//////////////////////////////////////////////////////////////////////////////////////////////////
140

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

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

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

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

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

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

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

    
250
    int viewID = mMaxRowCount<mRowCount ? R.layout.popup_object_withscroll : R.layout.popup_object_scrollless;
251
    View view = inflate( act, viewID, null);
252
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
253

    
254
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
255
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
256

    
257
    objectGrid.setColumnCount(mColCount);
258
    objectGrid.setRowCount(mRowCount);
259

    
260
    LinearLayout bottomLayout = view.findViewById(R.id.bottomLayout);
261
    setupDetailsButton(act,layoutH);
262
    bottomLayout.addView(mDetailsButton);
263

    
264
    ViewGroup.LayoutParams paramsB = bottomLayout.getLayoutParams();
265
    paramsB.height = layoutH;
266
    bottomLayout.setLayoutParams(paramsB);
267

    
268
    mObjectPopup = new PopupWindow(act);
269
    mObjectPopup.setFocusable(true);
270
    mObjectPopup.setContentView(view);
271

    
272
    int[] nextInRow = new int[mRowCount];
273

    
274
    for(int row=0; row<mRowCount; row++)
275
      {
276
      rowSpecs[row] = GridLayout.spec(row);
277
      nextInRow[row]= 0;
278
      }
279
    for(int col=0; col<mColCount; col++)
280
      {
281
      colSpecs[col] = GridLayout.spec(col);
282
      }
283

    
284
    for(int object = 0; object< NUM_OBJECTS; object++)
285
      {
286
      final ObjectType type = ObjectType.getObject(object);
287
      int iconSize = RubikActivity.getDrawableSize();
288
      int icons = type.getIconID(iconSize);
289
      int row = object/NUM_COLUMNS;
290

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

    
306
          mObjectPopup.dismiss();
307
          }
308
        });
309

    
310
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
311
      params.bottomMargin = margin;
312
      params.topMargin    = margin;
313
      params.leftMargin   = margin;
314
      params.rightMargin  = margin;
315

    
316
      nextInRow[row]++;
317

    
318
      objectGrid.addView(button, params);
319
      }
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  private void setupMenuWindow(final RubikActivity act, final float width)
325
    {
326
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
327
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
328
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
329

    
330
    mMenuPopup = new PopupWindow(act);
331
    mMenuPopup.setContentView(layout);
332
    mMenuPopup.setFocusable(true);
333
    int margin  = (int)(width*RubikActivity.MARGIN);
334
    int padding = (int)(width*RubikActivity.PADDING);
335

    
336
    mMenuLayoutWidth = (int)(width/2);
337
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
338

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

    
341
    for(int i=0; i<NUM_BUTTONS; i++)
342
      {
343
      final int but = i;
344
      Button button = new Button(act);
345
      button.setLayoutParams(p);
346
      button.setText(BUTTON_LABELS[i]);
347
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
348

    
349
      button.setOnClickListener( new View.OnClickListener()
350
        {
351
        @Override
352
        public void onClick(View v)
353
          {
354
          mMenuPopup.dismiss();
355
          MenuAction(act,but);
356
          }
357
        });
358

    
359
      menuLayout.addView(button);
360
      }
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  private void setupPlayWindow(final RubikActivity act, final float width)
366
    {
367
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
368
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
369
    mPlayLayout = layout.findViewById(R.id.playGrid);
370

    
371
    mPlayLayoutWidth = (int)(width*0.4f);
372

    
373
    mPlayPopup = new PopupWindow(act);
374
    mPlayPopup.setContentView(layout);
375
    mPlayPopup.setFocusable(true);
376

    
377
    adjustLevels(act);
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

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

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  void setupDetailsButton(final RubikActivity act, int width)
410
    {
411
    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);
412
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT);
413
    mDetailsButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
414

    
415
    mDetailsButton.setOnClickListener( new View.OnClickListener()
416
      {
417
      @Override
418
      public void onClick(View v)
419
        {
420
        android.util.Log.e("D", "DETAILS CLICKED!!");
421
        }
422
      });
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  void setupSolveButton(final RubikActivity act)
428
    {
429
    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);
430
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
431
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
432

    
433
    mSolveButton.setOnClickListener( new View.OnClickListener()
434
      {
435
      @Override
436
      public void onClick(View v)
437
        {
438
        act.getControl().solveObject();
439
        mMovesController.clearMoves(act);
440
        }
441
      });
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  private void setupScrambleButton(final RubikActivity act)
447
    {
448
    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);
449
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
450
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
451

    
452
    mScrambleButton.setOnClickListener( new View.OnClickListener()
453
      {
454
      @Override
455
      public void onClick(View v)
456
        {
457
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
458
        int numScrambles = play.getObject().getNumScramble();
459
        mShouldReactToEndOfScrambling = false;
460
        act.getControl().scrambleObject(numScrambles);
461
        }
462
      });
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
  public void savePreferences(SharedPreferences.Editor editor)
468
    {
469
    editor.putString("statePlay_objName", mObject.name() );
470

    
471
    if( mObjectPopup!=null )
472
      {
473
      mObjectPopup.dismiss();
474
      mObjectPopup = null;
475
      }
476

    
477
    if( mMenuPopup!=null )
478
      {
479
      mMenuPopup.dismiss();
480
      mMenuPopup = null;
481
      }
482

    
483
    if( mPlayPopup!=null )
484
      {
485
      mPlayPopup.dismiss();
486
      mPlayPopup = null;
487
      }
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
  public void restorePreferences(SharedPreferences preferences)
493
    {
494
    String objName= preferences.getString("statePlay_objName", DEF_OBJECT.name() );
495
    int ordinal = ObjectType.getOrdinal(objName);
496
    mObject = ordinal>=0 && ordinal<NUM_OBJECTS ? ObjectType.values()[ordinal] : DEF_OBJECT;
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  public boolean setObject(RubikActivity act, ObjectType obj)
502
    {
503
    if( mObject!=obj )
504
      {
505
      mObject = obj;
506
      if( mPlayLayout!=null ) adjustLevels(act);
507
      return true;
508
      }
509

    
510
    return false;
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
515

    
516
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
517
    {
518
    View topLayout = act.findViewById(R.id.relativeLayout);
519
    boolean isFullScreen;
520

    
521
    if( topLayout!=null )
522
      {
523
      topLayout.getLocationOnScreen(mLocation);
524
      isFullScreen = (mLocation[1]==0);
525
      }
526
    else
527
      {
528
      isFullScreen = true;
529
      }
530

    
531
    try
532
      {
533
      // if on Android 11 or we are fullscreen
534
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
535
        {
536
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
537
        window.update(view, w, h);
538
        }
539
      else  // Android 10 or below in pop-up mode or split-screen mode
540
        {
541
        view.getLocationOnScreen(mLocation);
542
        int width  = view.getWidth();
543
        int height = view.getHeight();
544
        int x = mLocation[0]+(width-w)/2;
545
        int y = mLocation[1]+height+yoff;
546

    
547
        window.showAsDropDown(view);
548
        window.update(x,y,w,h);
549
        }
550
      }
551
    catch( IllegalArgumentException iae )
552
      {
553
      // ignore, this means window is 'not attached to window manager' -
554
      // which most probably is because we are already exiting the app.
555
      }
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
  public void adjustSolvedIcons()
561
    {
562
    int dbLevel = getDBLevel(mObject);
563
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
564
    RubikScores scores = RubikScores.getInstance();
565

    
566
    for(int i=0; i<numLevel; i++)
567
      {
568
      int level = i<numLevel-1 ? i+1 : dbLevel;
569
      Button button = (Button)mPlayLayout.getChildAt(i);
570
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
571
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
572
      }
573
    }
574

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

    
577
  private void adjustLevels(final RubikActivity act)
578
    {
579
    int dbLevel = getDBLevel(mObject);
580
    int numScrambles = mObject.getNumScramble();
581
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
582
    String[] levels = new String[numLevel];
583

    
584
    for(int i=0; i<numLevel-1; i++)
585
      {
586
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
587
      }
588

    
589
    if( numLevel>0 )
590
      {
591
      levels[numLevel-1] = act.getString(R.string.level_full);
592
      }
593

    
594
    if( mLevelValue>dbLevel || mLevelValue<1 ||
595
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
596
      {
597
      mLevelValue=1;
598
      }
599

    
600
    float width  = act.getScreenWidthInPixels();
601
    int margin   = (int)(width*RubikActivity.MARGIN);
602
    int padding  = (int)(width*RubikActivity.PADDING);
603
    int butWidth = mPlayLayoutWidth - 2*padding;
604
    int butHeight= (int)mMenuItemSize;
605
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
606

    
607
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
608
    pM.setMargins(margin, 0, margin, margin);
609
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
610
    pT.setMargins(margin, margin, margin, margin);
611
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
612
    pB.setMargins(margin, margin, margin, 2*margin);
613

    
614
    mPlayLayout.removeAllViews();
615

    
616
    RubikScores scores = RubikScores.getInstance();
617

    
618
    for(int i=0; i<numLevel; i++)
619
      {
620
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
621
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
622
      Button button = new Button(act);
623
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
624
      button.setText(levels[i]);
625
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
626

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

    
630
      button.setOnClickListener( new View.OnClickListener()
631
        {
632
        @Override
633
        public void onClick(View v)
634
          {
635
          ObjectControl control = act.getControl();
636

    
637
          if(control.isUINotBlocked())
638
            {
639
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
640
            mLevelValue = level;
641
            mShouldReactToEndOfScrambling = true;
642
            control.scrambleObject(scrambles);
643
            }
644
          }
645
        });
646

    
647
      mPlayLayout.addView(button);
648
      }
649
    }
650

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

    
657
  public static int getDBLevel(ObjectType object)
658
    {
659
    switch(object)
660
      {
661
      case CUBE_3: return 16;
662
      case CUBE_4: return 20;
663
      case CUBE_5: return 24;
664
      case PYRA_4: return 15;
665
      case PYRA_5: return 20;
666
      case MEGA_5: return 35;
667
      case DIAM_2: return 10;
668
      case DIAM_3: return 18;
669
      case REDI_3: return 14;
670
      case HELI_3: return 18;
671
      case SKEW_3: return 17;
672
      case REX_3 : return 16;
673
      case MIRR_3: return 16;
674
      default    : return object.getNumScramble();
675
      }
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
  public int getLevel()
681
    {
682
    return mLevelValue;
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
  public boolean shouldReactToEndOfScrambling()
688
    {
689
    return mShouldReactToEndOfScrambling;
690
    }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693

    
694
  public ObjectType getObject()
695
    {
696
    return mObject;
697
    }
698
  }
(5-5/10)