Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 834b2618

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.Bundle;
26
import android.util.TypedValue;
27
import android.view.Gravity;
28
import android.view.LayoutInflater;
29
import android.view.View;
30
import android.widget.Button;
31
import android.widget.GridLayout;
32
import android.widget.ImageButton;
33
import android.widget.LinearLayout;
34
import android.widget.PopupWindow;
35

    
36
import org.distorted.control.RubikControl;
37
import org.distorted.dialogs.RubikDialogAbout;
38
import org.distorted.dialogs.RubikDialogPattern;
39
import org.distorted.dialogs.RubikDialogScores;
40
import org.distorted.dialogs.RubikDialogTutorial;
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.main.RubikPreRender;
44
import org.distorted.objects.ObjectList;
45
import org.distorted.network.RubikScores;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
public class RubikScreenPlay extends RubikScreenBase
50
  {
51
  public static final int LEVELS_SHOWN = 10;
52
  public static final int DEF_OBJECT= ObjectList.CUBE.ordinal();
53
  public static final int DEF_SIZE  =  3;
54

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

    
62
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
63
  private static final float LAST_BUTTON = 1.5f;
64

    
65
  private ImageButton mObjButton, mMenuButton, mSolveButton;
66
  private Button mPlayButton;
67
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
68
  private int mObject = DEF_OBJECT;
69
  private int mSize   = DEF_SIZE;
70
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
71
  private int mLevelValue;
72
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
73
  private int mColCount, mRowCount;
74
  private LinearLayout mPlayLayout;
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  void leaveScreen(RubikActivity act)
79
    {
80

    
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  void enterScreen(final RubikActivity act)
86
    {
87
    float width = act.getScreenWidthInPixels();
88

    
89
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
90
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
91
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
92

    
93
    mRowCount = ObjectList.getRowCount();
94
    mColCount = ObjectList.getColumnCount();
95

    
96
    // TOP ////////////////////////////
97
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
98
    layoutTop.removeAllViews();
99

    
100
    setupObjectWindow(act,width);
101
    setupObjectButton(act,width);
102
    layoutTop.addView(mObjButton);
103

    
104
    setupMenuWindow(act,width);
105
    setupMenuButton(act,width);
106
    layoutTop.addView(mMenuButton);
107

    
108
    setupPlayWindow(act,width);
109
    setupPlayButton(act,width);
110
    layoutTop.addView(mPlayButton);
111

    
112
    setupSolveButton(act,width);
113
    createBottomPane(act,width,mSolveButton);
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  private void setupObjectButton(final RubikActivity act, final float width)
119
    {
120
    final int margin  = (int)(width*RubikActivity.MARGIN);
121
    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);
122
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
123

    
124
    mObjButton.setOnClickListener( new View.OnClickListener()
125
      {
126
      @Override
127
      public void onClick(View view)
128
        {
129
        if( act.getPreRender().canPlay() )
130
          {
131
          if( mObjectPopup==null )
132
            {
133
            // I completely don't understand it, but Firebase says occasionally mObjectPopup is null here. Recreate.
134
            float width = act.getScreenWidthInPixels();
135
            setupObjectWindow(act,width);
136
            }
137

    
138
          mObjectPopup.setFocusable(false);
139
          mObjectPopup.update();
140

    
141
          View popupView = mObjectPopup.getContentView();
142
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
143

    
144
          mObjectPopup.showAsDropDown(view, margin, margin);
145
          mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*mRowCount);
146

    
147
          mObjectPopup.setFocusable(true);
148
          mObjectPopup.update();
149
          }
150
        }
151
      });
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  private void setupPlayButton(final RubikActivity act, final float width)
157
    {
158
    final int margin  = (int)(width*RubikActivity.MARGIN);
159
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
160

    
161
    mPlayButton.setOnClickListener( new View.OnClickListener()
162
      {
163
      @Override
164
      public void onClick(View view)
165
        {
166
        if( act.getPreRender().canPlay() )
167
          {
168
          if( mPlayPopup==null )
169
            {
170
            // I completely don't understand it, but Firebase says occasionally mPlayPopup is null here. Recreate.
171
            float width = act.getScreenWidthInPixels();
172
            setupPlayWindow(act,width);
173
            }
174

    
175
          mPlayPopup.setFocusable(false);
176
          mPlayPopup.update();
177

    
178
          View popupView = mPlayPopup.getContentView();
179
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
180

    
181
          final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
182
          final int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
183
          final int levelsShown = Math.min(maxLevel,LEVELS_SHOWN);
184

    
185
          mPlayPopup.showAsDropDown(view, margin, margin);
186
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
187
          mPlayPopup.setFocusable(true);
188
          mPlayPopup.update();
189
          }
190
        }
191
      });
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  private void setupMenuButton(final RubikActivity act, final float width)
197
    {
198
    final int margin  = (int)(width*RubikActivity.MARGIN);
199
    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);
200
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
201

    
202
    mMenuButton.setOnClickListener( new View.OnClickListener()
203
      {
204
      @Override
205
      public void onClick(View view)
206
        {
207
        if( act.getPreRender().canPlay() )
208
          {
209
          if( mMenuPopup==null )
210
            {
211
            // I completely don't understand it, but Firebase says occasionally mMenuPopup is null here. Recreate.
212
            float width = act.getScreenWidthInPixels();
213
            setupMenuWindow(act,width);
214
            }
215

    
216
          mMenuPopup.setFocusable(false);
217
          mMenuPopup.update();
218

    
219
          View popupView = mMenuPopup.getContentView();
220
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
221

    
222
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
223
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
224
          mMenuPopup.setFocusable(true);
225
          mMenuPopup.update();
226
          }
227
        }
228
      });
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  private void setupObjectWindow(final RubikActivity act, final float width)
234
    {
235
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
236
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
237
    GridLayout objectGrid = layout.findViewById(R.id.objectGrid);
238

    
239
    int[] indices = ObjectList.getIndices();
240

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

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

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

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

    
259
    mObjectPopup = new PopupWindow(act);
260
    mObjectPopup.setContentView(layout);
261
    mObjectPopup.setFocusable(true);
262
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
263

    
264
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
265
    int cubeWidth = bd.getIntrinsicWidth();
266
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
267
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
268

    
269
    for(int object=0; object< ObjectList.NUM_OBJECTS; object++)
270
      {
271
      final ObjectList list = ObjectList.getObject(object);
272
      final int[] sizes = list.getSizes();
273
      int[] icons = list.getIconIDs();
274
      int len = sizes.length;
275
      final int obj = object;
276
      int row = indices[object];
277

    
278
      for(int i=0; i<len; i++)
279
        {
280
        final int index = i;
281

    
282
        ImageButton button = new ImageButton(act);
283
        button.setBackgroundResource(icons[i]);
284
        button.setOnClickListener( new View.OnClickListener()
285
          {
286
          @Override
287
          public void onClick(View v)
288
            {
289
            if( act.getPreRender().canPlay() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
290
              {
291
              mObject = obj;
292
              mSize   = sizes[index];
293
              act.changeObject(list,sizes[index], true);
294
              adjustLevels(act);
295
              mMoves.clear();
296
              }
297

    
298
            mObjectPopup.dismiss();
299
            }
300
          });
301

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

    
308
        nextInRow[row]++;
309

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

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private void setupMenuWindow(final RubikActivity act, final float width)
318
    {
319
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
320
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
321
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
322

    
323
    mMenuPopup = new PopupWindow(act);
324
    mMenuPopup.setContentView(layout);
325
    mMenuPopup.setFocusable(true);
326
    int margin  = (int)(width*RubikActivity.MARGIN);
327
    int padding = (int)(width*RubikActivity.PADDING);
328

    
329
    mMenuLayoutWidth = (int)(width/2);
330
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
331

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

    
334
    for(int i=0; i<NUM_BUTTONS; i++)
335
      {
336
      final int but = i;
337
      Button button = new Button(act);
338
      button.setLayoutParams(p);
339
      button.setText(BUTTON_LABELS[i]);
340
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
341

    
342
      button.setOnClickListener( new View.OnClickListener()
343
        {
344
        @Override
345
        public void onClick(View v)
346
          {
347
          mMenuPopup.dismiss();
348
          MenuAction(act,but);
349
          }
350
        });
351

    
352
      menuLayout.addView(button);
353
      }
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  private void setupPlayWindow(final RubikActivity act, final float width)
359
    {
360
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
361
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
362
    mPlayLayout = layout.findViewById(R.id.playGrid);
363

    
364
    mPlayLayoutWidth = (int)(width*0.4f);
365

    
366
    mPlayPopup = new PopupWindow(act);
367
    mPlayPopup.setContentView(layout);
368
    mPlayPopup.setFocusable(true);
369

    
370
    adjustLevels(act);
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  private void MenuAction(RubikActivity act, int button)
376
    {
377
    switch(button)
378
      {
379
      case 0: RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
380
              int object = play.getObject();
381
              int size   = play.getSize();
382
              int sizeIndex = ObjectList.getSizeIndex(object,size);
383
              Bundle sBundle = new Bundle();
384
              sBundle.putInt("tab", ObjectList.pack(object,sizeIndex) );
385
              sBundle.putBoolean("submitting", false);
386
              RubikDialogScores scores = new RubikDialogScores();
387
              scores.setArguments(sBundle);
388
              scores.show(act.getSupportFragmentManager(), null);
389
              break;
390
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
391
              Bundle pBundle = new Bundle();
392
              int pOrd = getPatternOrdinal();
393
              pBundle.putInt("tab", pOrd );
394
              pDiag.setArguments(pBundle);
395
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
396
              break;
397
/*
398
      case 2: RubikControl control = RubikControl.getInstance();
399
              control.animateAll(act);
400
              break;
401
 */
402
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
403
              break;
404
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
405
              Bundle tBundle = new Bundle();
406
              int tOrd = getTutorialOrdinal();
407
              tBundle.putInt("tab", tOrd );
408
              tDiag.setArguments(tBundle);
409
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
410
              break;
411
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
412
              aDiag.show(act.getSupportFragmentManager(), null);
413
              break;
414
      }
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  void setupSolveButton(final RubikActivity act, final float width)
420
    {
421
    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);
422
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
423

    
424
    mSolveButton.setOnClickListener( new View.OnClickListener()
425
      {
426
      @Override
427
      public void onClick(View v)
428
        {
429
        act.getPreRender().solveObject();
430
        mMoves.clear();
431
        }
432
      });
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  public void savePreferences(SharedPreferences.Editor editor)
438
    {
439
    editor.putInt("statePlay_object", mObject);
440
    editor.putInt("statePlay_size"  , mSize);
441

    
442
    if( mObjectPopup!=null )
443
      {
444
      mObjectPopup.dismiss();
445
      mObjectPopup = null;
446
      }
447

    
448
    if( mMenuPopup!=null )
449
      {
450
      mMenuPopup.dismiss();
451
      mMenuPopup = null;
452
      }
453

    
454
    if( mPlayPopup!=null )
455
      {
456
      mPlayPopup.dismiss();
457
      mPlayPopup = null;
458
      }
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  public void restorePreferences(SharedPreferences preferences)
464
    {
465
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
466
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
467

    
468
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
469
    int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
470

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

    
475
    if( maxLevel==0 )
476
      {
477
      mObject = DEF_OBJECT;
478
      mSize   = DEF_SIZE;
479
      }
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
485
    {
486
    if( mObject!=obj.ordinal() || mSize != size )
487
      {
488
      boolean success = false;
489

    
490
      for( int s: obj.getSizes() )
491
        if( s==size )
492
          {
493
          success = true;
494
          break;
495
          }
496

    
497
      if( success )
498
        {
499
        mObject = obj.ordinal();
500
        mSize   = size;
501

    
502
        if( mPlayLayout!=null ) adjustLevels(act);
503
        }
504

    
505
      return success;
506
      }
507

    
508
    return true;
509
    }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
  private void adjustLevels(final RubikActivity act)
514
    {
515
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
516
    int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
517
    int numLevel = Math.min(maxLevel, LEVELS_SHOWN);
518
    String[] levels = new String[numLevel];
519

    
520
    for(int i=0; i<numLevel-1; i++)
521
      {
522
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
523
      }
524

    
525
    levels[numLevel-1] = act.getString(R.string.level_full);
526

    
527
    if( mLevelValue>maxLevel || mLevelValue<1 ||
528
       (mLevelValue<maxLevel || mLevelValue>LEVELS_SHOWN ) )
529
      {
530
      mLevelValue=1;
531
      }
532

    
533
    float width  = act.getScreenWidthInPixels();
534
    int margin   = (int)(width*RubikActivity.MARGIN);
535
    int padding  = (int)(width*RubikActivity.PADDING);
536
    int butWidth = mPlayLayoutWidth - 2*padding;
537
    int butHeight= (int)mMenuItemSize;
538
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
539

    
540
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
541
    pM.setMargins(margin, 0, margin, margin);
542
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
543
    pT.setMargins(margin, margin, margin, margin);
544
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
545
    pB.setMargins(margin, margin, margin, 2*margin);
546

    
547
    mPlayLayout.removeAllViews();
548

    
549
    RubikScores scores = RubikScores.getInstance();
550

    
551
    for(int i=0; i<numLevel; i++)
552
      {
553
      final int scrambles = i<numLevel-1 ? i+1 : maxLevel;
554
      Button button = new Button(act);
555
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
556
      button.setText(levels[i]);
557
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
558

    
559
      int icon = scores.isSolved(mObject, sizeIndex, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
560
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
561

    
562
      button.setOnClickListener( new View.OnClickListener()
563
        {
564
        @Override
565
        public void onClick(View v)
566
          {
567
          RubikPreRender pre = act.getPreRender();
568

    
569
          if( pre.canPlay() )
570
            {
571
            mPlayPopup.dismiss();
572
            mLevelValue = scrambles;
573
            pre.scrambleObject(mLevelValue);
574
            }
575
          }
576
        });
577

    
578
      mPlayLayout.addView(button);
579
      }
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  public int getLevel()
585
    {
586
    return mLevelValue;
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
  public int getObject()
592
    {
593
    return mObject;
594
    }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597

    
598
  public int getSize()
599
    {
600
    return mSize;
601
    }
602
  }
(5-5/12)