Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 9439f455

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 211b48f2 Leszek Koltunski
import android.util.DisplayMetrics;
27 4c0cd600 Leszek Koltunski
import android.view.Gravity;
28 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
29
import android.view.View;
30 85b09df4 Leszek Koltunski
import android.widget.AdapterView;
31
import android.widget.ArrayAdapter;
32 211b48f2 Leszek Koltunski
import android.widget.Button;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
36 66e777b0 Leszek Koltunski
import androidx.appcompat.widget.AppCompatSpinner;
37 211b48f2 Leszek Koltunski
38 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
39
import org.distorted.main.RubikActivity;
40
import org.distorted.objects.RubikObjectList;
41 211b48f2 Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 85b09df4 Leszek Koltunski
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener
45 211b48f2 Leszek Koltunski
  {
46 85b09df4 Leszek Koltunski
  private static final int DEF_LEVEL =  1;
47
  public  static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal();
48
  public  static final int DEF_SIZE  =  3;
49 211b48f2 Leszek Koltunski
50 4c0cd600 Leszek Koltunski
  private ImageButton mObjButton;
51 85b09df4 Leszek Koltunski
  private Button mBackButton, mSolveButton, mPlayButton;
52 4c0cd600 Leszek Koltunski
  private PopupWindow mPopup;
53 4888e97c Leszek Koltunski
  private int mObject = DEF_OBJECT;
54
  private int mSize   = DEF_SIZE;
55 85b09df4 Leszek Koltunski
  private int mLayoutWidth;
56 4c0cd600 Leszek Koltunski
  private LinearLayout mLayout;
57 8e3898c8 Leszek Koltunski
  private AppCompatSpinner mLevelSpinner;
58
  private ArrayAdapter<String> mSpinnerAdapter;
59 85b09df4 Leszek Koltunski
  private int mLevelValue;
60 211b48f2 Leszek Koltunski
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 329c0aeb Leszek Koltunski
  void leaveState(RubikActivity act)
64 211b48f2 Leszek Koltunski
    {
65 85b09df4 Leszek Koltunski
66 211b48f2 Leszek Koltunski
    }
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
71 211b48f2 Leszek Koltunski
    {
72 85b09df4 Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
73
    final float scale = metrics.density;
74 211b48f2 Leszek Koltunski
75
    // TOP ////////////////////////////
76 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
77 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
78 85b09df4 Leszek Koltunski
79
    if( mObjButton   ==null ) setupObjectButton(act,scale);
80
    layoutTop.addView(mObjButton);
81
    if( mLevelSpinner==null ) setupLevelSpinner(act,scale);
82
    layoutTop.addView(mLevelSpinner);
83
    if( mPlayButton  ==null ) setupPlayButton(act,scale);
84
    layoutTop.addView(mPlayButton);
85 211b48f2 Leszek Koltunski
86
    // BOT ////////////////////////////
87
88 85b09df4 Leszek Koltunski
    if( mSolveButton==null ) setupSolveButton(act,scale);
89 769d7b9f Leszek Koltunski
90 4c0cd600 Leszek Koltunski
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
91
    layoutLeft.removeAllViews();
92 85b09df4 Leszek Koltunski
    layoutLeft.addView(mSolveButton);
93 4888e97c Leszek Koltunski
94 769d7b9f Leszek Koltunski
    if( mBackButton==null ) setupBackButton(act,scale);
95
96 4c0cd600 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
97
    layoutRight.removeAllViews();
98
    layoutRight.addView(mBackButton);
99 211b48f2 Leszek Koltunski
100 769d7b9f Leszek Koltunski
    if( mPopup==null ) setupPopupWindow(act, scale);
101
    }
102 211b48f2 Leszek Koltunski
103 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104 4c0cd600 Leszek Koltunski
105 769d7b9f Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float scale)
106
    {
107
    int padding = (int)(3*scale + 0.5f);
108
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
109
    mObjButton = new ImageButton(act);
110
    mObjButton.setLayoutParams(objectParams);
111
    mObjButton.setPadding(padding,0,padding,0);
112
    mObjButton.setImageResource(R.drawable.cube_menu);
113
114
    mObjButton.setOnClickListener( new View.OnClickListener()
115
      {
116
      @Override
117
      public void onClick(View view)
118 4c0cd600 Leszek Koltunski
        {
119 a42e25a6 Leszek Koltunski
        if( act.getPostRender().canPlay() )
120
          {
121
          int total = RubikObjectList.getTotal();
122
          boolean vertical = act.isVertical();
123
          mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
124 4c0cd600 Leszek Koltunski
125 a42e25a6 Leszek Koltunski
          int width  = view.getWidth();
126 9439f455 Leszek Koltunski
          int layhei = mLayoutWidth * (vertical? total:1);
127 a42e25a6 Leszek Koltunski
          int laywid = mLayoutWidth * (vertical? 1:total);
128 4c0cd600 Leszek Koltunski
129 85b09df4 Leszek Koltunski
          mPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
130 9439f455 Leszek Koltunski
131
          if( android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 )
132
            {
133
            mPopup.update(view, laywid, layhei);
134
            }
135 a42e25a6 Leszek Koltunski
          }
136 769d7b9f Leszek Koltunski
        }
137
      });
138
    }
139 4c0cd600 Leszek Koltunski
140 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  private void setupLevelSpinner(final RubikActivity act, final float scale)
143
    {
144
    int spinnerPadding = (int)(scale* 10 + 0.5f);
145
    int spinnerMargin  = (int)(scale*  3 + 0.5f);
146
    int spinnerLength  = (int)(scale*150 + 0.5f);
147
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(spinnerLength,LinearLayout.LayoutParams.MATCH_PARENT);
148
    spinnerLayoutParams.topMargin    =   spinnerMargin;
149
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
150
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
151
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
152
153 8e3898c8 Leszek Koltunski
    mLevelSpinner = new AppCompatSpinner(act);
154 85b09df4 Leszek Koltunski
    mLevelSpinner.setLayoutParams(spinnerLayoutParams);
155
    mLevelSpinner.setPadding(spinnerPadding,0,spinnerPadding,0);
156
    mLevelSpinner.setBackgroundResource(R.drawable.spinner);
157
    mLevelSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
158
159
    mLevelSpinner.setOnItemSelectedListener(this);
160 8e3898c8 Leszek Koltunski
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
161
    int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
162
    String[] levels = new String[maxLevel];
163 85b09df4 Leszek Koltunski
164 8e3898c8 Leszek Koltunski
    for(int i=0; i<maxLevel; i++)
165 85b09df4 Leszek Koltunski
      {
166
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
167
      }
168
169 8e3898c8 Leszek Koltunski
    mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
170
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
171
    mLevelSpinner.setAdapter(mSpinnerAdapter);
172 85b09df4 Leszek Koltunski
    mLevelSpinner.setSelection(mLevelValue-1);
173
    }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
  private void setupPlayButton(final RubikActivity act, final float scale)
178
    {
179
    int padding = (int)(3*scale + 0.5f);
180
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
181
    mPlayButton = new Button(act);
182
    mPlayButton.setLayoutParams(backParams);
183
    mPlayButton.setPadding(padding,0,padding,0);
184
    mPlayButton.setText(R.string.play);
185
186
    mPlayButton.setOnClickListener( new View.OnClickListener()
187
      {
188
      @Override
189
      public void onClick(View v)
190
        {
191
        act.getPostRender().scrambleObject(mLevelValue);
192
        }
193
      });
194
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
  private void setupSolveButton(final RubikActivity act, final float scale)
199
    {
200
    int padding = (int)(3*scale + 0.5f);
201
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
202
    mSolveButton = new Button(act);
203
    mSolveButton.setLayoutParams(backParams);
204
    mSolveButton.setPadding(padding,0,padding,0);
205
    mSolveButton.setText(R.string.solve);
206
207
    mSolveButton.setOnClickListener( new View.OnClickListener()
208
      {
209
      @Override
210
      public void onClick(View v)
211
        {
212
        act.getPostRender().solveObject();
213
        }
214
      });
215
    }
216
217 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218 4c0cd600 Leszek Koltunski
219 769d7b9f Leszek Koltunski
  private void setupBackButton(final RubikActivity act, final float scale)
220
    {
221
    int padding = (int)(3*scale + 0.5f);
222
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
223
    mBackButton = new Button(act);
224
    mBackButton.setLayoutParams(backParams);
225
    mBackButton.setPadding(padding,0,padding,0);
226
    mBackButton.setText(R.string.back);
227 a6d3b158 Leszek Koltunski
228
    mBackButton.setOnClickListener( new View.OnClickListener()
229
      {
230
      @Override
231
      public void onClick(View v)
232
        {
233 a42e25a6 Leszek Koltunski
        if( act.getPostRender().canPlay() ) RubikState.goBack(act);
234 a6d3b158 Leszek Koltunski
        }
235
      });
236 211b48f2 Leszek Koltunski
    }
237
238 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240 769d7b9f Leszek Koltunski
  private void setupPopupWindow(final RubikActivity act, final float scale)
241 4c0cd600 Leszek Koltunski
    {
242 769d7b9f Leszek Koltunski
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
243
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
244
    mLayout = layout.findViewById(R.id.popup);
245
246
    mPopup = new PopupWindow(act);
247
    mPopup.setContentView(layout);
248
    mPopup.setFocusable(true);
249
    int margin = (int)(5*scale + 0.5f);
250
251
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
252
    int cubeWidth  = bd.getIntrinsicWidth();
253
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
254 4c0cd600 Leszek Koltunski
255 769d7b9f Leszek Koltunski
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
256
      {
257 53f23b64 Leszek Koltunski
      final RubikObjectList list = RubikObjectList.getObject(object);
258
      final int[] sizes = list.getSizes();
259 769d7b9f Leszek Koltunski
      int[] icons = list.getIconIDs();
260
      int len = sizes.length;
261
      final int obj = object;
262
263
      for(int i=0; i<len; i++)
264
        {
265
        final int size = i;
266
267
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
268
        p.setMargins(margin, margin, margin, margin);
269
270
        ImageButton button = new ImageButton(act);
271
        button.setLayoutParams(p);
272 4c0cd600 Leszek Koltunski
273 769d7b9f Leszek Koltunski
        button.setBackgroundResource(icons[i]);
274
        button.setOnClickListener( new View.OnClickListener()
275
          {
276
          @Override
277
          public void onClick(View v)
278
            {
279 a42e25a6 Leszek Koltunski
            if( act.getPostRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
280
              {
281
              mObject = obj;
282
              mSize   = sizes[size];
283
              act.changeObject(list,sizes[size],null);
284 8e3898c8 Leszek Koltunski
285
              int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
286
              int maxLevel  = RubikObjectList.getMaxLevel(mObject, sizeIndex);
287
              String[] levels = new String[maxLevel];
288
289
              for(int i=0; i<maxLevel; i++)
290
                {
291
                levels[i] = act.getString(R.string.lv_placeholder,i+1);
292
                }
293
294
              mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
295
              mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
296
              mLevelSpinner.setAdapter(mSpinnerAdapter);
297 a42e25a6 Leszek Koltunski
              }
298 8e3898c8 Leszek Koltunski
299 769d7b9f Leszek Koltunski
            mPopup.dismiss();
300
            }
301
          });
302
303
        mLayout.addView(button);
304
        }
305
      }
306 4c0cd600 Leszek Koltunski
    }
307
308 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
  public void savePreferences(SharedPreferences.Editor editor)
311
    {
312 85b09df4 Leszek Koltunski
    editor.putInt("statePlay_level" , mLevelValue);
313 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
314
    editor.putInt("statePlay_size"  , mSize);
315 4c0cd600 Leszek Koltunski
316 85b09df4 Leszek Koltunski
    mObjButton   = null;
317
    mBackButton  = null;
318
    mSolveButton = null;
319
    mPlayButton  = null;
320
    mLevelSpinner= null;
321 4c0cd600 Leszek Koltunski
322
    if( mPopup!=null )
323
      {
324
      mPopup.dismiss();
325 53f23b64 Leszek Koltunski
      mPopup = null;
326 4c0cd600 Leszek Koltunski
      }
327 211b48f2 Leszek Koltunski
    }
328
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
331
  public void restorePreferences(SharedPreferences preferences)
332
    {
333 85b09df4 Leszek Koltunski
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
334
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
335
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
336 211b48f2 Leszek Koltunski
    }
337
338 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
  public boolean setObjectAndSize(RubikObjectList obj, int size)
341
    {
342
    boolean success = false;
343
344
    for( int s: obj.getSizes() )
345
      if( s==size )
346
        {
347
        success = true;
348
        break;
349
        }
350
351
    if( success )
352
      {
353
      mObject = obj.ordinal();
354
      mSize   = size;
355
      }
356
357
    return success;
358
    }
359
360 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
361
362 85b09df4 Leszek Koltunski
  int getLevel()
363 211b48f2 Leszek Koltunski
    {
364 85b09df4 Leszek Koltunski
    return mLevelValue;
365 211b48f2 Leszek Koltunski
    }
366
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
369 4888e97c Leszek Koltunski
  public int getObject()
370 211b48f2 Leszek Koltunski
    {
371 4888e97c Leszek Koltunski
    return mObject;
372 211b48f2 Leszek Koltunski
    }
373
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
376 4888e97c Leszek Koltunski
  public int getSize()
377 211b48f2 Leszek Koltunski
    {
378 4888e97c Leszek Koltunski
    return mSize;
379
    }
380 85b09df4 Leszek Koltunski
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
383
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
384
    {
385
    mLevelValue = pos+1;
386
    }
387
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
390
  public void onNothingSelected(AdapterView<?> parent) { }
391 211b48f2 Leszek Koltunski
  }