Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 1f3bc259

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 = { R.string.scores,
70
                                               R.string.patterns,
71
                                               R.string.solver,
72
                                               R.string.tutorials,
73
                                               R.string.about };
74

    
75
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
76
  private static final float LAST_BUTTON = 1.5f;
77
  private static final int[] mLocation = new int[2];
78

    
79
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
80
  private TransparentButton mPlayButton;
81
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
82
  private LinearLayout mPlayLayout;
83
  private TextView mBubbleUpdates;
84
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
85
  private int mLevelValue;
86
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
87
  private int mColCount, mRowCount, mMaxRowCount;
88
  private int mUpperBarHeight;
89
  private boolean mShouldReactToEndOfScrambling;
90
  private int mBottomHeight;
91
  private float mScreenWidth;
92
  private WeakReference<RubikActivity> mWeakAct;
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  void leaveScreen(RubikActivity act)
97
    {
98

    
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  void enterScreen(final RubikActivity act)
104
    {
105
    mWeakAct = new WeakReference<>(act);
106
    int numObjects = RubikObjectList.getNumObjects();
107
    mScreenWidth = act.getScreenWidthInPixels();
108
    mUpperBarHeight = act.getHeightUpperBar();
109

    
110
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
111
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
112
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
113

    
114
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
115
    mColCount = NUM_COLUMNS;
116

    
117
    // TOP ////////////////////////////
118
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
119
    layoutTop.removeAllViews();
120

    
121
    setupObjectButton(act,mScreenWidth);
122
    layoutTop.addView(mObjButton);
123

    
124
    setupMenuButton(act,mScreenWidth);
125
    layoutTop.addView(mMenuButton);
126

    
127
    setupPlayButton(act,mScreenWidth);
128
    layoutTop.addView(mPlayButton);
129

    
130
    setupSolveButton(act);
131
    setupScrambleButton(act);
132
    createBottomPane(act,mSolveButton,mScrambleButton);
133
    }
134

    
135
//////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void setupObjectButton(final RubikActivity act, final float width)
138
    {
139
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
140
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
141
    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);
142
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
143
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
144

    
145
    mObjButton.setOnClickListener( new View.OnClickListener()
146
      {
147
      @Override
148
      public void onClick(View view)
149
        {
150
        if( mObjectPopup==null )
151
          {
152
          float width = act.getScreenWidthInPixels();
153
          float height= act.getScreenHeightInPixels();
154
          setupObjectWindow(act,width,height);
155
          }
156

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void setupPlayButton(final RubikActivity act, final float width)
171
    {
172
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
173

    
174
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
175

    
176
    mPlayButton.setOnClickListener( new View.OnClickListener()
177
      {
178
      @Override
179
      public void onClick(View view)
180
        {
181
         if( mPlayPopup==null )
182
          {
183
          float width = act.getScreenWidthInPixels();
184
          setupPlayWindow(act,width);
185
          }
186

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private void setupMenuButton(final RubikActivity act, final float width)
208
    {
209
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
210
    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);
211
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
212
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
213

    
214
    mMenuButton.setOnClickListener( new View.OnClickListener()
215
      {
216
      @Override
217
      public void onClick(View view)
218
        {
219
        if( mMenuPopup==null )
220
          {
221
          float width = act.getScreenWidthInPixels();
222
          setupMenuWindow(act,width);
223
          }
224

    
225
        if( act.getControl().isUINotBlocked())
226
          {
227
          View popupView = mMenuPopup.getContentView();
228
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
229
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
230
          }
231
        }
232
      });
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
238
    {
239
    int cubeWidth = (int)(width/9);
240
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
241
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
242
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
243

    
244
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
245
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
246

    
247
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
248
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
249

    
250
    objectGrid.setColumnCount(mColCount);
251
    objectGrid.setRowCount(mRowCount);
252

    
253
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
254
    setupBottomLayout(act,bottomLayout);
255

    
256
    mObjectPopup = new PopupWindow(act);
257
    mObjectPopup.setFocusable(true);
258
    mObjectPopup.setContentView(view);
259

    
260
    int[] nextInRow = new int[mRowCount];
261

    
262
    for(int row=0; row<mRowCount; row++)
263
      {
264
      rowSpecs[row] = GridLayout.spec(row);
265
      nextInRow[row]= 0;
266
      }
267
    for(int col=0; col<mColCount; col++)
268
      {
269
      colSpecs[col] = GridLayout.spec(col);
270
      }
271

    
272
    int numObjects = RubikObjectList.getNumObjects();
273

    
274
    for(int object=0; object<numObjects; object++)
275
      {
276
      final int ordinal = object;
277
      final RubikObject rObject = RubikObjectList.getObject(object);
278
      int row = object/NUM_COLUMNS;
279
      ImageButton button = new ImageButton(act);
280
      if( rObject!=null ) rObject.setIconTo(act,button);
281

    
282
      button.setOnClickListener( new View.OnClickListener()
283
        {
284
        @Override
285
        public void onClick(View v)
286
          {
287
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
288
            {
289
            RubikObjectList.setCurrObject(act,ordinal);
290
            act.changeObject(ordinal,true);
291
            if( mPlayLayout!=null ) adjustLevels(act);
292
            mMovesController.clearMoves(act);
293
            }
294

    
295
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
296
          }
297
        });
298

    
299
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
300
      params.bottomMargin = margin;
301
      params.topMargin    = margin;
302
      params.leftMargin   = margin;
303
      params.rightMargin  = margin;
304

    
305
      params.width = cubeWidth;
306
      params.height= cubeWidth;
307

    
308
      nextInRow[row]++;
309

    
310
      objectGrid.addView(button, params);
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

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

    
322
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
323
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
324
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
325

    
326
    buttonTut.setImageResource(iconT);
327
    buttonDow.setImageResource(iconD);
328
    buttonInf.setImageResource(iconI);
329

    
330
    Resources res = act.getResources();
331
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
332
    mBottomHeight = bd.getIntrinsicHeight();
333

    
334
    TypedValue outValue = new TypedValue();
335
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
336
    buttonTut.setBackgroundResource(outValue.resourceId);
337
    buttonDow.setBackgroundResource(outValue.resourceId);
338
    buttonInf.setBackgroundResource(outValue.resourceId);
339

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

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

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

    
373
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
374
    mBubbleUpdates.setVisibility(View.INVISIBLE);
375

    
376
    RubikNetwork network = RubikNetwork.getInstance();
377
    network.signUpForUpdates(this);
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

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

    
388
    mMenuPopup = new PopupWindow(act);
389
    mMenuPopup.setContentView(layout);
390
    mMenuPopup.setFocusable(true);
391
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
392
    int padding = (int)(width*RubikActivity.PADDING);
393

    
394
    mMenuLayoutWidth = (int)(width/2);
395
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
396

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

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

    
407
      button.setOnClickListener( new View.OnClickListener()
408
        {
409
        @Override
410
        public void onClick(View v)
411
          {
412
          mMenuPopup.dismiss();
413
          MenuAction(act,but);
414
          }
415
        });
416

    
417
      menuLayout.addView(button);
418
      }
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

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

    
429
    mPlayLayoutWidth = (int)(width*0.4f);
430

    
431
    mPlayPopup = new PopupWindow(act);
432
    mPlayPopup.setContentView(layout);
433
    mPlayPopup.setFocusable(true);
434

    
435
    adjustLevels(act);
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

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

    
474
    mSolveButton.setOnClickListener( new View.OnClickListener()
475
      {
476
      @Override
477
      public void onClick(View v)
478
        {
479
        act.getControl().solveObject();
480
        mMovesController.clearMoves(act);
481
        }
482
      });
483
    }
484

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

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

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

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

    
513
  public void savePreferences(SharedPreferences.Editor editor)
514
    {
515
    editor.putInt("play_LevelValue", mLevelValue );
516

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

    
523
    if( mMenuPopup!=null )
524
      {
525
      mMenuPopup.dismiss();
526
      mMenuPopup = null;
527
      }
528

    
529
    if( mPlayPopup!=null )
530
      {
531
      mPlayPopup.dismiss();
532
      mPlayPopup = null;
533
      }
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  public void restorePreferences(SharedPreferences preferences)
539
    {
540
    mLevelValue = preferences.getInt("play_LevelValue", 0);
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  public void setCurrObject(RubikActivity act)
546
    {
547
    if( mPlayLayout!=null ) adjustLevels(act);
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
552

    
553
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
554
    {
555
    View topLayout = act.findViewById(R.id.relativeLayout);
556
    boolean isFullScreen;
557

    
558
    if( topLayout!=null )
559
      {
560
      topLayout.getLocationOnScreen(mLocation);
561
      isFullScreen = (mLocation[1]==0);
562
      }
563
    else
564
      {
565
      isFullScreen = true;
566
      }
567

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

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

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

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

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

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

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

    
627
    for(int i=0; i<numLevel-1; i++)
628
      {
629
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
630
      }
631

    
632
    if( numLevel>0 )
633
      {
634
      levels[numLevel-1] = act.getString(R.string.level_full);
635
      }
636

    
637
    if( mLevelValue>dbLevel || mLevelValue<1 ||
638
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
639
      {
640
      mLevelValue=1;
641
      }
642

    
643
    float width  = act.getScreenWidthInPixels();
644
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
645
    int padding  = (int)(width*RubikActivity.PADDING);
646
    int butWidth = mPlayLayoutWidth - 2*padding;
647
    int butHeight= (int)mMenuItemSize;
648
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
649

    
650
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
651
    pM.setMargins(margin, 0, margin, margin);
652
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
653
    pT.setMargins(margin, margin, margin, margin);
654
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
655
    pB.setMargins(margin, margin, margin, 2*margin);
656

    
657
    mPlayLayout.removeAllViews();
658

    
659
    RubikScores scores = RubikScores.getInstance();
660

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

    
670
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
671
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
672

    
673
      button.setOnClickListener( new View.OnClickListener()
674
        {
675
        @Override
676
        public void onClick(View v)
677
          {
678
          ObjectControl control = act.getControl();
679

    
680
          if(control.isUINotBlocked())
681
            {
682
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
683
            mLevelValue = level;
684
            mShouldReactToEndOfScrambling = true;
685
            control.scrambleObject(scrambles);
686
            }
687
          }
688
        });
689

    
690
      mPlayLayout.addView(button);
691
      }
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
  public int getLevel()
697
    {
698
    return mLevelValue;
699
    }
700

    
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702

    
703
  public void recreatePopup()
704
    {
705
    mObjectPopup = null;
706

    
707
    int numObjects = RubikObjectList.getNumObjects();
708
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
709
    mColCount = NUM_COLUMNS;
710
    }
711

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

    
714
  public boolean shouldReactToEndOfScrambling()
715
    {
716
    return mShouldReactToEndOfScrambling;
717
    }
718

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

    
721
  public void receiveUpdate(RubikUpdates updates)
722
    {
723
    Activity act = mWeakAct.get();
724

    
725
    if( act!=null )
726
      {
727
      act.runOnUiThread(new Runnable()
728
        {
729
        @Override
730
        public void run()
731
          {
732
          int num = updates.getCompletedNumber();
733

    
734
          if( num>0 )
735
            {
736
            String shownNum = String.valueOf(num);
737
            mBubbleUpdates.setText(shownNum);
738
            mBubbleUpdates.setVisibility(View.VISIBLE);
739
            int height = (int)(0.05f*mScreenWidth);
740
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
741
            }
742
         else
743
            {
744
            mBubbleUpdates.setVisibility(View.INVISIBLE);
745
            }
746
          }
747
        });
748
      }
749
    }
750

    
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752

    
753
  public void errorUpdate()
754
    {
755
    android.util.Log.e("D", "Screen: Error receiving update");
756
    }
757
  }
(5-5/10)