Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.screens;
21

    
22
import java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25
import android.content.Context;
26
import android.content.SharedPreferences;
27
import android.content.res.Resources;
28
import android.graphics.drawable.BitmapDrawable;
29
import android.os.Build;
30
import android.os.Bundle;
31
import android.util.TypedValue;
32
import android.view.Gravity;
33
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.widget.Button;
36
import android.widget.GridLayout;
37
import android.widget.ImageButton;
38
import android.widget.LinearLayout;
39
import android.widget.PopupWindow;
40
import android.widget.RelativeLayout;
41
import android.widget.TextView;
42

    
43
import org.distorted.dialogs.RubikDialogUpdates;
44
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.RubikUpdates;
46
import org.distorted.objectlib.main.ObjectControl;
47

    
48
import org.distorted.main.R;
49
import org.distorted.main.RubikActivity;
50
import org.distorted.dialogs.RubikDialogAbout;
51
import org.distorted.dialogs.RubikDialogPattern;
52
import org.distorted.dialogs.RubikDialogScores;
53
import org.distorted.dialogs.RubikDialogTutorial;
54
import org.distorted.helpers.TransparentButton;
55
import org.distorted.helpers.TransparentImageButton;
56
import org.distorted.external.RubikScores;
57
import org.distorted.objects.RubikObject;
58
import org.distorted.objects.RubikObjectList;
59

    
60
import static android.view.View.inflate;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
65
  {
66
  public static final int NUM_COLUMNS  = 5;
67
  public static final int LEVELS_SHOWN = 10;
68

    
69
  private static final int[] BUTTON_LABELS_FULL = { R.string.scores,
70
                                                    R.string.patterns,
71
                                                    R.string.solver,
72
                                                    R.string.tutorials,
73
                                                    R.string.bandaged,
74
                                                    R.string.about };
75

    
76
  private static final int[] BUTTON_LABELS_LEAN = { R.string.scores,
77
                                                    R.string.patterns,
78
                                                    R.string.solver,
79
                                                    R.string.tutorials,
80
                                                    R.string.about };
81

    
82
  private static final float LAST_BUTTON = 1.5f;
83
  private static final int[] mLocation = new int[2];
84

    
85
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
86
  private TransparentButton mPlayButton;
87
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
88
  private LinearLayout mPlayLayout;
89
  private TextView mBubbleUpdates;
90
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
91
  private int mLevelValue;
92
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
93
  private int mColCount, mRowCount, mMaxRowCount;
94
  private int mUpperBarHeight;
95
  private boolean mShouldReactToEndOfScrambling;
96
  private int mBottomHeight;
97
  private float mScreenWidth;
98
  private WeakReference<RubikActivity> mWeakAct;
99
  private boolean mDisplayBandaged;
100
  private int mNumButtons;
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  void leaveScreen(RubikActivity act)
105
    {
106

    
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  void enterScreen(final RubikActivity act)
112
    {
113
    mDisplayBandaged = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
114
    mNumButtons = mDisplayBandaged ? BUTTON_LABELS_FULL.length : BUTTON_LABELS_LEAN.length;
115

    
116
    mWeakAct = new WeakReference<>(act);
117
    int numObjects = RubikObjectList.getNumObjects();
118
    mScreenWidth = act.getScreenWidthInPixels();
119
    mUpperBarHeight = act.getHeightUpperBar();
120

    
121
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
122
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
123
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
124

    
125
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
126
    mColCount = NUM_COLUMNS;
127

    
128
    // TOP ////////////////////////////
129
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
130
    layoutTop.removeAllViews();
131

    
132
    setupObjectButton(act,mScreenWidth);
133
    layoutTop.addView(mObjButton);
134

    
135
    setupMenuButton(act,mScreenWidth);
136
    layoutTop.addView(mMenuButton);
137

    
138
    setupPlayButton(act,mScreenWidth);
139
    layoutTop.addView(mPlayButton);
140

    
141
    setupSolveButton(act);
142
    setupScrambleButton(act);
143
    createBottomPane(act,mSolveButton,mScrambleButton);
144
    }
145

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

    
148
  private void setupObjectButton(final RubikActivity act, final float width)
149
    {
150
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
151
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
152
    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);
153
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
154
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
155

    
156
    mObjButton.setOnClickListener( new View.OnClickListener()
157
      {
158
      @Override
159
      public void onClick(View view)
160
        {
161
        if( mObjectPopup==null )
162
          {
163
          float width = act.getScreenWidthInPixels();
164
          float height= act.getScreenHeightInPixels();
165
          setupObjectWindow(act,width,height);
166
          }
167

    
168
        if( act.getControl().isUINotBlocked())
169
          {
170
          int rowCount = Math.min(mMaxRowCount,mRowCount);
171
          View popupView = mObjectPopup.getContentView();
172
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
173
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
174
          }
175
        }
176
      });
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private void setupPlayButton(final RubikActivity act, final float width)
182
    {
183
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
184

    
185
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
186

    
187
    mPlayButton.setOnClickListener( new View.OnClickListener()
188
      {
189
      @Override
190
      public void onClick(View view)
191
        {
192
        if( mPlayPopup==null )
193
          {
194
          float width = act.getScreenWidthInPixels();
195
          setupPlayWindow(act,width);
196
          }
197

    
198
        if( act.getControl().isUINotBlocked())
199
          {
200
          adjustSolvedIcons();
201
          float height= act.getScreenHeightInPixels();
202
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
203
          View popupView = mPlayPopup.getContentView();
204
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
205
          final int object  = RubikObjectList.getCurrObject();
206
          final int dbLevel = RubikObjectList.getDBLevel(object);
207
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
208
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
209
          final int realHeight = Math.min(popupHeight,maxHeight);
210
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
211
          }
212
        }
213
      });
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private void setupMenuButton(final RubikActivity act, final float width)
219
    {
220
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
221
    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);
222
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
223
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
224

    
225
    mMenuButton.setOnClickListener( new View.OnClickListener()
226
      {
227
      @Override
228
      public void onClick(View view)
229
        {
230
        if( mMenuPopup==null )
231
          {
232
          float width = act.getScreenWidthInPixels();
233
          setupMenuWindow(act,width);
234
          }
235

    
236
        if( act.getControl().isUINotBlocked())
237
          {
238
          View popupView = mMenuPopup.getContentView();
239
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
240
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
241
          }
242
        }
243
      });
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
249
    {
250
    int cubeWidth = (int)(width/9);
251
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
252
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
253
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
254

    
255
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
256
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
257

    
258
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
259
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
260

    
261
    objectGrid.setColumnCount(mColCount);
262
    objectGrid.setRowCount(mRowCount);
263

    
264
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
265
    setupBottomLayout(act,bottomLayout);
266

    
267
    mObjectPopup = new PopupWindow(act);
268
    mObjectPopup.setFocusable(true);
269
    mObjectPopup.setContentView(view);
270

    
271
    int[] nextInRow = new int[mRowCount];
272

    
273
    for(int row=0; row<mRowCount; row++)
274
      {
275
      rowSpecs[row] = GridLayout.spec(row);
276
      nextInRow[row]= 0;
277
      }
278
    for(int col=0; col<mColCount; col++)
279
      {
280
      colSpecs[col] = GridLayout.spec(col);
281
      }
282

    
283
    int numObjects = RubikObjectList.getNumObjects();
284

    
285
    for(int object=0; object<numObjects; object++)
286
      {
287
      final int ordinal = object;
288
      final RubikObject rObject = RubikObjectList.getObject(object);
289
      int row = object/NUM_COLUMNS;
290
      ImageButton button = new ImageButton(act);
291
      if( rObject!=null ) rObject.setIconTo(act,button);
292

    
293
      button.setOnClickListener( new View.OnClickListener()
294
        {
295
        @Override
296
        public void onClick(View v)
297
          {
298
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
299
            {
300
            RubikObjectList.setCurrObject(act,ordinal);
301
            act.changeObject(ordinal,true);
302
            if( mPlayLayout!=null ) adjustLevels(act);
303
            mMovesController.clearMoves(act);
304
            }
305

    
306
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
307
          }
308
        });
309

    
310
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
311
      params.bottomMargin = margin;
312
      params.topMargin    = margin;
313
      params.leftMargin   = margin;
314
      params.rightMargin  = margin;
315

    
316
      params.width = cubeWidth;
317
      params.height= cubeWidth;
318

    
319
      nextInRow[row]++;
320

    
321
      objectGrid.addView(button, params);
322
      }
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
328
    {
329
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
330
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
331
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
332

    
333
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
334
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
335
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
336

    
337
    buttonTut.setImageResource(iconT);
338
    buttonDow.setImageResource(iconD);
339
    buttonInf.setImageResource(iconI);
340

    
341
    Resources res = act.getResources();
342
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
343
    mBottomHeight = bd.getIntrinsicHeight();
344

    
345
    TypedValue outValue = new TypedValue();
346
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
347
    buttonTut.setBackgroundResource(outValue.resourceId);
348
    buttonDow.setBackgroundResource(outValue.resourceId);
349
    buttonInf.setBackgroundResource(outValue.resourceId);
350

    
351
    buttonTut.setOnClickListener( new View.OnClickListener()
352
      {
353
      @Override
354
      public void onClick(View v)
355
        {
356
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
357
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
358
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
359
        }
360
      });
361

    
362
    buttonDow.setOnClickListener( new View.OnClickListener()
363
      {
364
      @Override
365
      public void onClick(View v)
366
        {
367
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
368
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
369
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
370
        }
371
      });
372

    
373
    buttonInf.setOnClickListener( new View.OnClickListener()
374
      {
375
      @Override
376
      public void onClick(View v)
377
        {
378
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
379
        int currObject = RubikObjectList.getCurrObject();
380
        act.switchConfig(currObject);
381
        }
382
      });
383

    
384
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
385
    mBubbleUpdates.setVisibility(View.INVISIBLE);
386

    
387
    RubikNetwork network = RubikNetwork.getInstance();
388
    network.signUpForUpdates(this);
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
  private void setupMenuWindow(final RubikActivity act, final float width)
394
    {
395
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
396
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
397
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
398

    
399
    mMenuPopup = new PopupWindow(act);
400
    mMenuPopup.setContentView(layout);
401
    mMenuPopup.setFocusable(true);
402
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
403
    int padding = (int)(width*RubikActivity.PADDING);
404

    
405
    mMenuLayoutWidth = (int)(width/2);
406
    mMenuLayoutHeight= (int)(2*margin + mNumButtons*(mMenuItemSize+margin));
407

    
408
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
409

    
410
    int[] labels = mDisplayBandaged ? BUTTON_LABELS_FULL : BUTTON_LABELS_LEAN;
411

    
412
    for(int i=0; i<mNumButtons; i++)
413
      {
414
      final int but = i;
415
      Button button = new Button(act);
416
      button.setLayoutParams(p);
417
      button.setText(labels[i]);
418
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
419

    
420
      button.setOnClickListener( new View.OnClickListener()
421
        {
422
        @Override
423
        public void onClick(View v)
424
          {
425
          mMenuPopup.dismiss();
426
          MenuAction(act,but);
427
          }
428
        });
429

    
430
      menuLayout.addView(button);
431
      }
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

    
436
  private void setupPlayWindow(final RubikActivity act, final float width)
437
    {
438
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
439
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
440
    mPlayLayout = layout.findViewById(R.id.playGrid);
441

    
442
    mPlayLayoutWidth = (int)(width*0.4f);
443

    
444
    mPlayPopup = new PopupWindow(act);
445
    mPlayPopup.setContentView(layout);
446
    mPlayPopup.setFocusable(true);
447

    
448
    adjustLevels(act);
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  private void MenuAction(RubikActivity act, int button)
454
    {
455
    switch(button)
456
      {
457
      case 0: Bundle sBundle = new Bundle();
458
              int currObject = RubikObjectList.getCurrObject();
459
              sBundle.putInt("tab", currObject );
460
              sBundle.putBoolean("submitting", false);
461
              RubikDialogScores scores = new RubikDialogScores();
462
              scores.setArguments(sBundle);
463
              scores.show(act.getSupportFragmentManager(), null);
464
              break;
465
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
466
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
467
              break;
468
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
469
              break;
470
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
471
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
472
              break;
473
      case 4: if( mDisplayBandaged )
474
                {
475
                act.switchToBandagedCreator();
476
                break;
477
                }
478
      case 5: RubikDialogAbout aDiag = new RubikDialogAbout();
479
              aDiag.show(act.getSupportFragmentManager(), null);
480
              break;
481
      }
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  void setupSolveButton(final RubikActivity act)
487
    {
488
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
489
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
490
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
491

    
492
    mSolveButton.setOnClickListener( new View.OnClickListener()
493
      {
494
      @Override
495
      public void onClick(View v)
496
        {
497
        act.getControl().solveObject();
498
        mMovesController.clearMoves(act);
499
        }
500
      });
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
  private void setupScrambleButton(final RubikActivity act)
506
    {
507
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble_new,R.drawable.ui_medium_cube_scramble_new, R.drawable.ui_big_cube_scramble_new, R.drawable.ui_huge_cube_scramble_new);
508
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
509
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
510

    
511
    mScrambleButton.setOnClickListener( new View.OnClickListener()
512
      {
513
      @Override
514
      public void onClick(View v)
515
        {
516
        int currObject = RubikObjectList.getCurrObject();
517
        RubikObject object = RubikObjectList.getObject(currObject);
518
        int numScrambles = object==null ? 0 : object.getNumScramble();
519
        mShouldReactToEndOfScrambling = false;
520
        act.getControl().scrambleObject(numScrambles);
521
        }
522
      });
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
527
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
528
// going to be called)
529
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
530

    
531
  public void savePreferences(SharedPreferences.Editor editor)
532
    {
533
    editor.putInt("play_LevelValue", mLevelValue );
534

    
535
    if( mObjectPopup!=null )
536
      {
537
      mObjectPopup.dismiss();
538
      mObjectPopup = null;
539
      }
540

    
541
    if( mMenuPopup!=null )
542
      {
543
      mMenuPopup.dismiss();
544
      mMenuPopup = null;
545
      }
546

    
547
    if( mPlayPopup!=null )
548
      {
549
      mPlayPopup.dismiss();
550
      mPlayPopup = null;
551
      }
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
  public void restorePreferences(SharedPreferences preferences)
557
    {
558
    mLevelValue = preferences.getInt("play_LevelValue", 0);
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
  public void setCurrObject(RubikActivity act)
564
    {
565
    if( mPlayLayout!=null ) adjustLevels(act);
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
570

    
571
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
572
    {
573
    View topLayout = act.findViewById(R.id.relativeLayout);
574
    boolean isFullScreen;
575

    
576
    if( topLayout!=null )
577
      {
578
      topLayout.getLocationOnScreen(mLocation);
579
      isFullScreen = (mLocation[1]==0);
580
      }
581
    else
582
      {
583
      isFullScreen = true;
584
      }
585

    
586
    try
587
      {
588
      // if on Android 11 or we are fullscreen
589
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
590
        {
591
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
592
        window.update(view, w, h);
593
        }
594
      else  // Android 10 or below in pop-up mode or split-screen mode
595
        {
596
        view.getLocationOnScreen(mLocation);
597
        int width  = view.getWidth();
598
        int height = view.getHeight();
599
        int x = mLocation[0]+(width-w)/2;
600
        int y = mLocation[1]+height+yoff;
601

    
602
        window.showAsDropDown(view);
603
        window.update(x,y,w,h);
604
        }
605
      }
606
    catch( IllegalArgumentException iae )
607
      {
608
      // ignore, this means window is 'not attached to window manager' -
609
      // which most probably is because we are already exiting the app.
610
      }
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614

    
615
  private void adjustSolvedIcons()
616
    {
617
    if( mPlayLayout!=null )
618
      {
619
      int currObject = RubikObjectList.getCurrObject();
620
      int dbLevel = RubikObjectList.getDBLevel(currObject);
621
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
622
      RubikScores scores = RubikScores.getInstance();
623

    
624
      for(int i=0; i<numLevel; i++)
625
        {
626
        int level = i<numLevel-1 ? i+1 : dbLevel;
627
        Button button = (Button)mPlayLayout.getChildAt(i);
628
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
629
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
630
        }
631
      }
632
    }
633

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635

    
636
  private void adjustLevels(final RubikActivity act)
637
    {
638
    int currObject = RubikObjectList.getCurrObject();
639
    int dbLevel = RubikObjectList.getDBLevel(currObject);
640
    RubikObject object = RubikObjectList.getObject(currObject);
641
    int numScrambles = object==null ? 0 : object.getNumScramble();
642
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
643
    String[] levels = new String[numLevel];
644

    
645
    for(int i=0; i<numLevel-1; i++)
646
      {
647
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
648
      }
649

    
650
    if( numLevel>0 )
651
      {
652
      levels[numLevel-1] = act.getString(R.string.level_full);
653
      }
654

    
655
    if( mLevelValue>dbLevel || mLevelValue<1 ||
656
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
657
      {
658
      mLevelValue=1;
659
      }
660

    
661
    float width  = act.getScreenWidthInPixels();
662
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
663
    int padding  = (int)(width*RubikActivity.PADDING);
664
    int butWidth = mPlayLayoutWidth - 2*padding;
665
    int butHeight= (int)mMenuItemSize;
666
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
667

    
668
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
669
    pM.setMargins(margin, 0, margin, margin);
670
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
671
    pT.setMargins(margin, margin, margin, margin);
672
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
673
    pB.setMargins(margin, margin, margin, 2*margin);
674

    
675
    mPlayLayout.removeAllViews();
676

    
677
    RubikScores scores = RubikScores.getInstance();
678

    
679
    for(int i=0; i<numLevel; i++)
680
      {
681
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
682
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
683
      Button button = new Button(act);
684
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
685
      button.setText(levels[i]);
686
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
687

    
688
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
689
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
690

    
691
      button.setOnClickListener( new View.OnClickListener()
692
        {
693
        @Override
694
        public void onClick(View v)
695
          {
696
          ObjectControl control = act.getControl();
697

    
698
          if(control.isUINotBlocked())
699
            {
700
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
701
            mLevelValue = level;
702
            mShouldReactToEndOfScrambling = true;
703
            control.scrambleObject(scrambles);
704
            }
705
          }
706
        });
707

    
708
      mPlayLayout.addView(button);
709
      }
710
    }
711

    
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713

    
714
  public int getLevel()
715
    {
716
    return mLevelValue;
717
    }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
  public void recreatePopup()
722
    {
723
    mObjectPopup = null;
724

    
725
    int numObjects = RubikObjectList.getNumObjects();
726
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
727
    mColCount = NUM_COLUMNS;
728
    }
729

    
730
///////////////////////////////////////////////////////////////////////////////////////////////////
731

    
732
  public boolean shouldReactToEndOfScrambling()
733
    {
734
    return mShouldReactToEndOfScrambling;
735
    }
736

    
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738

    
739
  public void receiveUpdate(RubikUpdates updates)
740
    {
741
    Activity act = mWeakAct.get();
742

    
743
    if( act!=null )
744
      {
745
      act.runOnUiThread(new Runnable()
746
        {
747
        @Override
748
        public void run()
749
          {
750
          int num = updates.getCompletedNumber();
751

    
752
          if( num>0 )
753
            {
754
            String shownNum = String.valueOf(num);
755
            mBubbleUpdates.setText(shownNum);
756
            mBubbleUpdates.setVisibility(View.VISIBLE);
757
            int height = (int)(0.05f*mScreenWidth);
758
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
759
            }
760
         else
761
            {
762
            mBubbleUpdates.setVisibility(View.INVISIBLE);
763
            }
764
          }
765
        });
766
      }
767
    }
768

    
769
///////////////////////////////////////////////////////////////////////////////////////////////////
770

    
771
  public void errorUpdate()
772
    {
773
    android.util.Log.e("D", "Screen: Error receiving update");
774
    }
775
  }
(5-5/10)