Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 40c8c5fa

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 1f9772f3 Leszek Koltunski
package org.distorted.states;
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 9439f455 Leszek Koltunski
import android.os.Build;
26 e03e0352 Leszek Koltunski
import android.os.Bundle;
27 211b48f2 Leszek Koltunski
import android.util.DisplayMetrics;
28 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
29 4c0cd600 Leszek Koltunski
import android.view.Gravity;
30 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
31
import android.view.View;
32 9513f213 Leszek Koltunski
import android.view.ViewGroup;
33 85b09df4 Leszek Koltunski
import android.widget.AdapterView;
34
import android.widget.ArrayAdapter;
35 211b48f2 Leszek Koltunski
import android.widget.Button;
36
import android.widget.ImageButton;
37
import android.widget.LinearLayout;
38 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
39 e3c74c0f Leszek Koltunski
import android.widget.TextView;
40
41 9513f213 Leszek Koltunski
import androidx.annotation.NonNull;
42 66e777b0 Leszek Koltunski
import androidx.appcompat.widget.AppCompatSpinner;
43 211b48f2 Leszek Koltunski
44 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
45
import org.distorted.dialogs.RubikDialogScores;
46 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
47
import org.distorted.main.RubikActivity;
48 15846fe4 Leszek Koltunski
import org.distorted.main.RubikPreRender;
49
import org.distorted.objects.RubikObject;
50 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
51 211b48f2 Leszek Koltunski
52 15846fe4 Leszek Koltunski
import java.util.ArrayList;
53
54 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 15846fe4 Leszek Koltunski
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener,
57
                                                                  RubikPreRender.ActionFinishedListener
58 211b48f2 Leszek Koltunski
  {
59 15846fe4 Leszek Koltunski
  private static final int DURATION_MILLIS = 750;
60 85b09df4 Leszek Koltunski
  private static final int DEF_LEVEL =  1;
61
  public  static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal();
62
  public  static final int DEF_SIZE  =  3;
63 211b48f2 Leszek Koltunski
64 e03e0352 Leszek Koltunski
  private static int[] BUTTON_LABELS = { R.string.scores, R.string.patterns, R.string.solver, R.string.about };
65
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
66
67 15846fe4 Leszek Koltunski
  private ImageButton mObjButton, mMenuButton, mPrevButton, mSolveButton;
68
  private Button mPlayButton;
69 e03e0352 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup;
70 4888e97c Leszek Koltunski
  private int mObject = DEF_OBJECT;
71
  private int mSize   = DEF_SIZE;
72 e03e0352 Leszek Koltunski
  private int mObjectLayoutWidth, mMenuLayoutHeight;
73
  private LinearLayout mObjectLayout, mMenuLayout;
74 8e3898c8 Leszek Koltunski
  private AppCompatSpinner mLevelSpinner;
75
  private ArrayAdapter<String> mSpinnerAdapter;
76 85b09df4 Leszek Koltunski
  private int mLevelValue;
77 e3c74c0f Leszek Koltunski
  private float mButtonSize, mTitleSize;
78 211b48f2 Leszek Koltunski
79 15846fe4 Leszek Koltunski
  private ArrayList<Move> mMoves;
80
  private boolean mCanPrevMove;
81
82
  private static class Move
83
    {
84
    private int mAxis, mRow, mAngle;
85
86
    Move(int axis, int row, int angle)
87
      {
88
      mAxis = axis;
89
      mRow  = row;
90
      mAngle= angle;
91
      }
92
    }
93
94 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96 329c0aeb Leszek Koltunski
  void leaveState(RubikActivity act)
97 211b48f2 Leszek Koltunski
    {
98 85b09df4 Leszek Koltunski
99 211b48f2 Leszek Koltunski
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
104 211b48f2 Leszek Koltunski
    {
105 85b09df4 Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
106
    final float scale = metrics.density;
107 211b48f2 Leszek Koltunski
108 e3c74c0f Leszek Koltunski
    float width = act.getScreenWidthInPixels();
109
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
110
    mTitleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
111
112 15846fe4 Leszek Koltunski
    mCanPrevMove = true;
113
114
    if( mMoves==null ) mMoves = new ArrayList<>();
115
    else               mMoves.clear();
116
117 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
118 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
119 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
120 85b09df4 Leszek Koltunski
121 4f36e418 Leszek Koltunski
    setupObjectButton(act,scale);
122 85b09df4 Leszek Koltunski
    layoutTop.addView(mObjButton);
123 4f36e418 Leszek Koltunski
    setupLevelSpinner(act,scale);
124 85b09df4 Leszek Koltunski
    layoutTop.addView(mLevelSpinner);
125 4f36e418 Leszek Koltunski
    setupPlayButton(act,scale);
126 85b09df4 Leszek Koltunski
    layoutTop.addView(mPlayButton);
127 211b48f2 Leszek Koltunski
128 e03e0352 Leszek Koltunski
    setupObjectWindow(act, scale);
129
130 211b48f2 Leszek Koltunski
    // BOT ////////////////////////////
131
132 4c0cd600 Leszek Koltunski
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
133
    layoutLeft.removeAllViews();
134 4888e97c Leszek Koltunski
135 15846fe4 Leszek Koltunski
    setupPrevButton(act,scale,width);
136
    layoutLeft.addView(mPrevButton);
137
    setupSolveButton(act,scale,width);
138 31227bf6 Leszek Koltunski
    layoutLeft.addView(mSolveButton);
139 769d7b9f Leszek Koltunski
140 4c0cd600 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
141
    layoutRight.removeAllViews();
142 31227bf6 Leszek Koltunski
143 15846fe4 Leszek Koltunski
    setupMenuButton(act,scale);
144
    layoutRight.addView(mMenuButton);
145 211b48f2 Leszek Koltunski
146 e03e0352 Leszek Koltunski
    setupMenuWindow(act, scale);
147 769d7b9f Leszek Koltunski
    }
148 211b48f2 Leszek Koltunski
149 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150 4c0cd600 Leszek Koltunski
151 769d7b9f Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float scale)
152
    {
153
    int padding = (int)(3*scale + 0.5f);
154 e31abc1e Leszek Koltunski
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
155 769d7b9f Leszek Koltunski
    mObjButton = new ImageButton(act);
156
    mObjButton.setLayoutParams(objectParams);
157
    mObjButton.setPadding(padding,0,padding,0);
158
    mObjButton.setImageResource(R.drawable.cube_menu);
159
160
    mObjButton.setOnClickListener( new View.OnClickListener()
161
      {
162
      @Override
163
      public void onClick(View view)
164 4c0cd600 Leszek Koltunski
        {
165 5a4d4fba Leszek Koltunski
        if( act.getPreRender().canPlay() )
166 a42e25a6 Leszek Koltunski
          {
167
          int total = RubikObjectList.getTotal();
168
          boolean vertical = act.isVertical();
169 e03e0352 Leszek Koltunski
          mObjectLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
170 4c0cd600 Leszek Koltunski
171 a42e25a6 Leszek Koltunski
          int width  = view.getWidth();
172 e03e0352 Leszek Koltunski
          int layhei = mObjectLayoutWidth * (vertical? total:1);
173
          int laywid = mObjectLayoutWidth * (vertical? 1:total);
174 4c0cd600 Leszek Koltunski
175 e03e0352 Leszek Koltunski
          mObjectPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
176 9439f455 Leszek Koltunski
177
          if( android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 )
178
            {
179 e03e0352 Leszek Koltunski
            mObjectPopup.update(view, laywid, layhei);
180 9439f455 Leszek Koltunski
            }
181 a42e25a6 Leszek Koltunski
          }
182 769d7b9f Leszek Koltunski
        }
183
      });
184
    }
185 4c0cd600 Leszek Koltunski
186 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
  private void setupLevelSpinner(final RubikActivity act, final float scale)
189
    {
190 15846fe4 Leszek Koltunski
    int padding = (int)(scale* 10 + 0.5f);
191
    int margin  = (int)(scale*  3 + 0.5f);
192 e31abc1e Leszek Koltunski
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
193 15846fe4 Leszek Koltunski
    spinnerLayoutParams.topMargin    = margin;
194
    spinnerLayoutParams.bottomMargin = margin;
195
    spinnerLayoutParams.leftMargin   = margin;
196
    spinnerLayoutParams.rightMargin  = margin;
197 85b09df4 Leszek Koltunski
198 8e3898c8 Leszek Koltunski
    mLevelSpinner = new AppCompatSpinner(act);
199 85b09df4 Leszek Koltunski
    mLevelSpinner.setLayoutParams(spinnerLayoutParams);
200 15846fe4 Leszek Koltunski
    mLevelSpinner.setPadding(padding,0,padding,0);
201 85b09df4 Leszek Koltunski
    mLevelSpinner.setBackgroundResource(R.drawable.spinner);
202
    mLevelSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
203
204
    mLevelSpinner.setOnItemSelectedListener(this);
205 8e3898c8 Leszek Koltunski
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
206
    int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
207
    String[] levels = new String[maxLevel];
208 85b09df4 Leszek Koltunski
209 8e3898c8 Leszek Koltunski
    for(int i=0; i<maxLevel; i++)
210 85b09df4 Leszek Koltunski
      {
211
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
212
      }
213
214 c7f9e82d Leszek Koltunski
    if( mLevelValue>maxLevel ) mLevelValue=1;
215
216 9513f213 Leszek Koltunski
    mSpinnerAdapter = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, levels)
217
      {
218
      @NonNull
219
      public View getView(int position, View convertView, @NonNull ViewGroup parent)
220
        {
221
        View v = super.getView(position, convertView, parent);
222
        TextView tv = ((TextView) v);
223
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
224
        return v;
225
        }
226
      };
227
228 8e3898c8 Leszek Koltunski
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
229
    mLevelSpinner.setAdapter(mSpinnerAdapter);
230 85b09df4 Leszek Koltunski
    mLevelSpinner.setSelection(mLevelValue-1);
231
    }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235
  private void setupPlayButton(final RubikActivity act, final float scale)
236
    {
237
    int padding = (int)(3*scale + 0.5f);
238 e31abc1e Leszek Koltunski
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
239 85b09df4 Leszek Koltunski
    mPlayButton = new Button(act);
240
    mPlayButton.setLayoutParams(backParams);
241
    mPlayButton.setPadding(padding,0,padding,0);
242 e3c74c0f Leszek Koltunski
    mPlayButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
243 85b09df4 Leszek Koltunski
    mPlayButton.setText(R.string.play);
244
245
    mPlayButton.setOnClickListener( new View.OnClickListener()
246
      {
247
      @Override
248
      public void onClick(View v)
249
        {
250 5a4d4fba Leszek Koltunski
        act.getPreRender().scrambleObject(mLevelValue);
251 85b09df4 Leszek Koltunski
        }
252
      });
253
    }
254
255 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257 15846fe4 Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float scale)
258 e31abc1e Leszek Koltunski
    {
259
    int padding = (int)(3*scale + 0.5f);
260 15846fe4 Leszek Koltunski
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
261 e03e0352 Leszek Koltunski
    mMenuButton = new ImageButton(act);
262
    mMenuButton.setLayoutParams(objectParams);
263
    mMenuButton.setPadding(padding,0,padding,0);
264
    mMenuButton.setImageResource(R.drawable.menu);
265 e31abc1e Leszek Koltunski
266 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
267 e31abc1e Leszek Koltunski
      {
268
      @Override
269
      public void onClick(View view)
270
        {
271 e03e0352 Leszek Koltunski
        if( act.getPreRender().canPlay() )
272
          {
273
          int total = RubikObjectList.getTotal();
274
          boolean vertical = act.isVertical();
275
          mMenuLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
276
277
          mMenuPopup.showAsDropDown(view, 0, -3*mMenuLayoutHeight, Gravity.LEFT);
278
          mMenuPopup.update();
279
          }
280 e31abc1e Leszek Koltunski
        }
281
      });
282
    }
283
284 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286 15846fe4 Leszek Koltunski
  private void setupSolveButton(final RubikActivity act, final float scale, final float width)
287 85b09df4 Leszek Koltunski
    {
288
    int padding = (int)(3*scale + 0.5f);
289 15846fe4 Leszek Koltunski
    int widthBut = (int)(width/6);
290
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(widthBut, LinearLayout.LayoutParams.MATCH_PARENT);
291
    mSolveButton = new ImageButton(act);
292 85b09df4 Leszek Koltunski
    mSolveButton.setLayoutParams(backParams);
293
    mSolveButton.setPadding(padding,0,padding,0);
294 15846fe4 Leszek Koltunski
    mSolveButton.setImageResource(R.drawable.cube_solve);
295 85b09df4 Leszek Koltunski
296
    mSolveButton.setOnClickListener( new View.OnClickListener()
297
      {
298
      @Override
299
      public void onClick(View v)
300
        {
301 5a4d4fba Leszek Koltunski
        act.getPreRender().solveObject();
302 85b09df4 Leszek Koltunski
        }
303
      });
304
    }
305
306 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
307 4c0cd600 Leszek Koltunski
308 15846fe4 Leszek Koltunski
  private void setupPrevButton(final RubikActivity act, final float scale, final float width)
309 769d7b9f Leszek Koltunski
    {
310
    int padding = (int)(3*scale + 0.5f);
311 15846fe4 Leszek Koltunski
    int widthBut = (int)(width/6);
312
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(widthBut, LinearLayout.LayoutParams.MATCH_PARENT);
313
    mPrevButton = new ImageButton(act);
314
    mPrevButton.setLayoutParams(backParams);
315
    mPrevButton.setPadding(padding,0,padding,0);
316
    mPrevButton.setImageResource(R.drawable.cube_back);
317
318
    mPrevButton.setOnClickListener( new View.OnClickListener()
319 a6d3b158 Leszek Koltunski
      {
320
      @Override
321
      public void onClick(View v)
322
        {
323 15846fe4 Leszek Koltunski
        RubikPreRender pre = act.getPreRender();
324
        backMove(pre);
325 a6d3b158 Leszek Koltunski
        }
326
      });
327 211b48f2 Leszek Koltunski
    }
328
329 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331 e03e0352 Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float scale)
332 4c0cd600 Leszek Koltunski
    {
333 769d7b9f Leszek Koltunski
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
334
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
335 e03e0352 Leszek Koltunski
    mObjectLayout = layout.findViewById(R.id.popup);
336 769d7b9f Leszek Koltunski
337 e03e0352 Leszek Koltunski
    mObjectPopup = new PopupWindow(act);
338
    mObjectPopup.setContentView(layout);
339
    mObjectPopup.setFocusable(true);
340 769d7b9f Leszek Koltunski
    int margin = (int)(5*scale + 0.5f);
341
342
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
343
    int cubeWidth  = bd.getIntrinsicWidth();
344 e03e0352 Leszek Koltunski
    mObjectLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
345 4c0cd600 Leszek Koltunski
346 769d7b9f Leszek Koltunski
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
347
      {
348 53f23b64 Leszek Koltunski
      final RubikObjectList list = RubikObjectList.getObject(object);
349
      final int[] sizes = list.getSizes();
350 769d7b9f Leszek Koltunski
      int[] icons = list.getIconIDs();
351
      int len = sizes.length;
352
      final int obj = object;
353
354
      for(int i=0; i<len; i++)
355
        {
356
        final int size = i;
357
358
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
359
        p.setMargins(margin, margin, margin, margin);
360
361
        ImageButton button = new ImageButton(act);
362
        button.setLayoutParams(p);
363 4c0cd600 Leszek Koltunski
364 769d7b9f Leszek Koltunski
        button.setBackgroundResource(icons[i]);
365
        button.setOnClickListener( new View.OnClickListener()
366
          {
367
          @Override
368
          public void onClick(View v)
369
            {
370 5a4d4fba Leszek Koltunski
            if( act.getPreRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
371 a42e25a6 Leszek Koltunski
              {
372
              mObject = obj;
373
              mSize   = sizes[size];
374 7eae2d49 Leszek Koltunski
              act.changeObject(list,sizes[size], true);
375 7b7d65ce Leszek Koltunski
              adjustSpinner(act);
376 40c8c5fa Leszek Koltunski
              mMoves.clear();
377 a42e25a6 Leszek Koltunski
              }
378 8e3898c8 Leszek Koltunski
379 e03e0352 Leszek Koltunski
            mObjectPopup.dismiss();
380 769d7b9f Leszek Koltunski
            }
381
          });
382
383 e03e0352 Leszek Koltunski
        mObjectLayout.addView(button);
384 769d7b9f Leszek Koltunski
        }
385
      }
386 4c0cd600 Leszek Koltunski
    }
387
388 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
389
390
  private void setupMenuWindow(final RubikActivity act, final float scale)
391
    {
392
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
393
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
394
    mMenuLayout = layout.findViewById(R.id.popup);
395
396
    mMenuPopup = new PopupWindow(act);
397
    mMenuPopup.setContentView(layout);
398
    mMenuPopup.setFocusable(true);
399
    int margin = (int)(5*scale + 0.5f);
400
401
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
402
    p.setMargins(margin, margin, margin, margin);
403
404
    for(int i=0; i<NUM_BUTTONS; i++)
405
      {
406
      final int but = i;
407
      Button button = new Button(act);
408
      button.setLayoutParams(p);
409
      button.setText(BUTTON_LABELS[i]);
410
411
      button.setOnClickListener( new View.OnClickListener()
412
        {
413
        @Override
414
        public void onClick(View v)
415
          {
416
          mMenuPopup.dismiss();
417
          Action(act,but);
418
          }
419
        });
420
421
      mMenuLayout.addView(button);
422
      }
423
424
    mMenuLayoutHeight= (int)(margin + NUM_BUTTONS*(mButtonSize+margin));
425
    }
426
427 15846fe4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
428
429
  private void backMove(RubikPreRender pre)
430
    {
431
    if( mCanPrevMove )
432
      {
433
      int numMoves = mMoves.size();
434
435
      if( numMoves>0 )
436
        {
437
        Move move = mMoves.remove(numMoves-1);
438
        RubikObject object = pre.getObject();
439
440
        int axis  = move.mAxis;
441
        int row   = (1<<move.mRow);
442
        int angle = move.mAngle;
443
        int numRot= Math.abs(angle*object.getBasicAngle()/360);
444
445
        if( angle!=0 )
446
          {
447
          mCanPrevMove = false;
448
          pre.addRotation(this, axis, row, -angle, numRot*DURATION_MILLIS);
449
          }
450
        else
451
          {
452
          android.util.Log.e("solution", "error: trying to back move of angle 0");
453
          }
454
        }
455
      else
456
        {
457
        android.util.Log.e("solv", "error: no moves to back!");
458
        }
459
      }
460
    }
461 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
462
463
  private void Action(RubikActivity act, int button)
464
    {
465
    switch(button)
466
      {
467
      case 0: RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
468
              int object = play.getObject();
469
              int size   = play.getSize();
470
              int sizeIndex = RubikObjectList.getSizeIndex(object,size);
471
472
              Bundle bundle = new Bundle();
473
              bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
474
              bundle.putBoolean("submitting", false);
475
476
              RubikDialogScores scores = new RubikDialogScores();
477
              scores.setArguments(bundle);
478
              scores.show(act.getSupportFragmentManager(), null);
479
              break;
480
      case 1: RubikState.switchState(act,RubikState.PATT);
481
              break;
482
      case 2: RubikState.switchState(act,RubikState.SVER);
483
              break;
484
      case 3: RubikDialogAbout diag = new RubikDialogAbout();
485
              diag.show(act.getSupportFragmentManager(), null);
486
              break;
487
      }
488
    }
489
490 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
491
492
  public void savePreferences(SharedPreferences.Editor editor)
493
    {
494 85b09df4 Leszek Koltunski
    editor.putInt("statePlay_level" , mLevelValue);
495 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
496
    editor.putInt("statePlay_size"  , mSize);
497 4c0cd600 Leszek Koltunski
498 e03e0352 Leszek Koltunski
    if( mObjectPopup!=null )
499
      {
500
      mObjectPopup.dismiss();
501
      mObjectPopup = null;
502
      }
503
504
    if( mMenuPopup!=null )
505 4c0cd600 Leszek Koltunski
      {
506 e03e0352 Leszek Koltunski
      mMenuPopup.dismiss();
507
      mMenuPopup = null;
508 4c0cd600 Leszek Koltunski
      }
509 211b48f2 Leszek Koltunski
    }
510
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512
513
  public void restorePreferences(SharedPreferences preferences)
514
    {
515 85b09df4 Leszek Koltunski
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
516
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
517
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
518 211b48f2 Leszek Koltunski
    }
519
520 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
521
522 7b7d65ce Leszek Koltunski
  public boolean setObjectAndSize(RubikActivity act, RubikObjectList obj, int size)
523 53f23b64 Leszek Koltunski
    {
524 7b7d65ce Leszek Koltunski
    if( mObject!=obj.ordinal() || mSize != size )
525
      {
526
      boolean success = false;
527 53f23b64 Leszek Koltunski
528 7b7d65ce Leszek Koltunski
      for( int s: obj.getSizes() )
529
        if( s==size )
530
          {
531
          success = true;
532
          break;
533
          }
534
535
      if( success )
536 53f23b64 Leszek Koltunski
        {
537 7b7d65ce Leszek Koltunski
        mObject = obj.ordinal();
538
        mSize   = size;
539 5f8c532e Leszek Koltunski
540
        if( mLevelSpinner!=null ) adjustSpinner(act);
541 53f23b64 Leszek Koltunski
        }
542
543 7b7d65ce Leszek Koltunski
      return success;
544
      }
545
546
    return true;
547
    }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
551
  private void adjustSpinner(RubikActivity act)
552
    {
553
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
554
    int maxLevel  = RubikObjectList.getMaxLevel(mObject, sizeIndex);
555
    String[] levels = new String[maxLevel];
556
557
    for(int i=0; i<maxLevel; i++)
558 53f23b64 Leszek Koltunski
      {
559 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
560 53f23b64 Leszek Koltunski
      }
561
562 011fcfe0 Leszek Koltunski
    mSpinnerAdapter = new ArrayAdapter<String>(act, android.R.layout.simple_spinner_item, levels)
563
      {
564
      @NonNull
565
      public View getView(int position, View convertView, @NonNull ViewGroup parent)
566
        {
567
        View v = super.getView(position, convertView, parent);
568
        TextView tv = ((TextView) v);
569
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
570
        return v;
571
        }
572
      };
573
574 7b7d65ce Leszek Koltunski
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
575
    mLevelSpinner.setAdapter(mSpinnerAdapter);
576 53f23b64 Leszek Koltunski
    }
577
578 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
579
580 85b09df4 Leszek Koltunski
  int getLevel()
581 211b48f2 Leszek Koltunski
    {
582 85b09df4 Leszek Koltunski
    return mLevelValue;
583 211b48f2 Leszek Koltunski
    }
584
585 15846fe4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
586
587
  public void addMove(int axis, int row, int angle)
588
    {
589
    mMoves.add(new Move(axis,row,angle));
590
    }
591
592 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
593
594 4888e97c Leszek Koltunski
  public int getObject()
595 211b48f2 Leszek Koltunski
    {
596 4888e97c Leszek Koltunski
    return mObject;
597 211b48f2 Leszek Koltunski
    }
598
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600
601 4888e97c Leszek Koltunski
  public int getSize()
602 211b48f2 Leszek Koltunski
    {
603 4888e97c Leszek Koltunski
    return mSize;
604
    }
605 85b09df4 Leszek Koltunski
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607
608
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
609
    {
610
    mLevelValue = pos+1;
611
    }
612
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
615
  public void onNothingSelected(AdapterView<?> parent) { }
616 15846fe4 Leszek Koltunski
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618
619
  public void onActionFinished(final long effectID)
620
    {
621
    mCanPrevMove = true;
622
    }
623 211b48f2 Leszek Koltunski
  }