Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.screens;
11

    
12
import java.lang.ref.WeakReference;
13

    
14
import android.app.Activity;
15
import android.content.Context;
16
import android.content.SharedPreferences;
17
import android.content.res.ColorStateList;
18
import android.content.res.Resources;
19
import android.os.Build;
20
import android.os.Bundle;
21
import android.util.TypedValue;
22
import android.view.Gravity;
23
import android.view.LayoutInflater;
24
import android.view.View;
25
import android.widget.Button;
26
import android.widget.GridLayout;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29
import android.widget.PopupWindow;
30
import android.widget.RelativeLayout;
31
import android.widget.TextView;
32

    
33
import org.distorted.dialogs.RubikDialogSolvers;
34
import org.distorted.dialogs.RubikDialogStarsStatus;
35
import org.distorted.dialogs.RubikDialogUpdates;
36
import org.distorted.external.RubikNetwork;
37
import org.distorted.external.RubikScores;
38
import org.distorted.external.RubikUpdates;
39

    
40
import org.distorted.helpers.PopupCreator;
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.dialogs.RubikDialogAbout;
44
import org.distorted.dialogs.RubikDialogPattern;
45
import org.distorted.dialogs.RubikDialogScores;
46
import org.distorted.dialogs.RubikDialogTutorial;
47
import org.distorted.helpers.TransparentImageButton;
48
import org.distorted.objectlib.effects.BaseEffect;
49
import org.distorted.objectlib.main.ObjectControl;
50
import org.distorted.objects.RubikObject;
51
import org.distorted.objects.RubikObjectList;
52

    
53
import static android.view.View.GONE;
54
import static android.view.View.inflate;
55
import static org.distorted.main.RubikActivity.USE_IAP;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
60
  {
61
  private static final float MENU_BUTTON_HEIGHT  = 0.068f;
62
  private static final float MENU_TEXT_SIZE      = 0.027f;
63
  private static final float MENU_MARGIN         = 0.008f;
64
  private static final float MENU_WIDTH          = 0.650f;
65
  private static final float LEVEL_BUTTON_HEIGHT = 0.089f;
66

    
67
  public static final int NUM_COLUMNS  = 5;
68
  public static final int LEVELS_SHOWN = 8;
69
  private static final int[] mLocation = new int[2];
70

    
71
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
72
  private PopupWindow mObjectPopup, mMenuPopup;
73
  private WeakReference<RubikActivity> mWeakAct;
74
  private TextView mBubbleUpdates;
75
  private Button[] mLevel;
76
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
77
  private int mLevelValue;
78
  private int mColCount, mRowCount, mMaxRowCount;
79
  private int mUpperBarHeight;
80
  private boolean mShouldReactToEndOfScrambling;
81
  private float mScreenWidth, mScreenHeight;
82
  private int mLevelHeight, mLevelWidth;
83
  private int mOldNumScramble;
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  void leaveScreen(RubikActivity act)
88
    {
89

    
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  void enterScreen(final RubikActivity act)
95
    {
96
    mWeakAct = new WeakReference<>(act);
97
    mScreenWidth  = act.getScreenWidthInPixels();
98
    mScreenHeight = act.getScreenHeightInPixels();
99
    mUpperBarHeight = act.getHeightUpperBar();
100

    
101
    mMenuButtonHeight = (int)(mScreenHeight*MENU_BUTTON_HEIGHT);
102
    mMenuTextSize     = (int)(mScreenHeight*MENU_TEXT_SIZE);
103

    
104
    mObjectPopup = null;
105
    mOldNumScramble = 1000; // used to remember which 'level' buttons are visible; initially all visible
106

    
107
    // TOP ////////////////////////////
108
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
109
    layoutTop.removeAllViews();
110

    
111
    setupSolveButton(act);
112
    layoutTop.addView(mSolveButton);
113
    setupMenuButton(act,mScreenWidth, mScreenHeight);
114
    layoutTop.addView(mMenuButton);
115
    setupScrambleButton(act);
116
    layoutTop.addView(mScrambleButton);
117

    
118
    // BOTTOM /////////////////////////
119
    setupObjectButton(act,mScreenWidth);
120
    createBottomPane(act,mObjButton,null);
121
    }
122

    
123
//////////////////////////////////////////////////////////////////////////////////////////////////
124

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

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

    
144
        int rowCount = Math.min(mMaxRowCount,mRowCount);
145
        View popupView = mObjectPopup.getContentView();
146
        popupView.setSystemUiVisibility(RubikActivity.FLAGS);
147
        displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
148
        }
149
      });
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private void setupMenuButton(final RubikActivity act, final float width, final float height)
155
    {
156
    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);
157
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
158
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
159

    
160
    mMenuButton.setOnClickListener( new View.OnClickListener()
161
      {
162
      @Override
163
      public void onClick(View view)
164
        {
165
        if( mMenuPopup==null )
166
          {
167
          float width = act.getScreenWidthInPixels();
168
          setupMenuWindow(act,width,height);
169
          }
170

    
171
        View popupView = mMenuPopup.getContentView();
172
        popupView.setSystemUiVisibility(RubikActivity.FLAGS);
173
        setupLevelButtonVisibilityAndColor(act);
174
        displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
175
        }
176
      });
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
182
    {
183
    int numObjects = RubikObjectList.getNumObjects();
184
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
185
    mColCount = NUM_COLUMNS;
186

    
187
    int cubeSize = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
188
    int margin   = (int)(height*RubikActivity.POPUP_MARGIN);
189
    int padding  = (int)(height*RubikActivity.POPUP_PADDING);
190
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
191
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
192

    
193
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
194
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
195
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
196
    setupBottomLayout(act,bottomLayout);
197

    
198
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
199

    
200
    for(int child=0; child<numObjects; child++)
201
      {
202
      final RubikObject obj = RubikObjectList.getObject(child);
203
      View v = objectGrid.getChildAt(child);
204
      ImageButton button = PopupCreator.getButton(obj,v);
205
      final int ordinal = child;
206

    
207
      button.setOnClickListener( new View.OnClickListener()
208
        {
209
        @Override
210
        public void onClick(View v)
211
          {
212
          if( obj!=null && act.getControl().isScramblingAndSolvingNotBlocked() && ScreenList.getCurrentScreen()==ScreenList.PLAY )
213
            {
214
            if( obj.isFree() )
215
              {
216
              RubikObjectList.setCurrObject(ordinal);
217
              act.changeObject(ordinal,true);
218
              if( mMenuPopup!=null ) setupLevelButtonVisibilityAndColor(act);
219
              mMovesController.clearMoves(act);
220
              }
221
            else
222
              {
223
              act.switchToPurchase(ordinal);
224
              }
225
            }
226

    
227
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
228
          }
229
        });
230
      }
231

    
232
    mObjectPopup = new PopupWindow(act);
233
    mObjectPopup.setFocusable(true);
234
    mObjectPopup.setContentView(view);
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
240
    {
241
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
242
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
243
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
244

    
245
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
246
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
247
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
248

    
249
    buttonTut.setImageResource(iconT);
250
    buttonDow.setImageResource(iconD);
251
    buttonInf.setImageResource(iconI);
252

    
253
    TypedValue outValue = new TypedValue();
254
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
255
    buttonTut.setBackgroundResource(outValue.resourceId);
256
    buttonDow.setBackgroundResource(outValue.resourceId);
257
    buttonInf.setBackgroundResource(outValue.resourceId);
258

    
259
    buttonTut.setOnClickListener( new View.OnClickListener()
260
      {
261
      @Override
262
      public void onClick(View v)
263
        {
264
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
265
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
266
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
267
        }
268
      });
269

    
270
    buttonDow.setOnClickListener( new View.OnClickListener()
271
      {
272
      @Override
273
      public void onClick(View v)
274
        {
275
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
276
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
277
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
278
        }
279
      });
280

    
281
    buttonInf.setOnClickListener( new View.OnClickListener()
282
      {
283
      @Override
284
      public void onClick(View v)
285
        {
286
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
287
        int currObject = RubikObjectList.getCurrObject();
288
        act.switchConfig(currObject);
289
        }
290
      });
291

    
292
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
293
    mBubbleUpdates.setVisibility(View.INVISIBLE);
294

    
295
    RubikNetwork network = RubikNetwork.getInstance();
296
    network.signUpForUpdates(this);
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  void setupSolveButton(final RubikActivity act)
302
    {
303
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
304
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
305
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
306

    
307
    mSolveButton.setOnClickListener( new View.OnClickListener()
308
      {
309
      @Override
310
      public void onClick(View v)
311
        {
312
        act.getControl().solveObject();
313
        mMovesController.clearMoves(act);
314
        }
315
      });
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  private void setupScrambleButton(final RubikActivity act)
321
    {
322
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble,R.drawable.ui_medium_cube_scramble, R.drawable.ui_big_cube_scramble, R.drawable.ui_huge_cube_scramble);
323
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
324
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
325

    
326
    mScrambleButton.setOnClickListener( new View.OnClickListener()
327
      {
328
      @Override
329
      public void onClick(View v)
330
        {
331
        mShouldReactToEndOfScrambling = false;
332
        int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
333
        act.getControl().fastScrambleObject(duration,RubikObject.FAST_SCRAMBLES);
334
        }
335
      });
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  private void setupMenuWindow(final RubikActivity act, final float width, final float height)
341
    {
342
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
343
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
344

    
345
    mMenuPopup = new PopupWindow(act);
346
    mMenuPopup.setContentView(layout);
347
    mMenuPopup.setFocusable(true);
348
    int padding = (int)(height*MENU_MARGIN);
349
    int numButtons = USE_IAP ? 7 : 6;
350
    mLevelHeight = (int)(height*LEVEL_BUTTON_HEIGHT);
351
    mMenuLayoutHeight= padding + numButtons*(mMenuButtonHeight+padding) + mLevelHeight/3 + 3*(mLevelHeight+padding);
352
    mMenuLayoutWidth = Math.min((int)(width*MENU_WIDTH),mMenuLayoutHeight/2);
353
    mLevelWidth = (mMenuLayoutWidth-4*padding)/3;
354

    
355
    layout.setPadding(padding,0,padding,0);
356

    
357
    if( USE_IAP )
358
      {
359
      Button stars = layout.findViewById(R.id.menuStars);
360
      stars.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
361
      stars.setOnClickListener( new View.OnClickListener()
362
          {
363
          @Override
364
          public void onClick(View v)
365
            {
366
            mMenuPopup.dismiss();
367
            RubikDialogStarsStatus d = new RubikDialogStarsStatus();
368
            d.show(act.getSupportFragmentManager(), null);
369
            }
370
          });
371
      }
372
    else
373
      {
374
      Button stars = layout.findViewById(R.id.menuStars);
375
      stars.setVisibility(GONE);
376
      }
377

    
378
    Button solver = layout.findViewById(R.id.menuSolver);
379
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
380
    solver.setOnClickListener( new View.OnClickListener()
381
        {
382
        @Override
383
        public void onClick(View v)
384
          {
385
          mMenuPopup.dismiss();
386
          RubikDialogSolvers solvers = new RubikDialogSolvers();
387
          solvers.show(act.getSupportFragmentManager(), RubikDialogSolvers.getDialogTag() );
388
          }
389
        });
390

    
391
    Button highScores = layout.findViewById(R.id.menuHighScores);
392
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
393
    highScores.setOnClickListener( new View.OnClickListener()
394
        {
395
        @Override
396
        public void onClick(View v)
397
          {
398
          mMenuPopup.dismiss();
399
          Bundle sBundle = new Bundle();
400
          sBundle.putString("argument", "false");
401
          RubikDialogScores scores = new RubikDialogScores();
402
          scores.setArguments(sBundle);
403
          scores.show(act.getSupportFragmentManager(), null);
404
          }
405
        });
406

    
407
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
408
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
409
    prettyPatterns.setOnClickListener( new View.OnClickListener()
410
        {
411
        @Override
412
        public void onClick(View v)
413
          {
414
          mMenuPopup.dismiss();
415
          RubikDialogPattern pDiag = new RubikDialogPattern();
416
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
417
          }
418
        });
419

    
420
    Button tutorials = layout.findViewById(R.id.menuTutorials);
421
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
422
    tutorials.setOnClickListener( new View.OnClickListener()
423
        {
424
        @Override
425
        public void onClick(View v)
426
          {
427
          mMenuPopup.dismiss();
428
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
429
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
430
          }
431
        });
432

    
433
    Button bandaged = layout.findViewById(R.id.menuBandaged);
434
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
435
    bandaged.setOnClickListener( new View.OnClickListener()
436
        {
437
        @Override
438
        public void onClick(View v)
439
          {
440
          mMenuPopup.dismiss();
441
          act.switchToBandagedCreator();
442
          }
443
        });
444

    
445
    Button about = layout.findViewById(R.id.menuAbout);
446
    about.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
447
    about.setOnClickListener( new View.OnClickListener()
448
        {
449
        @Override
450
        public void onClick(View v)
451
          {
452
          mMenuPopup.dismiss();
453
          RubikDialogAbout aDiag = new RubikDialogAbout();
454
          aDiag.show(act.getSupportFragmentManager(), null);
455
          }
456
        });
457

    
458
    TextView levels = layout.findViewById(R.id.menuLevels);
459
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
460

    
461
    setupLevelButtons(act,layout,padding/2);
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  private void setupLevelButtons(RubikActivity act, View layout, int margin)
467
    {
468
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mLevelWidth,mLevelHeight);
469
    params.setMargins(margin,margin,margin,margin);
470

    
471
    mLevel = new Button[LEVELS_SHOWN+1];
472

    
473
    mLevel[0] = layout.findViewById(R.id.level1);
474
    mLevel[1] = layout.findViewById(R.id.level2);
475
    mLevel[2] = layout.findViewById(R.id.level3);
476
    mLevel[3] = layout.findViewById(R.id.level4);
477
    mLevel[4] = layout.findViewById(R.id.level5);
478
    mLevel[5] = layout.findViewById(R.id.level6);
479
    mLevel[6] = layout.findViewById(R.id.level7);
480
    mLevel[7] = layout.findViewById(R.id.level8);
481
    mLevel[8] = layout.findViewById(R.id.levelM);
482

    
483
    for(int i=0; i<=LEVELS_SHOWN; i++)
484
      {
485
      final int ii = i;
486
      mLevel[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
487
      mLevel[i].setLayoutParams(params);
488
      mLevel[i].setOnClickListener( new View.OnClickListener()
489
        {
490
        @Override
491
        public void onClick(View v)
492
          {
493
          ObjectControl control = act.getControl();
494

    
495
          if( control.isScramblingAndSolvingNotBlocked() )
496
            {
497
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
498

    
499
            int currObject = RubikObjectList.getCurrObject();
500
            RubikObject object = RubikObjectList.getObject(currObject);
501
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
502
            mLevelValue = ii+1;
503
            mShouldReactToEndOfScrambling = true;
504
            control.scrambleObject(scrambles);
505
            }
506
          }
507
        });
508
      }
509
    }
510

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

    
513
  private void setupLevelButtonVisibilityAndColor(RubikActivity act)
514
    {
515
    int currObject = RubikObjectList.getCurrObject();
516
    RubikObject object = RubikObjectList.getObject(currObject);
517
    int numScramble = object==null ? 1 : object.getNumScramble();
518
    RubikScores scores = RubikScores.getInstance();
519
    Resources res = act.getResources();
520
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
521
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
522

    
523
    for(int level=0; level<=LEVELS_SHOWN; level++)
524
      {
525
      boolean isSolved = scores.isSolved(currObject,level);
526
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
527
      }
528

    
529
    if( numScramble<=LEVELS_SHOWN || mOldNumScramble<=LEVELS_SHOWN )
530
      {
531
      if( numScramble<mOldNumScramble )
532
        {
533
        int max = Math.min(LEVELS_SHOWN,mOldNumScramble-1);
534
        for(int level=numScramble; level<=max; level++) mLevel[level].setVisibility(View.INVISIBLE);
535
        mLevel[numScramble-1].setText(R.string.levelM);
536
        }
537
      if( numScramble>mOldNumScramble )
538
        {
539
        int max = Math.min(LEVELS_SHOWN,numScramble-1);
540
        mLevel[mOldNumScramble-1].setText( String.valueOf(mOldNumScramble) );
541

    
542
        for(int level=mOldNumScramble; level<=max; level++)
543
          {
544
          mLevel[level].setVisibility(View.VISIBLE);
545
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
546
          else            mLevel[level].setText( R.string.levelM );
547
          }
548
        }
549
      }
550

    
551
    mOldNumScramble = numScramble;
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
556
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
557
// going to be called)
558
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
559

    
560
  public void savePreferences(SharedPreferences.Editor editor)
561
    {
562
    editor.putInt("play_LevelValue", mLevelValue );
563

    
564
    if( mObjectPopup!=null )
565
      {
566
      mObjectPopup.dismiss();
567
      mObjectPopup = null;
568
      }
569

    
570
    if( mMenuPopup!=null )
571
      {
572
      mMenuPopup.dismiss();
573
      mMenuPopup = null;
574
      }
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
  public void restorePreferences(SharedPreferences preferences)
580
    {
581
    mLevelValue = preferences.getInt("play_LevelValue", 0);
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
586

    
587
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
588
    {
589
    View topLayout = act.findViewById(R.id.relativeLayout);
590
    boolean isFullScreen;
591

    
592
    if( topLayout!=null )
593
      {
594
      topLayout.getLocationOnScreen(mLocation);
595
      isFullScreen = (mLocation[1]==0);
596
      }
597
    else
598
      {
599
      isFullScreen = true;
600
      }
601

    
602
    try
603
      {
604
      // if on Android 11 or we are fullscreen
605
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
606
        {
607
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
608
        window.update(view, w, h);
609
        }
610
      else  // Android 10 or below in pop-up mode or split-screen mode
611
        {
612
        view.getLocationOnScreen(mLocation);
613
        int width  = view.getWidth();
614
        int height = view.getHeight();
615
        int x = mLocation[0]+(width-w)/2;
616
        int y = mLocation[1]+height+yoff;
617

    
618
        window.showAsDropDown(view);
619
        window.update(x,y,w,h);
620
        }
621
      }
622
    catch( IllegalArgumentException iae )
623
      {
624
      // ignore, this means window is 'not attached to window manager' -
625
      // which most probably is because we are already exiting the app.
626
      }
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630

    
631
  public int getLevel()
632
    {
633
    return mLevelValue;
634
    }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
  public void recreatePopup()
639
    {
640
    mObjectPopup = null;
641
    }
642

    
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644

    
645
  public boolean shouldReactToEndOfScrambling()
646
    {
647
    return mShouldReactToEndOfScrambling;
648
    }
649

    
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651

    
652
  public void receiveUpdate(RubikUpdates updates)
653
    {
654
    Activity act = mWeakAct.get();
655

    
656
    if( act!=null )
657
      {
658
      act.runOnUiThread(new Runnable()
659
        {
660
        @Override
661
        public void run()
662
          {
663
          int num = updates.getCompletedNumber();
664

    
665
          if( num>0 )
666
            {
667
            String shownNum = String.valueOf(num);
668
            mBubbleUpdates.setText(shownNum);
669
            mBubbleUpdates.setVisibility(View.VISIBLE);
670
            int height = (int)(0.05f*mScreenWidth);
671
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
672
            }
673
         else
674
            {
675
            mBubbleUpdates.setVisibility(View.INVISIBLE);
676
            }
677
          }
678
        });
679
      }
680
    }
681

    
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683

    
684
  public void errorUpdate()
685
    {
686
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
687
    }
688
  }
(5-5/10)