Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.states;
21

    
22
import android.content.Context;
23
import android.content.SharedPreferences;
24
import android.graphics.drawable.BitmapDrawable;
25
import android.os.Build;
26
import android.util.DisplayMetrics;
27
import android.view.Gravity;
28
import android.view.LayoutInflater;
29
import android.view.View;
30
import android.widget.AdapterView;
31
import android.widget.ArrayAdapter;
32
import android.widget.Button;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35
import android.widget.PopupWindow;
36
import androidx.appcompat.widget.AppCompatSpinner;
37

    
38
import org.distorted.main.R;
39
import org.distorted.main.RubikActivity;
40
import org.distorted.objects.RubikObjectList;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener
45
  {
46
  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

    
50
  private ImageButton mObjButton;
51
  private Button mBackButton, mSolveButton, mPlayButton;
52
  private PopupWindow mPopup;
53
  private int mObject = DEF_OBJECT;
54
  private int mSize   = DEF_SIZE;
55
  private int mLayoutWidth;
56
  private LinearLayout mLayout;
57
  private AppCompatSpinner mLevelSpinner;
58
  private ArrayAdapter<String> mSpinnerAdapter;
59
  private int mLevelValue;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  void leaveState(RubikActivity act)
64
    {
65

    
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  void enterState(final RubikActivity act)
71
    {
72
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
73
    final float scale = metrics.density;
74

    
75
    // TOP ////////////////////////////
76
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
77
    layoutTop.removeAllViews();
78

    
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

    
86
    // BOT ////////////////////////////
87

    
88
    if( mSolveButton==null ) setupSolveButton(act,scale);
89

    
90
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
91
    layoutLeft.removeAllViews();
92
    layoutLeft.addView(mSolveButton);
93

    
94
    if( mBackButton==null ) setupBackButton(act,scale);
95

    
96
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
97
    layoutRight.removeAllViews();
98
    layoutRight.addView(mBackButton);
99

    
100
    if( mPopup==null ) setupPopupWindow(act, scale);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  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
        {
119
        if( act.getPostRender().canPlay() )
120
          {
121
          int total = RubikObjectList.getTotal();
122
          boolean vertical = act.isVertical();
123
          mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
124

    
125
          int width  = view.getWidth();
126
          int layhei = mLayoutWidth * (vertical? total:1);
127
          int laywid = mLayoutWidth * (vertical? 1:total);
128

    
129
          mPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
130

    
131
          if( android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 )
132
            {
133
            mPopup.update(view, laywid, layhei);
134
            }
135
          }
136
        }
137
      });
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    mLevelSpinner = new AppCompatSpinner(act);
154
    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
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
161
    int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
162
    String[] levels = new String[maxLevel];
163

    
164
    for(int i=0; i<maxLevel; i++)
165
      {
166
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
167
      }
168

    
169
    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
    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
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  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

    
228
    mBackButton.setOnClickListener( new View.OnClickListener()
229
      {
230
      @Override
231
      public void onClick(View v)
232
        {
233
        if( act.getPostRender().canPlay() ) RubikState.goBack(act);
234
        }
235
      });
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  private void setupPopupWindow(final RubikActivity act, final float scale)
241
    {
242
    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

    
255
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
256
      {
257
      final RubikObjectList list = RubikObjectList.getObject(object);
258
      final int[] sizes = list.getSizes();
259
      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

    
273
        button.setBackgroundResource(icons[i]);
274
        button.setOnClickListener( new View.OnClickListener()
275
          {
276
          @Override
277
          public void onClick(View v)
278
            {
279
            if( act.getPostRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
280
              {
281
              mObject = obj;
282
              mSize   = sizes[size];
283
              act.changeObject(list,sizes[size],null);
284

    
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
              }
298

    
299
            mPopup.dismiss();
300
            }
301
          });
302

    
303
        mLayout.addView(button);
304
        }
305
      }
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309

    
310
  public void savePreferences(SharedPreferences.Editor editor)
311
    {
312
    editor.putInt("statePlay_level" , mLevelValue);
313
    editor.putInt("statePlay_object", mObject);
314
    editor.putInt("statePlay_size"  , mSize);
315

    
316
    mObjButton   = null;
317
    mBackButton  = null;
318
    mSolveButton = null;
319
    mPlayButton  = null;
320
    mLevelSpinner= null;
321

    
322
    if( mPopup!=null )
323
      {
324
      mPopup.dismiss();
325
      mPopup = null;
326
      }
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  public void restorePreferences(SharedPreferences preferences)
332
    {
333
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
334
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
335
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  int getLevel()
363
    {
364
    return mLevelValue;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  public int getObject()
370
    {
371
    return mObject;
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  public int getSize()
377
    {
378
    return mSize;
379
    }
380

    
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
  }
(5-5/8)