Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 85038b84

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
    //relLayout.setBackgroundColor(Color.parseColor("#000000"));
136
    layoutBot.addView(relLayout);
137

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

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

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

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

    
153
label.setText("s:"+mScreenHeight+" u:"+mUpperBarHeight+" l:"+mLowerBarHeight);
154

    
155
    ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
156
    hiddenView.addView(scroll);
157

    
158
    setupScrollingObjects(act,scroll,mScreenWidth,mScreenHeight);
159
    }
160

    
161
//////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void setupAboutButton(final RubikActivity act, RelativeLayout relLayout, TypedValue value)
164
    {
165
    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);
166
    mAboutButton = relLayout.findViewById(R.id.buttonAbout);
167
    mAboutButton.setImageResource(icon);
168
    mAboutButton.setBackgroundResource(value.resourceId);
169

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

    
181
//////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout, TypedValue value)
184
    {
185
    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);
186
    mUpdatesButton = relLayout.findViewById(R.id.buttonDown);
187
    mUpdatesButton.setImageResource(icon);
188
    mUpdatesButton.setBackgroundResource(value.resourceId);
189

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

    
200
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
201
    mBubbleUpdates.setVisibility(View.INVISIBLE);
202

    
203
    RubikNetwork network = RubikNetwork.getInstance();
204
    network.signUpForUpdates(this);
205
    }
206

    
207
//////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  private void setupExitButton(final RubikActivity act, RelativeLayout relLayout, TypedValue value)
210
    {
211
    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);
212
    mExitButton = relLayout.findViewById(R.id.buttonExit);
213
    mExitButton.setImageResource(icon);
214
    mExitButton.setBackgroundResource(value.resourceId);
215

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

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

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

    
233
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
234
    mMaxRowCount = (int)((height-mUpperBarHeight-mLowerBarHeight)/mObjectSize);
235

    
236
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
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
    int numObjects = RubikObjectList.getNumObjects();
257

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

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

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

    
284
      params.width = cubeWidth;
285
      params.height= cubeWidth;
286

    
287
      nextInRow[row]++;
288

    
289
      objectGrid.addView(button, params);
290
      }
291
    }
292

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

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

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

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  private void setupPlayButton(final RubikActivity act, final float width)
329
    {
330
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
331

    
332
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
333

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

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

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

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

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

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

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

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

    
402
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
403
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
404

    
405
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
406
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
407

    
408
    objectGrid.setColumnCount(mColCount);
409
    objectGrid.setRowCount(mRowCount);
410

    
411
    mObjectPopup = new PopupWindow(act);
412
    mObjectPopup.setFocusable(true);
413
    mObjectPopup.setContentView(view);
414

    
415
    int[] nextInRow = new int[mRowCount];
416

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

    
427
    int numObjects = RubikObjectList.getNumObjects();
428

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

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

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

    
452
      params.width = cubeWidth;
453
      params.height= cubeWidth;
454

    
455
      nextInRow[row]++;
456

    
457
      objectGrid.addView(button, params);
458
      }
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

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

    
469
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
470
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
471
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
472

    
473
    buttonTut.setImageResource(iconT);
474
    buttonDow.setImageResource(iconD);
475
    buttonInf.setImageResource(iconI);
476

    
477
    Resources res = act.getResources();
478
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
479
    mBottomHeight = bd.getIntrinsicHeight();
480

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

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

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

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

    
520
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
521
    mBubbleUpdates.setVisibility(View.INVISIBLE);
522

    
523
    RubikNetwork network = RubikNetwork.getInstance();
524
    network.signUpForUpdates(this);
525
    }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

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

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

    
541
    mMenuLayoutWidth = (int)(width/2);
542
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
543

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

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

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

    
564
      menuLayout.addView(button);
565
      }
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569

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

    
576
    mPlayLayoutWidth = (int)(width*0.4f);
577

    
578
    mPlayPopup = new PopupWindow(act);
579
    mPlayPopup.setContentView(layout);
580
    mPlayPopup.setFocusable(true);
581

    
582
    adjustLevels(act);
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

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

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616

    
617
  void setupSolveButton(final RubikActivity act)
618
    {
619
    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);
620
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
621
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
622

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

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635

    
636
  private void setupScrambleButton(final RubikActivity act)
637
    {
638
    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);
639
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
640
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
641

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

    
658
  public void savePreferences(SharedPreferences.Editor editor)
659
    {
660
    editor.putInt("play_LevelValue", mLevelValue );
661
    }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664

    
665
  public void restorePreferences(SharedPreferences preferences)
666
    {
667
    mLevelValue = preferences.getInt("play_LevelValue", 0);
668
    }
669

    
670
///////////////////////////////////////////////////////////////////////////////////////////////////
671

    
672
  public void setCurrObject(RubikActivity act)
673
    {
674
    if( mPlayLayout!=null ) adjustLevels(act);
675
    }
676

    
677
///////////////////////////////////////////////////////////////////////////////////////////////////
678
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
679

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

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

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

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

    
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723

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

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

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744

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

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

    
759
    if( numLevel>0 )
760
      {
761
      levels[numLevel-1] = act.getString(R.string.level_full);
762
      }
763

    
764
    if( mLevelValue>dbLevel || mLevelValue<1 ||
765
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
766
      {
767
      mLevelValue=1;
768
      }
769

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

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

    
784
    mPlayLayout.removeAllViews();
785

    
786
    RubikScores scores = RubikScores.getInstance();
787

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

    
797
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
798
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
799

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

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

    
817
      mPlayLayout.addView(button);
818
      }
819
    }
820

    
821
///////////////////////////////////////////////////////////////////////////////////////////////////
822

    
823
  public int getLevel()
824
    {
825
    return mLevelValue;
826
    }
827

    
828
///////////////////////////////////////////////////////////////////////////////////////////////////
829

    
830
  public void recreatePopup()
831
    {
832
    int numObjects = RubikObjectList.getNumObjects();
833
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
834
    mColCount = NUM_COLUMNS;
835

    
836
    RubikActivity act = mWeakAct.get();
837
    LayoutInflater inflater = act.getLayoutInflater();
838

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

    
850
        mCurrentBubbleNumber--;
851
        updateNumberOfNewObjects();
852
        }
853
      });
854
    }
855

    
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857

    
858
  public boolean shouldReactToEndOfScrambling()
859
    {
860
    return mShouldReactToEndOfScrambling;
861
    }
862

    
863
///////////////////////////////////////////////////////////////////////////////////////////////////
864

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

    
881
///////////////////////////////////////////////////////////////////////////////////////////////////
882

    
883
  public void receiveUpdate(RubikUpdates updates)
884
    {
885
    Activity act = mWeakAct.get();
886

    
887
    if( act!=null )
888
      {
889
      mCurrentBubbleNumber = updates.getCompletedNumber();
890

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

    
902
///////////////////////////////////////////////////////////////////////////////////////////////////
903

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