Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 985f3dfa

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
  private 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

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  void leaveState(RubikActivity act)
69
    {
70

    
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

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

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

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

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

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
114
    mObjButton.setOnClickListener( new View.OnClickListener()
115
      {
116
      @Override
117
      public void onClick(View view)
118
        {
119
        if( act.getPreRender().canPlay() )
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

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

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

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

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

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

    
175
          mPlayPopup.showAsDropDown(view, margin, margin);
176
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+2*margin));
177

    
178
          mPlayPopup.setFocusable(true);
179
          mPlayPopup.update();
180
          }
181
        }
182
      });
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

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

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

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

    
213
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin);
214
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
215

    
216
          mMenuPopup.setFocusable(true);
217
          mMenuPopup.update();
218
          }
219
        }
220
      });
221
    }
222

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

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

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

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

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

    
239
    int[] nextInColumn = new int[mColCount];
240

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

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

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

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

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

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

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

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

    
300
        nextInColumn[col]++;
301

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

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

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

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

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

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

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

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

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

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

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

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

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

    
367
    adjustLevels(act);
368
    }
369

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

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

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

    
411
  void setupSolveButton(final RubikActivity act, final float width)
412
    {
413
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
414
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
415

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

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

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

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

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

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

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

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

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

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

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

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

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

    
484
      return success;
485
      }
486

    
487
    return true;
488
    }
489

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

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

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

    
503
    if( mLevelValue>maxLevel ) mLevelValue=1;
504

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

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

    
516
    mPlayLayout.removeAllViews();
517

    
518
    RubikScores scores = RubikScores.getInstance();
519

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

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

    
531
      button.setOnClickListener( new View.OnClickListener()
532
        {
533
        @Override
534
        public void onClick(View v)
535
          {
536
          mPlayPopup.dismiss();
537
          mLevelValue = but+1;
538
          act.getPreRender().scrambleObject(mLevelValue);
539
          }
540
        });
541

    
542
      mPlayLayout.addView(button);
543
      }
544
    }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547

    
548
  public int getLevel()
549
    {
550
    return mLevelValue;
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

    
555
  public int getObject()
556
    {
557
    return mObject;
558
    }
559

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561

    
562
  public int getSize()
563
    {
564
    return mSize;
565
    }
566
  }
(5-5/12)