Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 7b2a8ef3

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.graphics.drawable.BitmapDrawable;
25
import android.os.Build;
26
import android.os.Bundle;
27
import android.util.TypedValue;
28
import android.view.Gravity;
29
import android.view.LayoutInflater;
30
import android.view.View;
31
import android.widget.Button;
32
import android.widget.GridLayout;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35
import android.widget.PopupWindow;
36
import android.widget.ScrollView;
37

    
38
import org.distorted.objectlib.main.ObjectControl;
39
import org.distorted.objectlib.main.ObjectType;
40

    
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.dialogs.RubikDialogAbout;
44
import org.distorted.dialogs.RubikDialogPattern;
45
import org.distorted.dialogs.RubikDialogScores;
46
import org.distorted.dialogs.RubikDialogTutorial;
47
import org.distorted.helpers.TransparentButton;
48
import org.distorted.helpers.TransparentImageButton;
49
import org.distorted.network.RubikScores;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
public class RubikScreenPlay extends RubikScreenBase
54
  {
55
  public static final int NUM_COLUMNS  = 4;
56
  public static final int LEVELS_SHOWN = 10;
57
  public static int MAX_LEVEL;
58
  public static final ObjectType DEF_OBJECT= ObjectType.CUBE_3;
59

    
60
  private static final int[] BUTTON_LABELS = { R.string.scores,
61
                                               R.string.patterns,
62
                                               R.string.solver,
63
                                               R.string.tutorials,
64
                                               R.string.about };
65

    
66
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
67
  private static final float LAST_BUTTON = 1.5f;
68
  private static final int[] mLocation = new int[2];
69

    
70
  private ImageButton mObjButton, mMenuButton, mSolveButton;
71
  private Button mPlayButton;
72
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
73
  private ObjectType mObject = DEF_OBJECT;
74
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
75
  private int mLevelValue;
76
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
77
  private int mColCount, mRowCount, mMaxRowCount;
78
  private LinearLayout mPlayLayout;
79
  private int mUpperBarHeight;
80

    
81
  static
82
    {
83
    ObjectType[] types = ObjectType.values();
84
    int max = Integer.MIN_VALUE;
85

    
86
    for (ObjectType type : types)
87
      {
88
      int cur = getDBLevel(type);
89
      if( cur>max ) max = cur;
90
      }
91

    
92
    MAX_LEVEL = max;
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  void leaveScreen(RubikActivity act)
98
    {
99

    
100
    }
101

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

    
104
  void enterScreen(final RubikActivity act)
105
    {
106
    float width = act.getScreenWidthInPixels();
107
    float height= act.getScreenHeightInPixels();
108
    mUpperBarHeight = act.getHeightUpperBar();
109

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

    
114
    mRowCount = (ObjectType.NUM_OBJECTS + 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
    setupObjectWindow(act,width,height);
122
    setupObjectButton(act,width);
123
    layoutTop.addView(mObjButton);
124

    
125
    setupMenuWindow(act,width);
126
    setupMenuButton(act,width);
127
    layoutTop.addView(mMenuButton);
128

    
129
    setupPlayWindow(act,width);
130
    setupPlayButton(act,width,height);
131
    layoutTop.addView(mPlayButton);
132

    
133
    setupSolveButton(act,width);
134
    createBottomPane(act,width,mSolveButton);
135
    }
136

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

    
139
  private void setupObjectButton(final RubikActivity act, final float width)
140
    {
141
    final int margin  = (int)(width*RubikActivity.MARGIN);
142
    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);
143

    
144
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
145

    
146
    mObjButton.setOnClickListener( new View.OnClickListener()
147
      {
148
      @Override
149
      public void onClick(View view)
150
        {
151
        if( mObjectPopup!=null && act.getControl().isUINotBlocked())
152
          {
153
          int rowCount = Math.min(mMaxRowCount,mRowCount);
154
          View popupView = mObjectPopup.getContentView();
155
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
156
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount,margin,margin);
157
          }
158
        }
159
      });
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  private void setupPlayButton(final RubikActivity act, final float width, final float height)
165
    {
166
    final int margin   = (int)(width*RubikActivity.MARGIN);
167
    final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
168

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

    
171
    mPlayButton.setOnClickListener( new View.OnClickListener()
172
      {
173
      @Override
174
      public void onClick(View view)
175
        {
176
        if( mPlayPopup!=null && act.getControl().isUINotBlocked())
177
          {
178
          View popupView = mPlayPopup.getContentView();
179
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
180
          final int dbLevel = getDBLevel(mObject);
181
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
182
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
183
          final int realHeight = Math.min(popupHeight,maxHeight);
184
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
185
          }
186
        }
187
      });
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private void setupMenuButton(final RubikActivity act, final float width)
193
    {
194
    final int margin  = (int)(width*RubikActivity.MARGIN);
195
    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);
196

    
197
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
198

    
199
    mMenuButton.setOnClickListener( new View.OnClickListener()
200
      {
201
      @Override
202
      public void onClick(View view)
203
        {
204
        if( mMenuPopup!=null && act.getControl().isUINotBlocked())
205
          {
206
          View popupView = mMenuPopup.getContentView();
207
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
208
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
209
          }
210
        }
211
      });
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
217
    {
218
    int icon = RubikActivity.getDrawable(R.drawable.s_cube_2,R.drawable.m_cube_2, R.drawable.b_cube_2, R.drawable.h_cube_2);
219

    
220
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
221
    int cubeWidth = bd.getIntrinsicWidth();
222
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
223
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
224
    mMaxRowCount = (int)(0.9f*(height-mUpperBarHeight)/mObjectSize);
225
    GridLayout objectGrid = new GridLayout(act);
226
    mObjectPopup = new PopupWindow(act);
227
    mObjectPopup.setFocusable(true);
228

    
229
    if( mMaxRowCount<mRowCount )
230
      {
231
      ScrollView scrollView = new ScrollView(act);
232
      scrollView.addView(objectGrid);
233
      mObjectPopup.setContentView(scrollView);
234
      }
235
    else
236
      {
237
      mObjectPopup.setContentView(objectGrid);
238
      }
239

    
240
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
241
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
242

    
243
    objectGrid.setColumnCount(mColCount);
244
    objectGrid.setRowCount(mRowCount);
245

    
246
    int[] nextInRow = new int[mRowCount];
247

    
248
    for(int row=0; row<mRowCount; row++)
249
      {
250
      rowSpecs[row] = GridLayout.spec(row);
251
      nextInRow[row]= 0;
252
      }
253
    for(int col=0; col<mColCount; col++)
254
      {
255
      colSpecs[col] = GridLayout.spec(col);
256
      }
257

    
258
    for(int object = 0; object< ObjectType.NUM_OBJECTS; object++)
259
      {
260
      final ObjectType type = ObjectType.getObject(object);
261
      int iconSize = RubikActivity.getDrawableSize();
262
      int icons = type.getIconID(iconSize);
263
      int row = object/NUM_COLUMNS;
264

    
265
      ImageButton button = new ImageButton(act);
266
      button.setBackgroundResource(icons);
267
      button.setOnClickListener( new View.OnClickListener()
268
        {
269
        @Override
270
        public void onClick(View v)
271
          {
272
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
273
            {
274
            mObject = type;
275
            act.changeObject(type, true);
276
            adjustLevels(act);
277
            mMovesController.clearMoves(act);
278
            }
279

    
280
          mObjectPopup.dismiss();
281
          }
282
        });
283

    
284
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
285
      params.bottomMargin = margin;
286
      params.topMargin    = margin;
287
      params.leftMargin   = margin;
288
      params.rightMargin  = margin;
289

    
290
      nextInRow[row]++;
291

    
292
      objectGrid.addView(button, params);
293
      }
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  private void setupMenuWindow(final RubikActivity act, final float width)
299
    {
300
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
301
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
302
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
303

    
304
    mMenuPopup = new PopupWindow(act);
305
    mMenuPopup.setContentView(layout);
306
    mMenuPopup.setFocusable(true);
307
    int margin  = (int)(width*RubikActivity.MARGIN);
308
    int padding = (int)(width*RubikActivity.PADDING);
309

    
310
    mMenuLayoutWidth = (int)(width/2);
311
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
312

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

    
315
    for(int i=0; i<NUM_BUTTONS; i++)
316
      {
317
      final int but = i;
318
      Button button = new Button(act);
319
      button.setLayoutParams(p);
320
      button.setText(BUTTON_LABELS[i]);
321
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
322

    
323
      button.setOnClickListener( new View.OnClickListener()
324
        {
325
        @Override
326
        public void onClick(View v)
327
          {
328
          mMenuPopup.dismiss();
329
          MenuAction(act,but);
330
          }
331
        });
332

    
333
      menuLayout.addView(button);
334
      }
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  private void setupPlayWindow(final RubikActivity act, final float width)
340
    {
341
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
342
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
343
    mPlayLayout = layout.findViewById(R.id.playGrid);
344

    
345
    mPlayLayoutWidth = (int)(width*0.4f);
346

    
347
    mPlayPopup = new PopupWindow(act);
348
    mPlayPopup.setContentView(layout);
349
    mPlayPopup.setFocusable(true);
350

    
351
    adjustLevels(act);
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  private void MenuAction(RubikActivity act, int button)
357
    {
358
    switch(button)
359
      {
360
      case 0: Bundle sBundle = new Bundle();
361
              sBundle.putInt("tab", mObject.ordinal() );
362
              sBundle.putBoolean("submitting", false);
363
              RubikDialogScores scores = new RubikDialogScores();
364
              scores.setArguments(sBundle);
365
              scores.show(act.getSupportFragmentManager(), null);
366
              break;
367
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
368
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
369
              break;
370
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
371
              break;
372
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
373
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
374
              break;
375
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
376
              aDiag.show(act.getSupportFragmentManager(), null);
377
              break;
378
      }
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  void setupSolveButton(final RubikActivity act, final float width)
384
    {
385
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
386
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
387

    
388
    mSolveButton.setOnClickListener( new View.OnClickListener()
389
      {
390
      @Override
391
      public void onClick(View v)
392
        {
393
        act.getControl().solveObject();
394
        mMovesController.clearMoves(act);
395
        }
396
      });
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  public void savePreferences(SharedPreferences.Editor editor)
402
    {
403
    editor.putString("statePlay_objName", mObject.name() );
404

    
405
    if( mObjectPopup!=null )
406
      {
407
      mObjectPopup.dismiss();
408
      mObjectPopup = null;
409
      }
410

    
411
    if( mMenuPopup!=null )
412
      {
413
      mMenuPopup.dismiss();
414
      mMenuPopup = null;
415
      }
416

    
417
    if( mPlayPopup!=null )
418
      {
419
      mPlayPopup.dismiss();
420
      mPlayPopup = null;
421
      }
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public void restorePreferences(SharedPreferences preferences)
427
    {
428
    String objName= preferences.getString("statePlay_objName", DEF_OBJECT.name() );
429
    int ordinal = ObjectType.getOrdinal(objName);
430
    mObject = ordinal>=0 ? ObjectType.values()[ordinal] : DEF_OBJECT;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public boolean setObject(RubikActivity act, ObjectType obj)
436
    {
437
    if( mObject!=obj )
438
      {
439
      mObject = obj;
440
      if( mPlayLayout!=null ) adjustLevels(act);
441
      return true;
442
      }
443

    
444
    return false;
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
449

    
450
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
451
    {
452
    View topLayout = act.findViewById(R.id.relativeLayout);
453
    boolean isFullScreen;
454

    
455
    if( topLayout!=null )
456
      {
457
      topLayout.getLocationOnScreen(mLocation);
458
      isFullScreen = (mLocation[1]==0);
459
      }
460
    else
461
      {
462
      isFullScreen = true;
463
      }
464

    
465
    // if on Android 11 or we are fullscreen
466
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
467
      {
468
      window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
469
      window.update(view, w, h);
470
      }
471
    else  // Android 10 or below in pop-up mode or split-screen mode
472
      {
473
      view.getLocationOnScreen(mLocation);
474
      int width  = view.getWidth();
475
      int height = view.getHeight();
476
      int x = mLocation[0]+(width-w)/2;
477
      int y = mLocation[1]+height+yoff;
478

    
479
      window.showAsDropDown(view);
480
      window.update(x,y,w,h);
481
      }
482
    }
483

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

    
486
  private void adjustLevels(final RubikActivity act)
487
    {
488
    int dbLevel = getDBLevel(mObject);
489
    int numScrambles = ObjectType.getNumScramble(mObject.ordinal());
490
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
491
    String[] levels = new String[numLevel];
492

    
493
    for(int i=0; i<numLevel-1; i++)
494
      {
495
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
496
      }
497

    
498
    if( numLevel>0 )
499
      {
500
      levels[numLevel-1] = act.getString(R.string.level_full);
501
      }
502

    
503
    if( mLevelValue>dbLevel || mLevelValue<1 ||
504
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
505
      {
506
      mLevelValue=1;
507
      }
508

    
509
    float width  = act.getScreenWidthInPixels();
510
    int margin   = (int)(width*RubikActivity.MARGIN);
511
    int padding  = (int)(width*RubikActivity.PADDING);
512
    int butWidth = mPlayLayoutWidth - 2*padding;
513
    int butHeight= (int)mMenuItemSize;
514
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
515

    
516
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
517
    pM.setMargins(margin, 0, margin, margin);
518
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
519
    pT.setMargins(margin, margin, margin, margin);
520
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
521
    pB.setMargins(margin, margin, margin, 2*margin);
522

    
523
    mPlayLayout.removeAllViews();
524

    
525
    RubikScores scores = RubikScores.getInstance();
526

    
527
    for(int i=0; i<numLevel; i++)
528
      {
529
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
530
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
531
      Button button = new Button(act);
532
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
533
      button.setText(levels[i]);
534
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
535

    
536
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
537
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
538

    
539
      button.setOnClickListener( new View.OnClickListener()
540
        {
541
        @Override
542
        public void onClick(View v)
543
          {
544
          ObjectControl control = act.getControl();
545

    
546
          if(control.isUINotBlocked())
547
            {
548
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
549
            mLevelValue = level;
550
            control.scrambleObject(scrambles);
551
            }
552
          }
553
        });
554

    
555
      mPlayLayout.addView(button);
556
      }
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
561
// those got remembered in the server-side DB already, so we need to keep using them. This function
562
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
563
// those two values the same.
564

    
565
  public static int getDBLevel(ObjectType object)
566
    {
567
    switch(object)
568
      {
569
      case CUBE_3: return 16;
570
      case CUBE_4: return 20;
571
      case CUBE_5: return 24;
572
      case PYRA_4: return 14;
573
      case PYRA_5: return 20;
574
      case MEGA_5: return 35;
575
      case DIAM_2: return 10;
576
      case DIAM_3: return 18;
577
      case REDI_3: return 14;
578
      case HELI_3: return 18;
579
      case SKEW_3: return 17;
580
      case REX_3 : return 16;
581
      case MIRR_3: return 16;
582
      default    : return ObjectType.getNumScramble(object.ordinal());
583
      }
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
  public int getLevel()
589
    {
590
    return mLevelValue;
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
  public ObjectType getObject()
596
    {
597
    return mObject;
598
    }
599
  }
(5-5/10)