Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ fcd5b990

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.screens;
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.network.RubikScores;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
public class RubikScreenPlay extends RubikScreenBase
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,
55
                                               R.string.patterns,
56
                                               R.string.control,
57
                                               R.string.solver,
58
                                               R.string.tutorials,
59
                                               R.string.about };
60

    
61
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
62
  private static final float LAST_BUTTON = 1.5f;
63

    
64
  private ImageButton mObjButton, mMenuButton, mSolveButton;
65
  private Button mPlayButton;
66
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
67
  private int mObject = DEF_OBJECT;
68
  private int mSize   = DEF_SIZE;
69
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
70
  private int mLevelValue;
71
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
72
  private int mColCount, mRowCount;
73
  private LinearLayout mPlayLayout;
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  void leaveState(RubikActivity act)
78
    {
79

    
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  void enterState(final RubikActivity act)
85
    {
86
    float width = act.getScreenWidthInPixels();
87

    
88
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
89
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
90
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
91

    
92
    mRowCount = ObjectList.getRowCount();
93
    mColCount = ObjectList.getColumnCount();
94

    
95
    // TOP ////////////////////////////
96
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
97
    layoutTop.removeAllViews();
98

    
99
    setupObjectWindow(act,width);
100
    setupObjectButton(act,width);
101
    layoutTop.addView(mObjButton);
102

    
103
    setupMenuWindow(act,width);
104
    setupMenuButton(act,width);
105
    layoutTop.addView(mMenuButton);
106

    
107
    setupPlayWindow(act,width);
108
    setupPlayButton(act,width);
109
    layoutTop.addView(mPlayButton);
110

    
111
    setupSolveButton(act,width);
112
    createBottomPane(act,width,mSolveButton);
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  private void setupObjectButton(final RubikActivity act, final float width)
118
    {
119
    final int margin  = (int)(width*RubikActivity.MARGIN);
120
    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);
121
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
122

    
123
    mObjButton.setOnClickListener( new View.OnClickListener()
124
      {
125
      @Override
126
      public void onClick(View view)
127
        {
128
        if( act.getPreRender().canPlay() )
129
          {
130
          if( mObjectPopup==null )
131
            {
132
            // I completely don't understand it, but Firebase says occasionally mObjectPopup is null here. Recreate.
133
            float width = act.getScreenWidthInPixels();
134
            setupObjectWindow(act,width);
135
            }
136

    
137
          mObjectPopup.setFocusable(false);
138
          mObjectPopup.update();
139

    
140
          View popupView = mObjectPopup.getContentView();
141
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
142

    
143
          mObjectPopup.showAsDropDown(view, margin, margin);
144
          mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*mRowCount);
145

    
146
          mObjectPopup.setFocusable(true);
147
          mObjectPopup.update();
148
          }
149
        }
150
      });
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  private void setupPlayButton(final RubikActivity act, final float width)
156
    {
157
    final int margin  = (int)(width*RubikActivity.MARGIN);
158
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
159

    
160
    mPlayButton.setOnClickListener( new View.OnClickListener()
161
      {
162
      @Override
163
      public void onClick(View view)
164
        {
165
        if( act.getPreRender().canPlay() )
166
          {
167
          if( mPlayPopup==null )
168
            {
169
            // I completely don't understand it, but Firebase says occasionally mPlayPopup is null here. Recreate.
170
            float width = act.getScreenWidthInPixels();
171
            setupPlayWindow(act,width);
172
            }
173

    
174
          mPlayPopup.setFocusable(false);
175
          mPlayPopup.update();
176

    
177
          View popupView = mPlayPopup.getContentView();
178
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
179

    
180
          final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
181
          final int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
182
          final int levelsShown = Math.min(maxLevel,LEVELS_SHOWN);
183

    
184
          mPlayPopup.showAsDropDown(view, margin, margin);
185
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
186
          mPlayPopup.setFocusable(true);
187
          mPlayPopup.update();
188
          }
189
        }
190
      });
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  private void setupMenuButton(final RubikActivity act, final float width)
196
    {
197
    final int margin  = (int)(width*RubikActivity.MARGIN);
198
    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);
199
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
200

    
201
    mMenuButton.setOnClickListener( new View.OnClickListener()
202
      {
203
      @Override
204
      public void onClick(View view)
205
        {
206
        if( act.getPreRender().canPlay() )
207
          {
208
          if( mMenuPopup==null )
209
            {
210
            // I completely don't understand it, but Firebase says occasionally mMenuPopup is null here. Recreate.
211
            float width = act.getScreenWidthInPixels();
212
            setupMenuWindow(act,width);
213
            }
214

    
215
          mMenuPopup.setFocusable(false);
216
          mMenuPopup.update();
217

    
218
          View popupView = mMenuPopup.getContentView();
219
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
220

    
221
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
222
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
223
          mMenuPopup.setFocusable(true);
224
          mMenuPopup.update();
225
          }
226
        }
227
      });
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  private void setupObjectWindow(final RubikActivity act, final float width)
233
    {
234
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
235
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
236
    GridLayout objectGrid = layout.findViewById(R.id.objectGrid);
237

    
238
    int[] indices = ObjectList.getIndices();
239

    
240
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
241
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
242

    
243
    objectGrid.setColumnCount(mColCount);
244
    objectGrid.setRowCount(mRowCount);
245

    
246
    int[] nextInRow = new int[mRowCount];
247

    
248
    for(int row=0; row<mRowCount; row++)
249
      {
250
      rowSpecs[row] = GridLayout.spec(row);
251
      nextInRow[row]= 0;
252
      }
253
    for(int col=0; col<mColCount; col++)
254
      {
255
      colSpecs[col] = GridLayout.spec(col);
256
      }
257

    
258
    mObjectPopup = new PopupWindow(act);
259
    mObjectPopup.setContentView(layout);
260
    mObjectPopup.setFocusable(true);
261
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
262

    
263
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
264
    int cubeWidth = bd.getIntrinsicWidth();
265
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
266
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
267

    
268
    for(int object=0; object< ObjectList.NUM_OBJECTS; object++)
269
      {
270
      final ObjectList list = ObjectList.getObject(object);
271
      final int[] sizes = list.getSizes();
272
      int[] icons = list.getIconIDs();
273
      int len = sizes.length;
274
      final int obj = object;
275
      int row = indices[object];
276

    
277
      for(int i=0; i<len; i++)
278
        {
279
        final int index = i;
280

    
281
        ImageButton button = new ImageButton(act);
282
        button.setBackgroundResource(icons[i]);
283
        button.setOnClickListener( new View.OnClickListener()
284
          {
285
          @Override
286
          public void onClick(View v)
287
            {
288
            if( act.getPreRender().canPlay() && ScreenList.getCurrentState()== ScreenList.PLAY )
289
              {
290
              mObject = obj;
291
              mSize   = sizes[index];
292
              act.changeObject(list,sizes[index], true);
293
              adjustLevels(act);
294
              mMoves.clear();
295
              }
296

    
297
            mObjectPopup.dismiss();
298
            }
299
          });
300

    
301
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
302
        params.bottomMargin = margin;
303
        params.topMargin    = margin;
304
        params.leftMargin   = margin;
305
        params.rightMargin  = margin;
306

    
307
        nextInRow[row]++;
308

    
309
        objectGrid.addView(button, params);
310
        }
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

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

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

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

    
331
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
332

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

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

    
351
      menuLayout.addView(button);
352
      }
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

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

    
363
    mPlayLayoutWidth = (int)(width*0.4f);
364

    
365
    mPlayPopup = new PopupWindow(act);
366
    mPlayPopup.setContentView(layout);
367
    mPlayPopup.setFocusable(true);
368

    
369
    adjustLevels(act);
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  private void controlTheCube()
375
    {
376
    android.util.Log.e("D", "Control");
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  void setupSolveButton(final RubikActivity act, final float width)
423
    {
424
    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);
425
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
426

    
427
    mSolveButton.setOnClickListener( new View.OnClickListener()
428
      {
429
      @Override
430
      public void onClick(View v)
431
        {
432
        act.getPreRender().solveObject();
433
        mMoves.clear();
434
        }
435
      });
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
  public void savePreferences(SharedPreferences.Editor editor)
441
    {
442
    editor.putInt("statePlay_object", mObject);
443
    editor.putInt("statePlay_size"  , mSize);
444

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

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

    
457
    if( mPlayPopup!=null )
458
      {
459
      mPlayPopup.dismiss();
460
      mPlayPopup = null;
461
      }
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  public void restorePreferences(SharedPreferences preferences)
467
    {
468
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
469
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
475
    {
476
    if( mObject!=obj.ordinal() || mSize != size )
477
      {
478
      boolean success = false;
479

    
480
      for( int s: obj.getSizes() )
481
        if( s==size )
482
          {
483
          success = true;
484
          break;
485
          }
486

    
487
      if( success )
488
        {
489
        mObject = obj.ordinal();
490
        mSize   = size;
491

    
492
        if( mPlayLayout!=null ) adjustLevels(act);
493
        }
494

    
495
      return success;
496
      }
497

    
498
    return true;
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
  private void adjustLevels(final RubikActivity act)
504
    {
505
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
506
    int maxLevel = ObjectList.getMaxLevel(mObject, sizeIndex);
507
    int numLevel = Math.min(maxLevel, LEVELS_SHOWN);
508
    String[] levels = new String[numLevel];
509

    
510
    for(int i=0; i<numLevel-1; i++)
511
      {
512
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
513
      }
514

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

    
517
    if( mLevelValue>maxLevel || mLevelValue<1 ||
518
       (mLevelValue<maxLevel || mLevelValue>LEVELS_SHOWN ) )
519
      {
520
      mLevelValue=1;
521
      }
522

    
523
    float width  = act.getScreenWidthInPixels();
524
    int margin   = (int)(width*RubikActivity.MARGIN);
525
    int padding  = (int)(width*RubikActivity.PADDING);
526
    int butWidth = mPlayLayoutWidth - 2*padding;
527
    int butHeight= (int)mMenuItemSize;
528
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
529

    
530
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
531
    pM.setMargins(margin, 0, margin, margin);
532
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
533
    pT.setMargins(margin, margin, margin, margin);
534
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
535
    pB.setMargins(margin, margin, margin, 2*margin);
536

    
537
    mPlayLayout.removeAllViews();
538

    
539
    RubikScores scores = RubikScores.getInstance();
540

    
541
    for(int i=0; i<numLevel; i++)
542
      {
543
      final int scrambles = i<numLevel-1 ? i+1 : maxLevel;
544
      Button button = new Button(act);
545
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
546
      button.setText(levels[i]);
547
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
548

    
549
      int icon = scores.isSolved(mObject, sizeIndex, i) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
550
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
551

    
552
      button.setOnClickListener( new View.OnClickListener()
553
        {
554
        @Override
555
        public void onClick(View v)
556
          {
557
          RubikPreRender pre = act.getPreRender();
558

    
559
          if( pre.canPlay() )
560
            {
561
            mPlayPopup.dismiss();
562
            mLevelValue = scrambles;
563
            pre.scrambleObject(mLevelValue);
564
            }
565
          }
566
        });
567

    
568
      mPlayLayout.addView(button);
569
      }
570
    }
571

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

    
574
  public int getLevel()
575
    {
576
    return mLevelValue;
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

    
581
  public int getObject()
582
    {
583
    return mObject;
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
  public int getSize()
589
    {
590
    return mSize;
591
    }
592
  }
(5-5/12)