Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 287e91a6

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 318c0a7d Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
39 3f7a4363 Leszek Koltunski
40
import org.distorted.main.R;
41
import org.distorted.main.RubikActivity;
42 eaf46415 Leszek Koltunski
import org.distorted.objectlib.main.ObjectPreRender;
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
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 fcd5b990 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase
54 211b48f2 Leszek Koltunski
  {
55 287e91a6 Leszek Koltunski
  public static final int NUM_COLUMNS  = 4;
56 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
57 318c0a7d Leszek Koltunski
  public static final int DEF_OBJECT= ObjectType.CUBE_3.ordinal();
58 211b48f2 Leszek Koltunski
59 7cf2637d Leszek Koltunski
  private static final int[] BUTTON_LABELS = { R.string.scores,
60
                                               R.string.patterns,
61 b2a92941 Leszek Koltunski
                                            //   R.string.control,
62 7cf2637d Leszek Koltunski
                                               R.string.solver,
63
                                               R.string.tutorials,
64
                                               R.string.about };
65
66 e03e0352 Leszek Koltunski
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
67 de62316a Leszek Koltunski
  private static final float LAST_BUTTON = 1.5f;
68 dc78f395 Leszek Koltunski
  private static final int[] mLocation = new int[2];
69 e03e0352 Leszek Koltunski
70 a8576d91 Leszek Koltunski
  private ImageButton mObjButton, mMenuButton, mSolveButton;
71 15846fe4 Leszek Koltunski
  private Button mPlayButton;
72 0254cfd7 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
73 4888e97c Leszek Koltunski
  private int mObject = DEF_OBJECT;
74 0254cfd7 Leszek Koltunski
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
75 85b09df4 Leszek Koltunski
  private int mLevelValue;
76 e07c48a2 Leszek Koltunski
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
77 598de3ee Leszek Koltunski
  private int mColCount, mRowCount, mMaxRowCount;
78 0254cfd7 Leszek Koltunski
  private LinearLayout mPlayLayout;
79 255492a0 Leszek Koltunski
  private int mUpperBarHeight;
80
81 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
84 211b48f2 Leszek Koltunski
    {
85 85b09df4 Leszek Koltunski
86 211b48f2 Leszek Koltunski
    }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
91 211b48f2 Leszek Koltunski
    {
92 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
93 598de3ee Leszek Koltunski
    float height= act.getScreenHeightInPixels();
94 255492a0 Leszek Koltunski
    mUpperBarHeight = act.getHeightUpperBar();
95 a8576d91 Leszek Koltunski
96 eb376d3a Leszek Koltunski
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
97 88fb92ba Leszek Koltunski
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
98
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
99 e3c74c0f Leszek Koltunski
100 287e91a6 Leszek Koltunski
    mRowCount = (ObjectType.NUM_OBJECTS + NUM_COLUMNS-1) / NUM_COLUMNS;
101
    mColCount = NUM_COLUMNS;
102 e07c48a2 Leszek Koltunski
103 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
104 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
105 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
106 85b09df4 Leszek Koltunski
107 598de3ee Leszek Koltunski
    setupObjectWindow(act,width,height);
108 ad0c8e0e Leszek Koltunski
    setupObjectButton(act,width);
109 85b09df4 Leszek Koltunski
    layoutTop.addView(mObjButton);
110 0254cfd7 Leszek Koltunski
111
    setupMenuWindow(act,width);
112
    setupMenuButton(act,width);
113
    layoutTop.addView(mMenuButton);
114
115
    setupPlayWindow(act,width);
116 d90c55cc Leszek Koltunski
    setupPlayButton(act,width,height);
117 85b09df4 Leszek Koltunski
    layoutTop.addView(mPlayButton);
118 211b48f2 Leszek Koltunski
119 ad0c8e0e Leszek Koltunski
    setupSolveButton(act,width);
120 a8576d91 Leszek Koltunski
    createBottomPane(act,width,mSolveButton);
121 769d7b9f Leszek Koltunski
    }
122 211b48f2 Leszek Koltunski
123 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124 4c0cd600 Leszek Koltunski
125 ad0c8e0e Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float width)
126 769d7b9f Leszek Koltunski
    {
127 e07c48a2 Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.MARGIN);
128
    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 dc78f395 Leszek Koltunski
130 da768c35 Leszek Koltunski
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
131 769d7b9f Leszek Koltunski
132
    mObjButton.setOnClickListener( new View.OnClickListener()
133
      {
134
      @Override
135
      public void onClick(View view)
136 4c0cd600 Leszek Koltunski
        {
137 598de3ee Leszek Koltunski
        if( mObjectPopup!=null && act.getPreRender().isUINotBlocked())
138 a42e25a6 Leszek Koltunski
          {
139 598de3ee Leszek Koltunski
          int rowCount = Math.min(mMaxRowCount,mRowCount);
140 c5b4af4a Leszek Koltunski
          View popupView = mObjectPopup.getContentView();
141
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
142 255492a0 Leszek Koltunski
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount,margin,margin);
143 c5b4af4a Leszek Koltunski
          }
144 769d7b9f Leszek Koltunski
        }
145
      });
146
    }
147 4c0cd600 Leszek Koltunski
148 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150 d90c55cc Leszek Koltunski
  private void setupPlayButton(final RubikActivity act, final float width, final float height)
151 85b09df4 Leszek Koltunski
    {
152 d90c55cc Leszek Koltunski
    final int margin   = (int)(width*RubikActivity.MARGIN);
153 255492a0 Leszek Koltunski
    final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
154 d90c55cc Leszek Koltunski
155 da768c35 Leszek Koltunski
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
156 85b09df4 Leszek Koltunski
157
    mPlayButton.setOnClickListener( new View.OnClickListener()
158
      {
159
      @Override
160 0254cfd7 Leszek Koltunski
      public void onClick(View view)
161 85b09df4 Leszek Koltunski
        {
162 598de3ee Leszek Koltunski
        if( mPlayPopup!=null && act.getPreRender().isUINotBlocked())
163 6e194411 Leszek Koltunski
          {
164 c5b4af4a Leszek Koltunski
          View popupView = mPlayPopup.getContentView();
165
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
166 318c0a7d Leszek Koltunski
          final int dbLevel = ObjectType.getDBLevel(mObject);
167 0a7aa15b Leszek Koltunski
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
168 d90c55cc Leszek Koltunski
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f));
169 255492a0 Leszek Koltunski
          final int realHeight = Math.min(popupHeight,maxHeight);
170
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
171 6fad862b Leszek Koltunski
          }
172 85b09df4 Leszek Koltunski
        }
173
      });
174
    }
175
176 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178 ad0c8e0e Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float width)
179 e31abc1e Leszek Koltunski
    {
180 0254cfd7 Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.MARGIN);
181
    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);
182 dc78f395 Leszek Koltunski
183 da768c35 Leszek Koltunski
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
184 e31abc1e Leszek Koltunski
185 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
186 e31abc1e Leszek Koltunski
      {
187
      @Override
188
      public void onClick(View view)
189
        {
190 598de3ee Leszek Koltunski
        if( mMenuPopup!=null && act.getPreRender().isUINotBlocked())
191 e03e0352 Leszek Koltunski
          {
192 c5b4af4a Leszek Koltunski
          View popupView = mMenuPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194 255492a0 Leszek Koltunski
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
195 6fad862b Leszek Koltunski
          }
196 e31abc1e Leszek Koltunski
        }
197
      });
198
    }
199
200 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202 598de3ee Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
203 4c0cd600 Leszek Koltunski
    {
204 7ac0ee88 Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.small_cube2,R.drawable.medium_cube2, R.drawable.big_cube2, R.drawable.huge_cube2);
205 598de3ee Leszek Koltunski
206
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
207
    int cubeWidth = bd.getIntrinsicWidth();
208
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
209
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
210 255492a0 Leszek Koltunski
    mMaxRowCount = (int)(0.9f*(height-mUpperBarHeight)/mObjectSize);
211 598de3ee Leszek Koltunski
    GridLayout objectGrid = new GridLayout(act);
212
    mObjectPopup = new PopupWindow(act);
213
    mObjectPopup.setFocusable(true);
214
215
    if( mMaxRowCount<mRowCount )
216
      {
217
      ScrollView scrollView = new ScrollView(act);
218
      scrollView.addView(objectGrid);
219
      mObjectPopup.setContentView(scrollView);
220
      }
221
    else
222
      {
223
      mObjectPopup.setContentView(objectGrid);
224
      }
225 e07c48a2 Leszek Koltunski
226
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
227
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
228
229 92843d3b Leszek Koltunski
    objectGrid.setColumnCount(mColCount);
230
    objectGrid.setRowCount(mRowCount);
231 e07c48a2 Leszek Koltunski
232 0501a4b8 Leszek Koltunski
    int[] nextInRow = new int[mRowCount];
233 fa679111 Leszek Koltunski
234 e07c48a2 Leszek Koltunski
    for(int row=0; row<mRowCount; row++)
235
      {
236
      rowSpecs[row] = GridLayout.spec(row);
237 0501a4b8 Leszek Koltunski
      nextInRow[row]= 0;
238 e07c48a2 Leszek Koltunski
      }
239
    for(int col=0; col<mColCount; col++)
240
      {
241
      colSpecs[col] = GridLayout.spec(col);
242
      }
243 769d7b9f Leszek Koltunski
244 318c0a7d Leszek Koltunski
    for(int object = 0; object< ObjectType.NUM_OBJECTS; object++)
245 769d7b9f Leszek Koltunski
      {
246 318c0a7d Leszek Koltunski
      final ObjectType list = ObjectType.getObject(object);
247 588ace55 Leszek Koltunski
      int iconSize = RubikActivity.getDrawableSize();
248 7ac0ee88 Leszek Koltunski
      int icons = list.getIconID(iconSize);
249 769d7b9f Leszek Koltunski
      final int obj = object;
250 287e91a6 Leszek Koltunski
      int row = object/NUM_COLUMNS;
251 769d7b9f Leszek Koltunski
252 7ac0ee88 Leszek Koltunski
      ImageButton button = new ImageButton(act);
253
      button.setBackgroundResource(icons);
254
      button.setOnClickListener( new View.OnClickListener()
255 769d7b9f Leszek Koltunski
        {
256 7ac0ee88 Leszek Koltunski
        @Override
257
        public void onClick(View v)
258 769d7b9f Leszek Koltunski
          {
259 7ac0ee88 Leszek Koltunski
          if( act.getPreRender().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
260 769d7b9f Leszek Koltunski
            {
261 7ac0ee88 Leszek Koltunski
            mObject = obj;
262
            act.changeObject(list, true);
263
            adjustLevels(act);
264 dd1a65c1 Leszek Koltunski
            mMovesController.clearMoves(act);
265 769d7b9f Leszek Koltunski
            }
266
267 7ac0ee88 Leszek Koltunski
          mObjectPopup.dismiss();
268
          }
269
        });
270 e07c48a2 Leszek Koltunski
271 7ac0ee88 Leszek Koltunski
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
272
      params.bottomMargin = margin;
273
      params.topMargin    = margin;
274
      params.leftMargin   = margin;
275
      params.rightMargin  = margin;
276 fa679111 Leszek Koltunski
277 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
278
279
      objectGrid.addView(button, params);
280 769d7b9f Leszek Koltunski
      }
281 4c0cd600 Leszek Koltunski
    }
282
283 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
284
285 ad0c8e0e Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width)
286 e03e0352 Leszek Koltunski
    {
287
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
288 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
289
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
290 e03e0352 Leszek Koltunski
291
    mMenuPopup = new PopupWindow(act);
292
    mMenuPopup.setContentView(layout);
293
    mMenuPopup.setFocusable(true);
294 ad0c8e0e Leszek Koltunski
    int margin  = (int)(width*RubikActivity.MARGIN);
295
    int padding = (int)(width*RubikActivity.PADDING);
296 e03e0352 Leszek Koltunski
297 0254cfd7 Leszek Koltunski
    mMenuLayoutWidth = (int)(width/2);
298
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
299
300 43162dfb Leszek Koltunski
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
301 e03e0352 Leszek Koltunski
302
    for(int i=0; i<NUM_BUTTONS; i++)
303
      {
304
      final int but = i;
305
      Button button = new Button(act);
306 43162dfb Leszek Koltunski
      button.setLayoutParams(p);
307 e03e0352 Leszek Koltunski
      button.setText(BUTTON_LABELS[i]);
308 88fb92ba Leszek Koltunski
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
309 e03e0352 Leszek Koltunski
310
      button.setOnClickListener( new View.OnClickListener()
311
        {
312
        @Override
313
        public void onClick(View v)
314
          {
315
          mMenuPopup.dismiss();
316 0254cfd7 Leszek Koltunski
          MenuAction(act,but);
317 e03e0352 Leszek Koltunski
          }
318
        });
319
320 e07c48a2 Leszek Koltunski
      menuLayout.addView(button);
321 e03e0352 Leszek Koltunski
      }
322 0254cfd7 Leszek Koltunski
    }
323 e03e0352 Leszek Koltunski
324 0254cfd7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326
  private void setupPlayWindow(final RubikActivity act, final float width)
327
    {
328
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
329
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
330
    mPlayLayout = layout.findViewById(R.id.playGrid);
331
332
    mPlayLayoutWidth = (int)(width*0.4f);
333
334
    mPlayPopup = new PopupWindow(act);
335
    mPlayPopup.setContentView(layout);
336
    mPlayPopup.setFocusable(true);
337
338
    adjustLevels(act);
339 e03e0352 Leszek Koltunski
    }
340
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
343 0254cfd7 Leszek Koltunski
  private void MenuAction(RubikActivity act, int button)
344 e03e0352 Leszek Koltunski
    {
345
    switch(button)
346
      {
347 f5da732a Leszek Koltunski
      case 0: RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
348 e03e0352 Leszek Koltunski
              int object = play.getObject();
349 a8576d91 Leszek Koltunski
              Bundle sBundle = new Bundle();
350 7ac0ee88 Leszek Koltunski
              sBundle.putInt("tab", object );
351 a8576d91 Leszek Koltunski
              sBundle.putBoolean("submitting", false);
352 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
353 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
354 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
355
              break;
356 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
357 a8576d91 Leszek Koltunski
              Bundle pBundle = new Bundle();
358 234a7582 Leszek Koltunski
              int pOrd = getPatternOrdinal();
359
              pBundle.putInt("tab", pOrd );
360 a8576d91 Leszek Koltunski
              pDiag.setArguments(pBundle);
361 234a7582 Leszek Koltunski
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
362 e03e0352 Leszek Koltunski
              break;
363 b2a92941 Leszek Koltunski
/*
364 314bffaf Leszek Koltunski
      case 2: RubikControl control = RubikControl.getInstance();
365 b9d4aa3b Leszek Koltunski
              //control.animateAll(act);
366
              control.animateRotate(act);
367 7cf2637d Leszek Koltunski
              break;
368 b2a92941 Leszek Koltunski
 */
369
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
370 e03e0352 Leszek Koltunski
              break;
371 b2a92941 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
372 234a7582 Leszek Koltunski
              Bundle tBundle = new Bundle();
373
              int tOrd = getTutorialOrdinal();
374
              tBundle.putInt("tab", tOrd );
375
              tDiag.setArguments(tBundle);
376
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
377 2971588c Leszek Koltunski
              break;
378 b2a92941 Leszek Koltunski
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
379 a8576d91 Leszek Koltunski
              aDiag.show(act.getSupportFragmentManager(), null);
380 e03e0352 Leszek Koltunski
              break;
381
      }
382
    }
383
384 46405bb4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
385
386 a8576d91 Leszek Koltunski
  void setupSolveButton(final RubikActivity act, final float width)
387 46405bb4 Leszek Koltunski
    {
388 a8576d91 Leszek Koltunski
    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);
389
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
390 46405bb4 Leszek Koltunski
391 a8576d91 Leszek Koltunski
    mSolveButton.setOnClickListener( new View.OnClickListener()
392 46405bb4 Leszek Koltunski
      {
393 a8576d91 Leszek Koltunski
      @Override
394
      public void onClick(View v)
395
        {
396
        act.getPreRender().solveObject();
397 dd1a65c1 Leszek Koltunski
        mMovesController.clearMoves(act);
398 a8576d91 Leszek Koltunski
        }
399
      });
400 46405bb4 Leszek Koltunski
    }
401
402 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
403
404
  public void savePreferences(SharedPreferences.Editor editor)
405
    {
406 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
407 4c0cd600 Leszek Koltunski
408 e03e0352 Leszek Koltunski
    if( mObjectPopup!=null )
409
      {
410
      mObjectPopup.dismiss();
411
      mObjectPopup = null;
412
      }
413
414
    if( mMenuPopup!=null )
415 4c0cd600 Leszek Koltunski
      {
416 e03e0352 Leszek Koltunski
      mMenuPopup.dismiss();
417
      mMenuPopup = null;
418 4c0cd600 Leszek Koltunski
      }
419 0254cfd7 Leszek Koltunski
420
    if( mPlayPopup!=null )
421
      {
422
      mPlayPopup.dismiss();
423
      mPlayPopup = null;
424
      }
425 211b48f2 Leszek Koltunski
    }
426
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428
429
  public void restorePreferences(SharedPreferences preferences)
430
    {
431 eb389a97 Leszek Koltunski
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
432 318c0a7d Leszek Koltunski
    int dbLevel = ObjectType.getDBLevel(mObject);
433 e9f567ac Leszek Koltunski
434
    // This means the app has been upgraded to a new version which swapped the
435
    // Object for a new one with larger sizeIndex and now getMaxLevel() returns
436
    // 0. Reset the object to default, otherwise we'll get a crash later on.
437
438 7ac0ee88 Leszek Koltunski
    if( dbLevel==0 ) mObject = DEF_OBJECT;
439 211b48f2 Leszek Koltunski
    }
440
441 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
442
443 318c0a7d Leszek Koltunski
  public boolean setObject(RubikActivity act, ObjectType obj)
444 53f23b64 Leszek Koltunski
    {
445 7ac0ee88 Leszek Koltunski
    if( mObject!=obj.ordinal() )
446 7b7d65ce Leszek Koltunski
      {
447 7ac0ee88 Leszek Koltunski
      mObject = obj.ordinal();
448
      if( mPlayLayout!=null ) adjustLevels(act);
449
      return true;
450 7b7d65ce Leszek Koltunski
      }
451
452 7ac0ee88 Leszek Koltunski
    return false;
453 7b7d65ce Leszek Koltunski
    }
454
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
457
458
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
459
    {
460
    View topLayout = act.findViewById(R.id.relativeLayout);
461 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
462 00aa398a Leszek Koltunski
463
    if( topLayout!=null )
464
      {
465
      topLayout.getLocationOnScreen(mLocation);
466 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
467 00aa398a Leszek Koltunski
      }
468
    else
469
      {
470 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
471 00aa398a Leszek Koltunski
      }
472 255492a0 Leszek Koltunski
473
    // if on Android 11 or we are fullscreen
474 7ac0ee88 Leszek Koltunski
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
475 255492a0 Leszek Koltunski
      {
476
      window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
477
      window.update(view, w, h);
478
      }
479
    else  // Android 10 or below in pop-up mode or split-screen mode
480
      {
481
      view.getLocationOnScreen(mLocation);
482
      int width  = view.getWidth();
483
      int height = view.getHeight();
484
      int x = mLocation[0]+(width-w)/2;
485
      int y = mLocation[1]+height+yoff;
486
487
      window.showAsDropDown(view);
488
      window.update(x,y,w,h);
489
      }
490
    }
491
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493 7b7d65ce Leszek Koltunski
494 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
495 7b7d65ce Leszek Koltunski
    {
496 318c0a7d Leszek Koltunski
    int dbLevel = ObjectType.getDBLevel(mObject);
497
    int numScrambles = ObjectType.getNumScramble(mObject);
498 0a7aa15b Leszek Koltunski
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
499 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
500 7b7d65ce Leszek Koltunski
501 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel-1; i++)
502 53f23b64 Leszek Koltunski
      {
503 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
504 53f23b64 Leszek Koltunski
      }
505
506 4fc7b1e6 Leszek Koltunski
    if( numLevel>0 )
507
      {
508
      levels[numLevel-1] = act.getString(R.string.level_full);
509
      }
510 00af5060 Leszek Koltunski
511 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
512
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
513 00af5060 Leszek Koltunski
      {
514
      mLevelValue=1;
515
      }
516 0254cfd7 Leszek Koltunski
517 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
518
    int margin   = (int)(width*RubikActivity.MARGIN);
519
    int padding  = (int)(width*RubikActivity.PADDING);
520
    int butWidth = mPlayLayoutWidth - 2*padding;
521
    int butHeight= (int)mMenuItemSize;
522
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
523 0254cfd7 Leszek Koltunski
524 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
525 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
526 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
527 0254cfd7 Leszek Koltunski
    pT.setMargins(margin, margin, margin, margin);
528 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
529 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
530
531
    mPlayLayout.removeAllViews();
532
533 d7e539d0 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
534
535 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
536 011fcfe0 Leszek Koltunski
      {
537 0a7aa15b Leszek Koltunski
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
538
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
539 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
540 00af5060 Leszek Koltunski
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
541 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
542
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
543
544 7ac0ee88 Leszek Koltunski
      int icon = scores.isSolved(mObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
545 d7e539d0 Leszek Koltunski
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
546
547 0254cfd7 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
548 011fcfe0 Leszek Koltunski
        {
549 0254cfd7 Leszek Koltunski
        @Override
550
        public void onClick(View v)
551
          {
552 eaf46415 Leszek Koltunski
          ObjectPreRender pre = act.getPreRender();
553 6fad862b Leszek Koltunski
554 0594c61f Leszek Koltunski
          if(pre.isUINotBlocked())
555 6fad862b Leszek Koltunski
            {
556 598de3ee Leszek Koltunski
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
557 0a7aa15b Leszek Koltunski
            mLevelValue = level;
558
            pre.scrambleObject(scrambles);
559 6fad862b Leszek Koltunski
            }
560 0254cfd7 Leszek Koltunski
          }
561
        });
562 011fcfe0 Leszek Koltunski
563 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
564
      }
565 53f23b64 Leszek Koltunski
    }
566
567 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
568
569 dca3888a Leszek Koltunski
  public int getLevel()
570 211b48f2 Leszek Koltunski
    {
571 85b09df4 Leszek Koltunski
    return mLevelValue;
572 211b48f2 Leszek Koltunski
    }
573
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575
576 4888e97c Leszek Koltunski
  public int getObject()
577 211b48f2 Leszek Koltunski
    {
578 4888e97c Leszek Koltunski
    return mObject;
579 211b48f2 Leszek Koltunski
    }
580
  }