Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 24cd23dd

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.075f;
61
  private static final float MENU_TEXT_SIZE      = 0.032f;
62
  private static final float MENU_MARGIN         = 0.008f;
63

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

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

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  void leaveScreen(RubikActivity act)
85
    {
86

    
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

    
98
    mMenuButtonHeight = (int)(mScreenHeight*MENU_BUTTON_HEIGHT);
99
    mMenuTextSize     = (int)(mScreenHeight*MENU_TEXT_SIZE);
100

    
101
    mObjectPopup = null;
102
    mOldNumScramble = 1000; // used to remember which 'level' buttons are visible; initially all visible
103

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

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

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

    
120
//////////////////////////////////////////////////////////////////////////////////////////////////
121

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

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

    
141
        if( act.getControl().isUINotBlocked())
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

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

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

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

    
201
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
202

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

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

    
230
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
231
          }
232
        });
233
      }
234

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

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

    
248
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
249
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
250
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
251

    
252
    buttonTut.setImageResource(iconT);
253
    buttonDow.setImageResource(iconD);
254
    buttonInf.setImageResource(iconI);
255

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

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

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

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

    
295
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
296
    mBubbleUpdates.setVisibility(View.INVISIBLE);
297

    
298
    RubikNetwork network = RubikNetwork.getInstance();
299
    network.signUpForUpdates(this);
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

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

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

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

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

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

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

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

    
348
    mMenuPopup = new PopupWindow(act);
349
    mMenuPopup.setContentView(layout);
350
    mMenuPopup.setFocusable(true);
351
    int padding = (int)(height*MENU_MARGIN);
352
    int numButtons = USE_IAP ? 7 : 6;
353
    mMenuLayoutWidth = (int)(width*0.65f);
354
    mLevelWidth = (mMenuLayoutWidth-4*padding)/3;
355
    mLevelHeight = (int)(mLevelWidth*0.75f);
356
    mMenuLayoutHeight= padding + numButtons*(mMenuButtonHeight+padding) + mLevelHeight/3 + 3*(mLevelHeight+padding);
357

    
358
    layout.setPadding(padding,0,padding,0);
359

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

    
381
    Button highScores = layout.findViewById(R.id.menuHighScores);
382
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
383
    highScores.setOnClickListener( new View.OnClickListener()
384
        {
385
        @Override
386
        public void onClick(View v)
387
          {
388
          mMenuPopup.dismiss();
389
          Bundle sBundle = new Bundle();
390
          int currObject = RubikObjectList.getCurrObject();
391
          sBundle.putInt("tab", currObject );
392
          sBundle.putBoolean("submitting", 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);
466
    }
467

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

    
470
  private void setupLevelButtons(RubikActivity act, View layout, int padding)
471
    {
472
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mLevelWidth,mLevelHeight);
473
    params.setMargins(padding/2,0,padding/2,0);
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)