Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 4c650458

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
    int viewID = R.layout.popup_object;
250

    
251
    LinearLayout view = (LinearLayout)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 iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
411
    LinearLayout.LayoutParams paramsT = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
412
    TransparentImageButton buttonTut = new TransparentImageButton(act, iconT, TransparentImageButton.GRAVITY_MIDDLE, paramsT);
413

    
414
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
415
    LinearLayout.LayoutParams paramsD = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
416
    TransparentImageButton buttonDet = new TransparentImageButton(act, iconD, TransparentImageButton.GRAVITY_MIDDLE, paramsD);
417

    
418
    buttonTut.setOnClickListener( new View.OnClickListener()
419
      {
420
      @Override
421
      public void onClick(View v)
422
        {
423
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
424
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
425
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
426
        }
427
      });
428

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

    
438
    LinearLayout.LayoutParams paramsNul = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
439
    Button buttonNul = new Button(act);
440
    buttonNul.setLayoutParams(paramsNul);
441
    buttonNul.setVisibility(View.INVISIBLE);
442

    
443
    layout.addView(buttonTut);
444
    layout.addView(buttonNul);
445
    layout.addView(buttonDet);
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

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

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

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

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

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

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

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

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

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

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

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

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

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

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

    
533
    return false;
534
    }
535

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

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

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

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

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

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582

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

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

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599

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

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

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

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

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

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

    
637
    mPlayLayout.removeAllViews();
638

    
639
    RubikScores scores = RubikScores.getInstance();
640

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

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

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

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

    
670
      mPlayLayout.addView(button);
671
      }
672
    }
673

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

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

    
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702

    
703
  public int getLevel()
704
    {
705
    return mLevelValue;
706
    }
707

    
708
///////////////////////////////////////////////////////////////////////////////////////////////////
709

    
710
  public boolean shouldReactToEndOfScrambling()
711
    {
712
    return mShouldReactToEndOfScrambling;
713
    }
714

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716

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