Project

General

Profile

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

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

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

    
243
    for(int row=0; row<mRowCount; row++)
244
      {
245
      rowSpecs[row] = GridLayout.spec(row);
246
      }
247
    for(int col=0; col<mColCount; col++)
248
      {
249
      colSpecs[col] = GridLayout.spec(col);
250
      nextInColumn[col] =0;
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 col = 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
              if( RubikRenderer.supportsGigaminx() || list!=ObjectList.MEGA || sizes[index]<=3 )
286
                {
287
                mObject = obj;
288
                mSize   = sizes[index];
289
                act.changeObject(list,sizes[index], true);
290
                adjustLevels(act);
291
                mMoves.clear();
292
                }
293
              }
294

    
295
            mObjectPopup.dismiss();
296
            }
297
          });
298

    
299
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[nextInColumn[col]],colSpecs[col]);
300
        params.bottomMargin = margin;
301
        params.topMargin    = margin;
302
        params.leftMargin   = margin;
303
        params.rightMargin  = margin;
304

    
305
        nextInColumn[col]++;
306

    
307
        objectGrid.addView(button, params);
308
        }
309
      }
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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

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

    
326
    mMenuLayoutWidth = (int)(width/2);
327
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
328

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

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

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

    
354
      menuLayout.addView(button);
355
      }
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

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

    
366
    mPlayLayoutWidth = (int)(width*0.4f);
367

    
368
    mPlayPopup = new PopupWindow(act);
369
    mPlayPopup.setContentView(layout);
370
    mPlayPopup.setFocusable(true);
371

    
372
    adjustLevels(act);
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

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

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

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

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

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
  public void savePreferences(SharedPreferences.Editor editor)
435
    {
436
    editor.putInt("statePlay_object", mObject);
437
    editor.putInt("statePlay_size"  , mSize);
438

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

    
445
    if( mMenuPopup!=null )
446
      {
447
      mMenuPopup.dismiss();
448
      mMenuPopup = null;
449
      }
450

    
451
    if( mPlayPopup!=null )
452
      {
453
      mPlayPopup.dismiss();
454
      mPlayPopup = null;
455
      }
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

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

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

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

    
474
      for( int s: obj.getSizes() )
475
        if( s==size )
476
          {
477
          success = true;
478
          break;
479
          }
480

    
481
      if( success )
482
        {
483
        mObject = obj.ordinal();
484
        mSize   = size;
485

    
486
        if( mPlayLayout!=null ) adjustLevels(act);
487
        }
488

    
489
      return success;
490
      }
491

    
492
    return true;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

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

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

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

    
511
    if( mLevelValue>maxLevel || mLevelValue<1 ||
512
       (mLevelValue<maxLevel || mLevelValue>LEVELS_SHOWN ) )
513
      {
514
      mLevelValue=1;
515
      }
516

    
517
    float width = act.getScreenWidthInPixels();
518
    int margin  = (int)(width*RubikActivity.MARGIN);
519
    int padding = (int)(width*RubikActivity.PADDING);
520

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

    
528
    mPlayLayout.removeAllViews();
529

    
530
    RubikScores scores = RubikScores.getInstance();
531

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

    
540
      int icon = scores.isSolved(mObject, sizeIndex, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
541
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
542

    
543
      button.setOnClickListener( new View.OnClickListener()
544
        {
545
        @Override
546
        public void onClick(View v)
547
          {
548
          mPlayPopup.dismiss();
549
          mLevelValue = scrambles;
550
          act.getPreRender().scrambleObject(mLevelValue);
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)