Project

General

Profile

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

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

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

    
39
import org.distorted.network.RubikNetwork;
40
import org.distorted.network.RubikUpdates;
41
import org.distorted.objectlib.main.ObjectControl;
42

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

    
55
import static android.view.View.inflate;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

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

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

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

    
88
  void leaveScreen(RubikActivity act)
89
    {
90

    
91
    }
92

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

    
95
  void enterScreen(final RubikActivity act)
96
    {
97
    int numObjects = RubikObjectList.getNumObjects();
98
    float width = act.getScreenWidthInPixels();
99
    mUpperBarHeight = act.getHeightUpperBar();
100

    
101
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
102
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
103
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
104

    
105
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
106
    mColCount = NUM_COLUMNS;
107

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

    
112
    setupObjectButton(act,width);
113
    layoutTop.addView(mObjButton);
114

    
115
    setupMenuButton(act,width);
116
    layoutTop.addView(mMenuButton);
117

    
118
    setupPlayButton(act,width);
119
    layoutTop.addView(mPlayButton);
120

    
121
    setupSolveButton(act);
122
    setupScrambleButton(act);
123
    createBottomPane(act,mSolveButton,mScrambleButton);
124
    }
125

    
126
//////////////////////////////////////////////////////////////////////////////////////////////////
127

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

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  private void setupPlayButton(final RubikActivity act, final float width)
162
    {
163
    final int margin = (int)(width*RubikActivity.MARGIN);
164

    
165
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
166

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

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

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

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

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

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

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

    
235
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
236
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
237

    
238
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
239
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
240

    
241
    objectGrid.setColumnCount(mColCount);
242
    objectGrid.setRowCount(mRowCount);
243

    
244
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
245
    setupBottomLayout(act,bottomLayout);
246

    
247
    mObjectPopup = new PopupWindow(act);
248
    mObjectPopup.setFocusable(true);
249
    mObjectPopup.setContentView(view);
250

    
251
    int[] nextInRow = new int[mRowCount];
252

    
253
    for(int row=0; row<mRowCount; row++)
254
      {
255
      rowSpecs[row] = GridLayout.spec(row);
256
      nextInRow[row]= 0;
257
      }
258
    for(int col=0; col<mColCount; col++)
259
      {
260
      colSpecs[col] = GridLayout.spec(col);
261
      }
262

    
263
    int numObjects = RubikObjectList.getNumObjects();
264

    
265
    for(int object=0; object<numObjects; object++)
266
      {
267
      final RubikObject robject = RubikObjectList.getObject(object);
268
      int icons = robject==null ? 0 : robject.getIconID();
269
      int row = object/NUM_COLUMNS;
270
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
271

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

    
287
          mObjectPopup.dismiss();
288
          }
289
        });
290

    
291
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
292
      params.bottomMargin = margin;
293
      params.topMargin    = margin;
294
      params.leftMargin   = margin;
295
      params.rightMargin  = margin;
296

    
297
      params.width = cubeWidth;
298
      params.height= cubeWidth;
299

    
300
      nextInRow[row]++;
301

    
302
      objectGrid.addView(button, params);
303
      }
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

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

    
314
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
315
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
316
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
317

    
318
    buttonTut.setImageResource(iconT);
319
    buttonDow.setImageResource(iconD);
320
    buttonInf.setImageResource(iconI);
321

    
322
    Resources res = act.getResources();
323
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
324
    mBottomHeight = bd.getIntrinsicHeight();
325

    
326
    TypedValue outValue = new TypedValue();
327
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
328
    buttonTut.setBackgroundResource(outValue.resourceId);
329
    buttonDow.setBackgroundResource(outValue.resourceId);
330
    buttonInf.setBackgroundResource(outValue.resourceId);
331

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

    
343
    buttonDow.setOnClickListener( new View.OnClickListener()
344
      {
345
      @Override
346
      public void onClick(View v)
347
        {
348
        android.util.Log.e("D", "Download!!");
349
        }
350
      });
351

    
352
    buttonInf.setOnClickListener( new View.OnClickListener()
353
      {
354
      @Override
355
      public void onClick(View v)
356
        {
357
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
358
        int currObject = RubikObjectList.getCurrObject();
359
        act.switchConfig(currObject);
360
        }
361
      });
362

    
363
    RubikNetwork network = RubikNetwork.getInstance();
364
    network.signUpForUpdates(this);
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  private void setupMenuWindow(final RubikActivity act, final float width)
370
    {
371
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
372
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
373
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
374

    
375
    mMenuPopup = new PopupWindow(act);
376
    mMenuPopup.setContentView(layout);
377
    mMenuPopup.setFocusable(true);
378
    int margin  = (int)(width*RubikActivity.MARGIN);
379
    int padding = (int)(width*RubikActivity.PADDING);
380

    
381
    mMenuLayoutWidth = (int)(width/2);
382
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
383

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

    
386
    for(int i=0; i<NUM_BUTTONS; i++)
387
      {
388
      final int but = i;
389
      Button button = new Button(act);
390
      button.setLayoutParams(p);
391
      button.setText(BUTTON_LABELS[i]);
392
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
393

    
394
      button.setOnClickListener( new View.OnClickListener()
395
        {
396
        @Override
397
        public void onClick(View v)
398
          {
399
          mMenuPopup.dismiss();
400
          MenuAction(act,but);
401
          }
402
        });
403

    
404
      menuLayout.addView(button);
405
      }
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  private void setupPlayWindow(final RubikActivity act, final float width)
411
    {
412
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
413
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
414
    mPlayLayout = layout.findViewById(R.id.playGrid);
415

    
416
    mPlayLayoutWidth = (int)(width*0.4f);
417

    
418
    mPlayPopup = new PopupWindow(act);
419
    mPlayPopup.setContentView(layout);
420
    mPlayPopup.setFocusable(true);
421

    
422
    adjustLevels(act);
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  private void MenuAction(RubikActivity act, int button)
428
    {
429
    switch(button)
430
      {
431
      case 0: Bundle sBundle = new Bundle();
432
              int currObject = RubikObjectList.getCurrObject();
433
              sBundle.putInt("tab", currObject );
434
              sBundle.putBoolean("submitting", false);
435
              RubikDialogScores scores = new RubikDialogScores();
436
              scores.setArguments(sBundle);
437
              scores.show(act.getSupportFragmentManager(), null);
438
              break;
439
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
440
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
441
              break;
442
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
443
              break;
444
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
445
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
446
              break;
447
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
448
              aDiag.show(act.getSupportFragmentManager(), null);
449
              break;
450
      }
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  void setupSolveButton(final RubikActivity act)
456
    {
457
    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);
458
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
459
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
460

    
461
    mSolveButton.setOnClickListener( new View.OnClickListener()
462
      {
463
      @Override
464
      public void onClick(View v)
465
        {
466
        act.getControl().solveObject();
467
        mMovesController.clearMoves(act);
468
        }
469
      });
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  private void setupScrambleButton(final RubikActivity act)
475
    {
476
    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);
477
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
478
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
479

    
480
    mScrambleButton.setOnClickListener( new View.OnClickListener()
481
      {
482
      @Override
483
      public void onClick(View v)
484
        {
485
        int currObject = RubikObjectList.getCurrObject();
486
        RubikObject object = RubikObjectList.getObject(currObject);
487
        int numScrambles = object==null ? 0 : object.getNumScramble();
488
        mShouldReactToEndOfScrambling = false;
489
        act.getControl().scrambleObject(numScrambles);
490
        }
491
      });
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
496
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
497
// going to be called)
498
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
499

    
500
  public void savePreferences(SharedPreferences.Editor editor)
501
    {
502
    editor.putInt("play_LevelValue", mLevelValue );
503

    
504
    if( mObjectPopup!=null )
505
      {
506
      mObjectPopup.dismiss();
507
      mObjectPopup = null;
508
      }
509

    
510
    if( mMenuPopup!=null )
511
      {
512
      mMenuPopup.dismiss();
513
      mMenuPopup = null;
514
      }
515

    
516
    if( mPlayPopup!=null )
517
      {
518
      mPlayPopup.dismiss();
519
      mPlayPopup = null;
520
      }
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  public void restorePreferences(SharedPreferences preferences)
526
    {
527
    mLevelValue = preferences.getInt("play_LevelValue", 0);
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
  public void setCurrObject(RubikActivity act)
533
    {
534
    if( mPlayLayout!=null ) adjustLevels(act);
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
539

    
540
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
541
    {
542
    View topLayout = act.findViewById(R.id.relativeLayout);
543
    boolean isFullScreen;
544

    
545
    if( topLayout!=null )
546
      {
547
      topLayout.getLocationOnScreen(mLocation);
548
      isFullScreen = (mLocation[1]==0);
549
      }
550
    else
551
      {
552
      isFullScreen = true;
553
      }
554

    
555
    try
556
      {
557
      // if on Android 11 or we are fullscreen
558
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
559
        {
560
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
561
        window.update(view, w, h);
562
        }
563
      else  // Android 10 or below in pop-up mode or split-screen mode
564
        {
565
        view.getLocationOnScreen(mLocation);
566
        int width  = view.getWidth();
567
        int height = view.getHeight();
568
        int x = mLocation[0]+(width-w)/2;
569
        int y = mLocation[1]+height+yoff;
570

    
571
        window.showAsDropDown(view);
572
        window.update(x,y,w,h);
573
        }
574
      }
575
    catch( IllegalArgumentException iae )
576
      {
577
      // ignore, this means window is 'not attached to window manager' -
578
      // which most probably is because we are already exiting the app.
579
      }
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  private void adjustSolvedIcons()
585
    {
586
    if( mPlayLayout!=null )
587
      {
588
      int currObject = RubikObjectList.getCurrObject();
589
      int dbLevel = RubikObjectList.getDBLevel(currObject);
590
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
591
      RubikScores scores = RubikScores.getInstance();
592

    
593
      for(int i=0; i<numLevel; i++)
594
        {
595
        int level = i<numLevel-1 ? i+1 : dbLevel;
596
        Button button = (Button)mPlayLayout.getChildAt(i);
597
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
598
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
599
        }
600
      }
601
    }
602

    
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604

    
605
  private void adjustLevels(final RubikActivity act)
606
    {
607
    int currObject = RubikObjectList.getCurrObject();
608
    int dbLevel = RubikObjectList.getDBLevel(currObject);
609
    RubikObject object = RubikObjectList.getObject(currObject);
610
    int numScrambles = object==null ? 0 : object.getNumScramble();
611
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
612
    String[] levels = new String[numLevel];
613

    
614
    for(int i=0; i<numLevel-1; i++)
615
      {
616
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
617
      }
618

    
619
    if( numLevel>0 )
620
      {
621
      levels[numLevel-1] = act.getString(R.string.level_full);
622
      }
623

    
624
    if( mLevelValue>dbLevel || mLevelValue<1 ||
625
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
626
      {
627
      mLevelValue=1;
628
      }
629

    
630
    float width  = act.getScreenWidthInPixels();
631
    int margin   = (int)(width*RubikActivity.MARGIN);
632
    int padding  = (int)(width*RubikActivity.PADDING);
633
    int butWidth = mPlayLayoutWidth - 2*padding;
634
    int butHeight= (int)mMenuItemSize;
635
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
636

    
637
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
638
    pM.setMargins(margin, 0, margin, margin);
639
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
640
    pT.setMargins(margin, margin, margin, margin);
641
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
642
    pB.setMargins(margin, margin, margin, 2*margin);
643

    
644
    mPlayLayout.removeAllViews();
645

    
646
    RubikScores scores = RubikScores.getInstance();
647

    
648
    for(int i=0; i<numLevel; i++)
649
      {
650
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
651
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
652
      Button button = new Button(act);
653
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
654
      button.setText(levels[i]);
655
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
656

    
657
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
658
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
659

    
660
      button.setOnClickListener( new View.OnClickListener()
661
        {
662
        @Override
663
        public void onClick(View v)
664
          {
665
          ObjectControl control = act.getControl();
666

    
667
          if(control.isUINotBlocked())
668
            {
669
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
670
            mLevelValue = level;
671
            mShouldReactToEndOfScrambling = true;
672
            control.scrambleObject(scrambles);
673
            }
674
          }
675
        });
676

    
677
      mPlayLayout.addView(button);
678
      }
679
    }
680

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

    
683
  public int getLevel()
684
    {
685
    return mLevelValue;
686
    }
687

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689

    
690
  public boolean shouldReactToEndOfScrambling()
691
    {
692
    return mShouldReactToEndOfScrambling;
693
    }
694

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

    
697
  public void receiveUpdate(RubikUpdates updates)
698
    {
699
    updates.showDebug();
700
    }
701

    
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703

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