Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 66e777b0

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 66e777b0 Leszek Koltunski
import androidx.appcompat.widget.AppCompatSpinner;
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 DEF_OBJECT= RubikObjectList.CUBE.ordinal();
47
  public  static final int DEF_SIZE  =  3;
48 211b48f2 Leszek Koltunski
49 4c0cd600 Leszek Koltunski
  private ImageButton mObjButton;
50 85b09df4 Leszek Koltunski
  private Button mBackButton, mSolveButton, mPlayButton;
51 4c0cd600 Leszek Koltunski
  private PopupWindow mPopup;
52 4888e97c Leszek Koltunski
  private int mObject = DEF_OBJECT;
53
  private int mSize   = DEF_SIZE;
54 85b09df4 Leszek Koltunski
  private int mLayoutWidth;
55 4c0cd600 Leszek Koltunski
  private LinearLayout mLayout;
56 8e3898c8 Leszek Koltunski
  private AppCompatSpinner mLevelSpinner;
57
  private ArrayAdapter<String> mSpinnerAdapter;
58 85b09df4 Leszek Koltunski
  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 8e3898c8 Leszek Koltunski
    mLevelSpinner = new AppCompatSpinner(act);
147 85b09df4 Leszek Koltunski
    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 8e3898c8 Leszek Koltunski
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
154
    int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
155
    String[] levels = new String[maxLevel];
156 85b09df4 Leszek Koltunski
157 8e3898c8 Leszek Koltunski
    for(int i=0; i<maxLevel; i++)
158 85b09df4 Leszek Koltunski
      {
159
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
160
      }
161
162 8e3898c8 Leszek Koltunski
    mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
163
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
164
    mLevelSpinner.setAdapter(mSpinnerAdapter);
165 85b09df4 Leszek Koltunski
    mLevelSpinner.setSelection(mLevelValue-1);
166
    }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170
  private void setupPlayButton(final RubikActivity act, final float scale)
171
    {
172
    int padding = (int)(3*scale + 0.5f);
173
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
174
    mPlayButton = new Button(act);
175
    mPlayButton.setLayoutParams(backParams);
176
    mPlayButton.setPadding(padding,0,padding,0);
177
    mPlayButton.setText(R.string.play);
178
179
    mPlayButton.setOnClickListener( new View.OnClickListener()
180
      {
181
      @Override
182
      public void onClick(View v)
183
        {
184
        act.getPostRender().scrambleObject(mLevelValue);
185
        }
186
      });
187
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
  private void setupSolveButton(final RubikActivity act, final float scale)
192
    {
193
    int padding = (int)(3*scale + 0.5f);
194
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
195
    mSolveButton = new Button(act);
196
    mSolveButton.setLayoutParams(backParams);
197
    mSolveButton.setPadding(padding,0,padding,0);
198
    mSolveButton.setText(R.string.solve);
199
200
    mSolveButton.setOnClickListener( new View.OnClickListener()
201
      {
202
      @Override
203
      public void onClick(View v)
204
        {
205
        act.getPostRender().solveObject();
206
        }
207
      });
208
    }
209
210 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211 4c0cd600 Leszek Koltunski
212 769d7b9f Leszek Koltunski
  private void setupBackButton(final RubikActivity act, final float scale)
213
    {
214
    int padding = (int)(3*scale + 0.5f);
215
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
216
    mBackButton = new Button(act);
217
    mBackButton.setLayoutParams(backParams);
218
    mBackButton.setPadding(padding,0,padding,0);
219
    mBackButton.setText(R.string.back);
220 a6d3b158 Leszek Koltunski
221
    mBackButton.setOnClickListener( new View.OnClickListener()
222
      {
223
      @Override
224
      public void onClick(View v)
225
        {
226 a42e25a6 Leszek Koltunski
        if( act.getPostRender().canPlay() ) RubikState.goBack(act);
227 a6d3b158 Leszek Koltunski
        }
228
      });
229 211b48f2 Leszek Koltunski
    }
230
231 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233 769d7b9f Leszek Koltunski
  private void setupPopupWindow(final RubikActivity act, final float scale)
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
    mLayout = layout.findViewById(R.id.popup);
238
239
    mPopup = new PopupWindow(act);
240
    mPopup.setContentView(layout);
241
    mPopup.setFocusable(true);
242
    int margin = (int)(5*scale + 0.5f);
243
244
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
245
    int cubeWidth  = bd.getIntrinsicWidth();
246
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
247 4c0cd600 Leszek Koltunski
248 769d7b9f Leszek Koltunski
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
249
      {
250 53f23b64 Leszek Koltunski
      final RubikObjectList list = RubikObjectList.getObject(object);
251
      final int[] sizes = list.getSizes();
252 769d7b9f Leszek Koltunski
      int[] icons = list.getIconIDs();
253
      int len = sizes.length;
254
      final int obj = object;
255
256
      for(int i=0; i<len; i++)
257
        {
258
        final int size = i;
259
260
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
261
        p.setMargins(margin, margin, margin, margin);
262
263
        ImageButton button = new ImageButton(act);
264
        button.setLayoutParams(p);
265 4c0cd600 Leszek Koltunski
266 769d7b9f Leszek Koltunski
        button.setBackgroundResource(icons[i]);
267
        button.setOnClickListener( new View.OnClickListener()
268
          {
269
          @Override
270
          public void onClick(View v)
271
            {
272 a42e25a6 Leszek Koltunski
            if( act.getPostRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
273
              {
274
              mObject = obj;
275
              mSize   = sizes[size];
276
              act.changeObject(list,sizes[size],null);
277 8e3898c8 Leszek Koltunski
278
              int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
279
              int maxLevel  = RubikObjectList.getMaxLevel(mObject, sizeIndex);
280
              String[] levels = new String[maxLevel];
281
282
              for(int i=0; i<maxLevel; i++)
283
                {
284
                levels[i] = act.getString(R.string.lv_placeholder,i+1);
285
                }
286
287
              mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
288
              mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
289
              mLevelSpinner.setAdapter(mSpinnerAdapter);
290 a42e25a6 Leszek Koltunski
              }
291 8e3898c8 Leszek Koltunski
292 769d7b9f Leszek Koltunski
            mPopup.dismiss();
293
            }
294
          });
295
296
        mLayout.addView(button);
297
        }
298
      }
299 4c0cd600 Leszek Koltunski
    }
300
301 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
  public void savePreferences(SharedPreferences.Editor editor)
304
    {
305 85b09df4 Leszek Koltunski
    editor.putInt("statePlay_level" , mLevelValue);
306 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
307
    editor.putInt("statePlay_size"  , mSize);
308 4c0cd600 Leszek Koltunski
309 85b09df4 Leszek Koltunski
    mObjButton   = null;
310
    mBackButton  = null;
311
    mSolveButton = null;
312
    mPlayButton  = null;
313
    mLevelSpinner= null;
314 4c0cd600 Leszek Koltunski
315
    if( mPopup!=null )
316
      {
317
      mPopup.dismiss();
318 53f23b64 Leszek Koltunski
      mPopup = null;
319 4c0cd600 Leszek Koltunski
      }
320 211b48f2 Leszek Koltunski
    }
321
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324
  public void restorePreferences(SharedPreferences preferences)
325
    {
326 85b09df4 Leszek Koltunski
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
327
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
328
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
329 211b48f2 Leszek Koltunski
    }
330
331 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
  public boolean setObjectAndSize(RubikObjectList obj, int size)
334
    {
335
    boolean success = false;
336
337
    for( int s: obj.getSizes() )
338
      if( s==size )
339
        {
340
        success = true;
341
        break;
342
        }
343
344
    if( success )
345
      {
346
      mObject = obj.ordinal();
347
      mSize   = size;
348
      }
349
350
    return success;
351
    }
352
353 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355 85b09df4 Leszek Koltunski
  int getLevel()
356 211b48f2 Leszek Koltunski
    {
357 85b09df4 Leszek Koltunski
    return mLevelValue;
358 211b48f2 Leszek Koltunski
    }
359
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
362 4888e97c Leszek Koltunski
  public int getObject()
363 211b48f2 Leszek Koltunski
    {
364 4888e97c Leszek Koltunski
    return mObject;
365 211b48f2 Leszek Koltunski
    }
366
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
369 4888e97c Leszek Koltunski
  public int getSize()
370 211b48f2 Leszek Koltunski
    {
371 4888e97c Leszek Koltunski
    return mSize;
372
    }
373 85b09df4 Leszek Koltunski
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
376
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
377
    {
378
    mLevelValue = pos+1;
379
    }
380
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
383
  public void onNothingSelected(AdapterView<?> parent) { }
384 211b48f2 Leszek Koltunski
  }