Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 0b5e585c

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.ImageView;
29
import android.widget.LinearLayout;
30
import android.widget.PopupWindow;
31
import android.widget.RelativeLayout;
32
import android.widget.TextView;
33

    
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.main.R;
40
import org.distorted.main.RubikActivity;
41
import org.distorted.dialogs.RubikDialogAbout;
42
import org.distorted.dialogs.RubikDialogPattern;
43
import org.distorted.dialogs.RubikDialogScores;
44
import org.distorted.dialogs.RubikDialogTutorial;
45
import org.distorted.helpers.TransparentImageButton;
46
import org.distorted.objectlib.main.ObjectControl;
47
import org.distorted.objects.RubikObject;
48
import org.distorted.objects.RubikObjectList;
49

    
50
import static android.view.View.inflate;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
55
  {
56
  public static final int NUM_COLUMNS  = 6;
57
  public static final int LEVELS_SHOWN = 8;
58
  private static final int NUM_BUTTONS = 6;
59
  private static final int[] mLocation = new int[2];
60

    
61
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
62
  private PopupWindow mObjectPopup, mMenuPopup;
63
  private WeakReference<RubikActivity> mWeakAct;
64
  private TextView mBubbleUpdates;
65
  private Button[] mLevel;
66
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
67
  private int mLevelValue;
68
  private int mColCount, mRowCount, mMaxRowCount;
69
  private int mBarHeight;
70
  private boolean mShouldReactToEndOfScrambling;
71
  private float mScreenWidth, mScreenHeight;
72
  private int mMargin;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  void leaveScreen(RubikActivity act)
77
    {
78

    
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  void enterScreen(final RubikActivity act)
84
    {
85
    mWeakAct     = new WeakReference<>(act);
86
    mScreenWidth = act.getScreenWidthInPixels();
87
    mScreenHeight= act.getScreenHeightInPixels();
88
    mBarHeight   = act.getHeightUpperBar();
89

    
90
    mMenuButtonHeight = (int)(mScreenWidth*RubikActivity.MENU_BUTTON_HEIGHT);
91
    mMenuTextSize     = (int)(mScreenWidth*RubikActivity.MENU_MAIN_TEXT_SIZE);
92
    mMargin           = (int)(mScreenWidth*RubikActivity.LARGE_MARGIN);
93

    
94
    mObjectPopup = null;
95

    
96
    // TOP ////////////////////////////
97
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
98
    layoutTop.removeAllViews();
99

    
100
    setupSolveButton(act);
101
    layoutTop.addView(mSolveButton);
102
    setupMenuButton(act,mScreenWidth);
103
    layoutTop.addView(mMenuButton);
104
    setupScrambleButton(act);
105
    layoutTop.addView(mScrambleButton);
106

    
107
    // BOTTOM /////////////////////////
108
    setupObjectButton(act,mScreenWidth,mScreenHeight);
109
    createBottomPane(act,mObjButton,null);
110
    }
111

    
112
//////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  private void setupObjectButton(final RubikActivity act, final float width, final float height)
115
    {
116
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
117
    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);
118
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
119
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
120

    
121
    mObjButton.setOnClickListener( new View.OnClickListener()
122
      {
123
      @Override
124
      public void onClick(View view)
125
        {
126
        if( mObjectPopup==null )
127
          {
128
          float width = act.getScreenWidthInPixels();
129
          float height= act.getScreenHeightInPixels();
130
          setupObjectWindow(act,width,height);
131
          }
132

    
133
        if( act.getControl().isUINotBlocked())
134
          {
135
          //int rowCount = Math.min(mMaxRowCount,mRowCount);
136
          View popupView = mObjectPopup.getContentView();
137
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
138
          int popupWidth = (int)width;
139
          int popupHeight= (int)(height-2*mBarHeight);
140
          displayPopup(act,view,mObjectPopup,popupWidth,popupHeight,margin,margin);
141
          }
142
        }
143
      });
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private void setupMenuButton(final RubikActivity act, final float width)
149
    {
150
    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);
151
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
152
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
153

    
154
    mMenuButton.setOnClickListener( new View.OnClickListener()
155
      {
156
      @Override
157
      public void onClick(View view)
158
        {
159
        if( mMenuPopup==null )
160
          {
161
          float width = act.getScreenWidthInPixels();
162
          setupMenuWindow(act,width);
163
          }
164

    
165
        if( act.getControl().isUINotBlocked())
166
          {
167
          View popupView = mMenuPopup.getContentView();
168
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
169
          setupLevelColors(act);
170
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
171
          }
172
        }
173
      });
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
179
    {
180
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
181
    LinearLayout layout = view.findViewById(R.id.objectLayout);
182

    
183
    int numObjects = RubikObjectList.getNumObjects();
184
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
185
    mColCount = NUM_COLUMNS;
186

    
187
    int cubeSize = (int)(width/9);
188
    mObjectSize  = (int)(cubeSize + 2*mMargin + 0.5f);
189
    mMaxRowCount = (int)((height-1.8f*mBarHeight)/mObjectSize);
190

    
191
    GridLayout section0 = produceSection(act, R.drawable.difficulty1, 0, cubeSize);
192
    GridLayout section1 = produceSection(act, R.drawable.difficulty2, 1, cubeSize);
193
    GridLayout section2 = produceSection(act, R.drawable.difficulty3, 2, cubeSize);
194
    GridLayout section3 = produceSection(act, R.drawable.difficulty4, 3, cubeSize);
195
    GridLayout section4 = produceSection(act, R.drawable.difficulty5, 4, cubeSize);
196

    
197
    layout.addView(section0);
198
    layout.addView(section1);
199
    layout.addView(section2);
200
    layout.addView(section3);
201
    layout.addView(section4);
202

    
203
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
204
    setupBottomLayout(act,bottomLayout);
205

    
206
    mObjectPopup = new PopupWindow(act);
207
    mObjectPopup.setFocusable(true);
208
    mObjectPopup.setContentView(view);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  private GridLayout produceSection(final RubikActivity act, int iconID, int difficulty, int cubeSize)
214
    {
215
    GridLayout objectGrid = (GridLayout)inflate( act, R.layout.popup_object_section, null);
216

    
217
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
218
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
219
    params.setMargins(0, (difficulty==0? 0:mMargin) ,0,0);
220
    objectGrid.setLayoutParams(params);
221

    
222
    int[] ordinals = RubikObjectList.produceListOfOrdinals(difficulty);
223
    int numObjects = ordinals.length;
224
    int colCount = NUM_COLUMNS-1;
225
    int rowCount = (numObjects + colCount-1) / colCount;
226

    
227
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
228
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount+1];
229

    
230
    objectGrid.setColumnCount(colCount+1);
231
    objectGrid.setRowCount(rowCount);
232

    
233
    int[] nextInRow = new int[rowCount];
234

    
235
    for(int row=0; row<rowCount; row++)
236
      {
237
      rowSpecs[row] = GridLayout.spec(row);
238
      nextInRow[row]= 1;
239
      }
240
    for(int col=0; col<colCount+1; col++)
241
      {
242
      colSpecs[col] = GridLayout.spec(col);
243
      }
244

    
245
    GridLayout.Spec rs = rowSpecs[0];
246
    GridLayout.Spec cs = colSpecs[0];
247
    GridLayout.LayoutParams ps = new GridLayout.LayoutParams(rs,cs);
248
    ps.bottomMargin = mMargin;
249
    ps.topMargin    = mMargin;
250
    ps.leftMargin   = mMargin;
251
    ps.rightMargin  = mMargin;
252
    ps.width        = cubeSize;
253
    ps.height       = cubeSize;
254

    
255
    ImageView image = new ImageView(act);
256
    image.setImageResource(iconID);
257
    objectGrid.addView(image,ps);
258

    
259
    for(int object=0; object<numObjects; object++)
260
      {
261
      final int ordinal = ordinals[object];
262
      final RubikObject rObject = RubikObjectList.getObject(ordinal);
263
      int row = object/colCount;
264
      ImageButton button = new ImageButton(act);
265
      if( rObject!=null ) rObject.setIconTo(act,button);
266

    
267
      button.setOnClickListener( new View.OnClickListener()
268
        {
269
        @Override
270
        public void onClick(View v)
271
          {
272
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
273
            {
274
            RubikObjectList.setCurrObject(ordinal);
275
            act.changeObject(ordinal,true);
276
            if( mMenuPopup!=null ) setupLevelColors(act);
277
            mMovesController.clearMoves(act);
278
            }
279

    
280
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
281
          }
282
        });
283

    
284
      GridLayout.Spec r = rowSpecs[row];
285
      GridLayout.Spec c = colSpecs[nextInRow[row]];
286
      GridLayout.LayoutParams p = new GridLayout.LayoutParams(r,c);
287
      p.bottomMargin = mMargin;
288
      p.topMargin    = mMargin;
289
      p.leftMargin   = mMargin;
290
      p.rightMargin  = mMargin;
291
      p.width        = cubeSize;
292
      p.height       = cubeSize;
293

    
294
      nextInRow[row]++;
295

    
296
      objectGrid.addView(button,p);
297
      }
298

    
299
    return objectGrid;
300
    }
301

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

    
304
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
305
    {
306
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
307
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
308
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
309

    
310
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
311
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
312
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
313

    
314
    buttonTut.setImageResource(iconT);
315
    buttonDow.setImageResource(iconD);
316
    buttonInf.setImageResource(iconI);
317

    
318
    TypedValue outValue = new TypedValue();
319
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
320
    buttonTut.setBackgroundResource(outValue.resourceId);
321
    buttonDow.setBackgroundResource(outValue.resourceId);
322
    buttonInf.setBackgroundResource(outValue.resourceId);
323

    
324
    buttonTut.setOnClickListener( new View.OnClickListener()
325
      {
326
      @Override
327
      public void onClick(View v)
328
        {
329
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
330
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
331
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
332
        }
333
      });
334

    
335
    buttonDow.setOnClickListener( new View.OnClickListener()
336
      {
337
      @Override
338
      public void onClick(View v)
339
        {
340
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
341
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
342
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
343
        }
344
      });
345

    
346
    buttonInf.setOnClickListener( new View.OnClickListener()
347
      {
348
      @Override
349
      public void onClick(View v)
350
        {
351
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
352
        int currObject = RubikObjectList.getCurrObject();
353
        act.switchConfig(currObject);
354
        }
355
      });
356

    
357
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
358
    mBubbleUpdates.setVisibility(View.INVISIBLE);
359

    
360
    RubikNetwork network = RubikNetwork.getInstance();
361
    network.signUpForUpdates(this);
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  void setupSolveButton(final RubikActivity act)
367
    {
368
    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);
369
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
370
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
371

    
372
    mSolveButton.setOnClickListener( new View.OnClickListener()
373
      {
374
      @Override
375
      public void onClick(View v)
376
        {
377
        act.getControl().solveObject();
378
        mMovesController.clearMoves(act);
379
        }
380
      });
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
  private void setupScrambleButton(final RubikActivity act)
386
    {
387
    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);
388
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
389
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
390

    
391
    mScrambleButton.setOnClickListener( new View.OnClickListener()
392
      {
393
      @Override
394
      public void onClick(View v)
395
        {
396
        mShouldReactToEndOfScrambling = false;
397
        act.getControl().fastScrambleObject(RubikObject.FAST_SCRAMBLES);
398
        }
399
      });
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  private void setupMenuWindow(final RubikActivity act, final float width)
405
    {
406
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
407
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
408

    
409
    mMenuPopup = new PopupWindow(act);
410
    mMenuPopup.setContentView(layout);
411
    mMenuPopup.setFocusable(true);
412
    int padding = (int)(width*RubikActivity.MEDIUM_MARGIN);
413

    
414
    mMenuLayoutWidth = (int)(width*0.6f);
415
    mMenuLayoutHeight= padding + NUM_BUTTONS*(mMenuButtonHeight+padding) + 4*mMenuButtonHeight+6*padding;
416

    
417
    layout.setPadding(padding,0,padding,0);
418

    
419
    Button highScores = layout.findViewById(R.id.menuHighScores);
420
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
421
    highScores.setOnClickListener( new View.OnClickListener()
422
        {
423
        @Override
424
        public void onClick(View v)
425
          {
426
          mMenuPopup.dismiss();
427
          Bundle sBundle = new Bundle();
428
          int currObject = RubikObjectList.getCurrObject();
429
          sBundle.putInt("tab", currObject );
430
          sBundle.putBoolean("submitting", false);
431
          RubikDialogScores scores = new RubikDialogScores();
432
          scores.setArguments(sBundle);
433
          scores.show(act.getSupportFragmentManager(), null);
434
          }
435
        });
436

    
437
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
438
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
439
    prettyPatterns.setOnClickListener( new View.OnClickListener()
440
        {
441
        @Override
442
        public void onClick(View v)
443
          {
444
          mMenuPopup.dismiss();
445
          RubikDialogPattern pDiag = new RubikDialogPattern();
446
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
447
          }
448
        });
449

    
450
    Button solver = layout.findViewById(R.id.menuSolver);
451
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
452
    solver.setOnClickListener( new View.OnClickListener()
453
        {
454
        @Override
455
        public void onClick(View v)
456
          {
457
          mMenuPopup.dismiss();
458
          ScreenList.switchScreen(act, ScreenList.SVER);
459
          }
460
        });
461

    
462
    Button tutorials = layout.findViewById(R.id.menuTutorials);
463
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
464
    tutorials.setOnClickListener( new View.OnClickListener()
465
        {
466
        @Override
467
        public void onClick(View v)
468
          {
469
          mMenuPopup.dismiss();
470
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
471
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
472
          }
473
        });
474

    
475
    Button bandaged = layout.findViewById(R.id.menuBandaged);
476
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
477
    bandaged.setOnClickListener( new View.OnClickListener()
478
        {
479
        @Override
480
        public void onClick(View v)
481
          {
482
          mMenuPopup.dismiss();
483
          act.switchToBandagedCreator();
484
          }
485
        });
486

    
487
    Button about = layout.findViewById(R.id.menuAbout);
488
    about.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
489
    about.setOnClickListener( new View.OnClickListener()
490
        {
491
        @Override
492
        public void onClick(View v)
493
          {
494
          mMenuPopup.dismiss();
495
          RubikDialogAbout aDiag = new RubikDialogAbout();
496
          aDiag.show(act.getSupportFragmentManager(), null);
497
          }
498
        });
499

    
500
    TextView levels = layout.findViewById(R.id.menuLevels);
501
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
502

    
503
    setupLevelButtons(act,layout,padding);
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  private void setupLevelButtons(RubikActivity act, View layout, int padding)
509
    {
510
    int sizeW = (mMenuLayoutWidth-4*padding)/3;
511
    int sizeH = (int)(sizeW*0.8f);
512
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(sizeW,sizeH);
513
    params.setMargins(padding/2,0,padding/2,0);
514

    
515
    mLevel = new Button[LEVELS_SHOWN+1];
516

    
517
    mLevel[0] = layout.findViewById(R.id.level1);
518
    mLevel[1] = layout.findViewById(R.id.level2);
519
    mLevel[2] = layout.findViewById(R.id.level3);
520
    mLevel[3] = layout.findViewById(R.id.level4);
521
    mLevel[4] = layout.findViewById(R.id.level5);
522
    mLevel[5] = layout.findViewById(R.id.level6);
523
    mLevel[6] = layout.findViewById(R.id.level7);
524
    mLevel[7] = layout.findViewById(R.id.level8);
525
    mLevel[8] = layout.findViewById(R.id.levelM);
526

    
527
    for(int i=0; i<=LEVELS_SHOWN; i++)
528
      {
529
      final int ii = i;
530
      mLevel[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
531
      mLevel[i].setLayoutParams(params);
532
      mLevel[i].setOnClickListener( new View.OnClickListener()
533
        {
534
        @Override
535
        public void onClick(View v)
536
          {
537
          ObjectControl control = act.getControl();
538

    
539
          if(control.isUINotBlocked())
540
            {
541
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
542

    
543
            int currObject = RubikObjectList.getCurrObject();
544
            RubikObject object = RubikObjectList.getObject(currObject);
545
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
546
            mLevelValue = ii+1;
547
            mShouldReactToEndOfScrambling = true;
548
            control.scrambleObject(scrambles);
549
            }
550
          }
551
        });
552
      }
553
    }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

    
557
  private void setupLevelColors(RubikActivity act)
558
    {
559
    int currObject = RubikObjectList.getCurrObject();
560
    RubikScores scores = RubikScores.getInstance();
561
    Resources res = act.getResources();
562
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
563
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
564

    
565
    for(int level=0; level<=LEVELS_SHOWN; level++)
566
      {
567
      boolean isSolved = scores.isSolved(currObject,level);
568
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
569
      }
570
    }
571

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

    
578
  public void savePreferences(SharedPreferences.Editor editor)
579
    {
580
    editor.putInt("play_LevelValue", mLevelValue );
581

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

    
588
    if( mMenuPopup!=null )
589
      {
590
      mMenuPopup.dismiss();
591
      mMenuPopup = null;
592
      }
593
    }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
  public void restorePreferences(SharedPreferences preferences)
598
    {
599
    mLevelValue = preferences.getInt("play_LevelValue", 0);
600
    }
601

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
604

    
605
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
606
    {
607
    View topLayout = act.findViewById(R.id.relativeLayout);
608
    boolean isFullScreen;
609

    
610
    if( topLayout!=null )
611
      {
612
      topLayout.getLocationOnScreen(mLocation);
613
      isFullScreen = (mLocation[1]==0);
614
      }
615
    else
616
      {
617
      isFullScreen = true;
618
      }
619

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

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

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

    
649
  public int getLevel()
650
    {
651
    return mLevelValue;
652
    }
653

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

    
656
  public void recreatePopup()
657
    {
658
    mObjectPopup = null;
659
    }
660

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

    
663
  public boolean shouldReactToEndOfScrambling()
664
    {
665
    return mShouldReactToEndOfScrambling;
666
    }
667

    
668
///////////////////////////////////////////////////////////////////////////////////////////////////
669

    
670
  public void receiveUpdate(RubikUpdates updates)
671
    {
672
    Activity act = mWeakAct.get();
673

    
674
    if( act!=null )
675
      {
676
      act.runOnUiThread(new Runnable()
677
        {
678
        @Override
679
        public void run()
680
          {
681
          int num = updates.getCompletedNumber();
682

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

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701

    
702
  public void errorUpdate()
703
    {
704
    android.util.Log.e("D", "Screen: Error receiving update");
705
    }
706
  }
(5-5/10)