Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 876803e6

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 java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25
import android.content.Context;
26
import android.content.SharedPreferences;
27
import android.content.res.Resources;
28
import android.graphics.Color;
29
import android.graphics.drawable.BitmapDrawable;
30
import android.os.Build;
31
import android.os.Bundle;
32
import android.util.TypedValue;
33
import android.view.Gravity;
34
import android.view.LayoutInflater;
35
import android.view.View;
36
import android.view.ViewGroup;
37
import android.widget.Button;
38
import android.widget.GridLayout;
39
import android.widget.ImageButton;
40
import android.widget.LinearLayout;
41
import android.widget.PopupWindow;
42
import android.widget.RelativeLayout;
43
import android.widget.ScrollView;
44
import android.widget.TextView;
45

    
46
import org.distorted.config.ConfigActivity;
47
import org.distorted.dialogs.RubikDialogUpdates;
48
import org.distorted.external.RubikNetwork;
49
import org.distorted.external.RubikUpdates;
50
import org.distorted.main.RubikRenderer;
51
import org.distorted.objectlib.main.ObjectControl;
52

    
53
import org.distorted.main.R;
54
import org.distorted.main.RubikActivity;
55
import org.distorted.dialogs.RubikDialogAbout;
56
import org.distorted.dialogs.RubikDialogPattern;
57
import org.distorted.dialogs.RubikDialogScores;
58
import org.distorted.dialogs.RubikDialogTutorial;
59
import org.distorted.helpers.TransparentButton;
60
import org.distorted.helpers.TransparentImageButton;
61
import org.distorted.external.RubikScores;
62
import org.distorted.objects.RubikObject;
63
import org.distorted.objects.RubikObjectList;
64

    
65
import static android.view.View.inflate;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
public class RubikScreenPlay extends RubikScreenAbstract implements RubikNetwork.Updatee
70
  {
71
  public static final int NUM_COLUMNS  = 5;
72
  public static final int LEVELS_SHOWN = 10;
73

    
74
  private static final float LAST_BUTTON = 1.5f;
75
  private static final int[] mLocation = new int[2];
76

    
77
  private ImageButton mAboutButton, mUpdatesButton, mExitButton;
78
 // private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
79
  private LinearLayout mPlayLayout;
80
  private TextView mBubbleUpdates;
81
  private int mCurrentBubbleNumber;
82
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
83
  private int mLevelValue;
84
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
85
  private int mColCount, mRowCount, mMaxRowCount;
86
  private int mUpperBarHeight, mLowerBarHeight;
87
  private boolean mShouldReactToEndOfScrambling;
88
  private int mBottomHeight;
89
  private float mScreenWidth, mScreenHeight;
90
  private WeakReference<RubikActivity> mWeakAct;
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  void leaveScreen(RubikActivity act)
95
    {
96
    act.switchRenderingOn();
97
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
98
    hiddenView.removeAllViews();
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  void enterScreen(final RubikActivity act)
104
    {
105
    act.switchRenderingOff();
106
    mWeakAct = new WeakReference<>(act);
107
    int numObjects = RubikObjectList.getNumObjects();
108
    mScreenWidth = act.getScreenWidthInPixels();
109
    mScreenHeight= act.getScreenHeightInPixels();
110
    mUpperBarHeight = act.getHeightUpperBar();
111
    mLowerBarHeight = act.getHeightLowerBar();
112

    
113
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
114
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
115
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
116

    
117
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
118
    mColCount = NUM_COLUMNS;
119

    
120
    float titleSize = mScreenWidth*RubikActivity.TITLE_TEXT_SIZE;
121
    LayoutInflater inflater = act.getLayoutInflater();
122

    
123
    // TOP ////////////////////////////
124
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
125
    layoutTop.removeAllViews();
126
    TextView label = (TextView)inflater.inflate(R.layout.upper_text, null);
127
    label.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
128
    label.setText(R.string.app_name);
129
    layoutTop.addView(label);
130

    
131
    // BOTTOM /////////////////////////
132
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
133
    layoutBot.removeAllViews();
134
    RelativeLayout relLayout = (RelativeLayout)inflater.inflate(R.layout.play_bottom_bar, null);
135
    layoutBot.addView(relLayout);
136

    
137
    TypedValue outValue = new TypedValue();
138
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
139

    
140
    setupAboutButton(act,relLayout,outValue);
141
    setupUpdatesButtonAndBubble(act,relLayout,outValue);
142
    setupExitButton(act,relLayout,outValue);
143

    
144
    // POPUP //////////////////////////
145
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
146
    hiddenView.removeAllViews();
147

    
148
    ViewGroup.LayoutParams paramsMid = hiddenView.getLayoutParams();
149
    paramsMid.height = (int)(mScreenHeight-mUpperBarHeight-mLowerBarHeight);
150
    hiddenView.setLayoutParams(paramsMid);
151

    
152
    ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
153
    hiddenView.addView(scroll);
154

    
155
    setupScrollingObjects(act,scroll,mScreenWidth,mScreenHeight);
156
    }
157

    
158
//////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  private void setupAboutButton(final RubikActivity act, RelativeLayout relLayout, TypedValue value)
161
    {
162
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
163
    mAboutButton = relLayout.findViewById(R.id.buttonAbout);
164
    mAboutButton.setImageResource(icon);
165
    mAboutButton.setBackgroundResource(value.resourceId);
166

    
167
    mAboutButton.setOnClickListener( new View.OnClickListener()
168
      {
169
      @Override
170
      public void onClick(View v)
171
        {
172
        RubikDialogAbout aDiag = new RubikDialogAbout();
173
        aDiag.show(act.getSupportFragmentManager(), null);
174
        }
175
      });
176
    }
177

    
178
//////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout, TypedValue value)
181
    {
182
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
183
    mUpdatesButton = relLayout.findViewById(R.id.buttonDown);
184
    mUpdatesButton.setImageResource(icon);
185
    mUpdatesButton.setBackgroundResource(value.resourceId);
186

    
187
    mUpdatesButton.setOnClickListener( new View.OnClickListener()
188
      {
189
      @Override
190
      public void onClick(View v)
191
        {
192
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
193
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
194
        }
195
      });
196

    
197
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
198
    mBubbleUpdates.setVisibility(View.INVISIBLE);
199

    
200
    RubikNetwork network = RubikNetwork.getInstance();
201
    network.signUpForUpdates(this);
202
    }
203

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

    
206
  private void setupExitButton(final RubikActivity act, RelativeLayout relLayout, TypedValue value)
207
    {
208
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_exit,R.drawable.ui_medium_exit, R.drawable.ui_big_exit, R.drawable.ui_huge_exit);
209
    mExitButton = relLayout.findViewById(R.id.buttonExit);
210
    mExitButton.setImageResource(icon);
211
    mExitButton.setBackgroundResource(value.resourceId);
212

    
213
    mExitButton.setOnClickListener( new View.OnClickListener()
214
      {
215
      @Override
216
      public void onClick(View v)
217
        {
218
        act.finish();
219
        }
220
      });
221
    }
222

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

    
225
  private void setupScrollingObjects(final RubikActivity act, final ScrollView view, final float width, final float height)
226
    {
227
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
228
    int cubeWidth = (int)(width/NUM_COLUMNS) - 2*margin;
229

    
230
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
231
    mMaxRowCount = (int)((height-mUpperBarHeight-mLowerBarHeight)/mObjectSize);
232

    
233
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
234

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

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

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

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

    
253
    int numObjects = RubikObjectList.getNumObjects();
254

    
255
    for(int object=0; object<numObjects; object++)
256
      {
257
      final int ordinal = object;
258
      RubikObject rObject = RubikObjectList.getObject(ordinal);
259
      int row = object/NUM_COLUMNS;
260
      ImageButton button = new ImageButton(act);
261
      if( rObject!=null ) rObject.setIconTo(act,button);
262

    
263
      button.setOnClickListener( new View.OnClickListener()
264
        {
265
        @Override
266
        public void onClick(View v)
267
          {
268
          // TODO
269
          android.util.Log.e("D", "object "+ordinal+" clicked");
270
          }
271
        });
272

    
273
      GridLayout.Spec rowS = rowSpecs[row];
274
      GridLayout.Spec colS = colSpecs[nextInRow[row]];
275
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowS,colS);
276
      params.bottomMargin = margin;
277
      params.topMargin    = margin;
278
      params.leftMargin   = margin;
279
      params.rightMargin  = margin;
280

    
281
      params.width = cubeWidth;
282
      params.height= cubeWidth;
283

    
284
      nextInRow[row]++;
285

    
286
      objectGrid.addView(button, params);
287
      }
288
    }
289

    
290
//////////////////////////////////////////////////////////////////////////////////////////////////
291
/*
292
  private void setupObjectButton(final RubikActivity act, final float width)
293
    {
294
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
295
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
296
    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);
297
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
298
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
299

    
300
    mObjButton.setOnClickListener( new View.OnClickListener()
301
      {
302
      @Override
303
      public void onClick(View view)
304
        {
305
        if( mObjectPopup==null )
306
          {
307
          float width = act.getScreenWidthInPixels();
308
          float height= act.getScreenHeightInPixels();
309
          setupObjectWindow(act,width,height);
310
          }
311

    
312
        if( act.getControl().isUINotBlocked())
313
          {
314
          int rowCount = Math.min(mMaxRowCount,mRowCount);
315
          View popupView = mObjectPopup.getContentView();
316
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
317
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
318
          }
319
        }
320
      });
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

    
325
  private void setupPlayButton(final RubikActivity act, final float width)
326
    {
327
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
328

    
329
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
330

    
331
    mPlayButton.setOnClickListener( new View.OnClickListener()
332
      {
333
      @Override
334
      public void onClick(View view)
335
        {
336
        if( mPlayPopup==null )
337
          {
338
          float width = act.getScreenWidthInPixels();
339
          setupPlayWindow(act,width);
340
          }
341

    
342
        if( act.getControl().isUINotBlocked())
343
          {
344
          adjustSolvedIcons();
345
          float height= act.getScreenHeightInPixels();
346
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
347
          View popupView = mPlayPopup.getContentView();
348
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
349
          final int object  = RubikObjectList.getCurrObject();
350
          final int dbLevel = RubikObjectList.getDBLevel(object);
351
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
352
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
353
          final int realHeight = Math.min(popupHeight,maxHeight);
354
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
355
          }
356
        }
357
      });
358
    }
359

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

    
362
  private void setupMenuButton(final RubikActivity act, final float width)
363
    {
364
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
365
    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);
366
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
367
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
368

    
369
    mMenuButton.setOnClickListener( new View.OnClickListener()
370
      {
371
      @Override
372
      public void onClick(View view)
373
        {
374
        if( mMenuPopup==null )
375
          {
376
          float width = act.getScreenWidthInPixels();
377
          setupMenuWindow(act,width);
378
          }
379

    
380
        if( act.getControl().isUINotBlocked())
381
          {
382
          View popupView = mMenuPopup.getContentView();
383
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
384
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
385
          }
386
        }
387
      });
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
393
    {
394
    int cubeWidth = (int)(width/9);
395
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
396
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
397
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
398

    
399
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
400
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
401

    
402
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
403
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
404

    
405
    objectGrid.setColumnCount(mColCount);
406
    objectGrid.setRowCount(mRowCount);
407

    
408
    mObjectPopup = new PopupWindow(act);
409
    mObjectPopup.setFocusable(true);
410
    mObjectPopup.setContentView(view);
411

    
412
    int[] nextInRow = new int[mRowCount];
413

    
414
    for(int row=0; row<mRowCount; row++)
415
      {
416
      rowSpecs[row] = GridLayout.spec(row);
417
      nextInRow[row]= 0;
418
      }
419
    for(int col=0; col<mColCount; col++)
420
      {
421
      colSpecs[col] = GridLayout.spec(col);
422
      }
423

    
424
    int numObjects = RubikObjectList.getNumObjects();
425

    
426
    for(int object=0; object<numObjects; object++)
427
      {
428
      final int ordinal = object;
429
      final RubikObject rObject = RubikObjectList.getObject(object);
430
      int row = object/NUM_COLUMNS;
431
      ImageButton button = new ImageButton(act);
432
      if( rObject!=null ) rObject.setIconTo(act,button);
433

    
434
      button.setOnClickListener( new View.OnClickListener()
435
        {
436
        @Override
437
        public void onClick(View v)
438
          {
439
          // TODO
440
          }
441
        });
442

    
443
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
444
      params.bottomMargin = margin;
445
      params.topMargin    = margin;
446
      params.leftMargin   = margin;
447
      params.rightMargin  = margin;
448

    
449
      params.width = cubeWidth;
450
      params.height= cubeWidth;
451

    
452
      nextInRow[row]++;
453

    
454
      objectGrid.addView(button, params);
455
      }
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
461
    {
462
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
463
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
464
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
465

    
466
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
467
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
468
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
469

    
470
    buttonTut.setImageResource(iconT);
471
    buttonDow.setImageResource(iconD);
472
    buttonInf.setImageResource(iconI);
473

    
474
    Resources res = act.getResources();
475
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
476
    mBottomHeight = bd.getIntrinsicHeight();
477

    
478
    TypedValue outValue = new TypedValue();
479
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
480
    buttonTut.setBackgroundResource(outValue.resourceId);
481
    buttonDow.setBackgroundResource(outValue.resourceId);
482
    buttonInf.setBackgroundResource(outValue.resourceId);
483

    
484
    buttonTut.setOnClickListener( new View.OnClickListener()
485
      {
486
      @Override
487
      public void onClick(View v)
488
        {
489
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
490
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
491
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
492
        }
493
      });
494

    
495
    buttonDow.setOnClickListener( new View.OnClickListener()
496
      {
497
      @Override
498
      public void onClick(View v)
499
        {
500
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
501
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
502
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
503
        }
504
      });
505

    
506
    buttonInf.setOnClickListener( new View.OnClickListener()
507
      {
508
      @Override
509
      public void onClick(View v)
510
        {
511
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
512
        int currObject = RubikObjectList.getCurrObject();
513
        act.switchConfig(currObject);
514
        }
515
      });
516

    
517
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
518
    mBubbleUpdates.setVisibility(View.INVISIBLE);
519

    
520
    RubikNetwork network = RubikNetwork.getInstance();
521
    network.signUpForUpdates(this);
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
  private void setupMenuWindow(final RubikActivity act, final float width)
527
    {
528
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
529
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
530
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
531

    
532
    mMenuPopup = new PopupWindow(act);
533
    mMenuPopup.setContentView(layout);
534
    mMenuPopup.setFocusable(true);
535
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
536
    int padding = (int)(width*RubikActivity.PADDING);
537

    
538
    mMenuLayoutWidth = (int)(width/2);
539
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
540

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

    
543
    for(int i=0; i<NUM_BUTTONS; i++)
544
      {
545
      final int but = i;
546
      Button button = new Button(act);
547
      button.setLayoutParams(p);
548
      button.setText(BUTTON_LABELS[i]);
549
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
550

    
551
      button.setOnClickListener( new View.OnClickListener()
552
        {
553
        @Override
554
        public void onClick(View v)
555
          {
556
          mMenuPopup.dismiss();
557
          MenuAction(act,but);
558
          }
559
        });
560

    
561
      menuLayout.addView(button);
562
      }
563
    }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
  private void setupPlayWindow(final RubikActivity act, final float width)
568
    {
569
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
570
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
571
    mPlayLayout = layout.findViewById(R.id.playGrid);
572

    
573
    mPlayLayoutWidth = (int)(width*0.4f);
574

    
575
    mPlayPopup = new PopupWindow(act);
576
    mPlayPopup.setContentView(layout);
577
    mPlayPopup.setFocusable(true);
578

    
579
    adjustLevels(act);
580
    }
581

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

    
584
  private void MenuAction(RubikActivity act, int button)
585
    {
586
    switch(button)
587
      {
588
      case 0: Bundle sBundle = new Bundle();
589
              int currObject = RubikObjectList.getCurrObject();
590
              sBundle.putInt("tab", currObject );
591
              sBundle.putBoolean("submitting", false);
592
              RubikDialogScores scores = new RubikDialogScores();
593
              scores.setArguments(sBundle);
594
              scores.show(act.getSupportFragmentManager(), null);
595
              break;
596
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
597
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
598
              break;
599
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
600
              break;
601
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
602
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
603
              break;
604
      case 4: act.switchToBandagedCreator();
605
              break;
606
      case 5: RubikDialogAbout aDiag = new RubikDialogAbout();
607
              aDiag.show(act.getSupportFragmentManager(), null);
608
              break;
609
      }
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613

    
614
  void setupSolveButton(final RubikActivity act)
615
    {
616
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
617
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
618
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
619

    
620
    mSolveButton.setOnClickListener( new View.OnClickListener()
621
      {
622
      @Override
623
      public void onClick(View v)
624
        {
625
        act.getControl().solveObject();
626
        mMovesController.clearMoves(act);
627
        }
628
      });
629
    }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632

    
633
  private void setupScrambleButton(final RubikActivity act)
634
    {
635
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble_new,R.drawable.ui_medium_cube_scramble_new, R.drawable.ui_big_cube_scramble_new, R.drawable.ui_huge_cube_scramble_new);
636
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
637
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
638

    
639
    mScrambleButton.setOnClickListener( new View.OnClickListener()
640
      {
641
      @Override
642
      public void onClick(View v)
643
        {
644
        int currObject = RubikObjectList.getCurrObject();
645
        RubikObject object = RubikObjectList.getObject(currObject);
646
        int numScrambles = object==null ? 0 : object.getNumScramble();
647
        mShouldReactToEndOfScrambling = false;
648
        act.getControl().scrambleObject(numScrambles);
649
        }
650
      });
651
    }
652
*/
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654

    
655
  public void savePreferences(SharedPreferences.Editor editor)
656
    {
657
    editor.putInt("play_LevelValue", mLevelValue );
658
    }
659

    
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661

    
662
  public void restorePreferences(SharedPreferences preferences)
663
    {
664
    mLevelValue = preferences.getInt("play_LevelValue", 0);
665
    }
666

    
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668

    
669
  public void setCurrObject(RubikActivity act)
670
    {
671
    if( mPlayLayout!=null ) adjustLevels(act);
672
    }
673

    
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
676

    
677
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
678
    {
679
    View topLayout = act.findViewById(R.id.relativeLayout);
680
    boolean isFullScreen;
681

    
682
    if( topLayout!=null )
683
      {
684
      topLayout.getLocationOnScreen(mLocation);
685
      isFullScreen = (mLocation[1]==0);
686
      }
687
    else
688
      {
689
      isFullScreen = true;
690
      }
691

    
692
    try
693
      {
694
      // if on Android 11 or we are fullscreen
695
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
696
        {
697
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
698
        window.update(view, w, h);
699
        }
700
      else  // Android 10 or below in pop-up mode or split-screen mode
701
        {
702
        view.getLocationOnScreen(mLocation);
703
        int width  = view.getWidth();
704
        int height = view.getHeight();
705
        int x = mLocation[0]+(width-w)/2;
706
        int y = mLocation[1]+height+yoff;
707

    
708
        window.showAsDropDown(view);
709
        window.update(x,y,w,h);
710
        }
711
      }
712
    catch( IllegalArgumentException iae )
713
      {
714
      // ignore, this means window is 'not attached to window manager' -
715
      // which most probably is because we are already exiting the app.
716
      }
717
    }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
  private void adjustSolvedIcons()
722
    {
723
    if( mPlayLayout!=null )
724
      {
725
      int currObject = RubikObjectList.getCurrObject();
726
      int dbLevel = RubikObjectList.getDBLevel(currObject);
727
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
728
      RubikScores scores = RubikScores.getInstance();
729

    
730
      for(int i=0; i<numLevel; i++)
731
        {
732
        int level = i<numLevel-1 ? i+1 : dbLevel;
733
        Button button = (Button)mPlayLayout.getChildAt(i);
734
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
735
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
736
        }
737
      }
738
    }
739

    
740
///////////////////////////////////////////////////////////////////////////////////////////////////
741

    
742
  private void adjustLevels(final RubikActivity act)
743
    {
744
    int currObject = RubikObjectList.getCurrObject();
745
    int dbLevel = RubikObjectList.getDBLevel(currObject);
746
    RubikObject object = RubikObjectList.getObject(currObject);
747
    int numScrambles = object==null ? 0 : object.getNumScramble();
748
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
749
    String[] levels = new String[numLevel];
750

    
751
    for(int i=0; i<numLevel-1; i++)
752
      {
753
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
754
      }
755

    
756
    if( numLevel>0 )
757
      {
758
      levels[numLevel-1] = act.getString(R.string.level_full);
759
      }
760

    
761
    if( mLevelValue>dbLevel || mLevelValue<1 ||
762
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
763
      {
764
      mLevelValue=1;
765
      }
766

    
767
    float width  = act.getScreenWidthInPixels();
768
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
769
    int padding  = (int)(width*RubikActivity.PADDING);
770
    int butWidth = mPlayLayoutWidth - 2*padding;
771
    int butHeight= (int)mMenuItemSize;
772
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
773

    
774
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
775
    pM.setMargins(margin, 0, margin, margin);
776
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
777
    pT.setMargins(margin, margin, margin, margin);
778
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
779
    pB.setMargins(margin, margin, margin, 2*margin);
780

    
781
    mPlayLayout.removeAllViews();
782

    
783
    RubikScores scores = RubikScores.getInstance();
784

    
785
    for(int i=0; i<numLevel; i++)
786
      {
787
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
788
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
789
      Button button = new Button(act);
790
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
791
      button.setText(levels[i]);
792
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
793

    
794
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
795
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
796

    
797
      button.setOnClickListener( new View.OnClickListener()
798
        {
799
        @Override
800
        public void onClick(View v)
801
          {
802
          ObjectControl control = act.getControl();
803

    
804
          if(control.isUINotBlocked())
805
            {
806
         //   if( mPlayPopup!=null ) mPlayPopup.dismiss();
807
            mLevelValue = level;
808
            mShouldReactToEndOfScrambling = true;
809
            control.scrambleObject(scrambles);
810
            }
811
          }
812
        });
813

    
814
      mPlayLayout.addView(button);
815
      }
816
    }
817

    
818
///////////////////////////////////////////////////////////////////////////////////////////////////
819

    
820
  public int getLevel()
821
    {
822
    return mLevelValue;
823
    }
824

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826

    
827
  public void recreatePopup()
828
    {
829
    int numObjects = RubikObjectList.getNumObjects();
830
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
831
    mColCount = NUM_COLUMNS;
832

    
833
    RubikActivity act = mWeakAct.get();
834
    LayoutInflater inflater = act.getLayoutInflater();
835

    
836
    act.runOnUiThread(new Runnable()
837
      {
838
      @Override
839
      public void run()
840
        {
841
        LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
842
        hiddenView.removeAllViews();
843
        ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
844
        hiddenView.addView(scroll);
845
        setupScrollingObjects(act,scroll,mScreenWidth,mScreenHeight);
846

    
847
        mCurrentBubbleNumber--;
848
        updateNumberOfNewObjects();
849
        }
850
      });
851
    }
852

    
853
///////////////////////////////////////////////////////////////////////////////////////////////////
854

    
855
  public boolean shouldReactToEndOfScrambling()
856
    {
857
    return mShouldReactToEndOfScrambling;
858
    }
859

    
860
///////////////////////////////////////////////////////////////////////////////////////////////////
861

    
862
  private void updateNumberOfNewObjects()
863
    {
864
    if( mCurrentBubbleNumber>0 )
865
      {
866
      String shownNum = String.valueOf(mCurrentBubbleNumber);
867
      mBubbleUpdates.setText(shownNum);
868
      mBubbleUpdates.setVisibility(View.VISIBLE);
869
      int height = (int)(0.05f*mScreenWidth);
870
      mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
871
      }
872
    else
873
      {
874
      mBubbleUpdates.setVisibility(View.INVISIBLE);
875
      }
876
    }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879

    
880
  public void receiveUpdate(RubikUpdates updates)
881
    {
882
    Activity act = mWeakAct.get();
883

    
884
    if( act!=null )
885
      {
886
      mCurrentBubbleNumber = updates.getCompletedNumber();
887

    
888
      act.runOnUiThread(new Runnable()
889
        {
890
        @Override
891
        public void run()
892
          {
893
          updateNumberOfNewObjects();
894
          }
895
        });
896
      }
897
    }
898

    
899
///////////////////////////////////////////////////////////////////////////////////////////////////
900

    
901
  public void errorUpdate()
902
    {
903
    android.util.Log.e("D", "Screen: Error receiving update");
904
    }
905
  }
(7-7/12)