Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 00af5060

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.objects.ObjectList;
42
import org.distorted.scores.RubikScores;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  void leaveState(RubikActivity act)
70
    {
71

    
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

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

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

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

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

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

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

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

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

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

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

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

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

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

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

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

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

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

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

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

    
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

    
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[] nextInColumn = new int[mColCount];
241

    
242
    for(int row=0; row<mRowCount; row++)
243
      {
244
      rowSpecs[row] = GridLayout.spec(row);
245
      }
246
    for(int col=0; col<mColCount; col++)
247
      {
248
      colSpecs[col] = GridLayout.spec(col);
249
      nextInColumn[col] =0;
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 col = 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[nextInColumn[col]],colSpecs[col]);
296
        params.bottomMargin = margin;
297
        params.topMargin    = margin;
298
        params.leftMargin   = margin;
299
        params.rightMargin  = margin;
300

    
301
        nextInColumn[col]++;
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
          mPlayPopup.dismiss();
545
          mLevelValue = scrambles;
546
          act.getPreRender().scrambleObject(mLevelValue);
547
          }
548
        });
549

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

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

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

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

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

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

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