Project

General

Profile

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

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

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.RubikPreRender;
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
          mPlayPopup.setFocusable(true);
180
          mPlayPopup.update();
181
          }
182
        }
183
      });
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

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

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

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

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

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

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

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

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

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

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

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

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

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

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

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

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

    
300
        nextInRow[row]++;
301

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

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

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

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

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

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

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

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

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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

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

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

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

    
367
    adjustLevels(act);
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

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

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

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

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

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

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

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

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

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

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

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

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

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

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

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

    
484
      return success;
485
      }
486

    
487
    return true;
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

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

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

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

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

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

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

    
523
    mPlayLayout.removeAllViews();
524

    
525
    RubikScores scores = RubikScores.getInstance();
526

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

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

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

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

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

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

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

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

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

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

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