Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 5a4ee169

1 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 3782bd44 Leszek Koltunski
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10 fcd5b990 Leszek Koltunski
package org.distorted.screens;
11 211b48f2 Leszek Koltunski
12 179f7189 Leszek Koltunski
import java.lang.ref.WeakReference;
13
14
import android.app.Activity;
15 4c0cd600 Leszek Koltunski
import android.content.Context;
16 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
17 be65535e Leszek Koltunski
import android.content.res.ColorStateList;
18 8a47642f Leszek Koltunski
import android.content.res.Resources;
19 255492a0 Leszek Koltunski
import android.os.Build;
20 e03e0352 Leszek Koltunski
import android.os.Bundle;
21 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
22 255492a0 Leszek Koltunski
import android.view.Gravity;
23 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
24
import android.view.View;
25
import android.widget.Button;
26 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
27 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
30 d6e7c7fb Leszek Koltunski
import android.widget.RelativeLayout;
31 10373dc7 Leszek Koltunski
import android.widget.TextView;
32 211b48f2 Leszek Koltunski
33 5a4ee169 leszek
import org.distorted.dialogs.RubikDialogCreators;
34 93a1c364 Leszek Koltunski
import org.distorted.dialogs.RubikDialogSolvers;
35 6b034729 Leszek Koltunski
import org.distorted.dialogs.RubikDialogStarsStatus;
36 5f3801d3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogUpdates;
37 e71baee1 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
38 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
39 1088a219 Leszek Koltunski
import org.distorted.external.RubikScores;
40 acabdd83 Leszek Koltunski
import org.distorted.external.RubikUpdates;
41 3f7a4363 Leszek Koltunski
42 432a5f2c Leszek Koltunski
import org.distorted.helpers.PopupCreator;
43 3f7a4363 Leszek Koltunski
import org.distorted.main.R;
44
import org.distorted.main.RubikActivity;
45 a8576d91 Leszek Koltunski
import org.distorted.dialogs.RubikDialogPattern;
46 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogScores;
47 234a7582 Leszek Koltunski
import org.distorted.dialogs.RubikDialogTutorial;
48 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
49 b1629e16 Leszek Koltunski
import org.distorted.objectlib.effects.BaseEffect;
50 4f3e2d90 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
51 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
52
import org.distorted.objects.RubikObjectList;
53 211b48f2 Leszek Koltunski
54 6b034729 Leszek Koltunski
import static android.view.View.GONE;
55 b600ccd9 Leszek Koltunski
import static android.view.View.inflate;
56 6b034729 Leszek Koltunski
import static org.distorted.main.RubikActivity.USE_IAP;
57 88d28110 Leszek Koltunski
58 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 63dd19c4 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
61 211b48f2 Leszek Koltunski
  {
62 cc0fc0c5 Leszek Koltunski
  private static final float MENU_BUTTON_HEIGHT  = 0.068f;
63 a0aff1b4 Leszek Koltunski
  private static final float MENU_TEXT_SIZE      = 0.024f;
64 24cd23dd Leszek Koltunski
  private static final float MENU_MARGIN         = 0.008f;
65 09bcbc45 Leszek Koltunski
  private static final float MENU_WIDTH          = 0.650f;
66
  private static final float LEVEL_BUTTON_HEIGHT = 0.089f;
67 24cd23dd Leszek Koltunski
68 6e3fcb91 Leszek Koltunski
  public static final int NUM_COLUMNS  = 5;
69 adbd16d9 Leszek Koltunski
  public static final int LEVELS_SHOWN = 8;
70 dc78f395 Leszek Koltunski
  private static final int[] mLocation = new int[2];
71 e03e0352 Leszek Koltunski
72 756f152c Leszek Koltunski
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
73 4f3e2d90 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup;
74
  private WeakReference<RubikActivity> mWeakAct;
75 10373dc7 Leszek Koltunski
  private TextView mBubbleUpdates;
76 1088a219 Leszek Koltunski
  private Button[] mLevel;
77 369a0fce Leszek Koltunski
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mMenuButtonHeight, mMenuTextSize;
78 85b09df4 Leszek Koltunski
  private int mLevelValue;
79 598de3ee Leszek Koltunski
  private int mColCount, mRowCount, mMaxRowCount;
80 255492a0 Leszek Koltunski
  private int mUpperBarHeight;
81 dd874ae8 Leszek Koltunski
  private boolean mShouldReactToEndOfScrambling;
82 9dfb553f Leszek Koltunski
  private float mScreenWidth, mScreenHeight;
83 24cd23dd Leszek Koltunski
  private int mLevelHeight, mLevelWidth;
84 2011391d Leszek Koltunski
  private int mOldNumScramble;
85 255492a0 Leszek Koltunski
86 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
89 211b48f2 Leszek Koltunski
    {
90 85b09df4 Leszek Koltunski
91 211b48f2 Leszek Koltunski
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
96 211b48f2 Leszek Koltunski
    {
97 179f7189 Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
98 9dfb553f Leszek Koltunski
    mScreenWidth  = act.getScreenWidthInPixels();
99
    mScreenHeight = act.getScreenHeightInPixels();
100 255492a0 Leszek Koltunski
    mUpperBarHeight = act.getHeightUpperBar();
101 a8576d91 Leszek Koltunski
102 24cd23dd Leszek Koltunski
    mMenuButtonHeight = (int)(mScreenHeight*MENU_BUTTON_HEIGHT);
103
    mMenuTextSize     = (int)(mScreenHeight*MENU_TEXT_SIZE);
104 e3c74c0f Leszek Koltunski
105 ac1900c3 Leszek Koltunski
    mObjectPopup = null;
106 e07c48a2 Leszek Koltunski
107 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
108 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
109 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
110 85b09df4 Leszek Koltunski
111 c1df05fa Leszek Koltunski
    setupSolveButton(act);
112
    layoutTop.addView(mSolveButton);
113 24cd23dd Leszek Koltunski
    setupMenuButton(act,mScreenWidth, mScreenHeight);
114 0254cfd7 Leszek Koltunski
    layoutTop.addView(mMenuButton);
115 756f152c Leszek Koltunski
    setupScrambleButton(act);
116 c1df05fa Leszek Koltunski
    layoutTop.addView(mScrambleButton);
117
118
    // BOTTOM /////////////////////////
119
    setupObjectButton(act,mScreenWidth);
120 c95b37a4 Leszek Koltunski
    createBottomPane(act,mObjButton);
121 769d7b9f Leszek Koltunski
    }
122 211b48f2 Leszek Koltunski
123 8ab435b9 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
124 4c0cd600 Leszek Koltunski
125 ad0c8e0e Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float width)
126 769d7b9f Leszek Koltunski
    {
127 c651024f Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
128 e07c48a2 Leszek Koltunski
    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);
129 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
130
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
131 769d7b9f Leszek Koltunski
132
    mObjButton.setOnClickListener( new View.OnClickListener()
133
      {
134
      @Override
135
      public void onClick(View view)
136 4c0cd600 Leszek Koltunski
        {
137 c95b37a4 Leszek Koltunski
        if( act.getControl().isScramblingAndSolvingNotBlocked() )
138 2da68298 Leszek Koltunski
          {
139 c95b37a4 Leszek Koltunski
          if( mObjectPopup==null )
140
            {
141
            float width = act.getScreenWidthInPixels();
142
            float height= act.getScreenHeightInPixels();
143
            setupObjectWindow(act,width,height);
144
            }
145
146
          int rowCount = Math.min(mMaxRowCount,mRowCount);
147
          View popupView = mObjectPopup.getContentView();
148
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
149
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
150 2da68298 Leszek Koltunski
          }
151 769d7b9f Leszek Koltunski
        }
152
      });
153
    }
154 4c0cd600 Leszek Koltunski
155 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 24cd23dd Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float width, final float height)
158 e31abc1e Leszek Koltunski
    {
159 0254cfd7 Leszek Koltunski
    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);
160 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
161
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
162 e31abc1e Leszek Koltunski
163 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
164 e31abc1e Leszek Koltunski
      {
165
      @Override
166
      public void onClick(View view)
167
        {
168 c95b37a4 Leszek Koltunski
        if( act.getControl().isScramblingAndSolvingNotBlocked() )
169 2da68298 Leszek Koltunski
          {
170 c95b37a4 Leszek Koltunski
          if( mMenuPopup==null )
171
            {
172
            float width = act.getScreenWidthInPixels();
173
            setupMenuWindow(act,width,height);
174
            }
175
176
          View popupView = mMenuPopup.getContentView();
177
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
178
          setupLevelButtonVisibilityAndColor(act);
179
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-mMenuLayoutWidth/2 + width/6),0);
180 2da68298 Leszek Koltunski
          }
181 e31abc1e Leszek Koltunski
        }
182
      });
183
    }
184
185 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187 598de3ee Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
188 4c0cd600 Leszek Koltunski
    {
189 fad10885 Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
190
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
191 ac1900c3 Leszek Koltunski
    mColCount = NUM_COLUMNS;
192
193 27401bea Leszek Koltunski
    int cubeSize = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
194
    int margin   = (int)(height*RubikActivity.POPUP_MARGIN);
195
    int padding  = (int)(height*RubikActivity.POPUP_PADDING);
196 adbd16d9 Leszek Koltunski
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
197 0b162619 Leszek Koltunski
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
198 598de3ee Leszek Koltunski
199 8d1da3f1 Leszek Koltunski
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
200 b600ccd9 Leszek Koltunski
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
201 d6e7c7fb Leszek Koltunski
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
202
    setupBottomLayout(act,bottomLayout);
203 0b162619 Leszek Koltunski
204 a88b947f Leszek Koltunski
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
205 769d7b9f Leszek Koltunski
206 432a5f2c Leszek Koltunski
    for(int child=0; child<numObjects; child++)
207 769d7b9f Leszek Koltunski
      {
208 302c76a0 Leszek Koltunski
      final RubikObject obj = RubikObjectList.getObject(child);
209
      View v = objectGrid.getChildAt(child);
210
      ImageButton button = PopupCreator.getButton(obj,v);
211 432a5f2c Leszek Koltunski
      final int ordinal = child;
212 314e9ff0 Leszek Koltunski
213 7ac0ee88 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
214 769d7b9f Leszek Koltunski
        {
215 7ac0ee88 Leszek Koltunski
        @Override
216
        public void onClick(View v)
217 769d7b9f Leszek Koltunski
          {
218 919b830e Leszek Koltunski
          if( obj!=null && act.getControl().isScramblingAndSolvingNotBlocked() && ScreenList.getCurrentScreen()==ScreenList.PLAY )
219 769d7b9f Leszek Koltunski
            {
220 c7238c67 Leszek Koltunski
            if( obj.isFree() )
221
              {
222
              RubikObjectList.setCurrObject(ordinal);
223
              act.changeObject(ordinal,true);
224 2011391d Leszek Koltunski
              if( mMenuPopup!=null ) setupLevelButtonVisibilityAndColor(act);
225 c7238c67 Leszek Koltunski
              mMovesController.clearMoves(act);
226
              }
227
            else
228
              {
229
              act.switchToPurchase(ordinal);
230
              }
231 769d7b9f Leszek Koltunski
            }
232
233 1f3bc259 Leszek Koltunski
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
234 7ac0ee88 Leszek Koltunski
          }
235
        });
236 769d7b9f Leszek Koltunski
      }
237 432a5f2c Leszek Koltunski
238
    mObjectPopup = new PopupWindow(act);
239
    mObjectPopup.setFocusable(true);
240
    mObjectPopup.setContentView(view);
241 4c0cd600 Leszek Koltunski
    }
242
243 8d1da3f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245 d6e7c7fb Leszek Koltunski
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
246 8d1da3f1 Leszek Koltunski
    {
247
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
248 d6e7c7fb Leszek Koltunski
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
249
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
250 8d1da3f1 Leszek Koltunski
251
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
252 d6e7c7fb Leszek Koltunski
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
253
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
254 8d1da3f1 Leszek Koltunski
255
    buttonTut.setImageResource(iconT);
256 d6e7c7fb Leszek Koltunski
    buttonDow.setImageResource(iconD);
257
    buttonInf.setImageResource(iconI);
258 8d1da3f1 Leszek Koltunski
259
    TypedValue outValue = new TypedValue();
260
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
261
    buttonTut.setBackgroundResource(outValue.resourceId);
262 d6e7c7fb Leszek Koltunski
    buttonDow.setBackgroundResource(outValue.resourceId);
263
    buttonInf.setBackgroundResource(outValue.resourceId);
264 8d1da3f1 Leszek Koltunski
265
    buttonTut.setOnClickListener( new View.OnClickListener()
266
      {
267
      @Override
268
      public void onClick(View v)
269
        {
270
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
271
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
272
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
273
        }
274
      });
275
276 d6e7c7fb Leszek Koltunski
    buttonDow.setOnClickListener( new View.OnClickListener()
277
      {
278
      @Override
279
      public void onClick(View v)
280
        {
281 5f3801d3 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
282
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
283
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
284 d6e7c7fb Leszek Koltunski
        }
285
      });
286
287
    buttonInf.setOnClickListener( new View.OnClickListener()
288 8d1da3f1 Leszek Koltunski
      {
289
      @Override
290
      public void onClick(View v)
291
        {
292 f12e4de9 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
293 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
294
        act.switchConfig(currObject);
295 8d1da3f1 Leszek Koltunski
        }
296
      });
297 d6e7c7fb Leszek Koltunski
298 10373dc7 Leszek Koltunski
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
299
    mBubbleUpdates.setVisibility(View.INVISIBLE);
300
301 d6e7c7fb Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
302
    network.signUpForUpdates(this);
303 8d1da3f1 Leszek Koltunski
    }
304
305 c1df05fa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
  void setupSolveButton(final RubikActivity act)
308
    {
309
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve,R.drawable.ui_medium_cube_solve, R.drawable.ui_big_cube_solve, R.drawable.ui_huge_cube_solve);
310
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
311
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
312
313
    mSolveButton.setOnClickListener( new View.OnClickListener()
314
      {
315
      @Override
316
      public void onClick(View v)
317
        {
318
        act.getControl().solveObject();
319
        mMovesController.clearMoves(act);
320
        }
321
      });
322
    }
323
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326
  private void setupScrambleButton(final RubikActivity act)
327
    {
328
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_scramble,R.drawable.ui_medium_cube_scramble, R.drawable.ui_big_cube_scramble, R.drawable.ui_huge_cube_scramble);
329
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
330
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
331
332
    mScrambleButton.setOnClickListener( new View.OnClickListener()
333
      {
334
      @Override
335
      public void onClick(View v)
336
        {
337 b1629e16 Leszek Koltunski
        int duration = BaseEffect.Type.FAST_SCRAMBLE.getDuration();
338 0c768d69 Leszek Koltunski
        boolean success = act.getControl().fastScrambleObject(duration,RubikObject.FAST_SCRAMBLES);
339
        if( success ) mShouldReactToEndOfScrambling = false;
340 c1df05fa Leszek Koltunski
        }
341
      });
342
    }
343
344 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
345
346 24cd23dd Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width, final float height)
347 e03e0352 Leszek Koltunski
    {
348
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
349 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
350 e03e0352 Leszek Koltunski
351
    mMenuPopup = new PopupWindow(act);
352
    mMenuPopup.setContentView(layout);
353
    mMenuPopup.setFocusable(true);
354 24cd23dd Leszek Koltunski
    int padding = (int)(height*MENU_MARGIN);
355 6b034729 Leszek Koltunski
    int numButtons = USE_IAP ? 7 : 6;
356 09bcbc45 Leszek Koltunski
    mLevelHeight = (int)(height*LEVEL_BUTTON_HEIGHT);
357 24cd23dd Leszek Koltunski
    mMenuLayoutHeight= padding + numButtons*(mMenuButtonHeight+padding) + mLevelHeight/3 + 3*(mLevelHeight+padding);
358 4279106d Leszek Koltunski
    mMenuLayoutWidth = Math.min((int)(width*MENU_WIDTH),mMenuLayoutHeight/2);
359
    mLevelWidth = (mMenuLayoutWidth-4*padding)/3;
360 e03e0352 Leszek Koltunski
361 0b458a90 Leszek Koltunski
    layout.setPadding(padding,0,padding,0);
362 e03e0352 Leszek Koltunski
363 6b034729 Leszek Koltunski
    if( USE_IAP )
364
      {
365
      Button stars = layout.findViewById(R.id.menuStars);
366
      stars.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
367
      stars.setOnClickListener( new View.OnClickListener()
368
          {
369
          @Override
370
          public void onClick(View v)
371
            {
372
            mMenuPopup.dismiss();
373
            RubikDialogStarsStatus d = new RubikDialogStarsStatus();
374
            d.show(act.getSupportFragmentManager(), null);
375
            }
376
          });
377
      }
378
    else
379
      {
380
      Button stars = layout.findViewById(R.id.menuStars);
381
      stars.setVisibility(GONE);
382
      }
383
384 93a1c364 Leszek Koltunski
    Button solver = layout.findViewById(R.id.menuSolver);
385
    solver.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
386
    solver.setOnClickListener( new View.OnClickListener()
387
        {
388
        @Override
389
        public void onClick(View v)
390
          {
391
          mMenuPopup.dismiss();
392
          RubikDialogSolvers solvers = new RubikDialogSolvers();
393
          solvers.show(act.getSupportFragmentManager(), RubikDialogSolvers.getDialogTag() );
394
          }
395
        });
396
397 0b458a90 Leszek Koltunski
    Button highScores = layout.findViewById(R.id.menuHighScores);
398 369a0fce Leszek Koltunski
    highScores.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
399 0b458a90 Leszek Koltunski
    highScores.setOnClickListener( new View.OnClickListener()
400 e03e0352 Leszek Koltunski
        {
401
        @Override
402
        public void onClick(View v)
403
          {
404
          mMenuPopup.dismiss();
405 0b458a90 Leszek Koltunski
          Bundle sBundle = new Bundle();
406 c02fa107 Leszek Koltunski
          sBundle.putString("argument", "false");
407 0b458a90 Leszek Koltunski
          RubikDialogScores scores = new RubikDialogScores();
408
          scores.setArguments(sBundle);
409
          scores.show(act.getSupportFragmentManager(), null);
410 e03e0352 Leszek Koltunski
          }
411
        });
412
413 0b458a90 Leszek Koltunski
    Button prettyPatterns = layout.findViewById(R.id.menuPrettyPatterns);
414 369a0fce Leszek Koltunski
    prettyPatterns.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
415 0b458a90 Leszek Koltunski
    prettyPatterns.setOnClickListener( new View.OnClickListener()
416
        {
417
        @Override
418
        public void onClick(View v)
419
          {
420
          mMenuPopup.dismiss();
421
          RubikDialogPattern pDiag = new RubikDialogPattern();
422
          pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
423
          }
424
        });
425 0254cfd7 Leszek Koltunski
426 0b458a90 Leszek Koltunski
    Button tutorials = layout.findViewById(R.id.menuTutorials);
427 369a0fce Leszek Koltunski
    tutorials.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
428 0b458a90 Leszek Koltunski
    tutorials.setOnClickListener( new View.OnClickListener()
429
        {
430
        @Override
431
        public void onClick(View v)
432
          {
433
          mMenuPopup.dismiss();
434
          RubikDialogTutorial tDiag = new RubikDialogTutorial();
435
          tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
436
          }
437
        });
438 e03e0352 Leszek Koltunski
439 fad10885 Leszek Koltunski
    Button bandaged = layout.findViewById(R.id.menuBandaged);
440
    bandaged.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
441
    bandaged.setOnClickListener( new View.OnClickListener()
442
        {
443
        @Override
444
        public void onClick(View v)
445
          {
446
          mMenuPopup.dismiss();
447 5a4ee169 leszek
          RubikDialogCreators creators = new RubikDialogCreators();
448
          creators.show(act.getSupportFragmentManager(), RubikDialogCreators.getDialogTag() );
449 fad10885 Leszek Koltunski
          }
450
        });
451
452 34d6b123 Leszek Koltunski
    Button whatsNew = layout.findViewById(R.id.menuNew);
453
    whatsNew.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
454
    whatsNew.setOnClickListener( new View.OnClickListener()
455 0b458a90 Leszek Koltunski
        {
456
        @Override
457
        public void onClick(View v)
458
          {
459
          mMenuPopup.dismiss();
460 e71baee1 Leszek Koltunski
          RubikDialogAbout aDiag = new RubikDialogAbout();
461 0b458a90 Leszek Koltunski
          aDiag.show(act.getSupportFragmentManager(), null);
462
          }
463
        });
464 4f3e2d90 Leszek Koltunski
465 369a0fce Leszek Koltunski
    TextView levels = layout.findViewById(R.id.menuLevels);
466
    levels.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
467
468 09bcbc45 Leszek Koltunski
    setupLevelButtons(act,layout,padding/2);
469 4f3e2d90 Leszek Koltunski
    }
470
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472
473 09bcbc45 Leszek Koltunski
  private void setupLevelButtons(RubikActivity act, View layout, int margin)
474 4f3e2d90 Leszek Koltunski
    {
475 24cd23dd Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mLevelWidth,mLevelHeight);
476 09bcbc45 Leszek Koltunski
    params.setMargins(margin,margin,margin,margin);
477 4f3e2d90 Leszek Koltunski
478 79422ed8 Leszek Koltunski
    mOldNumScramble = LEVELS_SHOWN+1;
479
480 1088a219 Leszek Koltunski
    mLevel = new Button[LEVELS_SHOWN+1];
481 4f3e2d90 Leszek Koltunski
482 1088a219 Leszek Koltunski
    mLevel[0] = layout.findViewById(R.id.level1);
483
    mLevel[1] = layout.findViewById(R.id.level2);
484
    mLevel[2] = layout.findViewById(R.id.level3);
485
    mLevel[3] = layout.findViewById(R.id.level4);
486
    mLevel[4] = layout.findViewById(R.id.level5);
487
    mLevel[5] = layout.findViewById(R.id.level6);
488
    mLevel[6] = layout.findViewById(R.id.level7);
489
    mLevel[7] = layout.findViewById(R.id.level8);
490
    mLevel[8] = layout.findViewById(R.id.levelM);
491 4f3e2d90 Leszek Koltunski
492 1088a219 Leszek Koltunski
    for(int i=0; i<=LEVELS_SHOWN; i++)
493
      {
494
      final int ii = i;
495
      mLevel[i].setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
496
      mLevel[i].setLayoutParams(params);
497
      mLevel[i].setOnClickListener( new View.OnClickListener()
498 4f3e2d90 Leszek Koltunski
        {
499
        @Override
500
        public void onClick(View v)
501
          {
502
          ObjectControl control = act.getControl();
503
504 919b830e Leszek Koltunski
          if( control.isScramblingAndSolvingNotBlocked() )
505 4f3e2d90 Leszek Koltunski
            {
506
            if( mMenuPopup!=null ) mMenuPopup.dismiss();
507
508
            int currObject = RubikObjectList.getCurrObject();
509
            RubikObject object = RubikObjectList.getObject(currObject);
510 1088a219 Leszek Koltunski
            final int scrambles = ii<LEVELS_SHOWN ? ii+1 : (object==null ? 0 : object.getNumScramble());
511 0c768d69 Leszek Koltunski
512
            boolean success = control.scrambleObject(scrambles);
513
            if( success )
514
              {
515
              mLevelValue = ii+1;
516
              mShouldReactToEndOfScrambling = true;
517
              }
518 4f3e2d90 Leszek Koltunski
            }
519
          }
520
        });
521 1088a219 Leszek Koltunski
      }
522
    }
523
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
526 2011391d Leszek Koltunski
  private void setupLevelButtonVisibilityAndColor(RubikActivity act)
527 1088a219 Leszek Koltunski
    {
528
    int currObject = RubikObjectList.getCurrObject();
529 2011391d Leszek Koltunski
    RubikObject object = RubikObjectList.getObject(currObject);
530
    int numScramble = object==null ? 1 : object.getNumScramble();
531 1088a219 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
532 8a47642f Leszek Koltunski
    Resources res = act.getResources();
533 be65535e Leszek Koltunski
    ColorStateList colorG = ColorStateList.valueOf(res.getColor(R.color.green));
534
    ColorStateList colorD = ColorStateList.valueOf(res.getColor(R.color.dark_grey));
535 1088a219 Leszek Koltunski
536 83018ac4 Leszek Koltunski
    for(int level=0; level<=LEVELS_SHOWN; level++)
537 1088a219 Leszek Koltunski
      {
538
      boolean isSolved = scores.isSolved(currObject,level);
539 83018ac4 Leszek Koltunski
      mLevel[level].setBackgroundTintList( isSolved ? colorG : colorD);
540 1088a219 Leszek Koltunski
      }
541 2011391d Leszek Koltunski
542
    if( numScramble<=LEVELS_SHOWN || mOldNumScramble<=LEVELS_SHOWN )
543
      {
544
      if( numScramble<mOldNumScramble )
545
        {
546
        int max = Math.min(LEVELS_SHOWN,mOldNumScramble-1);
547
        for(int level=numScramble; level<=max; level++) mLevel[level].setVisibility(View.INVISIBLE);
548
        mLevel[numScramble-1].setText(R.string.levelM);
549
        }
550
      if( numScramble>mOldNumScramble )
551
        {
552
        int max = Math.min(LEVELS_SHOWN,numScramble-1);
553
        mLevel[mOldNumScramble-1].setText( String.valueOf(mOldNumScramble) );
554
555
        for(int level=mOldNumScramble; level<=max; level++)
556
          {
557
          mLevel[level].setVisibility(View.VISIBLE);
558
          if( level<max ) mLevel[level].setText( String.valueOf(level+1) );
559
          else            mLevel[level].setText( R.string.levelM );
560
          }
561
        }
562
      }
563
564
    mOldNumScramble = numScramble;
565 e03e0352 Leszek Koltunski
    }
566
567 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
568 3398a606 Leszek Koltunski
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
569
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
570
// going to be called)
571
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
572 211b48f2 Leszek Koltunski
573
  public void savePreferences(SharedPreferences.Editor editor)
574
    {
575 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
576
577 3398a606 Leszek Koltunski
    if( mObjectPopup!=null )
578
      {
579
      mObjectPopup.dismiss();
580
      mObjectPopup = null;
581
      }
582 87d86e5f Leszek Koltunski
583 3398a606 Leszek Koltunski
    if( mMenuPopup!=null )
584
      {
585
      mMenuPopup.dismiss();
586
      mMenuPopup = null;
587
      }
588 69b66386 Leszek Koltunski
    }
589
590 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
591
592
  public void restorePreferences(SharedPreferences preferences)
593
    {
594 1088a219 Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
595 7b7d65ce Leszek Koltunski
    }
596
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
599
600
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
601
    {
602
    View topLayout = act.findViewById(R.id.relativeLayout);
603 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
604 00aa398a Leszek Koltunski
605
    if( topLayout!=null )
606
      {
607
      topLayout.getLocationOnScreen(mLocation);
608 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
609 00aa398a Leszek Koltunski
      }
610
    else
611
      {
612 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
613 00aa398a Leszek Koltunski
      }
614 255492a0 Leszek Koltunski
615 69a271f3 Leszek Koltunski
    try
616 255492a0 Leszek Koltunski
      {
617 69a271f3 Leszek Koltunski
      // if on Android 11 or we are fullscreen
618
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
619
        {
620
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
621
        window.update(view, w, h);
622
        }
623
      else  // Android 10 or below in pop-up mode or split-screen mode
624
        {
625
        view.getLocationOnScreen(mLocation);
626
        int width  = view.getWidth();
627
        int height = view.getHeight();
628
        int x = mLocation[0]+(width-w)/2;
629
        int y = mLocation[1]+height+yoff;
630
631
        window.showAsDropDown(view);
632
        window.update(x,y,w,h);
633
        }
634 255492a0 Leszek Koltunski
      }
635 69a271f3 Leszek Koltunski
    catch( IllegalArgumentException iae )
636 255492a0 Leszek Koltunski
      {
637 69a271f3 Leszek Koltunski
      // ignore, this means window is 'not attached to window manager' -
638
      // which most probably is because we are already exiting the app.
639 255492a0 Leszek Koltunski
      }
640
    }
641
642 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
643
644 dca3888a Leszek Koltunski
  public int getLevel()
645 211b48f2 Leszek Koltunski
    {
646 85b09df4 Leszek Koltunski
    return mLevelValue;
647 211b48f2 Leszek Koltunski
    }
648
649 84d746d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
650
651
  public void recreatePopup()
652
    {
653
    mObjectPopup = null;
654
    }
655
656 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
657
658
  public boolean shouldReactToEndOfScrambling()
659
    {
660
    return mShouldReactToEndOfScrambling;
661
    }
662 63dd19c4 Leszek Koltunski
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664
665
  public void receiveUpdate(RubikUpdates updates)
666
    {
667 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
668 10373dc7 Leszek Koltunski
669 179f7189 Leszek Koltunski
    if( act!=null )
670 903c7bbc Leszek Koltunski
      {
671 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
672
        {
673
        @Override
674
        public void run()
675
          {
676 2c9ab085 Leszek Koltunski
          int num = updates.getCompletedNumber();
677 179f7189 Leszek Koltunski
678
          if( num>0 )
679
            {
680
            String shownNum = String.valueOf(num);
681
            mBubbleUpdates.setText(shownNum);
682
            mBubbleUpdates.setVisibility(View.VISIBLE);
683
            int height = (int)(0.05f*mScreenWidth);
684
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
685
            }
686
         else
687
            {
688
            mBubbleUpdates.setVisibility(View.INVISIBLE);
689
            }
690
          }
691
        });
692 903c7bbc Leszek Koltunski
      }
693 63dd19c4 Leszek Koltunski
    }
694
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696
697
  public void errorUpdate()
698
    {
699 5305fdc8 Leszek Koltunski
    android.util.Log.e("D", "RubikScreenPlay: Error receiving downloaded objects update");
700 63dd19c4 Leszek Koltunski
    }
701 211b48f2 Leszek Koltunski
  }