Project

General

Profile

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

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

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 LEVELS_SHOWN = 10;
56
  public static final int DEF_OBJECT= ObjectType.CUBE_3.ordinal();
57

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

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

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

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  void leaveScreen(RubikActivity act)
83
    {
84

    
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

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

    
99
    mRowCount = ObjectType.getRowCount();
100
    mColCount = ObjectType.getColumnCount();
101

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

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

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

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

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

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

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

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

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

    
225
    int[] indices = ObjectType.getIndices();
226

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

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

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

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

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

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

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

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

    
278
      nextInRow[row]++;
279

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

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

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

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

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

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

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

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

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

    
339
    adjustLevels(act);
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

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

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

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

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

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

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

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

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

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

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

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

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

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

    
453
    return false;
454
    }
455

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

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

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

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

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

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

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

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

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

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

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

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

    
532
    mPlayLayout.removeAllViews();
533

    
534
    RubikScores scores = RubikScores.getInstance();
535

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

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

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

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

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

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

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

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

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