Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ dd874ae8

1 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fcd5b990 Leszek Koltunski
package org.distorted.screens;
21 211b48f2 Leszek Koltunski
22 4c0cd600 Leszek Koltunski
import android.content.Context;
23 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
24 4c0cd600 Leszek Koltunski
import android.graphics.drawable.BitmapDrawable;
25 255492a0 Leszek Koltunski
import android.os.Build;
26 e03e0352 Leszek Koltunski
import android.os.Bundle;
27 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
28 255492a0 Leszek Koltunski
import android.view.Gravity;
29 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
30
import android.view.View;
31
import android.widget.Button;
32 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
33 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
36 598de3ee Leszek Koltunski
import android.widget.ScrollView;
37 211b48f2 Leszek Koltunski
38 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
39 318c0a7d Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
40 3f7a4363 Leszek Koltunski
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
44 a8576d91 Leszek Koltunski
import org.distorted.dialogs.RubikDialogPattern;
45 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogScores;
46 234a7582 Leszek Koltunski
import org.distorted.dialogs.RubikDialogTutorial;
47 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentButton;
48
import org.distorted.helpers.TransparentImageButton;
49 6a083c6a Leszek Koltunski
import org.distorted.network.RubikScores;
50 211b48f2 Leszek Koltunski
51 88d28110 Leszek Koltunski
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
52
53 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 fcd5b990 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase
56 211b48f2 Leszek Koltunski
  {
57 287e91a6 Leszek Koltunski
  public static final int NUM_COLUMNS  = 4;
58 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
59 8ab435b9 Leszek Koltunski
  public static int MAX_LEVEL;
60
  public static final ObjectType DEF_OBJECT= ObjectType.CUBE_3;
61 211b48f2 Leszek Koltunski
62 7cf2637d Leszek Koltunski
  private static final int[] BUTTON_LABELS = { R.string.scores,
63
                                               R.string.patterns,
64
                                               R.string.solver,
65
                                               R.string.tutorials,
66
                                               R.string.about };
67
68 e03e0352 Leszek Koltunski
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
69 de62316a Leszek Koltunski
  private static final float LAST_BUTTON = 1.5f;
70 dc78f395 Leszek Koltunski
  private static final int[] mLocation = new int[2];
71 e03e0352 Leszek Koltunski
72 dd874ae8 Leszek Koltunski
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
73 15846fe4 Leszek Koltunski
  private Button mPlayButton;
74 0254cfd7 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
75 8ab435b9 Leszek Koltunski
  private ObjectType mObject = DEF_OBJECT;
76 0254cfd7 Leszek Koltunski
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
77 85b09df4 Leszek Koltunski
  private int mLevelValue;
78 e07c48a2 Leszek Koltunski
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
79 598de3ee Leszek Koltunski
  private int mColCount, mRowCount, mMaxRowCount;
80 0254cfd7 Leszek Koltunski
  private LinearLayout mPlayLayout;
81 255492a0 Leszek Koltunski
  private int mUpperBarHeight;
82 dd874ae8 Leszek Koltunski
  private boolean mShouldReactToEndOfScrambling;
83 255492a0 Leszek Koltunski
84 8ab435b9 Leszek Koltunski
  static
85
    {
86
    ObjectType[] types = ObjectType.values();
87
    int max = Integer.MIN_VALUE;
88
89
    for (ObjectType type : types)
90
      {
91
      int cur = getDBLevel(type);
92
      if( cur>max ) max = cur;
93
      }
94
95
    MAX_LEVEL = max;
96
    }
97
98 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
101 211b48f2 Leszek Koltunski
    {
102 85b09df4 Leszek Koltunski
103 211b48f2 Leszek Koltunski
    }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
108 211b48f2 Leszek Koltunski
    {
109 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
110 255492a0 Leszek Koltunski
    mUpperBarHeight = act.getHeightUpperBar();
111 a8576d91 Leszek Koltunski
112 eb376d3a Leszek Koltunski
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
113 88fb92ba Leszek Koltunski
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
114
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
115 e3c74c0f Leszek Koltunski
116 88d28110 Leszek Koltunski
    mRowCount = (NUM_OBJECTS + NUM_COLUMNS-1) / NUM_COLUMNS;
117 287e91a6 Leszek Koltunski
    mColCount = NUM_COLUMNS;
118 e07c48a2 Leszek Koltunski
119 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
120 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
121 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
122 85b09df4 Leszek Koltunski
123 ad0c8e0e Leszek Koltunski
    setupObjectButton(act,width);
124 85b09df4 Leszek Koltunski
    layoutTop.addView(mObjButton);
125 0254cfd7 Leszek Koltunski
126
    setupMenuButton(act,width);
127
    layoutTop.addView(mMenuButton);
128
129 2da68298 Leszek Koltunski
    setupPlayButton(act,width);
130 85b09df4 Leszek Koltunski
    layoutTop.addView(mPlayButton);
131 211b48f2 Leszek Koltunski
132 dd874ae8 Leszek Koltunski
    setupSolveButton(act);
133
    setupScrambleButton(act);
134
    createBottomPane(act,mSolveButton,mScrambleButton);
135 769d7b9f Leszek Koltunski
    }
136 211b48f2 Leszek Koltunski
137 8ab435b9 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
138 4c0cd600 Leszek Koltunski
139 ad0c8e0e Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float width)
140 769d7b9f Leszek Koltunski
    {
141 e07c48a2 Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.MARGIN);
142
    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);
143 dc78f395 Leszek Koltunski
144 dd874ae8 Leszek Koltunski
    mObjButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_MIDDLE);
145 769d7b9f Leszek Koltunski
146
    mObjButton.setOnClickListener( new View.OnClickListener()
147
      {
148
      @Override
149
      public void onClick(View view)
150 4c0cd600 Leszek Koltunski
        {
151 2da68298 Leszek Koltunski
        if( mObjectPopup==null )
152
          {
153
          float width = act.getScreenWidthInPixels();
154
          float height= act.getScreenHeightInPixels();
155
          setupObjectWindow(act,width,height);
156
          }
157
158
        if( act.getControl().isUINotBlocked())
159 a42e25a6 Leszek Koltunski
          {
160 598de3ee Leszek Koltunski
          int rowCount = Math.min(mMaxRowCount,mRowCount);
161 c5b4af4a Leszek Koltunski
          View popupView = mObjectPopup.getContentView();
162
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
163 255492a0 Leszek Koltunski
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount,margin,margin);
164 c5b4af4a Leszek Koltunski
          }
165 769d7b9f Leszek Koltunski
        }
166
      });
167
    }
168 4c0cd600 Leszek Koltunski
169 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 2da68298 Leszek Koltunski
  private void setupPlayButton(final RubikActivity act, final float width)
172 85b09df4 Leszek Koltunski
    {
173 2da68298 Leszek Koltunski
    final int margin = (int)(width*RubikActivity.MARGIN);
174 d90c55cc Leszek Koltunski
175 da768c35 Leszek Koltunski
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
176 85b09df4 Leszek Koltunski
177
    mPlayButton.setOnClickListener( new View.OnClickListener()
178
      {
179
      @Override
180 0254cfd7 Leszek Koltunski
      public void onClick(View view)
181 85b09df4 Leszek Koltunski
        {
182 2da68298 Leszek Koltunski
         if( mPlayPopup==null )
183
          {
184
          float width = act.getScreenWidthInPixels();
185
          setupPlayWindow(act,width);
186
          }
187
188
        if( act.getControl().isUINotBlocked())
189 6e194411 Leszek Koltunski
          {
190 2da68298 Leszek Koltunski
          float height= act.getScreenHeightInPixels();
191
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
192 c5b4af4a Leszek Koltunski
          View popupView = mPlayPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194 8ab435b9 Leszek Koltunski
          final int dbLevel = getDBLevel(mObject);
195 0a7aa15b Leszek Koltunski
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
196 d90c55cc Leszek Koltunski
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
197 255492a0 Leszek Koltunski
          final int realHeight = Math.min(popupHeight,maxHeight);
198
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
199 6fad862b Leszek Koltunski
          }
200 85b09df4 Leszek Koltunski
        }
201
      });
202
    }
203
204 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206 ad0c8e0e Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float width)
207 e31abc1e Leszek Koltunski
    {
208 2da68298 Leszek Koltunski
    final int margin = (int)(width*RubikActivity.MARGIN);
209 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);
210 dc78f395 Leszek Koltunski
211 dd874ae8 Leszek Koltunski
    mMenuButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_MIDDLE);
212 e31abc1e Leszek Koltunski
213 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
214 e31abc1e Leszek Koltunski
      {
215
      @Override
216
      public void onClick(View view)
217
        {
218 2da68298 Leszek Koltunski
        if( mMenuPopup==null )
219
          {
220
          float width = act.getScreenWidthInPixels();
221
          setupMenuWindow(act,width);
222
          }
223
224
        if( act.getControl().isUINotBlocked())
225 e03e0352 Leszek Koltunski
          {
226 c5b4af4a Leszek Koltunski
          View popupView = mMenuPopup.getContentView();
227
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
228 255492a0 Leszek Koltunski
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
229 6fad862b Leszek Koltunski
          }
230 e31abc1e Leszek Koltunski
        }
231
      });
232
    }
233
234 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236 598de3ee Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
237 4c0cd600 Leszek Koltunski
    {
238 d4415163 Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.cube_2s,R.drawable.cube_2m, R.drawable.cube_2b, R.drawable.cube_2h);
239 598de3ee Leszek Koltunski
240
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
241
    int cubeWidth = bd.getIntrinsicWidth();
242
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
243
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
244 255492a0 Leszek Koltunski
    mMaxRowCount = (int)(0.9f*(height-mUpperBarHeight)/mObjectSize);
245 598de3ee Leszek Koltunski
    GridLayout objectGrid = new GridLayout(act);
246
    mObjectPopup = new PopupWindow(act);
247
    mObjectPopup.setFocusable(true);
248
249
    if( mMaxRowCount<mRowCount )
250
      {
251
      ScrollView scrollView = new ScrollView(act);
252
      scrollView.addView(objectGrid);
253
      mObjectPopup.setContentView(scrollView);
254
      }
255
    else
256
      {
257
      mObjectPopup.setContentView(objectGrid);
258
      }
259 e07c48a2 Leszek Koltunski
260
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
261
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
262
263 92843d3b Leszek Koltunski
    objectGrid.setColumnCount(mColCount);
264
    objectGrid.setRowCount(mRowCount);
265 e07c48a2 Leszek Koltunski
266 0501a4b8 Leszek Koltunski
    int[] nextInRow = new int[mRowCount];
267 fa679111 Leszek Koltunski
268 e07c48a2 Leszek Koltunski
    for(int row=0; row<mRowCount; row++)
269
      {
270
      rowSpecs[row] = GridLayout.spec(row);
271 0501a4b8 Leszek Koltunski
      nextInRow[row]= 0;
272 e07c48a2 Leszek Koltunski
      }
273
    for(int col=0; col<mColCount; col++)
274
      {
275
      colSpecs[col] = GridLayout.spec(col);
276
      }
277 769d7b9f Leszek Koltunski
278 88d28110 Leszek Koltunski
    for(int object = 0; object< NUM_OBJECTS; object++)
279 769d7b9f Leszek Koltunski
      {
280 7b2a8ef3 Leszek Koltunski
      final ObjectType type = ObjectType.getObject(object);
281 588ace55 Leszek Koltunski
      int iconSize = RubikActivity.getDrawableSize();
282 7b2a8ef3 Leszek Koltunski
      int icons = type.getIconID(iconSize);
283 287e91a6 Leszek Koltunski
      int row = object/NUM_COLUMNS;
284 769d7b9f Leszek Koltunski
285 7ac0ee88 Leszek Koltunski
      ImageButton button = new ImageButton(act);
286
      button.setBackgroundResource(icons);
287
      button.setOnClickListener( new View.OnClickListener()
288 769d7b9f Leszek Koltunski
        {
289 7ac0ee88 Leszek Koltunski
        @Override
290
        public void onClick(View v)
291 769d7b9f Leszek Koltunski
          {
292 2afc6754 Leszek Koltunski
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
293 769d7b9f Leszek Koltunski
            {
294 7b2a8ef3 Leszek Koltunski
            mObject = type;
295
            act.changeObject(type, true);
296 4c9947bd Leszek Koltunski
            if( mPlayLayout!=null ) adjustLevels(act);
297 dd1a65c1 Leszek Koltunski
            mMovesController.clearMoves(act);
298 769d7b9f Leszek Koltunski
            }
299
300 7ac0ee88 Leszek Koltunski
          mObjectPopup.dismiss();
301
          }
302
        });
303 e07c48a2 Leszek Koltunski
304 7ac0ee88 Leszek Koltunski
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
305
      params.bottomMargin = margin;
306
      params.topMargin    = margin;
307
      params.leftMargin   = margin;
308
      params.rightMargin  = margin;
309 fa679111 Leszek Koltunski
310 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
311
312
      objectGrid.addView(button, params);
313 769d7b9f Leszek Koltunski
      }
314 4c0cd600 Leszek Koltunski
    }
315
316 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318 ad0c8e0e Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width)
319 e03e0352 Leszek Koltunski
    {
320
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
321 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
322
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
323 e03e0352 Leszek Koltunski
324
    mMenuPopup = new PopupWindow(act);
325
    mMenuPopup.setContentView(layout);
326
    mMenuPopup.setFocusable(true);
327 ad0c8e0e Leszek Koltunski
    int margin  = (int)(width*RubikActivity.MARGIN);
328
    int padding = (int)(width*RubikActivity.PADDING);
329 e03e0352 Leszek Koltunski
330 0254cfd7 Leszek Koltunski
    mMenuLayoutWidth = (int)(width/2);
331
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
332
333 43162dfb Leszek Koltunski
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
334 e03e0352 Leszek Koltunski
335
    for(int i=0; i<NUM_BUTTONS; i++)
336
      {
337
      final int but = i;
338
      Button button = new Button(act);
339 43162dfb Leszek Koltunski
      button.setLayoutParams(p);
340 e03e0352 Leszek Koltunski
      button.setText(BUTTON_LABELS[i]);
341 88fb92ba Leszek Koltunski
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
342 e03e0352 Leszek Koltunski
343
      button.setOnClickListener( new View.OnClickListener()
344
        {
345
        @Override
346
        public void onClick(View v)
347
          {
348
          mMenuPopup.dismiss();
349 0254cfd7 Leszek Koltunski
          MenuAction(act,but);
350 e03e0352 Leszek Koltunski
          }
351
        });
352
353 e07c48a2 Leszek Koltunski
      menuLayout.addView(button);
354 e03e0352 Leszek Koltunski
      }
355 0254cfd7 Leszek Koltunski
    }
356 e03e0352 Leszek Koltunski
357 0254cfd7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359
  private void setupPlayWindow(final RubikActivity act, final float width)
360
    {
361
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
362
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
363
    mPlayLayout = layout.findViewById(R.id.playGrid);
364
365
    mPlayLayoutWidth = (int)(width*0.4f);
366
367
    mPlayPopup = new PopupWindow(act);
368
    mPlayPopup.setContentView(layout);
369
    mPlayPopup.setFocusable(true);
370
371
    adjustLevels(act);
372 e03e0352 Leszek Koltunski
    }
373
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
376 0254cfd7 Leszek Koltunski
  private void MenuAction(RubikActivity act, int button)
377 e03e0352 Leszek Koltunski
    {
378
    switch(button)
379
      {
380 8ab435b9 Leszek Koltunski
      case 0: Bundle sBundle = new Bundle();
381
              sBundle.putInt("tab", mObject.ordinal() );
382 a8576d91 Leszek Koltunski
              sBundle.putBoolean("submitting", false);
383 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
384 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
385 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
386
              break;
387 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
388
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
389 e03e0352 Leszek Koltunski
              break;
390 b2a92941 Leszek Koltunski
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
391 e03e0352 Leszek Koltunski
              break;
392 b2a92941 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
393 234a7582 Leszek Koltunski
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
394 2971588c Leszek Koltunski
              break;
395 b2a92941 Leszek Koltunski
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
396 a8576d91 Leszek Koltunski
              aDiag.show(act.getSupportFragmentManager(), null);
397 e03e0352 Leszek Koltunski
              break;
398
      }
399
    }
400
401 46405bb4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
402
403 dd874ae8 Leszek Koltunski
  void setupSolveButton(final RubikActivity act)
404 46405bb4 Leszek Koltunski
    {
405 dd874ae8 Leszek Koltunski
    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);
406
    mSolveButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_END);
407 46405bb4 Leszek Koltunski
408 a8576d91 Leszek Koltunski
    mSolveButton.setOnClickListener( new View.OnClickListener()
409 46405bb4 Leszek Koltunski
      {
410 a8576d91 Leszek Koltunski
      @Override
411
      public void onClick(View v)
412
        {
413 2afc6754 Leszek Koltunski
        act.getControl().solveObject();
414 dd1a65c1 Leszek Koltunski
        mMovesController.clearMoves(act);
415 a8576d91 Leszek Koltunski
        }
416
      });
417 46405bb4 Leszek Koltunski
    }
418
419 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
420
421
  private void setupScrambleButton(final RubikActivity act)
422
    {
423
    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);
424
    mScrambleButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT, TransparentImageButton.GRAVITY_START);
425
426
    mScrambleButton.setOnClickListener( new View.OnClickListener()
427
      {
428
      @Override
429
      public void onClick(View v)
430
        {
431
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
432
        int numScrambles = play.getObject().getNumScramble();
433
        mShouldReactToEndOfScrambling = false;
434
        act.getControl().scrambleObject(numScrambles);
435
        }
436
      });
437
    }
438
439 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
440
441
  public void savePreferences(SharedPreferences.Editor editor)
442
    {
443 8ab435b9 Leszek Koltunski
    editor.putString("statePlay_objName", mObject.name() );
444 4c0cd600 Leszek Koltunski
445 e03e0352 Leszek Koltunski
    if( mObjectPopup!=null )
446
      {
447
      mObjectPopup.dismiss();
448
      mObjectPopup = null;
449
      }
450
451
    if( mMenuPopup!=null )
452 4c0cd600 Leszek Koltunski
      {
453 e03e0352 Leszek Koltunski
      mMenuPopup.dismiss();
454
      mMenuPopup = null;
455 4c0cd600 Leszek Koltunski
      }
456 0254cfd7 Leszek Koltunski
457
    if( mPlayPopup!=null )
458
      {
459
      mPlayPopup.dismiss();
460
      mPlayPopup = null;
461
      }
462 211b48f2 Leszek Koltunski
    }
463
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
466
  public void restorePreferences(SharedPreferences preferences)
467
    {
468 8ab435b9 Leszek Koltunski
    String objName= preferences.getString("statePlay_objName", DEF_OBJECT.name() );
469
    int ordinal = ObjectType.getOrdinal(objName);
470 88d28110 Leszek Koltunski
    mObject = ordinal>=0 && ordinal<NUM_OBJECTS ? ObjectType.values()[ordinal] : DEF_OBJECT;
471 211b48f2 Leszek Koltunski
    }
472
473 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
474
475 318c0a7d Leszek Koltunski
  public boolean setObject(RubikActivity act, ObjectType obj)
476 53f23b64 Leszek Koltunski
    {
477 8ab435b9 Leszek Koltunski
    if( mObject!=obj )
478 7b7d65ce Leszek Koltunski
      {
479 8ab435b9 Leszek Koltunski
      mObject = obj;
480 7ac0ee88 Leszek Koltunski
      if( mPlayLayout!=null ) adjustLevels(act);
481
      return true;
482 7b7d65ce Leszek Koltunski
      }
483
484 7ac0ee88 Leszek Koltunski
    return false;
485 7b7d65ce Leszek Koltunski
    }
486
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
489
490
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
491
    {
492
    View topLayout = act.findViewById(R.id.relativeLayout);
493 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
494 00aa398a Leszek Koltunski
495
    if( topLayout!=null )
496
      {
497
      topLayout.getLocationOnScreen(mLocation);
498 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
499 00aa398a Leszek Koltunski
      }
500
    else
501
      {
502 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
503 00aa398a Leszek Koltunski
      }
504 255492a0 Leszek Koltunski
505 69a271f3 Leszek Koltunski
    try
506 255492a0 Leszek Koltunski
      {
507 69a271f3 Leszek Koltunski
      // if on Android 11 or we are fullscreen
508
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
509
        {
510
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
511
        window.update(view, w, h);
512
        }
513
      else  // Android 10 or below in pop-up mode or split-screen mode
514
        {
515
        view.getLocationOnScreen(mLocation);
516
        int width  = view.getWidth();
517
        int height = view.getHeight();
518
        int x = mLocation[0]+(width-w)/2;
519
        int y = mLocation[1]+height+yoff;
520
521
        window.showAsDropDown(view);
522
        window.update(x,y,w,h);
523
        }
524 255492a0 Leszek Koltunski
      }
525 69a271f3 Leszek Koltunski
    catch( IllegalArgumentException iae )
526 255492a0 Leszek Koltunski
      {
527 69a271f3 Leszek Koltunski
      // ignore, this means window is 'not attached to window manager' -
528
      // which most probably is because we are already exiting the app.
529 255492a0 Leszek Koltunski
      }
530
    }
531
532 ae77f661 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
533
534
  public void adjustSolvedIcons()
535
    {
536
    int dbLevel = getDBLevel(mObject);
537
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
538
    RubikScores scores = RubikScores.getInstance();
539
540
    for(int i=0; i<numLevel; i++)
541
      {
542
      int level = i<numLevel-1 ? i+1 : dbLevel;
543
      Button button = (Button)mPlayLayout.getChildAt(i);
544
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
545
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
546
      }
547
    }
548
549 255492a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
550 7b7d65ce Leszek Koltunski
551 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
552 7b7d65ce Leszek Koltunski
    {
553 8ab435b9 Leszek Koltunski
    int dbLevel = getDBLevel(mObject);
554 88bad160 Leszek Koltunski
    int numScrambles = mObject.getNumScramble();
555 0a7aa15b Leszek Koltunski
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
556 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
557 7b7d65ce Leszek Koltunski
558 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel-1; i++)
559 53f23b64 Leszek Koltunski
      {
560 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
561 53f23b64 Leszek Koltunski
      }
562
563 4fc7b1e6 Leszek Koltunski
    if( numLevel>0 )
564
      {
565
      levels[numLevel-1] = act.getString(R.string.level_full);
566
      }
567 00af5060 Leszek Koltunski
568 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
569
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
570 00af5060 Leszek Koltunski
      {
571
      mLevelValue=1;
572
      }
573 0254cfd7 Leszek Koltunski
574 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
575
    int margin   = (int)(width*RubikActivity.MARGIN);
576
    int padding  = (int)(width*RubikActivity.PADDING);
577
    int butWidth = mPlayLayoutWidth - 2*padding;
578
    int butHeight= (int)mMenuItemSize;
579
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
580 0254cfd7 Leszek Koltunski
581 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
582 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
583 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
584 0254cfd7 Leszek Koltunski
    pT.setMargins(margin, margin, margin, margin);
585 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
586 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
587
588
    mPlayLayout.removeAllViews();
589
590 d7e539d0 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
591
592 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
593 011fcfe0 Leszek Koltunski
      {
594 0a7aa15b Leszek Koltunski
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
595
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
596 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
597 00af5060 Leszek Koltunski
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
598 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
599
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
600
601 8ab435b9 Leszek Koltunski
      int icon = scores.isSolved(mObject.ordinal(), level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
602 d7e539d0 Leszek Koltunski
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
603
604 0254cfd7 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
605 011fcfe0 Leszek Koltunski
        {
606 0254cfd7 Leszek Koltunski
        @Override
607
        public void onClick(View v)
608
          {
609 2afc6754 Leszek Koltunski
          ObjectControl control = act.getControl();
610 6fad862b Leszek Koltunski
611 2afc6754 Leszek Koltunski
          if(control.isUINotBlocked())
612 6fad862b Leszek Koltunski
            {
613 598de3ee Leszek Koltunski
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
614 0a7aa15b Leszek Koltunski
            mLevelValue = level;
615 dd874ae8 Leszek Koltunski
            mShouldReactToEndOfScrambling = true;
616 2afc6754 Leszek Koltunski
            control.scrambleObject(scrambles);
617 6fad862b Leszek Koltunski
            }
618 0254cfd7 Leszek Koltunski
          }
619
        });
620 011fcfe0 Leszek Koltunski
621 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
622
      }
623 53f23b64 Leszek Koltunski
    }
624
625 8ab435b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
626
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
627
// those got remembered in the server-side DB already, so we need to keep using them. This function
628
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
629
// those two values the same.
630
631
  public static int getDBLevel(ObjectType object)
632
    {
633
    switch(object)
634
      {
635
      case CUBE_3: return 16;
636
      case CUBE_4: return 20;
637
      case CUBE_5: return 24;
638 3f287c05 Leszek Koltunski
      case PYRA_4: return 15;
639 8ab435b9 Leszek Koltunski
      case PYRA_5: return 20;
640
      case MEGA_5: return 35;
641
      case DIAM_2: return 10;
642
      case DIAM_3: return 18;
643
      case REDI_3: return 14;
644
      case HELI_3: return 18;
645
      case SKEW_3: return 17;
646
      case REX_3 : return 16;
647
      case MIRR_3: return 16;
648 88bad160 Leszek Koltunski
      default    : return object.getNumScramble();
649 8ab435b9 Leszek Koltunski
      }
650
    }
651
652 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
653
654 dca3888a Leszek Koltunski
  public int getLevel()
655 211b48f2 Leszek Koltunski
    {
656 85b09df4 Leszek Koltunski
    return mLevelValue;
657 211b48f2 Leszek Koltunski
    }
658
659 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
660
661
  public boolean shouldReactToEndOfScrambling()
662
    {
663
    return mShouldReactToEndOfScrambling;
664
    }
665
666 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
667
668 8ab435b9 Leszek Koltunski
  public ObjectType getObject()
669 211b48f2 Leszek Koltunski
    {
670 4888e97c Leszek Koltunski
    return mObject;
671 211b48f2 Leszek Koltunski
    }
672
  }