Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 756f152c

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 = 8;
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.bandaged,
74
                                               R.string.about };
75
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
76

    
77
  private static final float BIG_BUTTON = 1.5f;
78
  private static final int[] mLocation = new int[2];
79

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  void leaveScreen(RubikActivity act)
99
    {
100

    
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
112
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
113
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
114
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
115

    
116
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
117
    mColCount = NUM_COLUMNS;
118

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

    
123
    setupObjectButton(act,mScreenWidth);
124
    layoutTop.addView(mObjButton);
125

    
126
    setupMenuButton(act,mScreenWidth);
127
    layoutTop.addView(mMenuButton);
128

    
129
    setupPlayButton(act,mScreenWidth);
130
    layoutTop.addView(mPlayButton);
131

    
132
    setupSolveButton(act);
133
    setupScrambleButton(act);
134
    createBottomPane(act,mSolveButton,mScrambleButton);
135
    }
136

    
137
//////////////////////////////////////////////////////////////////////////////////////////////////
138

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

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

    
176
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
177

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

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

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

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

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

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

    
249
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
250
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
251

    
252
    objectGrid.setColumnCount(mColCount);
253
    objectGrid.setRowCount(mRowCount);
254

    
255
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
256
    setupBottomLayout(act,bottomLayout);
257

    
258
    mObjectPopup = new PopupWindow(act);
259
    mObjectPopup.setFocusable(true);
260
    mObjectPopup.setContentView(view);
261

    
262
    int[] nextInRow = new int[mRowCount];
263

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

    
274
    int numObjects = RubikObjectList.getNumObjects();
275

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

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

    
297
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
298
          }
299
        });
300

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

    
307
      params.width = cubeSize;
308
      params.height= cubeSize;
309

    
310
      nextInRow[row]++;
311

    
312
      objectGrid.addView(button, params);
313
      }
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

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

    
324
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
325
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
326
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
327

    
328
    buttonTut.setImageResource(iconT);
329
    buttonDow.setImageResource(iconD);
330
    buttonInf.setImageResource(iconI);
331

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

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

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

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

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

    
375
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
376
    mBubbleUpdates.setVisibility(View.INVISIBLE);
377

    
378
    RubikNetwork network = RubikNetwork.getInstance();
379
    network.signUpForUpdates(this);
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

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

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

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

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

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

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

    
419
      menuLayout.addView(button);
420
      }
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

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

    
431
    mPlayLayoutWidth = (int)(width*0.4f);
432

    
433
    mPlayPopup = new PopupWindow(act);
434
    mPlayPopup.setContentView(layout);
435
    mPlayPopup.setFocusable(true);
436

    
437
    adjustLevels(act);
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

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

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

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

    
478
    mSolveButton.setOnClickListener( new View.OnClickListener()
479
      {
480
      @Override
481
      public void onClick(View v)
482
        {
483
        act.getControl().solveObject();
484
        mMovesController.clearMoves(act);
485
        }
486
      });
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

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

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

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

    
517
  public void savePreferences(SharedPreferences.Editor editor)
518
    {
519
    editor.putInt("play_LevelValue", mLevelValue );
520
    editor.putString("play_objectsPlayed", mObjectsPlayed );
521

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

    
528
    if( mMenuPopup!=null )
529
      {
530
      mMenuPopup.dismiss();
531
      mMenuPopup = null;
532
      }
533

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

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

    
543
  private void addToPlayedObjects()
544
    {
545
    String name = RubikObjectList.getCurrentName();
546
    if( !mObjectsPlayed.contains(name) ) mObjectsPlayed += (" "+name);
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
  private boolean hasBeenPlayed()
552
    {
553
    String name = RubikObjectList.getCurrentName();
554
    return mObjectsPlayed.contains(name);
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  public void restorePreferences(SharedPreferences preferences)
560
    {
561
    mLevelValue    = preferences.getInt("play_LevelValue", 0);
562
    mObjectsPlayed = preferences.getString("play_objectsPlayed", "");
563
    }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
  public void setCurrObject(RubikActivity act)
568
    {
569
    if( mPlayLayout!=null ) adjustLevels(act);
570
    }
571

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
574

    
575
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
576
    {
577
    View topLayout = act.findViewById(R.id.relativeLayout);
578
    boolean isFullScreen;
579

    
580
    if( topLayout!=null )
581
      {
582
      topLayout.getLocationOnScreen(mLocation);
583
      isFullScreen = (mLocation[1]==0);
584
      }
585
    else
586
      {
587
      isFullScreen = true;
588
      }
589

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

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

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618

    
619
  private void adjustSolvedIcons()
620
    {
621
    if( mPlayLayout!=null )
622
      {
623
      int currObject = RubikObjectList.getCurrObject();
624
      int dbLevel = RubikObjectList.getDBLevel(currObject);
625
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN)+1;
626
      RubikScores scores = RubikScores.getInstance();
627

    
628
      for(int i=0; i<numLevel; i++)
629
        {
630
        int level = i<numLevel-1 ? i : dbLevel;
631
        Button button = (Button)mPlayLayout.getChildAt(i);
632
        boolean isSolved = i>0 ? scores.isSolved(currObject, level-1) : hasBeenPlayed();
633
        int icon = isSolved ? R.drawable.ui_solved : R.drawable.ui_notsolved;
634
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
635
        }
636
      }
637
    }
638

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640

    
641
  private void adjustLevels(final RubikActivity act)
642
    {
643
    int currObject = RubikObjectList.getCurrObject();
644
    int dbLevel = RubikObjectList.getDBLevel(currObject);
645
    RubikObject object = RubikObjectList.getObject(currObject);
646
    int numScrambles = object==null ? 0 : object.getNumScramble();
647
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN)+1;
648
    String[] levels = new String[numLevel];
649

    
650
    levels[0] = act.getString(R.string.free_play);
651
    for(int i=1; i<numLevel-1; i++) levels[i] = act.getString(R.string.lv_placeholder,i);
652
    levels[numLevel-1] = act.getString(R.string.level_full);
653

    
654
    if( mLevelValue>dbLevel || mLevelValue<1 ||
655
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) ) mLevelValue=1;
656

    
657
    float width  = act.getScreenWidthInPixels();
658
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
659
    int padding  = (int)(width*RubikActivity.PADDING);
660
    int butWidth = mPlayLayoutWidth - 2*padding;
661
    int butHeight= (int)mMenuItemSize;
662
    int bigButH  = (int)(mMenuItemSize*BIG_BUTTON) ;
663

    
664
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
665
    pM.setMargins(margin, 0, margin, margin);
666
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, bigButH   );
667
    pB.setMargins(margin, margin, margin, 2*margin);
668

    
669
    mPlayLayout.removeAllViews();
670

    
671
    for(int i=0; i<numLevel; i++)
672
      {
673
      final int level     = i<numLevel-1 ? i : dbLevel;
674
      final int scrambles = i<numLevel-1 ? i : numScrambles;
675
      Button button = new Button(act);
676
      button.setLayoutParams(i==0 ? pB : (i==numLevel-1 ? pB : pM));
677
      button.setText(levels[i]);
678
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
679

    
680
      button.setOnClickListener( new View.OnClickListener()
681
        {
682
        @Override
683
        public void onClick(View v)
684
          {
685
          ObjectControl control = act.getControl();
686

    
687
          if(control.isUINotBlocked())
688
            {
689
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
690

    
691
            if( level>0 )
692
              {
693
              mLevelValue = level;
694
              mShouldReactToEndOfScrambling = true;
695
              control.scrambleObject(scrambles);
696
              }
697
            else
698
              {
699
              addToPlayedObjects();
700
              mShouldReactToEndOfScrambling = false;
701
              ScreenList.switchScreen(act, ScreenList.FREE);
702
              }
703
            }
704
          }
705
        });
706

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

    
711
///////////////////////////////////////////////////////////////////////////////////////////////////
712

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

    
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719

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

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

    
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730

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

    
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737

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

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

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

    
768
///////////////////////////////////////////////////////////////////////////////////////////////////
769

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