Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 4279106d

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.RubikDialogStarsStatus;
34
import org.distorted.dialogs.RubikDialogUpdates;
35
import org.distorted.external.RubikNetwork;
36
import org.distorted.external.RubikScores;
37
import org.distorted.external.RubikUpdates;
38

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

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

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

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  void leaveScreen(RubikActivity act)
87
    {
88

    
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

    
103
    mObjectPopup = null;
104
    mOldNumScramble = 1000; // used to remember which 'level' buttons are visible; initially all visible
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
        if( act.getControl().isUINotBlocked())
144
          {
145
          int rowCount = Math.min(mMaxRowCount,mRowCount);
146
          View popupView = mObjectPopup.getContentView();
147
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
148
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
149
          }
150
        }
151
      });
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

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

    
173
        if( act.getControl().isUINotBlocked())
174
          {
175
          View popupView = mMenuPopup.getContentView();
176
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
177
          setupLevelButtonVisibilityAndColor(act);
178
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
179
          }
180
        }
181
      });
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
187
    {
188
    int numObjects = RubikObjectList.getNumObjects();
189
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
190
    mColCount = NUM_COLUMNS;
191

    
192
    int cubeSize = (int)(width/9);
193
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
194
    int padding  = (int)(width*RubikActivity.POPUP_PADDING);
195
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
196
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
197

    
198
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
199
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
200
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
201
    setupBottomLayout(act,bottomLayout);
202

    
203
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
204

    
205
    for(int child=0; child<numObjects; child++)
206
      {
207
      final RubikObject obj = RubikObjectList.getObject(child);
208
      View v = objectGrid.getChildAt(child);
209
      ImageButton button = PopupCreator.getButton(obj,v);
210
      final int ordinal = child;
211

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

    
232
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
233
          }
234
        });
235
      }
236

    
237
    mObjectPopup = new PopupWindow(act);
238
    mObjectPopup.setFocusable(true);
239
    mObjectPopup.setContentView(view);
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
245
    {
246
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
247
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
248
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
249

    
250
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
251
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
252
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
253

    
254
    buttonTut.setImageResource(iconT);
255
    buttonDow.setImageResource(iconD);
256
    buttonInf.setImageResource(iconI);
257

    
258
    TypedValue outValue = new TypedValue();
259
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
260
    buttonTut.setBackgroundResource(outValue.resourceId);
261
    buttonDow.setBackgroundResource(outValue.resourceId);
262
    buttonInf.setBackgroundResource(outValue.resourceId);
263

    
264
    buttonTut.setOnClickListener( new View.OnClickListener()
265
      {
266
      @Override
267
      public void onClick(View v)
268
        {
269
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
270
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
271
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
272
        }
273
      });
274

    
275
    buttonDow.setOnClickListener( new View.OnClickListener()
276
      {
277
      @Override
278
      public void onClick(View v)
279
        {
280
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
281
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
282
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
283
        }
284
      });
285

    
286
    buttonInf.setOnClickListener( new View.OnClickListener()
287
      {
288
      @Override
289
      public void onClick(View v)
290
        {
291
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
292
        int currObject = RubikObjectList.getCurrObject();
293
        act.switchConfig(currObject);
294
        }
295
      });
296

    
297
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
298
    mBubbleUpdates.setVisibility(View.INVISIBLE);
299

    
300
    RubikNetwork network = RubikNetwork.getInstance();
301
    network.signUpForUpdates(this);
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  void setupSolveButton(final RubikActivity act)
307
    {
308
    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);
309
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
310
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
311

    
312
    mSolveButton.setOnClickListener( new View.OnClickListener()
313
      {
314
      @Override
315
      public void onClick(View v)
316
        {
317
        act.getControl().solveObject();
318
        mMovesController.clearMoves(act);
319
        }
320
      });
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  private void setupScrambleButton(final RubikActivity act)
326
    {
327
    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);
328
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
329
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
330

    
331
    mScrambleButton.setOnClickListener( new View.OnClickListener()
332
      {
333
      @Override
334
      public void onClick(View v)
335
        {
336
        mShouldReactToEndOfScrambling = false;
337
        int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
338
        act.getControl().fastScrambleObject(duration,RubikObject.FAST_SCRAMBLES);
339
        }
340
      });
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  private void setupMenuWindow(final RubikActivity act, final float width, final float height)
346
    {
347
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
348
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
349

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

    
360
    layout.setPadding(padding,0,padding,0);
361

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

    
383
    Button highScores = layout.findViewById(R.id.menuHighScores);
384
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
385
    highScores.setOnClickListener( new View.OnClickListener()
386
        {
387
        @Override
388
        public void onClick(View v)
389
          {
390
          mMenuPopup.dismiss();
391
          Bundle sBundle = new Bundle();
392
          int currObject = RubikObjectList.getCurrObject();
393
          sBundle.putInt("tab", currObject );
394
          sBundle.putBoolean("submitting", false);
395
          RubikDialogScores scores = new RubikDialogScores();
396
          scores.setArguments(sBundle);
397
          scores.show(act.getSupportFragmentManager(), null);
398
          }
399
        });
400

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

    
414
    Button solver = layout.findViewById(R.id.menuSolver);
415
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
416
    solver.setOnClickListener( new View.OnClickListener()
417
        {
418
        @Override
419
        public void onClick(View v)
420
          {
421
          mMenuPopup.dismiss();
422
          ScreenList.switchScreen(act, ScreenList.SVER);
423
          }
424
        });
425

    
426
    Button tutorials = layout.findViewById(R.id.menuTutorials);
427
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
428
    tutorials.setOnClickListener( new View.OnClickListener()
429
        {
430
        @Override
431
        public void onClick(View v)
432
          {
433
          mMenuPopup.dismiss();
434
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
435
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
436
          }
437
        });
438

    
439
    Button bandaged = layout.findViewById(R.id.menuBandaged);
440
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
441
    bandaged.setOnClickListener( new View.OnClickListener()
442
        {
443
        @Override
444
        public void onClick(View v)
445
          {
446
          mMenuPopup.dismiss();
447
          act.switchToBandagedCreator();
448
          }
449
        });
450

    
451
    Button about = layout.findViewById(R.id.menuAbout);
452
    about.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
453
    about.setOnClickListener( new View.OnClickListener()
454
        {
455
        @Override
456
        public void onClick(View v)
457
          {
458
          mMenuPopup.dismiss();
459
          RubikDialogAbout aDiag = new RubikDialogAbout();
460
          aDiag.show(act.getSupportFragmentManager(), null);
461
          }
462
        });
463

    
464
    TextView levels = layout.findViewById(R.id.menuLevels);
465
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
466

    
467
    setupLevelButtons(act,layout,padding/2);
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  private void setupLevelButtons(RubikActivity act, View layout, int margin)
473
    {
474
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mLevelWidth,mLevelHeight);
475
    params.setMargins(margin,margin,margin,margin);
476

    
477
    mLevel = new Button[LEVELS_SHOWN+1];
478

    
479
    mLevel[0] = layout.findViewById(R.id.level1);
480
    mLevel[1] = layout.findViewById(R.id.level2);
481
    mLevel[2] = layout.findViewById(R.id.level3);
482
    mLevel[3] = layout.findViewById(R.id.level4);
483
    mLevel[4] = layout.findViewById(R.id.level5);
484
    mLevel[5] = layout.findViewById(R.id.level6);
485
    mLevel[6] = layout.findViewById(R.id.level7);
486
    mLevel[7] = layout.findViewById(R.id.level8);
487
    mLevel[8] = layout.findViewById(R.id.levelM);
488

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

    
501
          if(control.isUINotBlocked())
502
            {
503
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
504

    
505
            int currObject = RubikObjectList.getCurrObject();
506
            RubikObject object = RubikObjectList.getObject(currObject);
507
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
508
            mLevelValue = ii+1;
509
            mShouldReactToEndOfScrambling = true;
510
            control.scrambleObject(scrambles);
511
            }
512
          }
513
        });
514
      }
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

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

    
529
    for(int level=0; level<=LEVELS_SHOWN; level++)
530
      {
531
      boolean isSolved = scores.isSolved(currObject,level);
532
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
533
      }
534

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

    
548
        for(int level=mOldNumScramble; level<=max; level++)
549
          {
550
          mLevel[level].setVisibility(View.VISIBLE);
551
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
552
          else            mLevel[level].setText( R.string.levelM );
553
          }
554
        }
555
      }
556

    
557
    mOldNumScramble = numScramble;
558
    }
559

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

    
566
  public void savePreferences(SharedPreferences.Editor editor)
567
    {
568
    editor.putInt("play_LevelValue", mLevelValue );
569

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

    
576
    if( mMenuPopup!=null )
577
      {
578
      mMenuPopup.dismiss();
579
      mMenuPopup = null;
580
      }
581
    }
582

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

    
585
  public void restorePreferences(SharedPreferences preferences)
586
    {
587
    mLevelValue = preferences.getInt("play_LevelValue", 0);
588
    }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
592

    
593
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
594
    {
595
    View topLayout = act.findViewById(R.id.relativeLayout);
596
    boolean isFullScreen;
597

    
598
    if( topLayout!=null )
599
      {
600
      topLayout.getLocationOnScreen(mLocation);
601
      isFullScreen = (mLocation[1]==0);
602
      }
603
    else
604
      {
605
      isFullScreen = true;
606
      }
607

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

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

    
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636

    
637
  public int getLevel()
638
    {
639
    return mLevelValue;
640
    }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

    
644
  public void recreatePopup()
645
    {
646
    mObjectPopup = null;
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

    
651
  public boolean shouldReactToEndOfScrambling()
652
    {
653
    return mShouldReactToEndOfScrambling;
654
    }
655

    
656
///////////////////////////////////////////////////////////////////////////////////////////////////
657

    
658
  public void receiveUpdate(RubikUpdates updates)
659
    {
660
    Activity act = mWeakAct.get();
661

    
662
    if( act!=null )
663
      {
664
      act.runOnUiThread(new Runnable()
665
        {
666
        @Override
667
        public void run()
668
          {
669
          int num = updates.getCompletedNumber();
670

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

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689

    
690
  public void errorUpdate()
691
    {
692
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
693
    }
694
  }
(5-5/10)