Project

General

Profile

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

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

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
          mObjectPopup.setFocusable(true);
143
          mObjectPopup.update();
144
          }
145
        }
146
      });
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

    
157
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
158

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

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

    
174
          mPlayPopup.showAsDropDown(view, margin, margin);
175
          mPlayPopup.update(view, mPlayLayoutWidth, Math.min(popupHeight,maxHeight));
176
          mPlayPopup.setFocusable(true);
177
          mPlayPopup.update();
178
          }
179
        }
180
      });
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

    
201
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
202
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
203
          mMenuPopup.setFocusable(true);
204
          }
205
        }
206
      });
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
212
    {
213
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
214

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

    
225
    if( mMaxRowCount<mRowCount )
226
      {
227
      ScrollView scrollView = new ScrollView(act);
228
      scrollView.addView(objectGrid);
229
      mObjectPopup.setContentView(scrollView);
230
      }
231
    else
232
      {
233
      mObjectPopup.setContentView(objectGrid);
234
      }
235

    
236
    int[] indices = ObjectList.getIndices();
237

    
238
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
239
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
240

    
241
    objectGrid.setColumnCount(mColCount);
242
    objectGrid.setRowCount(mRowCount);
243

    
244
    int[] nextInRow = new int[mRowCount];
245

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

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

    
265
      for(int i=0; i<len; i++)
266
        {
267
        final int index = i;
268

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

    
285
            mObjectPopup.dismiss();
286
            }
287
          });
288

    
289
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
290
        params.bottomMargin = margin;
291
        params.topMargin    = margin;
292
        params.leftMargin   = margin;
293
        params.rightMargin  = margin;
294

    
295
        nextInRow[row]++;
296

    
297
        objectGrid.addView(button, params);
298
        }
299
      }
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

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

    
310
    mMenuPopup = new PopupWindow(act);
311
    mMenuPopup.setContentView(layout);
312
    mMenuPopup.setFocusable(true);
313
    int margin  = (int)(width*RubikActivity.MARGIN);
314
    int padding = (int)(width*RubikActivity.PADDING);
315

    
316
    mMenuLayoutWidth = (int)(width/2);
317
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
318

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

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

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

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

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

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

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

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

    
357
    adjustLevels(act);
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  void setupSolveButton(final RubikActivity act, final float width)
408
    {
409
    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);
410
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
411

    
412
    mSolveButton.setOnClickListener( new View.OnClickListener()
413
      {
414
      @Override
415
      public void onClick(View v)
416
        {
417
        act.getPreRender().solveObject();
418
        mController.clearMoves(act);
419
        }
420
      });
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public void savePreferences(SharedPreferences.Editor editor)
426
    {
427
    editor.putInt("statePlay_object", mObject);
428
    editor.putInt("statePlay_size"  , mSize);
429

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

    
436
    if( mMenuPopup!=null )
437
      {
438
      mMenuPopup.dismiss();
439
      mMenuPopup = null;
440
      }
441

    
442
    if( mPlayPopup!=null )
443
      {
444
      mPlayPopup.dismiss();
445
      mPlayPopup = null;
446
      }
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public void restorePreferences(SharedPreferences preferences)
452
    {
453
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
454
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
455

    
456
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
457
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
458

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

    
463
    if( dbLevel==0 )
464
      {
465
      mObject = DEF_OBJECT;
466
      mSize   = DEF_SIZE;
467
      }
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

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

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

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

    
490
        if( mPlayLayout!=null ) adjustLevels(act);
491
        }
492

    
493
      return success;
494
      }
495

    
496
    return true;
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  private void adjustLevels(final RubikActivity act)
502
    {
503
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
504
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
505
    int numScrambles = ObjectList.getNumScramble(mObject, sizeIndex);
506
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
507
    String[] levels = new String[numLevel];
508

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

    
514
    if( numLevel>0 )
515
      {
516
      levels[numLevel-1] = act.getString(R.string.level_full);
517
      }
518

    
519
    if( mLevelValue>dbLevel || mLevelValue<1 ||
520
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
521
      {
522
      mLevelValue=1;
523
      }
524

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

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

    
539
    mPlayLayout.removeAllViews();
540

    
541
    RubikScores scores = RubikScores.getInstance();
542

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

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

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

    
562
          if(pre.isUINotBlocked())
563
            {
564
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
565
            mLevelValue = level;
566
            pre.scrambleObject(scrambles);
567
            }
568
          }
569
        });
570

    
571
      mPlayLayout.addView(button);
572
      }
573
    }
574

    
575
///////////////////////////////////////////////////////////////////////////////////////////////////
576

    
577
  public int getLevel()
578
    {
579
    return mLevelValue;
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  public int getObject()
585
    {
586
    return mObject;
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
  public int getSize()
592
    {
593
    return mSize;
594
    }
595
  }
(5-5/10)