Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 5a4ee169

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

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

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  void leaveScreen(RubikActivity act)
89
    {
90

    
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

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

    
102
    mMenuButtonHeight = (int)(mScreenHeight*MENU_BUTTON_HEIGHT);
103
    mMenuTextSize     = (int)(mScreenHeight*MENU_TEXT_SIZE);
104

    
105
    mObjectPopup = null;
106

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

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

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

    
123
//////////////////////////////////////////////////////////////////////////////////////////////////
124

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

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

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

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

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

    
193
    int cubeSize = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
194
    int margin   = (int)(height*RubikActivity.POPUP_MARGIN);
195
    int padding  = (int)(height*RubikActivity.POPUP_PADDING);
196
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
197
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
198

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

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

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

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

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

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

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

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

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

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

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

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

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

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

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

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

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

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

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

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

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

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

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

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

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

    
384
    Button solver = layout.findViewById(R.id.menuSolver);
385
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
386
    solver.setOnClickListener( new View.OnClickListener()
387
        {
388
        @Override
389
        public void onClick(View v)
390
          {
391
          mMenuPopup.dismiss();
392
          RubikDialogSolvers solvers = new RubikDialogSolvers();
393
          solvers.show(act.getSupportFragmentManager(), RubikDialogSolvers.getDialogTag() );
394
          }
395
        });
396

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

    
413
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
414
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
415
    prettyPatterns.setOnClickListener( new View.OnClickListener()
416
        {
417
        @Override
418
        public void onClick(View v)
419
          {
420
          mMenuPopup.dismiss();
421
          RubikDialogPattern pDiag = new RubikDialogPattern();
422
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
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
          RubikDialogCreators creators = new RubikDialogCreators();
448
          creators.show(act.getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
449
          }
450
        });
451

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

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

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

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

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

    
478
    mOldNumScramble = LEVELS_SHOWN+1;
479

    
480
    mLevel = new Button[LEVELS_SHOWN+1];
481

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

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

    
504
          if( control.isScramblingAndSolvingNotBlocked() )
505
            {
506
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
507

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

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

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

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

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

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

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

    
564
    mOldNumScramble = numScramble;
565
    }
566

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

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

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

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

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591

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

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

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

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

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

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

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

    
644
  public int getLevel()
645
    {
646
    return mLevelValue;
647
    }
648

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

    
651
  public void recreatePopup()
652
    {
653
    mObjectPopup = null;
654
    }
655

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

    
658
  public boolean shouldReactToEndOfScrambling()
659
    {
660
    return mShouldReactToEndOfScrambling;
661
    }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664

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

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

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

    
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696

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