Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ e31abc1e

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.dialogs.RubikDialogEffects;
39
import org.distorted.main.R;
40
import org.distorted.main.RubikActivity;
41
import org.distorted.objects.RubikObjectList;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener
46
  {
47
  private static final int DEF_LEVEL =  1;
48
  public  static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal();
49
  public  static final int DEF_SIZE  =  3;
50

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  void leaveState(RubikActivity act)
65
    {
66

    
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

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

    
80
    setupObjectButton(act,scale);
81
    layoutTop.addView(mObjButton);
82
    setupLevelSpinner(act,scale);
83
    layoutTop.addView(mLevelSpinner);
84
    setupPlayButton(act,scale);
85
    layoutTop.addView(mPlayButton);
86

    
87
    // BOT ////////////////////////////
88

    
89
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
90
    layoutLeft.removeAllViews();
91

    
92
    setupSettingsButton(act,scale);
93
    layoutLeft.addView(mSettingsButton);
94
    setupSolveButton(act,scale);
95
    layoutLeft.addView(mSolveButton);
96

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

    
100
    setupBackButton(act,scale);
101
    layoutRight.addView(mBackButton);
102

    
103
    setupPopupWindow(act, scale);
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private void setupObjectButton(final RubikActivity act, final float scale)
109
    {
110
    int padding = (int)(3*scale + 0.5f);
111
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
112
    mObjButton = new ImageButton(act);
113
    mObjButton.setLayoutParams(objectParams);
114
    mObjButton.setPadding(padding,0,padding,0);
115
    mObjButton.setImageResource(R.drawable.cube_menu);
116

    
117
    mObjButton.setOnClickListener( new View.OnClickListener()
118
      {
119
      @Override
120
      public void onClick(View view)
121
        {
122
        if( act.getPreRender().canPlay() )
123
          {
124
          int total = RubikObjectList.getTotal();
125
          boolean vertical = act.isVertical();
126
          mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
127

    
128
          int width  = view.getWidth();
129
          int layhei = mLayoutWidth * (vertical? total:1);
130
          int laywid = mLayoutWidth * (vertical? 1:total);
131

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

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private void setupLevelSpinner(final RubikActivity act, final float scale)
146
    {
147
    int spinnerPadding = (int)(scale* 10 + 0.5f);
148
    int spinnerMargin  = (int)(scale*  3 + 0.5f);
149
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
150
    spinnerLayoutParams.topMargin    =   spinnerMargin;
151
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
152
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
153
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
154

    
155
    mLevelSpinner = new AppCompatSpinner(act);
156
    mLevelSpinner.setLayoutParams(spinnerLayoutParams);
157
    mLevelSpinner.setPadding(spinnerPadding,0,spinnerPadding,0);
158
    mLevelSpinner.setBackgroundResource(R.drawable.spinner);
159
    mLevelSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
160

    
161
    mLevelSpinner.setOnItemSelectedListener(this);
162
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
163
    int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
164
    String[] levels = new String[maxLevel];
165

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

    
171
    if( mLevelValue>maxLevel ) mLevelValue=1;
172

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

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void setupSettingsButton(final RubikActivity act, final float scale)
203
    {
204
    int padding = (int)(3*scale + 0.5f);
205
    LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
206
    mSettingsButton = new ImageButton(act);
207
    mSettingsButton.setLayoutParams(objectParams);
208
    mSettingsButton.setPadding(padding,0,padding,0);
209
    mSettingsButton.setImageResource(R.drawable.settings);
210

    
211
    mSettingsButton.setOnClickListener( new View.OnClickListener()
212
      {
213
      @Override
214
      public void onClick(View view)
215
        {
216
        RubikDialogEffects settings = new RubikDialogEffects();
217
        settings.show(act.getSupportFragmentManager(), null);
218
        }
219
      });
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private void setupSolveButton(final RubikActivity act, final float scale)
225
    {
226
    int padding = (int)(3*scale + 0.5f);
227
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
228
    mSolveButton = new Button(act);
229
    mSolveButton.setLayoutParams(backParams);
230
    mSolveButton.setPadding(padding,0,padding,0);
231
    mSolveButton.setText(R.string.solve);
232

    
233
    mSolveButton.setOnClickListener( new View.OnClickListener()
234
      {
235
      @Override
236
      public void onClick(View v)
237
        {
238
        act.getPreRender().solveObject();
239
        }
240
      });
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  private void setupBackButton(final RubikActivity act, final float scale)
246
    {
247
    int padding = (int)(3*scale + 0.5f);
248
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
249
    mBackButton = new Button(act);
250
    mBackButton.setLayoutParams(backParams);
251
    mBackButton.setPadding(padding,0,padding,0);
252
    mBackButton.setText(R.string.back);
253

    
254
    mBackButton.setOnClickListener( new View.OnClickListener()
255
      {
256
      @Override
257
      public void onClick(View v)
258
        {
259
        if( act.getPreRender().canPlay() ) RubikState.goBack(act);
260
        }
261
      });
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  private void setupPopupWindow(final RubikActivity act, final float scale)
267
    {
268
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
269
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
270
    mLayout = layout.findViewById(R.id.popup);
271

    
272
    mPopup = new PopupWindow(act);
273
    mPopup.setContentView(layout);
274
    mPopup.setFocusable(true);
275
    int margin = (int)(5*scale + 0.5f);
276

    
277
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
278
    int cubeWidth  = bd.getIntrinsicWidth();
279
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
280

    
281
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
282
      {
283
      final RubikObjectList list = RubikObjectList.getObject(object);
284
      final int[] sizes = list.getSizes();
285
      int[] icons = list.getIconIDs();
286
      int len = sizes.length;
287
      final int obj = object;
288

    
289
      for(int i=0; i<len; i++)
290
        {
291
        final int size = i;
292

    
293
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
294
        p.setMargins(margin, margin, margin, margin);
295

    
296
        ImageButton button = new ImageButton(act);
297
        button.setLayoutParams(p);
298

    
299
        button.setBackgroundResource(icons[i]);
300
        button.setOnClickListener( new View.OnClickListener()
301
          {
302
          @Override
303
          public void onClick(View v)
304
            {
305
            if( act.getPreRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
306
              {
307
              mObject = obj;
308
              mSize   = sizes[size];
309
              act.changeObject(list,sizes[size], true);
310
              adjustSpinner(act);
311
              }
312

    
313
            mPopup.dismiss();
314
            }
315
          });
316

    
317
        mLayout.addView(button);
318
        }
319
      }
320
    }
321

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

    
324
  public void savePreferences(SharedPreferences.Editor editor)
325
    {
326
    editor.putInt("statePlay_level" , mLevelValue);
327
    editor.putInt("statePlay_object", mObject);
328
    editor.putInt("statePlay_size"  , mSize);
329

    
330
    if( mPopup!=null )
331
      {
332
      mPopup.dismiss();
333
      mPopup = null;
334
      }
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  public void restorePreferences(SharedPreferences preferences)
340
    {
341
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
342
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
343
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  public boolean setObjectAndSize(RubikActivity act, RubikObjectList obj, int size)
349
    {
350
    if( mObject!=obj.ordinal() || mSize != size )
351
      {
352
      boolean success = false;
353

    
354
      for( int s: obj.getSizes() )
355
        if( s==size )
356
          {
357
          success = true;
358
          break;
359
          }
360

    
361
      if( success )
362
        {
363
        mObject = obj.ordinal();
364
        mSize   = size;
365

    
366
        if( mLevelSpinner!=null ) adjustSpinner(act);
367
        }
368

    
369
      return success;
370
      }
371

    
372
    return true;
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  private void adjustSpinner(RubikActivity act)
378
    {
379
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
380
    int maxLevel  = RubikObjectList.getMaxLevel(mObject, sizeIndex);
381
    String[] levels = new String[maxLevel];
382

    
383
    for(int i=0; i<maxLevel; i++)
384
      {
385
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
386
      }
387

    
388
    mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
389
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
390
    mLevelSpinner.setAdapter(mSpinnerAdapter);
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  int getLevel()
396
    {
397
    return mLevelValue;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  public int getObject()
403
    {
404
    return mObject;
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  public int getSize()
410
    {
411
    return mSize;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
417
    {
418
    mLevelValue = pos+1;
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

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