Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 598de3ee

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);
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)
152
    {
153
    final int margin  = (int)(width*RubikActivity.MARGIN);
154
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
155

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

    
166
          final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
167
          final int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
168
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
169

    
170
          mPlayPopup.showAsDropDown(view, margin, margin);
171
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
172
          mPlayPopup.setFocusable(true);
173
          mPlayPopup.update();
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
          mMenuPopup.setFocusable(true);
200
          }
201
        }
202
      });
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

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

    
221
    if( mMaxRowCount<mRowCount )
222
      {
223
      ScrollView scrollView = new ScrollView(act);
224
      scrollView.addView(objectGrid);
225
      mObjectPopup.setContentView(scrollView);
226
      }
227
    else
228
      {
229
      mObjectPopup.setContentView(objectGrid);
230
      }
231

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

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

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

    
240
    int[] nextInRow = new int[mRowCount];
241

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

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

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

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

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

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

    
291
        nextInRow[row]++;
292

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

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

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

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

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

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

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

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

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

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

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

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

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

    
353
    adjustLevels(act);
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

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

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

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

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

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

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

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

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

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

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

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

    
450
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
451
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
452

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

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

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

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

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

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

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

    
487
      return success;
488
      }
489

    
490
    return true;
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
  private void adjustLevels(final RubikActivity act)
496
    {
497
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
498
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
499
    int numScrambles = ObjectList.getNumScramble(mObject, sizeIndex);
500
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
501
    String[] levels = new String[numLevel];
502

    
503
    for(int i=0; i<numLevel-1; i++)
504
      {
505
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
506
      }
507

    
508
    if( numLevel>0 )
509
      {
510
      levels[numLevel-1] = act.getString(R.string.level_full);
511
      }
512

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

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

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

    
533
    mPlayLayout.removeAllViews();
534

    
535
    RubikScores scores = RubikScores.getInstance();
536

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

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

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

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

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

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

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

    
576
///////////////////////////////////////////////////////////////////////////////////////////////////
577

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

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

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