Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 55e6be1d

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 e03e0352 Leszek Koltunski
import android.os.Bundle;
26 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
27 b0ed406c Leszek Koltunski
import android.view.Gravity;
28 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
29
import android.view.View;
30
import android.widget.Button;
31 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
32 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
33
import android.widget.LinearLayout;
34 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
35 211b48f2 Leszek Koltunski
36 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
37 a8576d91 Leszek Koltunski
import org.distorted.dialogs.RubikDialogPattern;
38 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogScores;
39 234a7582 Leszek Koltunski
import org.distorted.dialogs.RubikDialogTutorial;
40 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentButton;
41
import org.distorted.helpers.TransparentImageButton;
42 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
43
import org.distorted.main.RubikActivity;
44 6fad862b Leszek Koltunski
import org.distorted.main.RubikPreRender;
45 9c2f0c91 Leszek Koltunski
import org.distorted.objects.ObjectList;
46 6a083c6a Leszek Koltunski
import org.distorted.network.RubikScores;
47 211b48f2 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 fcd5b990 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase
51 211b48f2 Leszek Koltunski
  {
52 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
53
  public static final int DEF_OBJECT= ObjectList.CUBE.ordinal();
54
  public static final int DEF_SIZE  =  3;
55 211b48f2 Leszek Koltunski
56 7cf2637d Leszek Koltunski
  private static final int[] BUTTON_LABELS = { R.string.scores,
57
                                               R.string.patterns,
58 77d3e4f6 Leszek Koltunski
                                           //    R.string.control,
59 7cf2637d Leszek Koltunski
                                               R.string.solver,
60
                                               R.string.tutorials,
61
                                               R.string.about };
62
63 e03e0352 Leszek Koltunski
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
64 de62316a Leszek Koltunski
  private static final float LAST_BUTTON = 1.5f;
65 e03e0352 Leszek Koltunski
66 a8576d91 Leszek Koltunski
  private ImageButton mObjButton, mMenuButton, mSolveButton;
67 15846fe4 Leszek Koltunski
  private Button mPlayButton;
68 0254cfd7 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
69 4888e97c Leszek Koltunski
  private int mObject = DEF_OBJECT;
70
  private int mSize   = DEF_SIZE;
71 0254cfd7 Leszek Koltunski
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
72 85b09df4 Leszek Koltunski
  private int mLevelValue;
73 e07c48a2 Leszek Koltunski
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
74
  private int mColCount, mRowCount;
75 0254cfd7 Leszek Koltunski
  private LinearLayout mPlayLayout;
76 211b48f2 Leszek Koltunski
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
80 211b48f2 Leszek Koltunski
    {
81 85b09df4 Leszek Koltunski
82 211b48f2 Leszek Koltunski
    }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
87 211b48f2 Leszek Koltunski
    {
88 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
89 a8576d91 Leszek Koltunski
90 eb376d3a Leszek Koltunski
    mMenuTextSize = width*RubikActivity.MENU_MED_TEXT_SIZE;
91 88fb92ba Leszek Koltunski
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
92
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
93 e3c74c0f Leszek Koltunski
94 9c2f0c91 Leszek Koltunski
    mRowCount = ObjectList.getRowCount();
95
    mColCount = ObjectList.getColumnCount();
96 e07c48a2 Leszek Koltunski
97 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
98 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
99 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
100 85b09df4 Leszek Koltunski
101 b376bfd7 Leszek Koltunski
    setupObjectWindow(act,width);
102 ad0c8e0e Leszek Koltunski
    setupObjectButton(act,width);
103 85b09df4 Leszek Koltunski
    layoutTop.addView(mObjButton);
104 0254cfd7 Leszek Koltunski
105
    setupMenuWindow(act,width);
106
    setupMenuButton(act,width);
107
    layoutTop.addView(mMenuButton);
108
109
    setupPlayWindow(act,width);
110 ad0c8e0e Leszek Koltunski
    setupPlayButton(act,width);
111 85b09df4 Leszek Koltunski
    layoutTop.addView(mPlayButton);
112 211b48f2 Leszek Koltunski
113 ad0c8e0e Leszek Koltunski
    setupSolveButton(act,width);
114 a8576d91 Leszek Koltunski
    createBottomPane(act,width,mSolveButton);
115 769d7b9f Leszek Koltunski
    }
116 211b48f2 Leszek Koltunski
117 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118 4c0cd600 Leszek Koltunski
119 ad0c8e0e Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float width)
120 769d7b9f Leszek Koltunski
    {
121 e07c48a2 Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.MARGIN);
122
    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);
123 da768c35 Leszek Koltunski
    mObjButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
124 769d7b9f Leszek Koltunski
125
    mObjButton.setOnClickListener( new View.OnClickListener()
126
      {
127
      @Override
128
      public void onClick(View view)
129 4c0cd600 Leszek Koltunski
        {
130 0594c61f Leszek Koltunski
        if(act.getPreRender().isUINotBlocked())
131 a42e25a6 Leszek Koltunski
          {
132 c5b4af4a Leszek Koltunski
          if( mObjectPopup==null )
133
            {
134
            // I completely don't understand it, but Firebase says occasionally mObjectPopup is null here. Recreate.
135
            float width = act.getScreenWidthInPixels();
136
            setupObjectWindow(act,width);
137
            }
138 456780f1 Leszek Koltunski
139 c5b4af4a Leszek Koltunski
          mObjectPopup.setFocusable(false);
140
          mObjectPopup.update();
141 466b2ffc Leszek Koltunski
142 c5b4af4a Leszek Koltunski
          View popupView = mObjectPopup.getContentView();
143
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
144 466b2ffc Leszek Koltunski
145 c5b4af4a Leszek Koltunski
          mObjectPopup.showAsDropDown(view, margin, margin);
146
          mObjectPopup.update(view, mObjectSize*mColCount, mObjectSize*mRowCount);
147 466b2ffc Leszek Koltunski
148 c5b4af4a Leszek Koltunski
          mObjectPopup.setFocusable(true);
149
          mObjectPopup.update();
150
          }
151 769d7b9f Leszek Koltunski
        }
152
      });
153
    }
154 4c0cd600 Leszek Koltunski
155 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 ad0c8e0e Leszek Koltunski
  private void setupPlayButton(final RubikActivity act, final float width)
158 85b09df4 Leszek Koltunski
    {
159 0254cfd7 Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.MARGIN);
160 da768c35 Leszek Koltunski
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize, width);
161 85b09df4 Leszek Koltunski
162
    mPlayButton.setOnClickListener( new View.OnClickListener()
163
      {
164
      @Override
165 0254cfd7 Leszek Koltunski
      public void onClick(View view)
166 85b09df4 Leszek Koltunski
        {
167 0594c61f Leszek Koltunski
        if(act.getPreRender().isUINotBlocked())
168 6e194411 Leszek Koltunski
          {
169 c5b4af4a Leszek Koltunski
          if( mPlayPopup==null )
170
            {
171
            // I completely don't understand it, but Firebase says occasionally mPlayPopup is null here. Recreate.
172
            float width = act.getScreenWidthInPixels();
173
            setupPlayWindow(act,width);
174
            }
175
176
          mPlayPopup.setFocusable(false);
177
          mPlayPopup.update();
178
179
          View popupView = mPlayPopup.getContentView();
180
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
181
182
          final int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
183 0a7aa15b Leszek Koltunski
          final int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
184
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
185 c5b4af4a Leszek Koltunski
186
          mPlayPopup.showAsDropDown(view, margin, margin);
187
          mPlayPopup.update(view, mPlayLayoutWidth, (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_BUTTON-1.0f)));
188
          mPlayPopup.setFocusable(true);
189
          mPlayPopup.update();
190 6fad862b Leszek Koltunski
          }
191 85b09df4 Leszek Koltunski
        }
192
      });
193
    }
194
195 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197 ad0c8e0e Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float width)
198 e31abc1e Leszek Koltunski
    {
199 0254cfd7 Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.MARGIN);
200
    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);
201 da768c35 Leszek Koltunski
    mMenuButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
202 e31abc1e Leszek Koltunski
203 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
204 e31abc1e Leszek Koltunski
      {
205
      @Override
206
      public void onClick(View view)
207
        {
208 0594c61f Leszek Koltunski
        if(act.getPreRender().isUINotBlocked())
209 e03e0352 Leszek Koltunski
          {
210 c5b4af4a Leszek Koltunski
          if( mMenuPopup==null )
211
            {
212
            // I completely don't understand it, but Firebase says occasionally mMenuPopup is null here. Recreate.
213
            float width = act.getScreenWidthInPixels();
214
            setupMenuWindow(act,width);
215
            }
216
217
          mMenuPopup.setFocusable(false);
218
          mMenuPopup.update();
219
220
          View popupView = mMenuPopup.getContentView();
221
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
222
223 b0ed406c Leszek Koltunski
          mMenuPopup.showAsDropDown(view, (int)(-width/12), margin, Gravity.CENTER);
224 c5b4af4a Leszek Koltunski
          mMenuPopup.update(view, mMenuLayoutWidth, mMenuLayoutHeight);
225
          mMenuPopup.setFocusable(true);
226
          mMenuPopup.update();
227 6fad862b Leszek Koltunski
          }
228 e31abc1e Leszek Koltunski
        }
229
      });
230
    }
231
232 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234 ad0c8e0e Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float width)
235 4c0cd600 Leszek Koltunski
    {
236 769d7b9f Leszek Koltunski
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
237
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
238 92843d3b Leszek Koltunski
    GridLayout objectGrid = layout.findViewById(R.id.objectGrid);
239 e07c48a2 Leszek Koltunski
240 9c2f0c91 Leszek Koltunski
    int[] indices = ObjectList.getIndices();
241 e07c48a2 Leszek Koltunski
242
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
243
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
244
245 92843d3b Leszek Koltunski
    objectGrid.setColumnCount(mColCount);
246
    objectGrid.setRowCount(mRowCount);
247 e07c48a2 Leszek Koltunski
248 0501a4b8 Leszek Koltunski
    int[] nextInRow = new int[mRowCount];
249 fa679111 Leszek Koltunski
250 e07c48a2 Leszek Koltunski
    for(int row=0; row<mRowCount; row++)
251
      {
252
      rowSpecs[row] = GridLayout.spec(row);
253 0501a4b8 Leszek Koltunski
      nextInRow[row]= 0;
254 e07c48a2 Leszek Koltunski
      }
255
    for(int col=0; col<mColCount; col++)
256
      {
257
      colSpecs[col] = GridLayout.spec(col);
258
      }
259 769d7b9f Leszek Koltunski
260 e03e0352 Leszek Koltunski
    mObjectPopup = new PopupWindow(act);
261
    mObjectPopup.setContentView(layout);
262
    mObjectPopup.setFocusable(true);
263 31a9f38d Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
264 769d7b9f Leszek Koltunski
265 f6a9c538 Leszek Koltunski
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
266 e07c48a2 Leszek Koltunski
    int cubeWidth = bd.getIntrinsicWidth();
267
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
268
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
269 f6a9c538 Leszek Koltunski
270 0501a4b8 Leszek Koltunski
    for(int object=0; object< ObjectList.NUM_OBJECTS; object++)
271 769d7b9f Leszek Koltunski
      {
272 9c2f0c91 Leszek Koltunski
      final ObjectList list = ObjectList.getObject(object);
273 53f23b64 Leszek Koltunski
      final int[] sizes = list.getSizes();
274 769d7b9f Leszek Koltunski
      int[] icons = list.getIconIDs();
275
      int len = sizes.length;
276
      final int obj = object;
277 0501a4b8 Leszek Koltunski
      int row = indices[object];
278 769d7b9f Leszek Koltunski
279
      for(int i=0; i<len; i++)
280
        {
281 e07c48a2 Leszek Koltunski
        final int index = i;
282 769d7b9f Leszek Koltunski
283
        ImageButton button = new ImageButton(act);
284
        button.setBackgroundResource(icons[i]);
285
        button.setOnClickListener( new View.OnClickListener()
286
          {
287
          @Override
288
          public void onClick(View v)
289
            {
290 0594c61f Leszek Koltunski
            if( act.getPreRender().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
291 a42e25a6 Leszek Koltunski
              {
292 b4a9a34f Leszek Koltunski
              mObject = obj;
293
              mSize   = sizes[index];
294
              act.changeObject(list,sizes[index], true);
295
              adjustLevels(act);
296 55e6be1d Leszek Koltunski
              mController.clearMoves();
297 a42e25a6 Leszek Koltunski
              }
298 8e3898c8 Leszek Koltunski
299 e03e0352 Leszek Koltunski
            mObjectPopup.dismiss();
300 769d7b9f Leszek Koltunski
            }
301
          });
302
303 0501a4b8 Leszek Koltunski
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
304 e07c48a2 Leszek Koltunski
        params.bottomMargin = margin;
305
        params.topMargin    = margin;
306
        params.leftMargin   = margin;
307
        params.rightMargin  = margin;
308
309 0501a4b8 Leszek Koltunski
        nextInRow[row]++;
310 fa679111 Leszek Koltunski
311 92843d3b Leszek Koltunski
        objectGrid.addView(button, params);
312 769d7b9f Leszek Koltunski
        }
313
      }
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 f5da732a Leszek Koltunski
      case 0: RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
381 e03e0352 Leszek Koltunski
              int object = play.getObject();
382
              int size   = play.getSize();
383 9c2f0c91 Leszek Koltunski
              int sizeIndex = ObjectList.getSizeIndex(object,size);
384 a8576d91 Leszek Koltunski
              Bundle sBundle = new Bundle();
385
              sBundle.putInt("tab", ObjectList.pack(object,sizeIndex) );
386
              sBundle.putBoolean("submitting", false);
387 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
388 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
389 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
390
              break;
391 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
392 a8576d91 Leszek Koltunski
              Bundle pBundle = new Bundle();
393 234a7582 Leszek Koltunski
              int pOrd = getPatternOrdinal();
394
              pBundle.putInt("tab", pOrd );
395 a8576d91 Leszek Koltunski
              pDiag.setArguments(pBundle);
396 234a7582 Leszek Koltunski
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
397 e03e0352 Leszek Koltunski
              break;
398 77d3e4f6 Leszek Koltunski
/*
399 314bffaf Leszek Koltunski
      case 2: RubikControl control = RubikControl.getInstance();
400
              control.animateAll(act);
401 7cf2637d Leszek Koltunski
              break;
402 834b2618 Leszek Koltunski
 */
403 77d3e4f6 Leszek Koltunski
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
404 e03e0352 Leszek Koltunski
              break;
405 77d3e4f6 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
406 234a7582 Leszek Koltunski
              Bundle tBundle = new Bundle();
407
              int tOrd = getTutorialOrdinal();
408
              tBundle.putInt("tab", tOrd );
409
              tDiag.setArguments(tBundle);
410
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
411 2971588c Leszek Koltunski
              break;
412 77d3e4f6 Leszek Koltunski
      case 4: RubikDialogAbout aDiag = new RubikDialogAbout();
413 a8576d91 Leszek Koltunski
              aDiag.show(act.getSupportFragmentManager(), null);
414 e03e0352 Leszek Koltunski
              break;
415
      }
416
    }
417
418 46405bb4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
419
420 a8576d91 Leszek Koltunski
  void setupSolveButton(final RubikActivity act, final float width)
421 46405bb4 Leszek Koltunski
    {
422 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);
423
    mSolveButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
424 46405bb4 Leszek Koltunski
425 a8576d91 Leszek Koltunski
    mSolveButton.setOnClickListener( new View.OnClickListener()
426 46405bb4 Leszek Koltunski
      {
427 a8576d91 Leszek Koltunski
      @Override
428
      public void onClick(View v)
429
        {
430
        act.getPreRender().solveObject();
431 55e6be1d Leszek Koltunski
        mController.clearMoves();
432 a8576d91 Leszek Koltunski
        }
433
      });
434 46405bb4 Leszek Koltunski
    }
435
436 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
437
438
  public void savePreferences(SharedPreferences.Editor editor)
439
    {
440 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
441
    editor.putInt("statePlay_size"  , mSize);
442 4c0cd600 Leszek Koltunski
443 e03e0352 Leszek Koltunski
    if( mObjectPopup!=null )
444
      {
445
      mObjectPopup.dismiss();
446
      mObjectPopup = null;
447
      }
448
449
    if( mMenuPopup!=null )
450 4c0cd600 Leszek Koltunski
      {
451 e03e0352 Leszek Koltunski
      mMenuPopup.dismiss();
452
      mMenuPopup = null;
453 4c0cd600 Leszek Koltunski
      }
454 0254cfd7 Leszek Koltunski
455
    if( mPlayPopup!=null )
456
      {
457
      mPlayPopup.dismiss();
458
      mPlayPopup = null;
459
      }
460 211b48f2 Leszek Koltunski
    }
461
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463
464
  public void restorePreferences(SharedPreferences preferences)
465
    {
466 eb389a97 Leszek Koltunski
    mObject= preferences.getInt("statePlay_object", DEF_OBJECT);
467
    mSize  = preferences.getInt("statePlay_size"  , DEF_SIZE  );
468 e9f567ac Leszek Koltunski
469
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
470 0a7aa15b Leszek Koltunski
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
471 e9f567ac Leszek Koltunski
472
    // This means the app has been upgraded to a new version which swapped the
473
    // Object for a new one with larger sizeIndex and now getMaxLevel() returns
474
    // 0. Reset the object to default, otherwise we'll get a crash later on.
475
476 0a7aa15b Leszek Koltunski
    if( dbLevel==0 )
477 e9f567ac Leszek Koltunski
      {
478
      mObject = DEF_OBJECT;
479
      mSize   = DEF_SIZE;
480
      }
481 211b48f2 Leszek Koltunski
    }
482
483 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
484
485 9c2f0c91 Leszek Koltunski
  public boolean setObjectAndSize(RubikActivity act, ObjectList obj, int size)
486 53f23b64 Leszek Koltunski
    {
487 7b7d65ce Leszek Koltunski
    if( mObject!=obj.ordinal() || mSize != size )
488
      {
489
      boolean success = false;
490 53f23b64 Leszek Koltunski
491 7b7d65ce Leszek Koltunski
      for( int s: obj.getSizes() )
492
        if( s==size )
493
          {
494
          success = true;
495
          break;
496
          }
497
498
      if( success )
499 53f23b64 Leszek Koltunski
        {
500 7b7d65ce Leszek Koltunski
        mObject = obj.ordinal();
501
        mSize   = size;
502 5f8c532e Leszek Koltunski
503 0254cfd7 Leszek Koltunski
        if( mPlayLayout!=null ) adjustLevels(act);
504 53f23b64 Leszek Koltunski
        }
505
506 7b7d65ce Leszek Koltunski
      return success;
507
      }
508
509
    return true;
510
    }
511
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513
514 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
515 7b7d65ce Leszek Koltunski
    {
516 9c2f0c91 Leszek Koltunski
    int sizeIndex = ObjectList.getSizeIndex(mObject,mSize);
517 0a7aa15b Leszek Koltunski
    int dbLevel = ObjectList.getDBLevel(mObject, sizeIndex);
518
    int numScrambles = ObjectList.getNumScramble(mObject, sizeIndex);
519
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
520 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
521 7b7d65ce Leszek Koltunski
522 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel-1; i++)
523 53f23b64 Leszek Koltunski
      {
524 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
525 53f23b64 Leszek Koltunski
      }
526
527 4fc7b1e6 Leszek Koltunski
    if( numLevel>0 )
528
      {
529
      levels[numLevel-1] = act.getString(R.string.level_full);
530
      }
531 00af5060 Leszek Koltunski
532 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
533
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
534 00af5060 Leszek Koltunski
      {
535
      mLevelValue=1;
536
      }
537 0254cfd7 Leszek Koltunski
538 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
539
    int margin   = (int)(width*RubikActivity.MARGIN);
540
    int padding  = (int)(width*RubikActivity.PADDING);
541
    int butWidth = mPlayLayoutWidth - 2*padding;
542
    int butHeight= (int)mMenuItemSize;
543
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
544 0254cfd7 Leszek Koltunski
545 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
546 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
547 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
548 0254cfd7 Leszek Koltunski
    pT.setMargins(margin, margin, margin, margin);
549 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
550 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
551
552
    mPlayLayout.removeAllViews();
553
554 d7e539d0 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
555
556 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
557 011fcfe0 Leszek Koltunski
      {
558 0a7aa15b Leszek Koltunski
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
559
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
560 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
561 00af5060 Leszek Koltunski
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
562 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
563
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
564
565 0a7aa15b Leszek Koltunski
      int icon = scores.isSolved(mObject, sizeIndex, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
566 d7e539d0 Leszek Koltunski
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
567
568 0254cfd7 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
569 011fcfe0 Leszek Koltunski
        {
570 0254cfd7 Leszek Koltunski
        @Override
571
        public void onClick(View v)
572
          {
573 6fad862b Leszek Koltunski
          RubikPreRender pre = act.getPreRender();
574
575 0594c61f Leszek Koltunski
          if(pre.isUINotBlocked())
576 6fad862b Leszek Koltunski
            {
577
            mPlayPopup.dismiss();
578 0a7aa15b Leszek Koltunski
            mLevelValue = level;
579
            pre.scrambleObject(scrambles);
580 6fad862b Leszek Koltunski
            }
581 0254cfd7 Leszek Koltunski
          }
582
        });
583 011fcfe0 Leszek Koltunski
584 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
585
      }
586 53f23b64 Leszek Koltunski
    }
587
588 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
589
590 dca3888a Leszek Koltunski
  public int getLevel()
591 211b48f2 Leszek Koltunski
    {
592 85b09df4 Leszek Koltunski
    return mLevelValue;
593 211b48f2 Leszek Koltunski
    }
594
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596
597 4888e97c Leszek Koltunski
  public int getObject()
598 211b48f2 Leszek Koltunski
    {
599 4888e97c Leszek Koltunski
    return mObject;
600 211b48f2 Leszek Koltunski
    }
601
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603
604 4888e97c Leszek Koltunski
  public int getSize()
605 211b48f2 Leszek Koltunski
    {
606 4888e97c Leszek Koltunski
    return mSize;
607
    }
608 211b48f2 Leszek Koltunski
  }