Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 51baad0e

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
import android.widget.ScrollView;
36

    
37
import org.distorted.control.RubikControl;
38
import org.distorted.dialogs.RubikDialogAbout;
39
import org.distorted.dialogs.RubikDialogPattern;
40
import org.distorted.dialogs.RubikDialogScores;
41
import org.distorted.dialogs.RubikDialogTutorial;
42
import org.distorted.helpers.TransparentButton;
43
import org.distorted.helpers.TransparentImageButton;
44
import org.distorted.main.R;
45
import org.distorted.main.RubikActivity;
46
import org.distorted.main.RubikPreRender;
47
import org.distorted.objects.ObjectList;
48
import org.distorted.network.RubikScores;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class RubikScreenPlay extends RubikScreenBase
53
  {
54
  public static final int LEVELS_SHOWN = 10;
55
  public static final int DEF_OBJECT= ObjectList.CUBE.ordinal();
56
  public static final int DEF_SIZE  =  3;
57

    
58
  private static final int[] BUTTON_LABELS = { R.string.scores,
59
                                               R.string.patterns,
60
                                            //   R.string.control,
61
                                               R.string.solver,
62
                                               R.string.tutorials,
63
                                               R.string.about };
64

    
65
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
66
  private static final float LAST_BUTTON = 1.5f;
67

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  void leaveScreen(RubikActivity act)
82
    {
83

    
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  void enterScreen(final RubikActivity act)
89
    {
90
    float width = act.getScreenWidthInPixels();
91
    float height= act.getScreenHeightInPixels();
92

    
93
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
94
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
95
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
96

    
97
    mRowCount = ObjectList.getRowCount();
98
    mColCount = ObjectList.getColumnCount();
99

    
100
    // TOP ////////////////////////////
101
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
102
    layoutTop.removeAllViews();
103

    
104
    setupObjectWindow(act,width,height);
105
    setupObjectButton(act,width);
106
    layoutTop.addView(mObjButton);
107

    
108
    setupMenuWindow(act,width);
109
    setupMenuButton(act,width);
110
    layoutTop.addView(mMenuButton);
111

    
112
    setupPlayWindow(act,width);
113
    setupPlayButton(act,width,height);
114
    layoutTop.addView(mPlayButton);
115

    
116
    setupSolveButton(act,width);
117
    createBottomPane(act,width,mSolveButton);
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private void setupObjectButton(final RubikActivity act, final float width)
123
    {
124
    final int margin  = (int)(width*RubikActivity.MARGIN);
125
    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);
126
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
127

    
128
    mObjButton.setOnClickListener( new View.OnClickListener()
129
      {
130
      @Override
131
      public void onClick(View view)
132
        {
133
        if( mObjectPopup!=null && act.getPreRender().isUINotBlocked())
134
          {
135
          int rowCount = Math.min(mMaxRowCount,mRowCount);
136

    
137
          View popupView = mObjectPopup.getContentView();
138
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
139

    
140
          mObjectPopup.showAsDropDown(view, margin, margin);
141
          mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*rowCount);
142
          }
143
        }
144
      });
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void setupPlayButton(final RubikActivity act, final float width, final float height)
150
    {
151
    final int margin   = (int)(width*RubikActivity.MARGIN);
152
    int upperBarHeight = act.getHeightUpperBar();
153
    final int maxHeight= (int)(0.9f*(height-upperBarHeight) );
154

    
155
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
156

    
157
    mPlayButton.setOnClickListener( new View.OnClickListener()
158
      {
159
      @Override
160
      public void onClick(View view)
161
        {
162
        if( mPlayPopup!=null && act.getPreRender().isUINotBlocked())
163
          {
164
          View popupView = mPlayPopup.getContentView();
165
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
166

    
167
          final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
168
          final int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
169
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
170
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
171

    
172
          mPlayPopup.showAsDropDown(view, margin, margin);
173
          mPlayPopup.update(view, mPlayLayoutWidth, Math.min(popupHeight,maxHeight));
174
          }
175
        }
176
      });
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

    
187
    mMenuButton.setOnClickListener( new View.OnClickListener()
188
      {
189
      @Override
190
      public void onClick(View view)
191
        {
192
        if( mMenuPopup!=null && act.getPreRender().isUINotBlocked())
193
          {
194
          View popupView = mMenuPopup.getContentView();
195
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
196

    
197
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
198
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
199
          }
200
        }
201
      });
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
207
    {
208
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
209

    
210
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
211
    int cubeWidth = bd.getIntrinsicWidth();
212
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
213
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
214
    int upperBarHeight  = act.getHeightUpperBar();
215
    mMaxRowCount = (int)((height-upperBarHeight)/mObjectSize);
216
    GridLayout objectGrid = new GridLayout(act);
217
    mObjectPopup = new PopupWindow(act);
218
    mObjectPopup.setFocusable(true);
219

    
220
    if( mMaxRowCount<mRowCount )
221
      {
222
      ScrollView scrollView = new ScrollView(act);
223
      scrollView.addView(objectGrid);
224
      mObjectPopup.setContentView(scrollView);
225
      }
226
    else
227
      {
228
      mObjectPopup.setContentView(objectGrid);
229
      }
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[] nextInRow = new int[mRowCount];
240

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

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

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

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

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

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

    
290
        nextInRow[row]++;
291

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

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

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

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

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

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

    
316
    for(int i=0; i<NUM_BUTTONS; i++)
317
      {
318
      final int but = i;
319
      Button button = new Button(act);
320
      button.setLayoutParams(p);
321
      button.setText(BUTTON_LABELS[i]);
322
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
323

    
324
      button.setOnClickListener( new View.OnClickListener()
325
        {
326
        @Override
327
        public void onClick(View v)
328
          {
329
          mMenuPopup.dismiss();
330
          MenuAction(act,but);
331
          }
332
        });
333

    
334
      menuLayout.addView(button);
335
      }
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  private void setupPlayWindow(final RubikActivity act, final float width)
341
    {
342
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
343
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
344
    mPlayLayout = layout.findViewById(R.id.playGrid);
345

    
346
    mPlayLayoutWidth = (int)(width*0.4f);
347

    
348
    mPlayPopup = new PopupWindow(act);
349
    mPlayPopup.setContentView(layout);
350
    mPlayPopup.setFocusable(true);
351

    
352
    adjustLevels(act);
353
    }
354

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

    
357
  private void MenuAction(RubikActivity act, int button)
358
    {
359
    switch(button)
360
      {
361
      case 0: RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
362
              int object = play.getObject();
363
              int size   = play.getSize();
364
              int sizeIndex = ObjectList.getSizeIndex(object,size);
365
              Bundle sBundle = new Bundle();
366
              sBundle.putInt("tab", ObjectList.pack(object,sizeIndex) );
367
              sBundle.putBoolean("submitting", false);
368
              RubikDialogScores scores = new RubikDialogScores();
369
              scores.setArguments(sBundle);
370
              scores.show(act.getSupportFragmentManager(), null);
371
              break;
372
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
373
              Bundle pBundle = new Bundle();
374
              int pOrd = getPatternOrdinal();
375
              pBundle.putInt("tab", pOrd );
376
              pDiag.setArguments(pBundle);
377
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
378
              break;
379
/*
380
      case 2: RubikControl control = RubikControl.getInstance();
381
              //control.animateAll(act);
382
              control.animateRotate(act);
383
              break;
384
 */
385
      case 2: ScreenList.switchScreen(act, ScreenList.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
        mController.clearMoves(act);
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
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
452
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
453

    
454
    // This means the app has been upgraded to a new version which swapped the
455
    // Object for a new one with larger sizeIndex and now getMaxLevel() returns
456
    // 0. Reset the object to default, otherwise we'll get a crash later on.
457

    
458
    if( dbLevel==0 )
459
      {
460
      mObject = DEF_OBJECT;
461
      mSize   = DEF_SIZE;
462
      }
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

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

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

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

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

    
488
      return success;
489
      }
490

    
491
    return true;
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  private void adjustLevels(final RubikActivity act)
497
    {
498
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
499
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
500
    int numScrambles = ObjectList.getNumScramble(mObject, sizeIndex);
501
    int numLevel = Math.min(dbLevel, 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
    if( numLevel>0 )
510
      {
511
      levels[numLevel-1] = act.getString(R.string.level_full);
512
      }
513

    
514
    if( mLevelValue>dbLevel || mLevelValue<1 ||
515
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
516
      {
517
      mLevelValue=1;
518
      }
519

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

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

    
534
    mPlayLayout.removeAllViews();
535

    
536
    RubikScores scores = RubikScores.getInstance();
537

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

    
547
      int icon = scores.isSolved(mObject, sizeIndex, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
548
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
549

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

    
557
          if(pre.isUINotBlocked())
558
            {
559
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
560
            mLevelValue = level;
561
            pre.scrambleObject(scrambles);
562
            }
563
          }
564
        });
565

    
566
      mPlayLayout.addView(button);
567
      }
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

    
572
  public int getLevel()
573
    {
574
    return mLevelValue;
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
  public int getObject()
580
    {
581
    return mObject;
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

    
586
  public int getSize()
587
    {
588
    return mSize;
589
    }
590
  }
(5-5/10)