Project

General

Profile

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

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

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 android.content.Context;
23
import android.content.SharedPreferences;
24
import android.content.res.Resources;
25
import android.graphics.drawable.BitmapDrawable;
26
import android.os.Build;
27
import android.os.Bundle;
28
import android.util.TypedValue;
29
import android.view.Gravity;
30
import android.view.LayoutInflater;
31
import android.view.View;
32
import android.widget.Button;
33
import android.widget.GridLayout;
34
import android.widget.ImageButton;
35
import android.widget.LinearLayout;
36
import android.widget.PopupWindow;
37
import android.widget.RelativeLayout;
38
import android.widget.TextView;
39

    
40
import org.distorted.dialogs.RubikDialogUpdates;
41
import org.distorted.network.RubikNetwork;
42
import org.distorted.network.RubikUpdates;
43
import org.distorted.objectlib.main.ObjectControl;
44

    
45
import org.distorted.main.R;
46
import org.distorted.main.RubikActivity;
47
import org.distorted.dialogs.RubikDialogAbout;
48
import org.distorted.dialogs.RubikDialogPattern;
49
import org.distorted.dialogs.RubikDialogScores;
50
import org.distorted.dialogs.RubikDialogTutorial;
51
import org.distorted.helpers.TransparentButton;
52
import org.distorted.helpers.TransparentImageButton;
53
import org.distorted.network.RubikScores;
54
import org.distorted.objects.RubikObject;
55
import org.distorted.objects.RubikObjectList;
56

    
57
import static android.view.View.inflate;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
62
  {
63
  public static final int NUM_COLUMNS  = 5;
64
  public static final int LEVELS_SHOWN = 10;
65

    
66
  private static final int[] BUTTON_LABELS = { R.string.scores,
67
                                               R.string.patterns,
68
                                               R.string.solver,
69
                                               R.string.tutorials,
70
                                               R.string.about };
71

    
72
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
73
  private static final float LAST_BUTTON = 1.5f;
74
  private static final int[] mLocation = new int[2];
75

    
76
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
77
  private TransparentButton mPlayButton;
78
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
79
  private LinearLayout mPlayLayout;
80
  private TextView mBubbleUpdates;
81
  private RubikUpdates mUpdates;
82
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
83
  private int mLevelValue;
84
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
85
  private int mColCount, mRowCount, mMaxRowCount;
86
  private int mUpperBarHeight;
87
  private boolean mShouldReactToEndOfScrambling;
88
  private int mBottomHeight;
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  void leaveScreen(RubikActivity act)
93
    {
94

    
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  void enterScreen(final RubikActivity act)
100
    {
101
    int numObjects = RubikObjectList.getNumObjects();
102
    float width = act.getScreenWidthInPixels();
103
    mUpperBarHeight = act.getHeightUpperBar();
104

    
105
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
106
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
107
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
108

    
109
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
110
    mColCount = NUM_COLUMNS;
111

    
112
    // TOP ////////////////////////////
113
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
114
    layoutTop.removeAllViews();
115

    
116
    setupObjectButton(act,width);
117
    layoutTop.addView(mObjButton);
118

    
119
    setupMenuButton(act,width);
120
    layoutTop.addView(mMenuButton);
121

    
122
    setupPlayButton(act,width);
123
    layoutTop.addView(mPlayButton);
124

    
125
    setupSolveButton(act);
126
    setupScrambleButton(act);
127
    createBottomPane(act,mSolveButton,mScrambleButton);
128
    }
129

    
130
//////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private void setupObjectButton(final RubikActivity act, final float width)
133
    {
134
    final int margin  = (int)(width*RubikActivity.MARGIN);
135
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
136
    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);
137
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
138
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
139

    
140
    mObjButton.setOnClickListener( new View.OnClickListener()
141
      {
142
      @Override
143
      public void onClick(View view)
144
        {
145
        if( mObjectPopup==null )
146
          {
147
          float width = act.getScreenWidthInPixels();
148
          float height= act.getScreenHeightInPixels();
149
          setupObjectWindow(act,width,height);
150
          }
151

    
152
        if( act.getControl().isUINotBlocked())
153
          {
154
          int rowCount = Math.min(mMaxRowCount,mRowCount);
155
          View popupView = mObjectPopup.getContentView();
156
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
157
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
158
          }
159
        }
160
      });
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private void setupPlayButton(final RubikActivity act, final float width)
166
    {
167
    final int margin = (int)(width*RubikActivity.MARGIN);
168

    
169
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
170

    
171
    mPlayButton.setOnClickListener( new View.OnClickListener()
172
      {
173
      @Override
174
      public void onClick(View view)
175
        {
176
         if( mPlayPopup==null )
177
          {
178
          float width = act.getScreenWidthInPixels();
179
          setupPlayWindow(act,width);
180
          }
181

    
182
        if( act.getControl().isUINotBlocked())
183
          {
184
          adjustSolvedIcons();
185
          float height= act.getScreenHeightInPixels();
186
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
187
          View popupView = mPlayPopup.getContentView();
188
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
189
          final int object  = RubikObjectList.getCurrObject();
190
          final int dbLevel = RubikObjectList.getDBLevel(object);
191
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
192
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
193
          final int realHeight = Math.min(popupHeight,maxHeight);
194
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
195
          }
196
        }
197
      });
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void setupMenuButton(final RubikActivity act, final float width)
203
    {
204
    final int margin = (int)(width*RubikActivity.MARGIN);
205
    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);
206
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
207
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
208

    
209
    mMenuButton.setOnClickListener( new View.OnClickListener()
210
      {
211
      @Override
212
      public void onClick(View view)
213
        {
214
        if( mMenuPopup==null )
215
          {
216
          float width = act.getScreenWidthInPixels();
217
          setupMenuWindow(act,width);
218
          }
219

    
220
        if( act.getControl().isUINotBlocked())
221
          {
222
          View popupView = mMenuPopup.getContentView();
223
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
224
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
225
          }
226
        }
227
      });
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
233
    {
234
    int cubeWidth = (int)(width/9);
235
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
236
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
237
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
238

    
239
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
240
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
241

    
242
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
243
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
244

    
245
    objectGrid.setColumnCount(mColCount);
246
    objectGrid.setRowCount(mRowCount);
247

    
248
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
249
    setupBottomLayout(act,bottomLayout);
250

    
251
    mObjectPopup = new PopupWindow(act);
252
    mObjectPopup.setFocusable(true);
253
    mObjectPopup.setContentView(view);
254

    
255
    int[] nextInRow = new int[mRowCount];
256

    
257
    for(int row=0; row<mRowCount; row++)
258
      {
259
      rowSpecs[row] = GridLayout.spec(row);
260
      nextInRow[row]= 0;
261
      }
262
    for(int col=0; col<mColCount; col++)
263
      {
264
      colSpecs[col] = GridLayout.spec(col);
265
      }
266

    
267
    int numObjects = RubikObjectList.getNumObjects();
268

    
269
    for(int object=0; object<numObjects; object++)
270
      {
271
      final RubikObject robject = RubikObjectList.getObject(object);
272
      int icons = robject==null ? 0 : robject.getIconID();
273
      int row = object/NUM_COLUMNS;
274
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
275

    
276
      ImageButton button = new ImageButton(act);
277
      button.setBackgroundResource(icons);
278
      button.setOnClickListener( new View.OnClickListener()
279
        {
280
        @Override
281
        public void onClick(View v)
282
          {
283
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
284
            {
285
            RubikObjectList.setCurrObject(act,ordinal);
286
            act.changeObject(ordinal,true);
287
            if( mPlayLayout!=null ) adjustLevels(act);
288
            mMovesController.clearMoves(act);
289
            }
290

    
291
          mObjectPopup.dismiss();
292
          }
293
        });
294

    
295
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
296
      params.bottomMargin = margin;
297
      params.topMargin    = margin;
298
      params.leftMargin   = margin;
299
      params.rightMargin  = margin;
300

    
301
      params.width = cubeWidth;
302
      params.height= cubeWidth;
303

    
304
      nextInRow[row]++;
305

    
306
      objectGrid.addView(button, params);
307
      }
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
313
    {
314
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
315
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
316
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
317

    
318
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
319
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
320
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
321

    
322
    buttonTut.setImageResource(iconT);
323
    buttonDow.setImageResource(iconD);
324
    buttonInf.setImageResource(iconI);
325

    
326
    Resources res = act.getResources();
327
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
328
    mBottomHeight = bd.getIntrinsicHeight();
329

    
330
    TypedValue outValue = new TypedValue();
331
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
332
    buttonTut.setBackgroundResource(outValue.resourceId);
333
    buttonDow.setBackgroundResource(outValue.resourceId);
334
    buttonInf.setBackgroundResource(outValue.resourceId);
335

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

    
347
    buttonDow.setOnClickListener( new View.OnClickListener()
348
      {
349
      @Override
350
      public void onClick(View v)
351
        {
352
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
353
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
354
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
355
        }
356
      });
357

    
358
    buttonInf.setOnClickListener( new View.OnClickListener()
359
      {
360
      @Override
361
      public void onClick(View v)
362
        {
363
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
364
        int currObject = RubikObjectList.getCurrObject();
365
        act.switchConfig(currObject);
366
        }
367
      });
368

    
369
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
370
    mBubbleUpdates.setVisibility(View.INVISIBLE);
371

    
372
    RubikNetwork network = RubikNetwork.getInstance();
373
    network.signUpForUpdates(this);
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
  private void setupMenuWindow(final RubikActivity act, final float width)
379
    {
380
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
381
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
382
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
383

    
384
    mMenuPopup = new PopupWindow(act);
385
    mMenuPopup.setContentView(layout);
386
    mMenuPopup.setFocusable(true);
387
    int margin  = (int)(width*RubikActivity.MARGIN);
388
    int padding = (int)(width*RubikActivity.PADDING);
389

    
390
    mMenuLayoutWidth = (int)(width/2);
391
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
392

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

    
395
    for(int i=0; i<NUM_BUTTONS; i++)
396
      {
397
      final int but = i;
398
      Button button = new Button(act);
399
      button.setLayoutParams(p);
400
      button.setText(BUTTON_LABELS[i]);
401
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
402

    
403
      button.setOnClickListener( new View.OnClickListener()
404
        {
405
        @Override
406
        public void onClick(View v)
407
          {
408
          mMenuPopup.dismiss();
409
          MenuAction(act,but);
410
          }
411
        });
412

    
413
      menuLayout.addView(button);
414
      }
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  private void setupPlayWindow(final RubikActivity act, final float width)
420
    {
421
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
422
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
423
    mPlayLayout = layout.findViewById(R.id.playGrid);
424

    
425
    mPlayLayoutWidth = (int)(width*0.4f);
426

    
427
    mPlayPopup = new PopupWindow(act);
428
    mPlayPopup.setContentView(layout);
429
    mPlayPopup.setFocusable(true);
430

    
431
    adjustLevels(act);
432
    }
433

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

    
436
  private void MenuAction(RubikActivity act, int button)
437
    {
438
    switch(button)
439
      {
440
      case 0: Bundle sBundle = new Bundle();
441
              int currObject = RubikObjectList.getCurrObject();
442
              sBundle.putInt("tab", currObject );
443
              sBundle.putBoolean("submitting", false);
444
              RubikDialogScores scores = new RubikDialogScores();
445
              scores.setArguments(sBundle);
446
              scores.show(act.getSupportFragmentManager(), null);
447
              break;
448
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
449
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
450
              break;
451
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
452
              break;
453
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
454
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
455
              break;
456
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
457
              aDiag.show(act.getSupportFragmentManager(), null);
458
              break;
459
      }
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  void setupSolveButton(final RubikActivity act)
465
    {
466
    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);
467
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
468
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
469

    
470
    mSolveButton.setOnClickListener( new View.OnClickListener()
471
      {
472
      @Override
473
      public void onClick(View v)
474
        {
475
        act.getControl().solveObject();
476
        mMovesController.clearMoves(act);
477
        }
478
      });
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  private void setupScrambleButton(final RubikActivity act)
484
    {
485
    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);
486
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
487
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
488

    
489
    mScrambleButton.setOnClickListener( new View.OnClickListener()
490
      {
491
      @Override
492
      public void onClick(View v)
493
        {
494
        int currObject = RubikObjectList.getCurrObject();
495
        RubikObject object = RubikObjectList.getObject(currObject);
496
        int numScrambles = object==null ? 0 : object.getNumScramble();
497
        mShouldReactToEndOfScrambling = false;
498
        act.getControl().scrambleObject(numScrambles);
499
        }
500
      });
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
505
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
506
// going to be called)
507
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
508

    
509
  public void savePreferences(SharedPreferences.Editor editor)
510
    {
511
    editor.putInt("play_LevelValue", mLevelValue );
512

    
513
    if( mObjectPopup!=null )
514
      {
515
      mObjectPopup.dismiss();
516
      mObjectPopup = null;
517
      }
518

    
519
    if( mMenuPopup!=null )
520
      {
521
      mMenuPopup.dismiss();
522
      mMenuPopup = null;
523
      }
524

    
525
    if( mPlayPopup!=null )
526
      {
527
      mPlayPopup.dismiss();
528
      mPlayPopup = null;
529
      }
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

    
534
  public void restorePreferences(SharedPreferences preferences)
535
    {
536
    mLevelValue = preferences.getInt("play_LevelValue", 0);
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
  public void setCurrObject(RubikActivity act)
542
    {
543
    if( mPlayLayout!=null ) adjustLevels(act);
544
    }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
548

    
549
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
550
    {
551
    View topLayout = act.findViewById(R.id.relativeLayout);
552
    boolean isFullScreen;
553

    
554
    if( topLayout!=null )
555
      {
556
      topLayout.getLocationOnScreen(mLocation);
557
      isFullScreen = (mLocation[1]==0);
558
      }
559
    else
560
      {
561
      isFullScreen = true;
562
      }
563

    
564
    try
565
      {
566
      // if on Android 11 or we are fullscreen
567
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
568
        {
569
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
570
        window.update(view, w, h);
571
        }
572
      else  // Android 10 or below in pop-up mode or split-screen mode
573
        {
574
        view.getLocationOnScreen(mLocation);
575
        int width  = view.getWidth();
576
        int height = view.getHeight();
577
        int x = mLocation[0]+(width-w)/2;
578
        int y = mLocation[1]+height+yoff;
579

    
580
        window.showAsDropDown(view);
581
        window.update(x,y,w,h);
582
        }
583
      }
584
    catch( IllegalArgumentException iae )
585
      {
586
      // ignore, this means window is 'not attached to window manager' -
587
      // which most probably is because we are already exiting the app.
588
      }
589
    }
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592

    
593
  private void adjustSolvedIcons()
594
    {
595
    if( mPlayLayout!=null )
596
      {
597
      int currObject = RubikObjectList.getCurrObject();
598
      int dbLevel = RubikObjectList.getDBLevel(currObject);
599
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
600
      RubikScores scores = RubikScores.getInstance();
601

    
602
      for(int i=0; i<numLevel; i++)
603
        {
604
        int level = i<numLevel-1 ? i+1 : dbLevel;
605
        Button button = (Button)mPlayLayout.getChildAt(i);
606
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
607
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
608
        }
609
      }
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613

    
614
  private void adjustLevels(final RubikActivity act)
615
    {
616
    int currObject = RubikObjectList.getCurrObject();
617
    int dbLevel = RubikObjectList.getDBLevel(currObject);
618
    RubikObject object = RubikObjectList.getObject(currObject);
619
    int numScrambles = object==null ? 0 : object.getNumScramble();
620
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
621
    String[] levels = new String[numLevel];
622

    
623
    for(int i=0; i<numLevel-1; i++)
624
      {
625
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
626
      }
627

    
628
    if( numLevel>0 )
629
      {
630
      levels[numLevel-1] = act.getString(R.string.level_full);
631
      }
632

    
633
    if( mLevelValue>dbLevel || mLevelValue<1 ||
634
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
635
      {
636
      mLevelValue=1;
637
      }
638

    
639
    float width  = act.getScreenWidthInPixels();
640
    int margin   = (int)(width*RubikActivity.MARGIN);
641
    int padding  = (int)(width*RubikActivity.PADDING);
642
    int butWidth = mPlayLayoutWidth - 2*padding;
643
    int butHeight= (int)mMenuItemSize;
644
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
645

    
646
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
647
    pM.setMargins(margin, 0, margin, margin);
648
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
649
    pT.setMargins(margin, margin, margin, margin);
650
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
651
    pB.setMargins(margin, margin, margin, 2*margin);
652

    
653
    mPlayLayout.removeAllViews();
654

    
655
    RubikScores scores = RubikScores.getInstance();
656

    
657
    for(int i=0; i<numLevel; i++)
658
      {
659
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
660
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
661
      Button button = new Button(act);
662
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
663
      button.setText(levels[i]);
664
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
665

    
666
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
667
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
668

    
669
      button.setOnClickListener( new View.OnClickListener()
670
        {
671
        @Override
672
        public void onClick(View v)
673
          {
674
          ObjectControl control = act.getControl();
675

    
676
          if(control.isUINotBlocked())
677
            {
678
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
679
            mLevelValue = level;
680
            mShouldReactToEndOfScrambling = true;
681
            control.scrambleObject(scrambles);
682
            }
683
          }
684
        });
685

    
686
      mPlayLayout.addView(button);
687
      }
688
    }
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691

    
692
  public int getLevel()
693
    {
694
    return mLevelValue;
695
    }
696

    
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698

    
699
  public boolean shouldReactToEndOfScrambling()
700
    {
701
    return mShouldReactToEndOfScrambling;
702
    }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705

    
706
  public void receiveUpdate(RubikUpdates updates)
707
    {
708
    updates.showDebug();
709

    
710
    mUpdates = updates;
711
    int num = mUpdates.getNumberOfUpdates();
712

    
713
    if( num>0 )
714
      {
715
      String shownNum = String.valueOf(num);
716
      mBubbleUpdates.setText(shownNum);
717
      mBubbleUpdates.setVisibility(View.VISIBLE);
718
      }
719
    }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722

    
723
  public void errorUpdate()
724
    {
725
    android.util.Log.e("D", "Screen: Error receiving update");
726
    }
727
  }
(5-5/10)