Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 43162dfb

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 p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
326

    
327
    for(int i=0; i<NUM_BUTTONS; i++)
328
      {
329
      final int but = i;
330
      Button button = new Button(act);
331
      button.setLayoutParams(p);
332
      button.setText(BUTTON_LABELS[i]);
333
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
334

    
335
      button.setOnClickListener( new View.OnClickListener()
336
        {
337
        @Override
338
        public void onClick(View v)
339
          {
340
          mMenuPopup.dismiss();
341
          MenuAction(act,but);
342
          }
343
        });
344

    
345
      menuLayout.addView(button);
346
      }
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

    
357
    mPlayLayoutWidth = (int)(width*0.4f);
358

    
359
    mPlayPopup = new PopupWindow(act);
360
    mPlayPopup.setContentView(layout);
361
    mPlayPopup.setFocusable(true);
362

    
363
    adjustLevels(act);
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  void setupSolveButton(final RubikActivity act, final float width)
408
    {
409
    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);
410
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
411

    
412
    mSolveButton.setOnClickListener( new View.OnClickListener()
413
      {
414
      @Override
415
      public void onClick(View v)
416
        {
417
        act.getPreRender().solveObject();
418
        mMoves.clear();
419
        }
420
      });
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public void savePreferences(SharedPreferences.Editor editor)
426
    {
427
    editor.putInt("statePlay_object", mObject);
428
    editor.putInt("statePlay_size"  , mSize);
429

    
430
    if( mObjectPopup!=null )
431
      {
432
      mObjectPopup.dismiss();
433
      mObjectPopup = null;
434
      }
435

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

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

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public void restorePreferences(SharedPreferences preferences)
452
    {
453
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
454
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
455
    }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
460
    {
461
    if( mObject!=obj.ordinal() || mSize != size )
462
      {
463
      boolean success = false;
464

    
465
      for( int s: obj.getSizes() )
466
        if( s==size )
467
          {
468
          success = true;
469
          break;
470
          }
471

    
472
      if( success )
473
        {
474
        mObject = obj.ordinal();
475
        mSize   = size;
476

    
477
        if( mPlayLayout!=null ) adjustLevels(act);
478
        }
479

    
480
      return success;
481
      }
482

    
483
    return true;
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

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

    
495
    for(int i=0; i<numLevel-1; i++)
496
      {
497
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
498
      }
499

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

    
502
    if( mLevelValue>maxLevel || mLevelValue<1 ||
503
       (mLevelValue<maxLevel || mLevelValue>LEVELS_SHOWN ) )
504
      {
505
      mLevelValue=1;
506
      }
507

    
508
    float width = act.getScreenWidthInPixels();
509
    int margin  = (int)(width*RubikActivity.MARGIN);
510
    int padding = (int)(width*RubikActivity.PADDING);
511

    
512
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
513
    pM.setMargins(margin, 0, margin, margin);
514
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
515
    pT.setMargins(margin, margin, margin, margin);
516
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)(mMenuItemSize*LAST_BUTTON) );
517
    pB.setMargins(margin, margin, margin, 2*margin);
518

    
519
    mPlayLayout.removeAllViews();
520

    
521
    RubikScores scores = RubikScores.getInstance();
522

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

    
531
      int icon = scores.isSolved(mObject, sizeIndex, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
532
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
533

    
534
      button.setOnClickListener( new View.OnClickListener()
535
        {
536
        @Override
537
        public void onClick(View v)
538
          {
539
          RubikPreRender pre = act.getPreRender();
540

    
541
          if( pre.canPlay() )
542
            {
543
            mPlayPopup.dismiss();
544
            mLevelValue = scrambles;
545
            pre.scrambleObject(mLevelValue);
546
            }
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)