Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 0501a4b8

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

    
35
import org.distorted.dialogs.RubikDialogAbout;
36
import org.distorted.dialogs.RubikDialogPattern;
37
import org.distorted.dialogs.RubikDialogScores;
38
import org.distorted.dialogs.RubikDialogTutorial;
39
import org.distorted.main.R;
40
import org.distorted.main.RubikActivity;
41
import org.distorted.main.RubikRenderer;
42
import org.distorted.objects.ObjectList;
43
import org.distorted.scores.RubikScores;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

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

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  void leaveState(RubikActivity act)
71
    {
72

    
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

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

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

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

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

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

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

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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

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

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

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

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

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

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

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

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

    
177
          mPlayPopup.showAsDropDown(view, margin, margin);
178
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
179

    
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);
216
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
217

    
218
          mMenuPopup.setFocusable(true);
219
          mMenuPopup.update();
220
          }
221
        }
222
      });
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

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

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

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

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

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

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

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

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

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

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

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

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

    
302
        nextInRow[row]++;
303

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

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

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

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

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

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

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

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

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

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

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

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

    
369
    adjustLevels(act);
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

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

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

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

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

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

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

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

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

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

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

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

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

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

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

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

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

    
486
      return success;
487
      }
488

    
489
    return true;
490
    }
491

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

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

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

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

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

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

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

    
525
    mPlayLayout.removeAllViews();
526

    
527
    RubikScores scores = RubikScores.getInstance();
528

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

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

    
540
      button.setOnClickListener( new View.OnClickListener()
541
        {
542
        @Override
543
        public void onClick(View v)
544
          {
545
          mPlayPopup.dismiss();
546
          mLevelValue = scrambles;
547
          act.getPreRender().scrambleObject(mLevelValue);
548
          }
549
        });
550

    
551
      mPlayLayout.addView(button);
552
      }
553
    }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556

    
557
  public int getLevel()
558
    {
559
    return mLevelValue;
560
    }
561

    
562
///////////////////////////////////////////////////////////////////////////////////////////////////
563

    
564
  public int getObject()
565
    {
566
    return mObject;
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

    
571
  public int getSize()
572
    {
573
    return mSize;
574
    }
575
  }
(5-5/12)