Project

General

Profile

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

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

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
  private RubikControl mControlWhole;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  void leaveScreen(RubikActivity act)
80
    {
81

    
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

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

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

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

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

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

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

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

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

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

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

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

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

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

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

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

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

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

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

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

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

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

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

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

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

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

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

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

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

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

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

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

    
309
        nextInRow[row]++;
310

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

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

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

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

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

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

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

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

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

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

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

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

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

    
371
    adjustLevels(act);
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

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

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

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

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

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

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

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

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

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

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
471
    {
472
    if( mObject!=obj.ordinal() || mSize != size )
473
      {
474
      boolean success = false;
475

    
476
      for( int s: obj.getSizes() )
477
        if( s==size )
478
          {
479
          success = true;
480
          break;
481
          }
482

    
483
      if( success )
484
        {
485
        mObject = obj.ordinal();
486
        mSize   = size;
487

    
488
        if( mPlayLayout!=null ) adjustLevels(act);
489
        }
490

    
491
      return success;
492
      }
493

    
494
    return true;
495
    }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
  private void adjustLevels(final RubikActivity act)
500
    {
501
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
502
    int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
503
    int numLevel = Math.min(maxLevel, LEVELS_SHOWN);
504
    String[] levels = new String[numLevel];
505

    
506
    for(int i=0; i<numLevel-1; i++)
507
      {
508
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
509
      }
510

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

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

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

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

    
533
    mPlayLayout.removeAllViews();
534

    
535
    RubikScores scores = RubikScores.getInstance();
536

    
537
    for(int i=0; i<numLevel; i++)
538
      {
539
      final int scrambles = i<numLevel-1 ? i+1 : maxLevel;
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, sizeIndex, i) ? 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
          RubikPreRender pre = act.getPreRender();
554

    
555
          if( pre.canPlay() )
556
            {
557
            mPlayPopup.dismiss();
558
            mLevelValue = scrambles;
559
            pre.scrambleObject(mLevelValue);
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

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

    
584
  public int getSize()
585
    {
586
    return mSize;
587
    }
588
  }
(5-5/12)