Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 3f287c05

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