Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 5f3801d3

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
import android.widget.RelativeLayout;
38
import android.widget.TextView;
39

    
40
import org.distorted.dialogs.RubikDialogUpdates;
41
import org.distorted.network.RubikNetwork;
42
import org.distorted.network.RubikUpdates;
43
import org.distorted.objectlib.main.ObjectControl;
44

    
45
import org.distorted.main.R;
46
import org.distorted.main.RubikActivity;
47
import org.distorted.dialogs.RubikDialogAbout;
48
import org.distorted.dialogs.RubikDialogPattern;
49
import org.distorted.dialogs.RubikDialogScores;
50
import org.distorted.dialogs.RubikDialogTutorial;
51
import org.distorted.helpers.TransparentButton;
52
import org.distorted.helpers.TransparentImageButton;
53
import org.distorted.network.RubikScores;
54
import org.distorted.objects.RubikObject;
55
import org.distorted.objects.RubikObjectList;
56

    
57
import static android.view.View.inflate;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
62
  {
63
  public static final int NUM_COLUMNS  = 5;
64
  public static final int LEVELS_SHOWN = 10;
65

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

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

    
76
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
77
  private TransparentButton mPlayButton;
78
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
79
  private LinearLayout mPlayLayout;
80
  private TextView mBubbleUpdates;
81
  private RubikUpdates mUpdates;
82
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
83
  private int mLevelValue;
84
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
85
  private int mColCount, mRowCount, mMaxRowCount;
86
  private int mUpperBarHeight;
87
  private boolean mShouldReactToEndOfScrambling;
88
  private int mBottomHeight;
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  void leaveScreen(RubikActivity act)
93
    {
94

    
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  void enterScreen(final RubikActivity act)
100
    {
101
    int numObjects = RubikObjectList.getNumObjects();
102
    float width = act.getScreenWidthInPixels();
103
    mUpperBarHeight = act.getHeightUpperBar();
104

    
105
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
106
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
107
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
108

    
109
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
110
    mColCount = NUM_COLUMNS;
111

    
112
    // TOP ////////////////////////////
113
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
114
    layoutTop.removeAllViews();
115

    
116
    setupObjectButton(act,width);
117
    layoutTop.addView(mObjButton);
118

    
119
    setupMenuButton(act,width);
120
    layoutTop.addView(mMenuButton);
121

    
122
    setupPlayButton(act,width);
123
    layoutTop.addView(mPlayButton);
124

    
125
    setupSolveButton(act);
126
    setupScrambleButton(act);
127
    createBottomPane(act,mSolveButton,mScrambleButton);
128
    }
129

    
130
//////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private void setupObjectButton(final RubikActivity act, final float width)
133
    {
134
    final int margin  = (int)(width*RubikActivity.MARGIN);
135
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
136
    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);
137
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
138
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
139

    
140
    mObjButton.setOnClickListener( new View.OnClickListener()
141
      {
142
      @Override
143
      public void onClick(View view)
144
        {
145
        if( mObjectPopup==null )
146
          {
147
          float width = act.getScreenWidthInPixels();
148
          float height= act.getScreenHeightInPixels();
149
          setupObjectWindow(act,width,height);
150
          }
151

    
152
        if( act.getControl().isUINotBlocked())
153
          {
154
          int rowCount = Math.min(mMaxRowCount,mRowCount);
155
          View popupView = mObjectPopup.getContentView();
156
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
157
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
158
          }
159
        }
160
      });
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private void setupPlayButton(final RubikActivity act, final float width)
166
    {
167
    final int margin = (int)(width*RubikActivity.MARGIN);
168

    
169
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
170

    
171
    mPlayButton.setOnClickListener( new View.OnClickListener()
172
      {
173
      @Override
174
      public void onClick(View view)
175
        {
176
         if( mPlayPopup==null )
177
          {
178
          float width = act.getScreenWidthInPixels();
179
          setupPlayWindow(act,width);
180
          }
181

    
182
        if( act.getControl().isUINotBlocked())
183
          {
184
          adjustSolvedIcons();
185
          float height= act.getScreenHeightInPixels();
186
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
187
          View popupView = mPlayPopup.getContentView();
188
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
189
          final int object  = RubikObjectList.getCurrObject();
190
          final int dbLevel = RubikObjectList.getDBLevel(object);
191
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
192
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
193
          final int realHeight = Math.min(popupHeight,maxHeight);
194
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
195
          }
196
        }
197
      });
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void setupMenuButton(final RubikActivity act, final float width)
203
    {
204
    final int margin = (int)(width*RubikActivity.MARGIN);
205
    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);
206
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
207
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
208

    
209
    mMenuButton.setOnClickListener( new View.OnClickListener()
210
      {
211
      @Override
212
      public void onClick(View view)
213
        {
214
        if( mMenuPopup==null )
215
          {
216
          float width = act.getScreenWidthInPixels();
217
          setupMenuWindow(act,width);
218
          }
219

    
220
        if( act.getControl().isUINotBlocked())
221
          {
222
          View popupView = mMenuPopup.getContentView();
223
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
224
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
225
          }
226
        }
227
      });
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
233
    {
234
    int cubeWidth = (int)(width/9);
235
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
236
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
237
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
238

    
239
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
240
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
241

    
242
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
243
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
244

    
245
    objectGrid.setColumnCount(mColCount);
246
    objectGrid.setRowCount(mRowCount);
247

    
248
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
249
    setupBottomLayout(act,bottomLayout);
250

    
251
    mObjectPopup = new PopupWindow(act);
252
    mObjectPopup.setFocusable(true);
253
    mObjectPopup.setContentView(view);
254

    
255
    int[] nextInRow = new int[mRowCount];
256

    
257
    for(int row=0; row<mRowCount; row++)
258
      {
259
      rowSpecs[row] = GridLayout.spec(row);
260
      nextInRow[row]= 0;
261
      }
262
    for(int col=0; col<mColCount; col++)
263
      {
264
      colSpecs[col] = GridLayout.spec(col);
265
      }
266

    
267
    int numObjects = RubikObjectList.getNumObjects();
268

    
269
    for(int object=0; object<numObjects; object++)
270
      {
271
      final RubikObject robject = RubikObjectList.getObject(object);
272
      int icons = robject==null ? 0 : robject.getIconID();
273
      int row = object/NUM_COLUMNS;
274
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
275

    
276
      ImageButton button = new ImageButton(act);
277
      button.setBackgroundResource(icons);
278
      button.setOnClickListener( new View.OnClickListener()
279
        {
280
        @Override
281
        public void onClick(View v)
282
          {
283
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
284
            {
285
            RubikObjectList.setCurrObject(act,ordinal);
286
            act.changeObject(ordinal,true);
287
            if( mPlayLayout!=null ) adjustLevels(act);
288
            mMovesController.clearMoves(act);
289
            }
290

    
291
          mObjectPopup.dismiss();
292
          }
293
        });
294

    
295
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
296
      params.bottomMargin = margin;
297
      params.topMargin    = margin;
298
      params.leftMargin   = margin;
299
      params.rightMargin  = margin;
300

    
301
      params.width = cubeWidth;
302
      params.height= cubeWidth;
303

    
304
      nextInRow[row]++;
305

    
306
      objectGrid.addView(button, params);
307
      }
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
313
    {
314
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
315
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
316
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
317

    
318
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
319
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
320
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
321

    
322
    buttonTut.setImageResource(iconT);
323
    buttonDow.setImageResource(iconD);
324
    buttonInf.setImageResource(iconI);
325

    
326
    Resources res = act.getResources();
327
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
328
    mBottomHeight = bd.getIntrinsicHeight();
329

    
330
    TypedValue outValue = new TypedValue();
331
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
332
    buttonTut.setBackgroundResource(outValue.resourceId);
333
    buttonDow.setBackgroundResource(outValue.resourceId);
334
    buttonInf.setBackgroundResource(outValue.resourceId);
335

    
336
    buttonTut.setOnClickListener( new View.OnClickListener()
337
      {
338
      @Override
339
      public void onClick(View v)
340
        {
341
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
342
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
343
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
344
        }
345
      });
346

    
347
    buttonDow.setOnClickListener( new View.OnClickListener()
348
      {
349
      @Override
350
      public void onClick(View v)
351
        {
352
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
353
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
354
        uDiag.setUpdates(mUpdates);
355
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
356
        }
357
      });
358

    
359
    buttonInf.setOnClickListener( new View.OnClickListener()
360
      {
361
      @Override
362
      public void onClick(View v)
363
        {
364
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
365
        int currObject = RubikObjectList.getCurrObject();
366
        act.switchConfig(currObject);
367
        }
368
      });
369

    
370
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
371
    mBubbleUpdates.setVisibility(View.INVISIBLE);
372

    
373
    RubikNetwork network = RubikNetwork.getInstance();
374
    network.signUpForUpdates(this);
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  private void setupMenuWindow(final RubikActivity act, final float width)
380
    {
381
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
382
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
383
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
384

    
385
    mMenuPopup = new PopupWindow(act);
386
    mMenuPopup.setContentView(layout);
387
    mMenuPopup.setFocusable(true);
388
    int margin  = (int)(width*RubikActivity.MARGIN);
389
    int padding = (int)(width*RubikActivity.PADDING);
390

    
391
    mMenuLayoutWidth = (int)(width/2);
392
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
393

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

    
396
    for(int i=0; i<NUM_BUTTONS; i++)
397
      {
398
      final int but = i;
399
      Button button = new Button(act);
400
      button.setLayoutParams(p);
401
      button.setText(BUTTON_LABELS[i]);
402
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
403

    
404
      button.setOnClickListener( new View.OnClickListener()
405
        {
406
        @Override
407
        public void onClick(View v)
408
          {
409
          mMenuPopup.dismiss();
410
          MenuAction(act,but);
411
          }
412
        });
413

    
414
      menuLayout.addView(button);
415
      }
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  private void setupPlayWindow(final RubikActivity act, final float width)
421
    {
422
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
423
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
424
    mPlayLayout = layout.findViewById(R.id.playGrid);
425

    
426
    mPlayLayoutWidth = (int)(width*0.4f);
427

    
428
    mPlayPopup = new PopupWindow(act);
429
    mPlayPopup.setContentView(layout);
430
    mPlayPopup.setFocusable(true);
431

    
432
    adjustLevels(act);
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  private void MenuAction(RubikActivity act, int button)
438
    {
439
    switch(button)
440
      {
441
      case 0: Bundle sBundle = new Bundle();
442
              int currObject = RubikObjectList.getCurrObject();
443
              sBundle.putInt("tab", currObject );
444
              sBundle.putBoolean("submitting", false);
445
              RubikDialogScores scores = new RubikDialogScores();
446
              scores.setArguments(sBundle);
447
              scores.show(act.getSupportFragmentManager(), null);
448
              break;
449
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
450
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
451
              break;
452
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
453
              break;
454
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
455
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
456
              break;
457
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
458
              aDiag.show(act.getSupportFragmentManager(), null);
459
              break;
460
      }
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  void setupSolveButton(final RubikActivity act)
466
    {
467
    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);
468
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
469
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
470

    
471
    mSolveButton.setOnClickListener( new View.OnClickListener()
472
      {
473
      @Override
474
      public void onClick(View v)
475
        {
476
        act.getControl().solveObject();
477
        mMovesController.clearMoves(act);
478
        }
479
      });
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  private void setupScrambleButton(final RubikActivity act)
485
    {
486
    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);
487
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
488
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
489

    
490
    mScrambleButton.setOnClickListener( new View.OnClickListener()
491
      {
492
      @Override
493
      public void onClick(View v)
494
        {
495
        int currObject = RubikObjectList.getCurrObject();
496
        RubikObject object = RubikObjectList.getObject(currObject);
497
        int numScrambles = object==null ? 0 : object.getNumScramble();
498
        mShouldReactToEndOfScrambling = false;
499
        act.getControl().scrambleObject(numScrambles);
500
        }
501
      });
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
506
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
507
// going to be called)
508
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
509

    
510
  public void savePreferences(SharedPreferences.Editor editor)
511
    {
512
    editor.putInt("play_LevelValue", mLevelValue );
513

    
514
    if( mObjectPopup!=null )
515
      {
516
      mObjectPopup.dismiss();
517
      mObjectPopup = null;
518
      }
519

    
520
    if( mMenuPopup!=null )
521
      {
522
      mMenuPopup.dismiss();
523
      mMenuPopup = null;
524
      }
525

    
526
    if( mPlayPopup!=null )
527
      {
528
      mPlayPopup.dismiss();
529
      mPlayPopup = null;
530
      }
531
    }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534

    
535
  public void restorePreferences(SharedPreferences preferences)
536
    {
537
    mLevelValue = preferences.getInt("play_LevelValue", 0);
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  public void setCurrObject(RubikActivity act)
543
    {
544
    if( mPlayLayout!=null ) adjustLevels(act);
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
549

    
550
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
551
    {
552
    View topLayout = act.findViewById(R.id.relativeLayout);
553
    boolean isFullScreen;
554

    
555
    if( topLayout!=null )
556
      {
557
      topLayout.getLocationOnScreen(mLocation);
558
      isFullScreen = (mLocation[1]==0);
559
      }
560
    else
561
      {
562
      isFullScreen = true;
563
      }
564

    
565
    try
566
      {
567
      // if on Android 11 or we are fullscreen
568
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
569
        {
570
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
571
        window.update(view, w, h);
572
        }
573
      else  // Android 10 or below in pop-up mode or split-screen mode
574
        {
575
        view.getLocationOnScreen(mLocation);
576
        int width  = view.getWidth();
577
        int height = view.getHeight();
578
        int x = mLocation[0]+(width-w)/2;
579
        int y = mLocation[1]+height+yoff;
580

    
581
        window.showAsDropDown(view);
582
        window.update(x,y,w,h);
583
        }
584
      }
585
    catch( IllegalArgumentException iae )
586
      {
587
      // ignore, this means window is 'not attached to window manager' -
588
      // which most probably is because we are already exiting the app.
589
      }
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
  private void adjustSolvedIcons()
595
    {
596
    if( mPlayLayout!=null )
597
      {
598
      int currObject = RubikObjectList.getCurrObject();
599
      int dbLevel = RubikObjectList.getDBLevel(currObject);
600
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
601
      RubikScores scores = RubikScores.getInstance();
602

    
603
      for(int i=0; i<numLevel; i++)
604
        {
605
        int level = i<numLevel-1 ? i+1 : dbLevel;
606
        Button button = (Button)mPlayLayout.getChildAt(i);
607
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
608
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
609
        }
610
      }
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614

    
615
  private void adjustLevels(final RubikActivity act)
616
    {
617
    int currObject = RubikObjectList.getCurrObject();
618
    int dbLevel = RubikObjectList.getDBLevel(currObject);
619
    RubikObject object = RubikObjectList.getObject(currObject);
620
    int numScrambles = object==null ? 0 : object.getNumScramble();
621
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
622
    String[] levels = new String[numLevel];
623

    
624
    for(int i=0; i<numLevel-1; i++)
625
      {
626
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
627
      }
628

    
629
    if( numLevel>0 )
630
      {
631
      levels[numLevel-1] = act.getString(R.string.level_full);
632
      }
633

    
634
    if( mLevelValue>dbLevel || mLevelValue<1 ||
635
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
636
      {
637
      mLevelValue=1;
638
      }
639

    
640
    float width  = act.getScreenWidthInPixels();
641
    int margin   = (int)(width*RubikActivity.MARGIN);
642
    int padding  = (int)(width*RubikActivity.PADDING);
643
    int butWidth = mPlayLayoutWidth - 2*padding;
644
    int butHeight= (int)mMenuItemSize;
645
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
646

    
647
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
648
    pM.setMargins(margin, 0, margin, margin);
649
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
650
    pT.setMargins(margin, margin, margin, margin);
651
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
652
    pB.setMargins(margin, margin, margin, 2*margin);
653

    
654
    mPlayLayout.removeAllViews();
655

    
656
    RubikScores scores = RubikScores.getInstance();
657

    
658
    for(int i=0; i<numLevel; i++)
659
      {
660
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
661
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
662
      Button button = new Button(act);
663
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
664
      button.setText(levels[i]);
665
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
666

    
667
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
668
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
669

    
670
      button.setOnClickListener( new View.OnClickListener()
671
        {
672
        @Override
673
        public void onClick(View v)
674
          {
675
          ObjectControl control = act.getControl();
676

    
677
          if(control.isUINotBlocked())
678
            {
679
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
680
            mLevelValue = level;
681
            mShouldReactToEndOfScrambling = true;
682
            control.scrambleObject(scrambles);
683
            }
684
          }
685
        });
686

    
687
      mPlayLayout.addView(button);
688
      }
689
    }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692

    
693
  public int getLevel()
694
    {
695
    return mLevelValue;
696
    }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699

    
700
  public boolean shouldReactToEndOfScrambling()
701
    {
702
    return mShouldReactToEndOfScrambling;
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706

    
707
  public void receiveUpdate(RubikUpdates updates)
708
    {
709
    mUpdates = updates;
710
    int num = mUpdates.getNumberOfUpdates();
711

    
712
    if( num>0 )
713
      {
714
      String shownNum = String.valueOf(num);
715
      mBubbleUpdates.setText(shownNum);
716
      mBubbleUpdates.setVisibility(View.VISIBLE);
717
      }
718
    }
719

    
720
///////////////////////////////////////////////////////////////////////////////////////////////////
721

    
722
  public void errorUpdate()
723
    {
724
    android.util.Log.e("D", "Error receiving update");
725
    }
726
  }
(5-5/10)