Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 4f36e418

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
    setupObjectButton(act,scale);
80
    layoutTop.addView(mObjButton);
81
    setupLevelSpinner(act,scale);
82
    layoutTop.addView(mLevelSpinner);
83
    setupPlayButton(act,scale);
84
    layoutTop.addView(mPlayButton);
85

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

    
88
    setupSolveButton(act,scale);
89

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

    
94
    setupBackButton(act,scale);
95

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

    
100
    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
    if( mLevelValue>maxLevel ) mLevelValue=1;
170

    
171
    mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
172
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
173
    mLevelSpinner.setAdapter(mSpinnerAdapter);
174
    mLevelSpinner.setSelection(mLevelValue-1);
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void setupPlayButton(final RubikActivity act, final float scale)
180
    {
181
    int padding = (int)(3*scale + 0.5f);
182
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
183
    mPlayButton = new Button(act);
184
    mPlayButton.setLayoutParams(backParams);
185
    mPlayButton.setPadding(padding,0,padding,0);
186
    mPlayButton.setText(R.string.play);
187

    
188
    mPlayButton.setOnClickListener( new View.OnClickListener()
189
      {
190
      @Override
191
      public void onClick(View v)
192
        {
193
        act.getPostRender().scrambleObject(mLevelValue);
194
        }
195
      });
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void setupSolveButton(final RubikActivity act, final float scale)
201
    {
202
    int padding = (int)(3*scale + 0.5f);
203
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
204
    mSolveButton = new Button(act);
205
    mSolveButton.setLayoutParams(backParams);
206
    mSolveButton.setPadding(padding,0,padding,0);
207
    mSolveButton.setText(R.string.solve);
208

    
209
    mSolveButton.setOnClickListener( new View.OnClickListener()
210
      {
211
      @Override
212
      public void onClick(View v)
213
        {
214
        act.getPostRender().solveObject();
215
        }
216
      });
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private void setupBackButton(final RubikActivity act, final float scale)
222
    {
223
    int padding = (int)(3*scale + 0.5f);
224
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
225
    mBackButton = new Button(act);
226
    mBackButton.setLayoutParams(backParams);
227
    mBackButton.setPadding(padding,0,padding,0);
228
    mBackButton.setText(R.string.back);
229

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  private void setupPopupWindow(final RubikActivity act, final float scale)
243
    {
244
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
245
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
246
    mLayout = layout.findViewById(R.id.popup);
247

    
248
    mPopup = new PopupWindow(act);
249
    mPopup.setContentView(layout);
250
    mPopup.setFocusable(true);
251
    int margin = (int)(5*scale + 0.5f);
252

    
253
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
254
    int cubeWidth  = bd.getIntrinsicWidth();
255
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
256

    
257
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
258
      {
259
      final RubikObjectList list = RubikObjectList.getObject(object);
260
      final int[] sizes = list.getSizes();
261
      int[] icons = list.getIconIDs();
262
      int len = sizes.length;
263
      final int obj = object;
264

    
265
      for(int i=0; i<len; i++)
266
        {
267
        final int size = i;
268

    
269
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
270
        p.setMargins(margin, margin, margin, margin);
271

    
272
        ImageButton button = new ImageButton(act);
273
        button.setLayoutParams(p);
274

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

    
289
            mPopup.dismiss();
290
            }
291
          });
292

    
293
        mLayout.addView(button);
294
        }
295
      }
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  public void savePreferences(SharedPreferences.Editor editor)
301
    {
302
    editor.putInt("statePlay_level" , mLevelValue);
303
    editor.putInt("statePlay_object", mObject);
304
    editor.putInt("statePlay_size"  , mSize);
305

    
306
    if( mPopup!=null )
307
      {
308
      mPopup.dismiss();
309
      mPopup = null;
310
      }
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  public void restorePreferences(SharedPreferences preferences)
316
    {
317
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
318
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
319
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  public boolean setObjectAndSize(RubikActivity act, RubikObjectList obj, int size)
325
    {
326
    if( mObject!=obj.ordinal() || mSize != size )
327
      {
328
      boolean success = false;
329

    
330
      for( int s: obj.getSizes() )
331
        if( s==size )
332
          {
333
          success = true;
334
          break;
335
          }
336

    
337
      if( success )
338
        {
339
        mObject = obj.ordinal();
340
        mSize   = size;
341

    
342
        if( mLevelSpinner!=null ) adjustSpinner(act);
343
        }
344

    
345
      return success;
346
      }
347

    
348
    return true;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  private void adjustSpinner(RubikActivity act)
354
    {
355
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
356
    int maxLevel  = RubikObjectList.getMaxLevel(mObject, sizeIndex);
357
    String[] levels = new String[maxLevel];
358

    
359
    for(int i=0; i<maxLevel; i++)
360
      {
361
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
362
      }
363

    
364
    mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
365
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
366
    mLevelSpinner.setAdapter(mSpinnerAdapter);
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  int getLevel()
372
    {
373
    return mLevelValue;
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

    
378
  public int getObject()
379
    {
380
    return mObject;
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
  public int getSize()
386
    {
387
    return mSize;
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
393
    {
394
    mLevelValue = pos+1;
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
  public void onNothingSelected(AdapterView<?> parent) { }
400
  }
(6-6/10)