Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 113d9d67

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

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

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

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

    
132
    // BOTTOM /////////////////////////
133
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
134
    layoutBot.removeAllViews();
135

    
136
    float D = 0.20f;
137
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( (int)(mScreenWidth*     D ),LinearLayout.LayoutParams.MATCH_PARENT);
138
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( (int)(mScreenWidth*(1-2*D)),LinearLayout.LayoutParams.MATCH_PARENT);
139

    
140
    LinearLayout layoutLeft = new LinearLayout(act);
141

    
142
    layoutLeft.setLayoutParams(params1);
143
    RelativeLayout layoutMid = (RelativeLayout)inflater.inflate(R.layout.play_bottom_bar, null);
144
    layoutMid.setLayoutParams(params2);
145
    LinearLayout layoutRight = new LinearLayout(act);
146
    layoutRight.setLayoutParams(params1);
147

    
148
    setupAboutButton(act);
149
    setupUpdatesButtonAndBubble(act,layoutMid);
150
    setupExitButton(act);
151

    
152
    layoutLeft.addView(mAboutButton);
153
    layoutRight.addView(mExitButton);
154

    
155
    layoutBot.addView(layoutLeft);
156
    layoutBot.addView(layoutMid);
157
    layoutBot.addView(layoutRight);
158

    
159
    // POPUP //////////////////////////
160
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
161
    hiddenView.removeAllViews();
162

    
163
    ViewGroup.LayoutParams paramsMid = hiddenView.getLayoutParams();
164
    paramsMid.height = (int)(mScreenHeight-mUpperBarHeight-mLowerBarHeight);
165
    hiddenView.setLayoutParams(paramsMid);
166

    
167
    ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
168
    hiddenView.addView(scroll);
169

    
170
    setupScrollingObjects(act,scroll,mScreenWidth,mScreenHeight);
171
    }
172

    
173
//////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private void setupAboutButton(final RubikActivity act)
176
    {
177
    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);
178
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
179
    mAboutButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
180

    
181
    mAboutButton.setOnClickListener( new View.OnClickListener()
182
      {
183
      @Override
184
      public void onClick(View v)
185
        {
186
        RubikDialogAbout aDiag = new RubikDialogAbout();
187
        aDiag.show(act.getSupportFragmentManager(), null);
188
        }
189
      });
190
    }
191

    
192
//////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private void setupExitButton(final RubikActivity act)
195
    {
196
    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);
197
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
198
    mExitButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
199

    
200
    mExitButton.setOnClickListener( new View.OnClickListener()
201
      {
202
      @Override
203
      public void onClick(View v)
204
        {
205
        act.finish();
206
        }
207
      });
208
    }
209

    
210
//////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout)
213
    {
214
    TypedValue value = new TypedValue();
215
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, value, true);
216
    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);
217
    mUpdatesButton = relLayout.findViewById(R.id.buttonUpdates);
218
    mUpdatesButton.setImageResource(icon);
219
    mUpdatesButton.setBackgroundResource(value.resourceId);
220

    
221
    mUpdatesButton.setOnClickListener( new View.OnClickListener()
222
      {
223
      @Override
224
      public void onClick(View v)
225
        {
226
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
227
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
228
        }
229
      });
230

    
231
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
232
    mBubbleUpdates.setVisibility(View.INVISIBLE);
233

    
234
    RubikNetwork network = RubikNetwork.getInstance();
235
    network.signUpForUpdates(this);
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  private void setupScrollingObjects(final RubikActivity act, final ScrollView view, final float width, final float height)
241
    {
242
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
243
    int cubeWidth = (int)(width/NUM_COLUMNS) - 2*margin;
244

    
245
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
246
    mMaxRowCount = (int)((height-mUpperBarHeight-mLowerBarHeight)/mObjectSize);
247

    
248
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
249

    
250
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
251
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
252

    
253
    objectGrid.setColumnCount(mColCount);
254
    objectGrid.setRowCount(mRowCount);
255

    
256
    int[] nextInRow = new int[mRowCount];
257

    
258
    for(int row=0; row<mRowCount; row++)
259
      {
260
      rowSpecs[row] = GridLayout.spec(row);
261
      nextInRow[row]= 0;
262
      }
263
    for(int col=0; col<mColCount; col++)
264
      {
265
      colSpecs[col] = GridLayout.spec(col);
266
      }
267

    
268
    int numObjects = RubikObjectList.getNumObjects();
269

    
270
    for(int object=0; object<numObjects; object++)
271
      {
272
      final int ordinal = object;
273
      RubikObject rObject = RubikObjectList.getObject(ordinal);
274
      int row = object/NUM_COLUMNS;
275
      ImageButton button = new ImageButton(act);
276
      if( rObject!=null ) rObject.setIconTo(act,button);
277

    
278
      button.setOnClickListener( new View.OnClickListener()
279
        {
280
        @Override
281
        public void onClick(View v)
282
          {
283
          // TODO
284
          android.util.Log.e("D", "object "+ordinal+" clicked");
285
          }
286
        });
287

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

    
296
      params.width = cubeWidth;
297
      params.height= cubeWidth;
298

    
299
      nextInRow[row]++;
300

    
301
      objectGrid.addView(button, params);
302
      }
303
    }
304

    
305
//////////////////////////////////////////////////////////////////////////////////////////////////
306
/*
307
  private void setupObjectButton(final RubikActivity act, final float width)
308
    {
309
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
310
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
311
    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);
312
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
313
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
314

    
315
    mObjButton.setOnClickListener( new View.OnClickListener()
316
      {
317
      @Override
318
      public void onClick(View view)
319
        {
320
        if( mObjectPopup==null )
321
          {
322
          float width = act.getScreenWidthInPixels();
323
          float height= act.getScreenHeightInPixels();
324
          setupObjectWindow(act,width,height);
325
          }
326

    
327
        if( act.getControl().isUINotBlocked())
328
          {
329
          int rowCount = Math.min(mMaxRowCount,mRowCount);
330
          View popupView = mObjectPopup.getContentView();
331
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
332
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
333
          }
334
        }
335
      });
336
    }
337

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

    
340
  private void setupPlayButton(final RubikActivity act, final float width)
341
    {
342
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
343

    
344
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
345

    
346
    mPlayButton.setOnClickListener( new View.OnClickListener()
347
      {
348
      @Override
349
      public void onClick(View view)
350
        {
351
        if( mPlayPopup==null )
352
          {
353
          float width = act.getScreenWidthInPixels();
354
          setupPlayWindow(act,width);
355
          }
356

    
357
        if( act.getControl().isUINotBlocked())
358
          {
359
          adjustSolvedIcons();
360
          float height= act.getScreenHeightInPixels();
361
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
362
          View popupView = mPlayPopup.getContentView();
363
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
364
          final int object  = RubikObjectList.getCurrObject();
365
          final int dbLevel = RubikObjectList.getDBLevel(object);
366
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
367
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
368
          final int realHeight = Math.min(popupHeight,maxHeight);
369
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
370
          }
371
        }
372
      });
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  private void setupMenuButton(final RubikActivity act, final float width)
378
    {
379
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
380
    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);
381
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
382
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
383

    
384
    mMenuButton.setOnClickListener( new View.OnClickListener()
385
      {
386
      @Override
387
      public void onClick(View view)
388
        {
389
        if( mMenuPopup==null )
390
          {
391
          float width = act.getScreenWidthInPixels();
392
          setupMenuWindow(act,width);
393
          }
394

    
395
        if( act.getControl().isUINotBlocked())
396
          {
397
          View popupView = mMenuPopup.getContentView();
398
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
399
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
400
          }
401
        }
402
      });
403
    }
404

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

    
407
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
408
    {
409
    int cubeWidth = (int)(width/9);
410
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
411
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
412
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
413

    
414
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
415
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
416

    
417
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
418
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
419

    
420
    objectGrid.setColumnCount(mColCount);
421
    objectGrid.setRowCount(mRowCount);
422

    
423
    mObjectPopup = new PopupWindow(act);
424
    mObjectPopup.setFocusable(true);
425
    mObjectPopup.setContentView(view);
426

    
427
    int[] nextInRow = new int[mRowCount];
428

    
429
    for(int row=0; row<mRowCount; row++)
430
      {
431
      rowSpecs[row] = GridLayout.spec(row);
432
      nextInRow[row]= 0;
433
      }
434
    for(int col=0; col<mColCount; col++)
435
      {
436
      colSpecs[col] = GridLayout.spec(col);
437
      }
438

    
439
    int numObjects = RubikObjectList.getNumObjects();
440

    
441
    for(int object=0; object<numObjects; object++)
442
      {
443
      final int ordinal = object;
444
      final RubikObject rObject = RubikObjectList.getObject(object);
445
      int row = object/NUM_COLUMNS;
446
      ImageButton button = new ImageButton(act);
447
      if( rObject!=null ) rObject.setIconTo(act,button);
448

    
449
      button.setOnClickListener( new View.OnClickListener()
450
        {
451
        @Override
452
        public void onClick(View v)
453
          {
454
          // TODO
455
          }
456
        });
457

    
458
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
459
      params.bottomMargin = margin;
460
      params.topMargin    = margin;
461
      params.leftMargin   = margin;
462
      params.rightMargin  = margin;
463

    
464
      params.width = cubeWidth;
465
      params.height= cubeWidth;
466

    
467
      nextInRow[row]++;
468

    
469
      objectGrid.addView(button, params);
470
      }
471
    }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
476
    {
477
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
478
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
479
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
480

    
481
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
482
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
483
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
484

    
485
    buttonTut.setImageResource(iconT);
486
    buttonDow.setImageResource(iconD);
487
    buttonInf.setImageResource(iconI);
488

    
489
    Resources res = act.getResources();
490
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
491
    mBottomHeight = bd.getIntrinsicHeight();
492

    
493
    TypedValue outValue = new TypedValue();
494
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
495
    buttonTut.setBackgroundResource(outValue.resourceId);
496
    buttonDow.setBackgroundResource(outValue.resourceId);
497
    buttonInf.setBackgroundResource(outValue.resourceId);
498

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

    
510
    buttonDow.setOnClickListener( new View.OnClickListener()
511
      {
512
      @Override
513
      public void onClick(View v)
514
        {
515
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
516
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
517
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
518
        }
519
      });
520

    
521
    buttonInf.setOnClickListener( new View.OnClickListener()
522
      {
523
      @Override
524
      public void onClick(View v)
525
        {
526
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
527
        int currObject = RubikObjectList.getCurrObject();
528
        act.switchConfig(currObject);
529
        }
530
      });
531

    
532
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
533
    mBubbleUpdates.setVisibility(View.INVISIBLE);
534

    
535
    RubikNetwork network = RubikNetwork.getInstance();
536
    network.signUpForUpdates(this);
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
  private void setupMenuWindow(final RubikActivity act, final float width)
542
    {
543
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
544
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
545
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
546

    
547
    mMenuPopup = new PopupWindow(act);
548
    mMenuPopup.setContentView(layout);
549
    mMenuPopup.setFocusable(true);
550
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
551
    int padding = (int)(width*RubikActivity.PADDING);
552

    
553
    mMenuLayoutWidth = (int)(width/2);
554
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
555

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

    
558
    for(int i=0; i<NUM_BUTTONS; i++)
559
      {
560
      final int but = i;
561
      Button button = new Button(act);
562
      button.setLayoutParams(p);
563
      button.setText(BUTTON_LABELS[i]);
564
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
565

    
566
      button.setOnClickListener( new View.OnClickListener()
567
        {
568
        @Override
569
        public void onClick(View v)
570
          {
571
          mMenuPopup.dismiss();
572
          MenuAction(act,but);
573
          }
574
        });
575

    
576
      menuLayout.addView(button);
577
      }
578
    }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
  private void setupPlayWindow(final RubikActivity act, final float width)
583
    {
584
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
585
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
586
    mPlayLayout = layout.findViewById(R.id.playGrid);
587

    
588
    mPlayLayoutWidth = (int)(width*0.4f);
589

    
590
    mPlayPopup = new PopupWindow(act);
591
    mPlayPopup.setContentView(layout);
592
    mPlayPopup.setFocusable(true);
593

    
594
    adjustLevels(act);
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
  private void MenuAction(RubikActivity act, int button)
600
    {
601
    switch(button)
602
      {
603
      case 0: Bundle sBundle = new Bundle();
604
              int currObject = RubikObjectList.getCurrObject();
605
              sBundle.putInt("tab", currObject );
606
              sBundle.putBoolean("submitting", false);
607
              RubikDialogScores scores = new RubikDialogScores();
608
              scores.setArguments(sBundle);
609
              scores.show(act.getSupportFragmentManager(), null);
610
              break;
611
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
612
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
613
              break;
614
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
615
              break;
616
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
617
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
618
              break;
619
      case 4: act.switchToBandagedCreator();
620
              break;
621
      case 5: RubikDialogAbout aDiag = new RubikDialogAbout();
622
              aDiag.show(act.getSupportFragmentManager(), null);
623
              break;
624
      }
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628

    
629
  void setupSolveButton(final RubikActivity act)
630
    {
631
    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);
632
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
633
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
634

    
635
    mSolveButton.setOnClickListener( new View.OnClickListener()
636
      {
637
      @Override
638
      public void onClick(View v)
639
        {
640
        act.getControl().solveObject();
641
        mMovesController.clearMoves(act);
642
        }
643
      });
644
    }
645

    
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647

    
648
  private void setupScrambleButton(final RubikActivity act)
649
    {
650
    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);
651
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
652
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
653

    
654
    mScrambleButton.setOnClickListener( new View.OnClickListener()
655
      {
656
      @Override
657
      public void onClick(View v)
658
        {
659
        int currObject = RubikObjectList.getCurrObject();
660
        RubikObject object = RubikObjectList.getObject(currObject);
661
        int numScrambles = object==null ? 0 : object.getNumScramble();
662
        mShouldReactToEndOfScrambling = false;
663
        act.getControl().scrambleObject(numScrambles);
664
        }
665
      });
666
    }
667
*/
668
///////////////////////////////////////////////////////////////////////////////////////////////////
669

    
670
  public void savePreferences(SharedPreferences.Editor editor)
671
    {
672
    editor.putInt("play_LevelValue", mLevelValue );
673
    }
674

    
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676

    
677
  public void restorePreferences(SharedPreferences preferences)
678
    {
679
    mLevelValue = preferences.getInt("play_LevelValue", 0);
680
    }
681

    
682
///////////////////////////////////////////////////////////////////////////////////////////////////
683

    
684
  public void setCurrObject(RubikActivity act)
685
    {
686
    if( mPlayLayout!=null ) adjustLevels(act);
687
    }
688

    
689
///////////////////////////////////////////////////////////////////////////////////////////////////
690
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
691

    
692
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
693
    {
694
    View topLayout = act.findViewById(R.id.relativeLayout);
695
    boolean isFullScreen;
696

    
697
    if( topLayout!=null )
698
      {
699
      topLayout.getLocationOnScreen(mLocation);
700
      isFullScreen = (mLocation[1]==0);
701
      }
702
    else
703
      {
704
      isFullScreen = true;
705
      }
706

    
707
    try
708
      {
709
      // if on Android 11 or we are fullscreen
710
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
711
        {
712
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
713
        window.update(view, w, h);
714
        }
715
      else  // Android 10 or below in pop-up mode or split-screen mode
716
        {
717
        view.getLocationOnScreen(mLocation);
718
        int width  = view.getWidth();
719
        int height = view.getHeight();
720
        int x = mLocation[0]+(width-w)/2;
721
        int y = mLocation[1]+height+yoff;
722

    
723
        window.showAsDropDown(view);
724
        window.update(x,y,w,h);
725
        }
726
      }
727
    catch( IllegalArgumentException iae )
728
      {
729
      // ignore, this means window is 'not attached to window manager' -
730
      // which most probably is because we are already exiting the app.
731
      }
732
    }
733

    
734
///////////////////////////////////////////////////////////////////////////////////////////////////
735

    
736
  private void adjustSolvedIcons()
737
    {
738
    if( mPlayLayout!=null )
739
      {
740
      int currObject = RubikObjectList.getCurrObject();
741
      int dbLevel = RubikObjectList.getDBLevel(currObject);
742
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
743
      RubikScores scores = RubikScores.getInstance();
744

    
745
      for(int i=0; i<numLevel; i++)
746
        {
747
        int level = i<numLevel-1 ? i+1 : dbLevel;
748
        Button button = (Button)mPlayLayout.getChildAt(i);
749
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
750
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
751
        }
752
      }
753
    }
754

    
755
///////////////////////////////////////////////////////////////////////////////////////////////////
756

    
757
  private void adjustLevels(final RubikActivity act)
758
    {
759
    int currObject = RubikObjectList.getCurrObject();
760
    int dbLevel = RubikObjectList.getDBLevel(currObject);
761
    RubikObject object = RubikObjectList.getObject(currObject);
762
    int numScrambles = object==null ? 0 : object.getNumScramble();
763
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
764
    String[] levels = new String[numLevel];
765

    
766
    for(int i=0; i<numLevel-1; i++)
767
      {
768
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
769
      }
770

    
771
    if( numLevel>0 )
772
      {
773
      levels[numLevel-1] = act.getString(R.string.level_full);
774
      }
775

    
776
    if( mLevelValue>dbLevel || mLevelValue<1 ||
777
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
778
      {
779
      mLevelValue=1;
780
      }
781

    
782
    float width  = act.getScreenWidthInPixels();
783
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
784
    int padding  = (int)(width*RubikActivity.PADDING);
785
    int butWidth = mPlayLayoutWidth - 2*padding;
786
    int butHeight= (int)mMenuItemSize;
787
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
788

    
789
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
790
    pM.setMargins(margin, 0, margin, margin);
791
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
792
    pT.setMargins(margin, margin, margin, margin);
793
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
794
    pB.setMargins(margin, margin, margin, 2*margin);
795

    
796
    mPlayLayout.removeAllViews();
797

    
798
    RubikScores scores = RubikScores.getInstance();
799

    
800
    for(int i=0; i<numLevel; i++)
801
      {
802
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
803
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
804
      Button button = new Button(act);
805
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
806
      button.setText(levels[i]);
807
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
808

    
809
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
810
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
811

    
812
      button.setOnClickListener( new View.OnClickListener()
813
        {
814
        @Override
815
        public void onClick(View v)
816
          {
817
          ObjectControl control = act.getControl();
818

    
819
          if(control.isUINotBlocked())
820
            {
821
         //   if( mPlayPopup!=null ) mPlayPopup.dismiss();
822
            mLevelValue = level;
823
            mShouldReactToEndOfScrambling = true;
824
            control.scrambleObject(scrambles);
825
            }
826
          }
827
        });
828

    
829
      mPlayLayout.addView(button);
830
      }
831
    }
832

    
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834

    
835
  public int getLevel()
836
    {
837
    return mLevelValue;
838
    }
839

    
840
///////////////////////////////////////////////////////////////////////////////////////////////////
841

    
842
  public void recreatePopup()
843
    {
844
    int numObjects = RubikObjectList.getNumObjects();
845
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
846
    mColCount = NUM_COLUMNS;
847

    
848
    RubikActivity act = mWeakAct.get();
849
    LayoutInflater inflater = act.getLayoutInflater();
850

    
851
    act.runOnUiThread(new Runnable()
852
      {
853
      @Override
854
      public void run()
855
        {
856
        LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
857
        hiddenView.removeAllViews();
858
        ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
859
        hiddenView.addView(scroll);
860
        setupScrollingObjects(act,scroll,mScreenWidth,mScreenHeight);
861

    
862
        mCurrentBubbleNumber--;
863
        updateNumberOfNewObjects();
864
        }
865
      });
866
    }
867

    
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869

    
870
  public boolean shouldReactToEndOfScrambling()
871
    {
872
    return mShouldReactToEndOfScrambling;
873
    }
874

    
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876

    
877
  private void updateNumberOfNewObjects()
878
    {
879
    if( mCurrentBubbleNumber>0 )
880
      {
881
      String shownNum = String.valueOf(mCurrentBubbleNumber);
882
      mBubbleUpdates.setText(shownNum);
883
      mBubbleUpdates.setVisibility(View.VISIBLE);
884
      int height = (int)(0.05f*mScreenWidth);
885
      mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
886
      }
887
    else
888
      {
889
      mBubbleUpdates.setVisibility(View.INVISIBLE);
890
      }
891
    }
892

    
893
///////////////////////////////////////////////////////////////////////////////////////////////////
894

    
895
  public void receiveUpdate(RubikUpdates updates)
896
    {
897
    Activity act = mWeakAct.get();
898

    
899
    if( act!=null )
900
      {
901
      mCurrentBubbleNumber = updates.getCompletedNumber();
902

    
903
      act.runOnUiThread(new Runnable()
904
        {
905
        @Override
906
        public void run()
907
          {
908
          updateNumberOfNewObjects();
909
          }
910
        });
911
      }
912
    }
913

    
914
///////////////////////////////////////////////////////////////////////////////////////////////////
915

    
916
  public void errorUpdate()
917
    {
918
    android.util.Log.e("D", "Screen: Error receiving update");
919
    }
920
  }
(7-7/12)