Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 0873a75a

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;
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
    setupBottomLayout(act,bottomLayout,layoutH);
262

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

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

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

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

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

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

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

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

    
315
      nextInRow[row]++;
316

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

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

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

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

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

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

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

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

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

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

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

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

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

    
376
    adjustLevels(act);
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

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

    
408
  void setupBottomLayout(final RubikActivity act, final LinearLayout layout, int width)
409
    {
410
    int icon1 = 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);
411
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
412
    TransparentImageButton buttonTut = new TransparentImageButton(act, icon1, TransparentImageButton.GRAVITY_MIDDLE, params1);
413

    
414
    int icon2 = 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);
415
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
416
    TransparentImageButton buttonDet = new TransparentImageButton(act, icon2, TransparentImageButton.GRAVITY_MIDDLE, params2);
417

    
418
    buttonTut.setOnClickListener( new View.OnClickListener()
419
      {
420
      @Override
421
      public void onClick(View v)
422
        {
423
        android.util.Log.e("D", "BUTTON TUT CLICKED!!");
424
        }
425
      });
426

    
427
    buttonDet.setOnClickListener( new View.OnClickListener()
428
      {
429
      @Override
430
      public void onClick(View v)
431
        {
432
        android.util.Log.e("D", "BUTTON DET CLICKED!!");
433
        }
434
      });
435

    
436
    LinearLayout.LayoutParams paramsNul = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
437
    Button buttonNul = new Button(act);
438
    buttonNul.setLayoutParams(paramsNul);
439
    buttonNul.setVisibility(View.INVISIBLE);
440

    
441
    layout.addView(buttonTut);
442
    layout.addView(buttonNul);
443
    layout.addView(buttonDet);
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

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

    
454
    mSolveButton.setOnClickListener( new View.OnClickListener()
455
      {
456
      @Override
457
      public void onClick(View v)
458
        {
459
        act.getControl().solveObject();
460
        mMovesController.clearMoves(act);
461
        }
462
      });
463
    }
464

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

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

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

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  public void savePreferences(SharedPreferences.Editor editor)
489
    {
490
    editor.putString("statePlay_objName", mObject.name() );
491

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

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

    
504
    if( mPlayPopup!=null )
505
      {
506
      mPlayPopup.dismiss();
507
      mPlayPopup = null;
508
      }
509
    }
510

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

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

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

    
522
  public boolean setObject(RubikActivity act, ObjectType obj)
523
    {
524
    if( mObject!=obj )
525
      {
526
      mObject = obj;
527
      if( mPlayLayout!=null ) adjustLevels(act);
528
      return true;
529
      }
530

    
531
    return false;
532
    }
533

    
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
536

    
537
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
538
    {
539
    View topLayout = act.findViewById(R.id.relativeLayout);
540
    boolean isFullScreen;
541

    
542
    if( topLayout!=null )
543
      {
544
      topLayout.getLocationOnScreen(mLocation);
545
      isFullScreen = (mLocation[1]==0);
546
      }
547
    else
548
      {
549
      isFullScreen = true;
550
      }
551

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

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

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

    
581
  public void adjustSolvedIcons()
582
    {
583
    int dbLevel = getDBLevel(mObject);
584
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
585
    RubikScores scores = RubikScores.getInstance();
586

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

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597

    
598
  private void adjustLevels(final RubikActivity act)
599
    {
600
    int dbLevel = getDBLevel(mObject);
601
    int numScrambles = mObject.getNumScramble();
602
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
603
    String[] levels = new String[numLevel];
604

    
605
    for(int i=0; i<numLevel-1; i++)
606
      {
607
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
608
      }
609

    
610
    if( numLevel>0 )
611
      {
612
      levels[numLevel-1] = act.getString(R.string.level_full);
613
      }
614

    
615
    if( mLevelValue>dbLevel || mLevelValue<1 ||
616
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
617
      {
618
      mLevelValue=1;
619
      }
620

    
621
    float width  = act.getScreenWidthInPixels();
622
    int margin   = (int)(width*RubikActivity.MARGIN);
623
    int padding  = (int)(width*RubikActivity.PADDING);
624
    int butWidth = mPlayLayoutWidth - 2*padding;
625
    int butHeight= (int)mMenuItemSize;
626
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
627

    
628
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
629
    pM.setMargins(margin, 0, margin, margin);
630
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
631
    pT.setMargins(margin, margin, margin, margin);
632
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
633
    pB.setMargins(margin, margin, margin, 2*margin);
634

    
635
    mPlayLayout.removeAllViews();
636

    
637
    RubikScores scores = RubikScores.getInstance();
638

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

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

    
651
      button.setOnClickListener( new View.OnClickListener()
652
        {
653
        @Override
654
        public void onClick(View v)
655
          {
656
          ObjectControl control = act.getControl();
657

    
658
          if(control.isUINotBlocked())
659
            {
660
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
661
            mLevelValue = level;
662
            mShouldReactToEndOfScrambling = true;
663
            control.scrambleObject(scrambles);
664
            }
665
          }
666
        });
667

    
668
      mPlayLayout.addView(button);
669
      }
670
    }
671

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

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

    
699
///////////////////////////////////////////////////////////////////////////////////////////////////
700

    
701
  public int getLevel()
702
    {
703
    return mLevelValue;
704
    }
705

    
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707

    
708
  public boolean shouldReactToEndOfScrambling()
709
    {
710
    return mShouldReactToEndOfScrambling;
711
    }
712

    
713
///////////////////////////////////////////////////////////////////////////////////////////////////
714

    
715
  public ObjectType getObject()
716
    {
717
    return mObject;
718
    }
719
  }
(5-5/10)