Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 834b2618

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