Project

General

Profile

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

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

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)( (Math.min(width,(int)(height*0.7f))) / 9 );
193
    int margin   = (int)(height*RubikActivity.POPUP_MARGIN);
194
    int padding  = (int)(height*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
          sBundle.putString("argument", "false");
393
          RubikDialogScores scores = new RubikDialogScores();
394
          scores.setArguments(sBundle);
395
          scores.show(act.getSupportFragmentManager(), null);
396
          }
397
        });
398

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

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

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

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

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

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

    
465
    setupLevelButtons(act,layout,padding/2);
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

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

    
475
    mLevel = new Button[LEVELS_SHOWN+1];
476

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

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

    
499
          if(control.isUINotBlocked())
500
            {
501
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
502

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

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

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

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

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

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

    
555
    mOldNumScramble = numScramble;
556
    }
557

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

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

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

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

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

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

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

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

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

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

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

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
  public int getLevel()
636
    {
637
    return mLevelValue;
638
    }
639

    
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641

    
642
  public void recreatePopup()
643
    {
644
    mObjectPopup = null;
645
    }
646

    
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648

    
649
  public boolean shouldReactToEndOfScrambling()
650
    {
651
    return mShouldReactToEndOfScrambling;
652
    }
653

    
654
///////////////////////////////////////////////////////////////////////////////////////////////////
655

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

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

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

    
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687

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