Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 85b09df4

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