Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 6fad862b

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( mObjectPopup==null )
122
          {
123
          // I completely don't understand it, but Firebase says occasionally mObjectPopup is null here. Recreate.
124
          float width = act.getScreenWidthInPixels();
125
          setupObjectWindow(act,width);
126
          }
127

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

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

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

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

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

    
162
        mPlayPopup.setFocusable(false);
163
        mPlayPopup.update();
164

    
165
        View popupView = mPlayPopup.getContentView();
166
        popupView.setSystemUiVisibility(RubikActivity.FLAGS);
167

    
168
        final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
169
        final int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
170
        final int levelsShown = Math.min(maxLevel,LEVELS_SHOWN);
171

    
172
        mPlayPopup.showAsDropDown(view, margin, margin);
173
        mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
174
        mPlayPopup.setFocusable(true);
175
        mPlayPopup.update();
176
        }
177
      });
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  private void setupMenuButton(final RubikActivity act, final float width)
183
    {
184
    final int margin  = (int)(width*RubikActivity.MARGIN);
185
    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);
186
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
187

    
188
    mMenuButton.setOnClickListener( new View.OnClickListener()
189
      {
190
      @Override
191
      public void onClick(View view)
192
        {
193
        if( mMenuPopup==null )
194
          {
195
          // I completely don't understand it, but Firebase says occasionally mMenuPopup is null here. Recreate.
196
          float width = act.getScreenWidthInPixels();
197
          setupMenuWindow(act,width);
198
          }
199

    
200
        mMenuPopup.setFocusable(false);
201
        mMenuPopup.update();
202

    
203
        View popupView = mMenuPopup.getContentView();
204
        popupView.setSystemUiVisibility(RubikActivity.FLAGS);
205

    
206
        mMenuPopup.showAsDropDown(view, (int)(-width/12), margin);
207
        mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
208
        mMenuPopup.setFocusable(true);
209
        mMenuPopup.update();
210
        }
211
      });
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private void setupObjectWindow(final RubikActivity act, final float width)
217
    {
218
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
219
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
220
    GridLayout objectGrid = layout.findViewById(R.id.objectGrid);
221

    
222
    int[] indices = ObjectList.getIndices();
223

    
224
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
225
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
226

    
227
    objectGrid.setColumnCount(mColCount);
228
    objectGrid.setRowCount(mRowCount);
229

    
230
    int[] nextInRow = new int[mRowCount];
231

    
232
    for(int row=0; row<mRowCount; row++)
233
      {
234
      rowSpecs[row] = GridLayout.spec(row);
235
      nextInRow[row]= 0;
236
      }
237
    for(int col=0; col<mColCount; col++)
238
      {
239
      colSpecs[col] = GridLayout.spec(col);
240
      }
241

    
242
    mObjectPopup = new PopupWindow(act);
243
    mObjectPopup.setContentView(layout);
244
    mObjectPopup.setFocusable(true);
245
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
246

    
247
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
248
    int cubeWidth = bd.getIntrinsicWidth();
249
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
250
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
251

    
252
    for(int object=0; object< ObjectList.NUM_OBJECTS; object++)
253
      {
254
      final ObjectList list = ObjectList.getObject(object);
255
      final int[] sizes = list.getSizes();
256
      int[] icons = list.getIconIDs();
257
      int len = sizes.length;
258
      final int obj = object;
259
      int row = indices[object];
260

    
261
      for(int i=0; i<len; i++)
262
        {
263
        final int index = i;
264

    
265
        ImageButton button = new ImageButton(act);
266
        button.setBackgroundResource(icons[i]);
267
        button.setOnClickListener( new View.OnClickListener()
268
          {
269
          @Override
270
          public void onClick(View v)
271
            {
272
            if( act.getPreRender().canPlay() && StateList.getCurrentState()== StateList.PLAY )
273
              {
274
              mObject = obj;
275
              mSize   = sizes[index];
276
              act.changeObject(list,sizes[index], true);
277
              adjustLevels(act);
278
              mMoves.clear();
279
              }
280

    
281
            mObjectPopup.dismiss();
282
            }
283
          });
284

    
285
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
286
        params.bottomMargin = margin;
287
        params.topMargin    = margin;
288
        params.leftMargin   = margin;
289
        params.rightMargin  = margin;
290

    
291
        nextInRow[row]++;
292

    
293
        objectGrid.addView(button, params);
294
        }
295
      }
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private void setupMenuWindow(final RubikActivity act, final float width)
301
    {
302
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
303
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
304
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
305

    
306
    mMenuPopup = new PopupWindow(act);
307
    mMenuPopup.setContentView(layout);
308
    mMenuPopup.setFocusable(true);
309
    int margin  = (int)(width*RubikActivity.MARGIN);
310
    int padding = (int)(width*RubikActivity.PADDING);
311

    
312
    mMenuLayoutWidth = (int)(width/2);
313
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
314

    
315
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
316
    pT.setMargins(margin, 0, margin, margin);
317
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
318
    pM.setMargins(margin, margin, margin, margin);
319
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
320
    pB.setMargins(margin, margin, margin, 2*margin);
321

    
322
    for(int i=0; i<NUM_BUTTONS; i++)
323
      {
324
      final int but = i;
325
      Button button = new Button(act);
326
      button.setLayoutParams(pT);
327
      button.setText(BUTTON_LABELS[i]);
328
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
329

    
330
      button.setOnClickListener( new View.OnClickListener()
331
        {
332
        @Override
333
        public void onClick(View v)
334
          {
335
          mMenuPopup.dismiss();
336
          MenuAction(act,but);
337
          }
338
        });
339

    
340
      menuLayout.addView(button);
341
      }
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  private void setupPlayWindow(final RubikActivity act, final float width)
347
    {
348
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
349
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
350
    mPlayLayout = layout.findViewById(R.id.playGrid);
351

    
352
    mPlayLayoutWidth = (int)(width*0.4f);
353

    
354
    mPlayPopup = new PopupWindow(act);
355
    mPlayPopup.setContentView(layout);
356
    mPlayPopup.setFocusable(true);
357

    
358
    adjustLevels(act);
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

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

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  void setupSolveButton(final RubikActivity act, final float width)
403
    {
404
    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);
405
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
406

    
407
    mSolveButton.setOnClickListener( new View.OnClickListener()
408
      {
409
      @Override
410
      public void onClick(View v)
411
        {
412
        act.getPreRender().solveObject();
413
        mMoves.clear();
414
        }
415
      });
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public void savePreferences(SharedPreferences.Editor editor)
421
    {
422
    editor.putInt("statePlay_object", mObject);
423
    editor.putInt("statePlay_size"  , mSize);
424

    
425
    if( mObjectPopup!=null )
426
      {
427
      mObjectPopup.dismiss();
428
      mObjectPopup = null;
429
      }
430

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

    
437
    if( mPlayPopup!=null )
438
      {
439
      mPlayPopup.dismiss();
440
      mPlayPopup = null;
441
      }
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public void restorePreferences(SharedPreferences preferences)
447
    {
448
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
449
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
455
    {
456
    if( mObject!=obj.ordinal() || mSize != size )
457
      {
458
      boolean success = false;
459

    
460
      for( int s: obj.getSizes() )
461
        if( s==size )
462
          {
463
          success = true;
464
          break;
465
          }
466

    
467
      if( success )
468
        {
469
        mObject = obj.ordinal();
470
        mSize   = size;
471

    
472
        if( mPlayLayout!=null ) adjustLevels(act);
473
        }
474

    
475
      return success;
476
      }
477

    
478
    return true;
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  private void adjustLevels(final RubikActivity act)
484
    {
485
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
486
    int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
487
    int numLevel = Math.min(maxLevel, LEVELS_SHOWN);
488
    String[] levels = new String[numLevel];
489

    
490
    for(int i=0; i<numLevel-1; i++)
491
      {
492
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
493
      }
494

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

    
497
    if( mLevelValue>maxLevel || mLevelValue<1 ||
498
       (mLevelValue<maxLevel || mLevelValue>LEVELS_SHOWN ) )
499
      {
500
      mLevelValue=1;
501
      }
502

    
503
    float width = act.getScreenWidthInPixels();
504
    int margin  = (int)(width*RubikActivity.MARGIN);
505
    int padding = (int)(width*RubikActivity.PADDING);
506

    
507
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
508
    pM.setMargins(margin, 0, margin, margin);
509
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)mMenuItemSize);
510
    pT.setMargins(margin, margin, margin, margin);
511
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( mPlayLayoutWidth - 2*padding, (int)(mMenuItemSize*LAST_BUTTON) );
512
    pB.setMargins(margin, margin, margin, 2*margin);
513

    
514
    mPlayLayout.removeAllViews();
515

    
516
    RubikScores scores = RubikScores.getInstance();
517

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

    
526
      int icon = scores.isSolved(mObject, sizeIndex, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
527
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
528

    
529
      button.setOnClickListener( new View.OnClickListener()
530
        {
531
        @Override
532
        public void onClick(View v)
533
          {
534
          RubikPreRender pre = act.getPreRender();
535

    
536
          if( pre.canPlay() )
537
            {
538
            mPlayPopup.dismiss();
539
            mLevelValue = scrambles;
540
            pre.scrambleObject(mLevelValue);
541
            }
542
          }
543
        });
544

    
545
      mPlayLayout.addView(button);
546
      }
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
  public int getLevel()
552
    {
553
    return mLevelValue;
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
  public int getObject()
559
    {
560
    return mObject;
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
  public int getSize()
566
    {
567
    return mSize;
568
    }
569
  }
(5-5/12)