Project

General

Profile

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

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

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

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

    
52
import static android.view.View.inflate;
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

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

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

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

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  void leaveScreen(RubikActivity act)
85
    {
86

    
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  void enterScreen(final RubikActivity act)
92
    {
93
    int numObjects = RubikObjectList.getNumObjects();
94
    float width = act.getScreenWidthInPixels();
95
    mUpperBarHeight = act.getHeightUpperBar();
96

    
97
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
98
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
99
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
100

    
101
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
102
    mColCount = NUM_COLUMNS;
103

    
104
    // TOP ////////////////////////////
105
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
106
    layoutTop.removeAllViews();
107

    
108
    setupObjectButton(act,width);
109
    layoutTop.addView(mObjButton);
110

    
111
    setupMenuButton(act,width);
112
    layoutTop.addView(mMenuButton);
113

    
114
    setupPlayButton(act,width);
115
    layoutTop.addView(mPlayButton);
116

    
117
    setupSolveButton(act);
118
    setupScrambleButton(act);
119
    createBottomPane(act,mSolveButton,mScrambleButton);
120
    }
121

    
122
//////////////////////////////////////////////////////////////////////////////////////////////////
123

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

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

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

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  private void setupPlayButton(final RubikActivity act, final float width)
157
    {
158
    final int margin = (int)(width*RubikActivity.MARGIN);
159

    
160
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
161

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

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

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

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

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

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

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

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

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

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

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

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

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

    
262
    int numObjects = RubikObjectList.getNumObjects();
263

    
264
    for(int object=0; object<numObjects; object++)
265
      {
266
      final RubikObject robject = RubikObjectList.getObject(object);
267
      int iconSize = RubikActivity.getDrawableSize();
268
      int icons = robject==null ? 0 : robject.getIconID(iconSize);
269
      int row = object/NUM_COLUMNS;
270
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
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
            RubikObjectList.setCurrObject(act,ordinal);
282
            act.changeObject(ordinal,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
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
342
        int currObject = RubikObjectList.getCurrObject();
343
        act.switchConfig(currObject);
344
        }
345
      });
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

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

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

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

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

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

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

    
385
      menuLayout.addView(button);
386
      }
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

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

    
397
    mPlayLayoutWidth = (int)(width*0.4f);
398

    
399
    mPlayPopup = new PopupWindow(act);
400
    mPlayPopup.setContentView(layout);
401
    mPlayPopup.setFocusable(true);
402

    
403
    adjustLevels(act);
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

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

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

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

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

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

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

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
477
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
478
// going to be called)
479
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
480

    
481
  public void savePreferences(SharedPreferences.Editor editor)
482
    {
483
    editor.putInt("play_LevelValue", mLevelValue );
484

    
485
    if( mObjectPopup!=null )
486
      {
487
      mObjectPopup.dismiss();
488
      mObjectPopup = null;
489
      }
490

    
491
    if( mMenuPopup!=null )
492
      {
493
      mMenuPopup.dismiss();
494
      mMenuPopup = null;
495
      }
496

    
497
    if( mPlayPopup!=null )
498
      {
499
      mPlayPopup.dismiss();
500
      mPlayPopup = null;
501
      }
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public void restorePreferences(SharedPreferences preferences)
507
    {
508
    mLevelValue = preferences.getInt("play_LevelValue", 0);
509
    }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
  public void setCurrObject(RubikActivity act)
514
    {
515
    if( mPlayLayout!=null ) adjustLevels(act);
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
520

    
521
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
522
    {
523
    View topLayout = act.findViewById(R.id.relativeLayout);
524
    boolean isFullScreen;
525

    
526
    if( topLayout!=null )
527
      {
528
      topLayout.getLocationOnScreen(mLocation);
529
      isFullScreen = (mLocation[1]==0);
530
      }
531
    else
532
      {
533
      isFullScreen = true;
534
      }
535

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

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

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
  private void adjustSolvedIcons()
566
    {
567
    if( mPlayLayout!=null )
568
      {
569
      int currObject = RubikObjectList.getCurrObject();
570
      int dbLevel = RubikObjectList.getDBLevel(currObject);
571
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
572
      RubikScores scores = RubikScores.getInstance();
573

    
574
      for(int i=0; i<numLevel; i++)
575
        {
576
        int level = i<numLevel-1 ? i+1 : dbLevel;
577
        Button button = (Button)mPlayLayout.getChildAt(i);
578
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
579
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
580
        }
581
      }
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

    
586
  private void adjustLevels(final RubikActivity act)
587
    {
588
    int currObject = RubikObjectList.getCurrObject();
589
    int dbLevel = RubikObjectList.getDBLevel(currObject);
590
    RubikObject object = RubikObjectList.getObject(currObject);
591
    int numScrambles = object==null ? 0 : object.getNumScramble();
592
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
593
    String[] levels = new String[numLevel];
594

    
595
    for(int i=0; i<numLevel-1; i++)
596
      {
597
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
598
      }
599

    
600
    if( numLevel>0 )
601
      {
602
      levels[numLevel-1] = act.getString(R.string.level_full);
603
      }
604

    
605
    if( mLevelValue>dbLevel || mLevelValue<1 ||
606
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
607
      {
608
      mLevelValue=1;
609
      }
610

    
611
    float width  = act.getScreenWidthInPixels();
612
    int margin   = (int)(width*RubikActivity.MARGIN);
613
    int padding  = (int)(width*RubikActivity.PADDING);
614
    int butWidth = mPlayLayoutWidth - 2*padding;
615
    int butHeight= (int)mMenuItemSize;
616
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
617

    
618
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
619
    pM.setMargins(margin, 0, margin, margin);
620
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
621
    pT.setMargins(margin, margin, margin, margin);
622
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
623
    pB.setMargins(margin, margin, margin, 2*margin);
624

    
625
    mPlayLayout.removeAllViews();
626

    
627
    RubikScores scores = RubikScores.getInstance();
628

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

    
638
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
639
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
640

    
641
      button.setOnClickListener( new View.OnClickListener()
642
        {
643
        @Override
644
        public void onClick(View v)
645
          {
646
          ObjectControl control = act.getControl();
647

    
648
          if(control.isUINotBlocked())
649
            {
650
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
651
            mLevelValue = level;
652
            mShouldReactToEndOfScrambling = true;
653
            control.scrambleObject(scrambles);
654
            }
655
          }
656
        });
657

    
658
      mPlayLayout.addView(button);
659
      }
660
    }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663

    
664
  public int getLevel()
665
    {
666
    return mLevelValue;
667
    }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670

    
671
  public boolean shouldReactToEndOfScrambling()
672
    {
673
    return mShouldReactToEndOfScrambling;
674
    }
675
  }
(5-5/10)