Project

General

Profile

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

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

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.dialogs.RubikDialogAbout;
37
import org.distorted.external.RubikNetwork;
38
import org.distorted.external.RubikScores;
39
import org.distorted.external.RubikUpdates;
40

    
41
import org.distorted.helpers.PopupCreator;
42
import org.distorted.main.R;
43
import org.distorted.main.RubikActivity;
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);
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( act.getControl().isScramblingAndSolvingNotBlocked() )
137
          {
138
          if( mObjectPopup==null )
139
            {
140
            float width = act.getScreenWidthInPixels();
141
            float height= act.getScreenHeightInPixels();
142
            setupObjectWindow(act,width,height);
143
            }
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( act.getControl().isScramblingAndSolvingNotBlocked() )
168
          {
169
          if( mMenuPopup==null )
170
            {
171
            float width = act.getScreenWidthInPixels();
172
            setupMenuWindow(act,width,height);
173
            }
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().isScramblingAndSolvingNotBlocked() && 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
        int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
337
        boolean success = act.getControl().fastScrambleObject(duration,RubikObject.FAST_SCRAMBLES);
338
        if( success ) mShouldReactToEndOfScrambling = false;
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 solver = layout.findViewById(R.id.menuSolver);
384
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
385
    solver.setOnClickListener( new View.OnClickListener()
386
        {
387
        @Override
388
        public void onClick(View v)
389
          {
390
          mMenuPopup.dismiss();
391
          RubikDialogSolvers solvers = new RubikDialogSolvers();
392
          solvers.show(act.getSupportFragmentManager(), RubikDialogSolvers.getDialogTag() );
393
          }
394
        });
395

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

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

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

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

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

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

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

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

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

    
476
    mOldNumScramble = LEVELS_SHOWN+1;
477

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

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

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

    
502
          if( control.isScramblingAndSolvingNotBlocked() )
503
            {
504
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
505

    
506
            int currObject = RubikObjectList.getCurrObject();
507
            RubikObject object = RubikObjectList.getObject(currObject);
508
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
509

    
510
            boolean success = control.scrambleObject(scrambles);
511
            if( success )
512
              {
513
              mLevelValue = ii+1;
514
              mShouldReactToEndOfScrambling = true;
515
              }
516
            }
517
          }
518
        });
519
      }
520
    }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

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

    
534
    for(int level=0; level<=LEVELS_SHOWN; level++)
535
      {
536
      boolean isSolved = scores.isSolved(currObject,level);
537
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
538
      }
539

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

    
553
        for(int level=mOldNumScramble; level<=max; level++)
554
          {
555
          mLevel[level].setVisibility(View.VISIBLE);
556
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
557
          else            mLevel[level].setText( R.string.levelM );
558
          }
559
        }
560
      }
561

    
562
    mOldNumScramble = numScramble;
563
    }
564

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

    
571
  public void savePreferences(SharedPreferences.Editor editor)
572
    {
573
    editor.putInt("play_LevelValue", mLevelValue );
574

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

    
581
    if( mMenuPopup!=null )
582
      {
583
      mMenuPopup.dismiss();
584
      mMenuPopup = null;
585
      }
586
    }
587

    
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589

    
590
  public void restorePreferences(SharedPreferences preferences)
591
    {
592
    mLevelValue = preferences.getInt("play_LevelValue", 0);
593
    }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
597

    
598
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
599
    {
600
    View topLayout = act.findViewById(R.id.relativeLayout);
601
    boolean isFullScreen;
602

    
603
    if( topLayout!=null )
604
      {
605
      topLayout.getLocationOnScreen(mLocation);
606
      isFullScreen = (mLocation[1]==0);
607
      }
608
    else
609
      {
610
      isFullScreen = true;
611
      }
612

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

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

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

    
642
  public int getLevel()
643
    {
644
    return mLevelValue;
645
    }
646

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

    
649
  public void recreatePopup()
650
    {
651
    mObjectPopup = null;
652
    }
653

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

    
656
  public boolean shouldReactToEndOfScrambling()
657
    {
658
    return mShouldReactToEndOfScrambling;
659
    }
660

    
661
///////////////////////////////////////////////////////////////////////////////////////////////////
662

    
663
  public void receiveUpdate(RubikUpdates updates)
664
    {
665
    Activity act = mWeakAct.get();
666

    
667
    if( act!=null )
668
      {
669
      act.runOnUiThread(new Runnable()
670
        {
671
        @Override
672
        public void run()
673
          {
674
          int num = updates.getCompletedNumber();
675

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

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694

    
695
  public void errorUpdate()
696
    {
697
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
698
    }
699
  }
(5-5/10)