Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ b0ed406c

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.states;
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.dialogs.RubikDialogAbout;
37
import org.distorted.dialogs.RubikDialogPattern;
38
import org.distorted.dialogs.RubikDialogScores;
39
import org.distorted.dialogs.RubikDialogTutorial;
40
import org.distorted.main.R;
41
import org.distorted.main.RubikActivity;
42
import org.distorted.main.RubikPreRender;
43
import org.distorted.objects.ObjectList;
44
import org.distorted.scores.RubikScores;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
54
  private static final int[] BUTTON_LABELS = { R.string.scores, R.string.patterns, R.string.solver, R.string.tutorials, R.string.about };
55
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
56
  private static final float LAST_BUTTON = 1.4f;
57

    
58
  private ImageButton mObjButton, mMenuButton, mSolveButton;
59
  private Button mPlayButton;
60
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
61
  private int mObject = DEF_OBJECT;
62
  private int mSize   = DEF_SIZE;
63
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
64
  private int mLevelValue;
65
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
66
  private int mColCount, mRowCount;
67
  private LinearLayout mPlayLayout;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  void leaveState(RubikActivity act)
72
    {
73

    
74
    }
75

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

    
78
  void enterState(final RubikActivity act)
79
    {
80
    float width = act.getScreenWidthInPixels();
81

    
82
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
83
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
84
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
85

    
86
    mRowCount = ObjectList.getRowCount();
87
    mColCount = ObjectList.getColumnCount();
88

    
89
    // TOP ////////////////////////////
90
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
91
    layoutTop.removeAllViews();
92

    
93
    setupObjectWindow(act,width);
94
    setupObjectButton(act,width);
95
    layoutTop.addView(mObjButton);
96

    
97
    setupMenuWindow(act,width);
98
    setupMenuButton(act,width);
99
    layoutTop.addView(mMenuButton);
100

    
101
    setupPlayWindow(act,width);
102
    setupPlayButton(act,width);
103
    layoutTop.addView(mPlayButton);
104

    
105
    setupSolveButton(act,width);
106
    createBottomPane(act,width,mSolveButton);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  private void setupObjectButton(final RubikActivity act, final float width)
112
    {
113
    final int margin  = (int)(width*RubikActivity.MARGIN);
114
    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);
115
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
116

    
117
    mObjButton.setOnClickListener( new View.OnClickListener()
118
      {
119
      @Override
120
      public void onClick(View view)
121
        {
122
        if( act.getPreRender().canPlay() )
123
          {
124
          if( mObjectPopup==null )
125
            {
126
            // I completely don't understand it, but Firebase says occasionally mObjectPopup is null here. Recreate.
127
            float width = act.getScreenWidthInPixels();
128
            setupObjectWindow(act,width);
129
            }
130

    
131
          mObjectPopup.setFocusable(false);
132
          mObjectPopup.update();
133

    
134
          View popupView = mObjectPopup.getContentView();
135
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
136

    
137
          mObjectPopup.showAsDropDown(view, margin, margin);
138
          mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*mRowCount);
139

    
140
          mObjectPopup.setFocusable(true);
141
          mObjectPopup.update();
142
          }
143
        }
144
      });
145
    }
146

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

    
149
  private void setupPlayButton(final RubikActivity act, final float width)
150
    {
151
    final int margin  = (int)(width*RubikActivity.MARGIN);
152
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
153

    
154
    mPlayButton.setOnClickListener( new View.OnClickListener()
155
      {
156
      @Override
157
      public void onClick(View view)
158
        {
159
        if( act.getPreRender().canPlay() )
160
          {
161
          if( mPlayPopup==null )
162
            {
163
            // I completely don't understand it, but Firebase says occasionally mPlayPopup is null here. Recreate.
164
            float width = act.getScreenWidthInPixels();
165
            setupPlayWindow(act,width);
166
            }
167

    
168
          mPlayPopup.setFocusable(false);
169
          mPlayPopup.update();
170

    
171
          View popupView = mPlayPopup.getContentView();
172
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
173

    
174
          final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
175
          final int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
176
          final int levelsShown = Math.min(maxLevel,LEVELS_SHOWN);
177

    
178
          mPlayPopup.showAsDropDown(view, margin, margin);
179
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
180
          mPlayPopup.setFocusable(true);
181
          mPlayPopup.update();
182
          }
183
        }
184
      });
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void setupMenuButton(final RubikActivity act, final float width)
190
    {
191
    final int margin  = (int)(width*RubikActivity.MARGIN);
192
    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);
193
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
194

    
195
    mMenuButton.setOnClickListener( new View.OnClickListener()
196
      {
197
      @Override
198
      public void onClick(View view)
199
        {
200
        if( act.getPreRender().canPlay() )
201
          {
202
          if( mMenuPopup==null )
203
            {
204
            // I completely don't understand it, but Firebase says occasionally mMenuPopup is null here. Recreate.
205
            float width = act.getScreenWidthInPixels();
206
            setupMenuWindow(act,width);
207
            }
208

    
209
          mMenuPopup.setFocusable(false);
210
          mMenuPopup.update();
211

    
212
          View popupView = mMenuPopup.getContentView();
213
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
214

    
215
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
216
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
217
          mMenuPopup.setFocusable(true);
218
          mMenuPopup.update();
219
          }
220
        }
221
      });
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  private void setupObjectWindow(final RubikActivity act, final float width)
227
    {
228
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
229
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
230
    GridLayout objectGrid = layout.findViewById(R.id.objectGrid);
231

    
232
    int[] indices = ObjectList.getIndices();
233

    
234
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
235
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
236

    
237
    objectGrid.setColumnCount(mColCount);
238
    objectGrid.setRowCount(mRowCount);
239

    
240
    int[] nextInRow = new int[mRowCount];
241

    
242
    for(int row=0; row<mRowCount; row++)
243
      {
244
      rowSpecs[row] = GridLayout.spec(row);
245
      nextInRow[row]= 0;
246
      }
247
    for(int col=0; col<mColCount; col++)
248
      {
249
      colSpecs[col] = GridLayout.spec(col);
250
      }
251

    
252
    mObjectPopup = new PopupWindow(act);
253
    mObjectPopup.setContentView(layout);
254
    mObjectPopup.setFocusable(true);
255
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
256

    
257
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
258
    int cubeWidth = bd.getIntrinsicWidth();
259
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
260
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
261

    
262
    for(int object=0; object< ObjectList.NUM_OBJECTS; object++)
263
      {
264
      final ObjectList list = ObjectList.getObject(object);
265
      final int[] sizes = list.getSizes();
266
      int[] icons = list.getIconIDs();
267
      int len = sizes.length;
268
      final int obj = object;
269
      int row = indices[object];
270

    
271
      for(int i=0; i<len; i++)
272
        {
273
        final int index = i;
274

    
275
        ImageButton button = new ImageButton(act);
276
        button.setBackgroundResource(icons[i]);
277
        button.setOnClickListener( new View.OnClickListener()
278
          {
279
          @Override
280
          public void onClick(View v)
281
            {
282
            if( act.getPreRender().canPlay() && StateList.getCurrentState()== StateList.PLAY )
283
              {
284
              mObject = obj;
285
              mSize   = sizes[index];
286
              act.changeObject(list,sizes[index], true);
287
              adjustLevels(act);
288
              mMoves.clear();
289
              }
290

    
291
            mObjectPopup.dismiss();
292
            }
293
          });
294

    
295
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
296
        params.bottomMargin = margin;
297
        params.topMargin    = margin;
298
        params.leftMargin   = margin;
299
        params.rightMargin  = margin;
300

    
301
        nextInRow[row]++;
302

    
303
        objectGrid.addView(button, params);
304
        }
305
      }
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  private void setupMenuWindow(final RubikActivity act, final float width)
311
    {
312
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
313
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
314
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
315

    
316
    mMenuPopup = new PopupWindow(act);
317
    mMenuPopup.setContentView(layout);
318
    mMenuPopup.setFocusable(true);
319
    int margin  = (int)(width*RubikActivity.MARGIN);
320
    int padding = (int)(width*RubikActivity.PADDING);
321

    
322
    mMenuLayoutWidth = (int)(width/2);
323
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
324

    
325
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
326
    pT.setMargins(margin, 0, margin, margin);
327
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
328
    pM.setMargins(margin, margin, margin, margin);
329
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
330
    pB.setMargins(margin, margin, margin, 2*margin);
331

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

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

    
350
      menuLayout.addView(button);
351
      }
352
    }
353

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

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

    
362
    mPlayLayoutWidth = (int)(width*0.4f);
363

    
364
    mPlayPopup = new PopupWindow(act);
365
    mPlayPopup.setContentView(layout);
366
    mPlayPopup.setFocusable(true);
367

    
368
    adjustLevels(act);
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

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

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  void setupSolveButton(final RubikActivity act, final float width)
413
    {
414
    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);
415
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
416

    
417
    mSolveButton.setOnClickListener( new View.OnClickListener()
418
      {
419
      @Override
420
      public void onClick(View v)
421
        {
422
        act.getPreRender().solveObject();
423
        mMoves.clear();
424
        }
425
      });
426
    }
427

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

    
430
  public void savePreferences(SharedPreferences.Editor editor)
431
    {
432
    editor.putInt("statePlay_object", mObject);
433
    editor.putInt("statePlay_size"  , mSize);
434

    
435
    if( mObjectPopup!=null )
436
      {
437
      mObjectPopup.dismiss();
438
      mObjectPopup = null;
439
      }
440

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

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

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
  public void restorePreferences(SharedPreferences preferences)
457
    {
458
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
459
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
465
    {
466
    if( mObject!=obj.ordinal() || mSize != size )
467
      {
468
      boolean success = false;
469

    
470
      for( int s: obj.getSizes() )
471
        if( s==size )
472
          {
473
          success = true;
474
          break;
475
          }
476

    
477
      if( success )
478
        {
479
        mObject = obj.ordinal();
480
        mSize   = size;
481

    
482
        if( mPlayLayout!=null ) adjustLevels(act);
483
        }
484

    
485
      return success;
486
      }
487

    
488
    return true;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  private void adjustLevels(final RubikActivity act)
494
    {
495
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
496
    int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
497
    int numLevel = Math.min(maxLevel, LEVELS_SHOWN);
498
    String[] levels = new String[numLevel];
499

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

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

    
507
    if( mLevelValue>maxLevel || mLevelValue<1 ||
508
       (mLevelValue<maxLevel || mLevelValue>LEVELS_SHOWN ) )
509
      {
510
      mLevelValue=1;
511
      }
512

    
513
    float width = act.getScreenWidthInPixels();
514
    int margin  = (int)(width*RubikActivity.MARGIN);
515
    int padding = (int)(width*RubikActivity.PADDING);
516

    
517
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
518
    pM.setMargins(margin, 0, margin, margin);
519
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
520
    pT.setMargins(margin, margin, margin, margin);
521
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)(mMenuItemSize*LAST_BUTTON) );
522
    pB.setMargins(margin, margin, margin, 2*margin);
523

    
524
    mPlayLayout.removeAllViews();
525

    
526
    RubikScores scores = RubikScores.getInstance();
527

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

    
536
      int icon = scores.isSolved(mObject, sizeIndex, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
537
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
538

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

    
546
          if( pre.canPlay() )
547
            {
548
            mPlayPopup.dismiss();
549
            mLevelValue = scrambles;
550
            pre.scrambleObject(mLevelValue);
551
            }
552
          }
553
        });
554

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

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
  public int getLevel()
562
    {
563
    return mLevelValue;
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  public int getObject()
569
    {
570
    return mObject;
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
  public int getSize()
576
    {
577
    return mSize;
578
    }
579
  }
(5-5/12)