Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 79422ed8

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

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

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

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

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

    
124
  private void setupObjectButton(final RubikActivity act, final float width)
125
    {
126
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
127
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
128
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
129
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
130

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

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

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

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

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

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

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

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

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

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

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

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

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

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

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

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

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

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

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

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

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

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  void setupSolveButton(final RubikActivity act)
301
    {
302
    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);
303
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
304
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
305

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

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  private void setupScrambleButton(final RubikActivity act)
320
    {
321
    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);
322
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
323
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
324

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

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
470
    mOldNumScramble = LEVELS_SHOWN+1;
471

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

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

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

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

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

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

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

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

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

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

    
552
    mOldNumScramble = numScramble;
553
    }
554

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

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

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

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

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579

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

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

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

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

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

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

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631

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

    
637
///////////////////////////////////////////////////////////////////////////////////////////////////
638

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

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645

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

    
651
///////////////////////////////////////////////////////////////////////////////////////////////////
652

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

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

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

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684

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