Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 287e91a6

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.ObjectType;
39

    
40
import org.distorted.main.R;
41
import org.distorted.main.RubikActivity;
42
import org.distorted.objectlib.main.ObjectPreRender;
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 final int DEF_OBJECT= ObjectType.CUBE_3.ordinal();
58

    
59
  private static final int[] BUTTON_LABELS = { R.string.scores,
60
                                               R.string.patterns,
61
                                            //   R.string.control,
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 int 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
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  void leaveScreen(RubikActivity act)
84
    {
85

    
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  void enterScreen(final RubikActivity act)
91
    {
92
    float width = act.getScreenWidthInPixels();
93
    float height= act.getScreenHeightInPixels();
94
    mUpperBarHeight = act.getHeightUpperBar();
95

    
96
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
97
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
98
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
99

    
100
    mRowCount = (ObjectType.NUM_OBJECTS + NUM_COLUMNS-1) / NUM_COLUMNS;
101
    mColCount = NUM_COLUMNS;
102

    
103
    // TOP ////////////////////////////
104
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
105
    layoutTop.removeAllViews();
106

    
107
    setupObjectWindow(act,width,height);
108
    setupObjectButton(act,width);
109
    layoutTop.addView(mObjButton);
110

    
111
    setupMenuWindow(act,width);
112
    setupMenuButton(act,width);
113
    layoutTop.addView(mMenuButton);
114

    
115
    setupPlayWindow(act,width);
116
    setupPlayButton(act,width,height);
117
    layoutTop.addView(mPlayButton);
118

    
119
    setupSolveButton(act,width);
120
    createBottomPane(act,width,mSolveButton);
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private void setupObjectButton(final RubikActivity act, final float width)
126
    {
127
    final int margin  = (int)(width*RubikActivity.MARGIN);
128
    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);
129

    
130
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
131

    
132
    mObjButton.setOnClickListener( new View.OnClickListener()
133
      {
134
      @Override
135
      public void onClick(View view)
136
        {
137
        if( mObjectPopup!=null && act.getPreRender().isUINotBlocked())
138
          {
139
          int rowCount = Math.min(mMaxRowCount,mRowCount);
140
          View popupView = mObjectPopup.getContentView();
141
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
142
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount,margin,margin);
143
          }
144
        }
145
      });
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private void setupPlayButton(final RubikActivity act, final float width, final float height)
151
    {
152
    final int margin   = (int)(width*RubikActivity.MARGIN);
153
    final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
154

    
155
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
156

    
157
    mPlayButton.setOnClickListener( new View.OnClickListener()
158
      {
159
      @Override
160
      public void onClick(View view)
161
        {
162
        if( mPlayPopup!=null && act.getPreRender().isUINotBlocked())
163
          {
164
          View popupView = mPlayPopup.getContentView();
165
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
166
          final int dbLevel = ObjectType.getDBLevel(mObject);
167
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
168
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
169
          final int realHeight = Math.min(popupHeight,maxHeight);
170
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
171
          }
172
        }
173
      });
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private void setupMenuButton(final RubikActivity act, final float width)
179
    {
180
    final int margin  = (int)(width*RubikActivity.MARGIN);
181
    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);
182

    
183
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
184

    
185
    mMenuButton.setOnClickListener( new View.OnClickListener()
186
      {
187
      @Override
188
      public void onClick(View view)
189
        {
190
        if( mMenuPopup!=null && act.getPreRender().isUINotBlocked())
191
          {
192
          View popupView = mMenuPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
195
          }
196
        }
197
      });
198
    }
199

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

    
202
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
203
    {
204
    int icon = RubikActivity.getDrawable(R.drawable.small_cube2,R.drawable.medium_cube2, R.drawable.big_cube2, R.drawable.huge_cube2);
205

    
206
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
207
    int cubeWidth = bd.getIntrinsicWidth();
208
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
209
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
210
    mMaxRowCount = (int)(0.9f*(height-mUpperBarHeight)/mObjectSize);
211
    GridLayout objectGrid = new GridLayout(act);
212
    mObjectPopup = new PopupWindow(act);
213
    mObjectPopup.setFocusable(true);
214

    
215
    if( mMaxRowCount<mRowCount )
216
      {
217
      ScrollView scrollView = new ScrollView(act);
218
      scrollView.addView(objectGrid);
219
      mObjectPopup.setContentView(scrollView);
220
      }
221
    else
222
      {
223
      mObjectPopup.setContentView(objectGrid);
224
      }
225

    
226
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
227
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
228

    
229
    objectGrid.setColumnCount(mColCount);
230
    objectGrid.setRowCount(mRowCount);
231

    
232
    int[] nextInRow = new int[mRowCount];
233

    
234
    for(int row=0; row<mRowCount; row++)
235
      {
236
      rowSpecs[row] = GridLayout.spec(row);
237
      nextInRow[row]= 0;
238
      }
239
    for(int col=0; col<mColCount; col++)
240
      {
241
      colSpecs[col] = GridLayout.spec(col);
242
      }
243

    
244
    for(int object = 0; object< ObjectType.NUM_OBJECTS; object++)
245
      {
246
      final ObjectType list = ObjectType.getObject(object);
247
      int iconSize = RubikActivity.getDrawableSize();
248
      int icons = list.getIconID(iconSize);
249
      final int obj = object;
250
      int row = object/NUM_COLUMNS;
251

    
252
      ImageButton button = new ImageButton(act);
253
      button.setBackgroundResource(icons);
254
      button.setOnClickListener( new View.OnClickListener()
255
        {
256
        @Override
257
        public void onClick(View v)
258
          {
259
          if( act.getPreRender().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
260
            {
261
            mObject = obj;
262
            act.changeObject(list, true);
263
            adjustLevels(act);
264
            mMovesController.clearMoves(act);
265
            }
266

    
267
          mObjectPopup.dismiss();
268
          }
269
        });
270

    
271
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
272
      params.bottomMargin = margin;
273
      params.topMargin    = margin;
274
      params.leftMargin   = margin;
275
      params.rightMargin  = margin;
276

    
277
      nextInRow[row]++;
278

    
279
      objectGrid.addView(button, params);
280
      }
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  private void setupMenuWindow(final RubikActivity act, final float width)
286
    {
287
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
288
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
289
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
290

    
291
    mMenuPopup = new PopupWindow(act);
292
    mMenuPopup.setContentView(layout);
293
    mMenuPopup.setFocusable(true);
294
    int margin  = (int)(width*RubikActivity.MARGIN);
295
    int padding = (int)(width*RubikActivity.PADDING);
296

    
297
    mMenuLayoutWidth = (int)(width/2);
298
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
299

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

    
302
    for(int i=0; i<NUM_BUTTONS; i++)
303
      {
304
      final int but = i;
305
      Button button = new Button(act);
306
      button.setLayoutParams(p);
307
      button.setText(BUTTON_LABELS[i]);
308
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
309

    
310
      button.setOnClickListener( new View.OnClickListener()
311
        {
312
        @Override
313
        public void onClick(View v)
314
          {
315
          mMenuPopup.dismiss();
316
          MenuAction(act,but);
317
          }
318
        });
319

    
320
      menuLayout.addView(button);
321
      }
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  private void setupPlayWindow(final RubikActivity act, final float width)
327
    {
328
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
329
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
330
    mPlayLayout = layout.findViewById(R.id.playGrid);
331

    
332
    mPlayLayoutWidth = (int)(width*0.4f);
333

    
334
    mPlayPopup = new PopupWindow(act);
335
    mPlayPopup.setContentView(layout);
336
    mPlayPopup.setFocusable(true);
337

    
338
    adjustLevels(act);
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  private void MenuAction(RubikActivity act, int button)
344
    {
345
    switch(button)
346
      {
347
      case 0: RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
348
              int object = play.getObject();
349
              Bundle sBundle = new Bundle();
350
              sBundle.putInt("tab", object );
351
              sBundle.putBoolean("submitting", false);
352
              RubikDialogScores scores = new RubikDialogScores();
353
              scores.setArguments(sBundle);
354
              scores.show(act.getSupportFragmentManager(), null);
355
              break;
356
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
357
              Bundle pBundle = new Bundle();
358
              int pOrd = getPatternOrdinal();
359
              pBundle.putInt("tab", pOrd );
360
              pDiag.setArguments(pBundle);
361
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
362
              break;
363
/*
364
      case 2: RubikControl control = RubikControl.getInstance();
365
              //control.animateAll(act);
366
              control.animateRotate(act);
367
              break;
368
 */
369
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
370
              break;
371
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
372
              Bundle tBundle = new Bundle();
373
              int tOrd = getTutorialOrdinal();
374
              tBundle.putInt("tab", tOrd );
375
              tDiag.setArguments(tBundle);
376
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
377
              break;
378
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
379
              aDiag.show(act.getSupportFragmentManager(), null);
380
              break;
381
      }
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  void setupSolveButton(final RubikActivity act, final float width)
387
    {
388
    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);
389
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
390

    
391
    mSolveButton.setOnClickListener( new View.OnClickListener()
392
      {
393
      @Override
394
      public void onClick(View v)
395
        {
396
        act.getPreRender().solveObject();
397
        mMovesController.clearMoves(act);
398
        }
399
      });
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  public void savePreferences(SharedPreferences.Editor editor)
405
    {
406
    editor.putInt("statePlay_object", mObject);
407

    
408
    if( mObjectPopup!=null )
409
      {
410
      mObjectPopup.dismiss();
411
      mObjectPopup = null;
412
      }
413

    
414
    if( mMenuPopup!=null )
415
      {
416
      mMenuPopup.dismiss();
417
      mMenuPopup = null;
418
      }
419

    
420
    if( mPlayPopup!=null )
421
      {
422
      mPlayPopup.dismiss();
423
      mPlayPopup = null;
424
      }
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public void restorePreferences(SharedPreferences preferences)
430
    {
431
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
432
    int dbLevel = ObjectType.getDBLevel(mObject);
433

    
434
    // This means the app has been upgraded to a new version which swapped the
435
    // Object for a new one with larger sizeIndex and now getMaxLevel() returns
436
    // 0. Reset the object to default, otherwise we'll get a crash later on.
437

    
438
    if( dbLevel==0 ) mObject = DEF_OBJECT;
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

    
443
  public boolean setObject(RubikActivity act, ObjectType obj)
444
    {
445
    if( mObject!=obj.ordinal() )
446
      {
447
      mObject = obj.ordinal();
448
      if( mPlayLayout!=null ) adjustLevels(act);
449
      return true;
450
      }
451

    
452
    return false;
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
457

    
458
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
459
    {
460
    View topLayout = act.findViewById(R.id.relativeLayout);
461
    boolean isFullScreen;
462

    
463
    if( topLayout!=null )
464
      {
465
      topLayout.getLocationOnScreen(mLocation);
466
      isFullScreen = (mLocation[1]==0);
467
      }
468
    else
469
      {
470
      isFullScreen = true;
471
      }
472

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

    
487
      window.showAsDropDown(view);
488
      window.update(x,y,w,h);
489
      }
490
    }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

    
494
  private void adjustLevels(final RubikActivity act)
495
    {
496
    int dbLevel = ObjectType.getDBLevel(mObject);
497
    int numScrambles = ObjectType.getNumScramble(mObject);
498
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
499
    String[] levels = new String[numLevel];
500

    
501
    for(int i=0; i<numLevel-1; i++)
502
      {
503
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
504
      }
505

    
506
    if( numLevel>0 )
507
      {
508
      levels[numLevel-1] = act.getString(R.string.level_full);
509
      }
510

    
511
    if( mLevelValue>dbLevel || mLevelValue<1 ||
512
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
513
      {
514
      mLevelValue=1;
515
      }
516

    
517
    float width  = act.getScreenWidthInPixels();
518
    int margin   = (int)(width*RubikActivity.MARGIN);
519
    int padding  = (int)(width*RubikActivity.PADDING);
520
    int butWidth = mPlayLayoutWidth - 2*padding;
521
    int butHeight= (int)mMenuItemSize;
522
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
523

    
524
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
525
    pM.setMargins(margin, 0, margin, margin);
526
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
527
    pT.setMargins(margin, margin, margin, margin);
528
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
529
    pB.setMargins(margin, margin, margin, 2*margin);
530

    
531
    mPlayLayout.removeAllViews();
532

    
533
    RubikScores scores = RubikScores.getInstance();
534

    
535
    for(int i=0; i<numLevel; i++)
536
      {
537
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
538
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
539
      Button button = new Button(act);
540
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
541
      button.setText(levels[i]);
542
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
543

    
544
      int icon = scores.isSolved(mObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
545
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
546

    
547
      button.setOnClickListener( new View.OnClickListener()
548
        {
549
        @Override
550
        public void onClick(View v)
551
          {
552
          ObjectPreRender pre = act.getPreRender();
553

    
554
          if(pre.isUINotBlocked())
555
            {
556
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
557
            mLevelValue = level;
558
            pre.scrambleObject(scrambles);
559
            }
560
          }
561
        });
562

    
563
      mPlayLayout.addView(button);
564
      }
565
    }
566

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568

    
569
  public int getLevel()
570
    {
571
    return mLevelValue;
572
    }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

    
576
  public int getObject()
577
    {
578
    return mObject;
579
    }
580
  }
(5-5/10)