Project

General

Profile

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

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

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.util.DisplayMetrics;
26
import android.view.Gravity;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.widget.AdapterView;
30
import android.widget.ArrayAdapter;
31
import android.widget.Button;
32
import android.widget.ImageButton;
33
import android.widget.LinearLayout;
34
import android.widget.PopupWindow;
35
import androidx.appcompat.widget.AppCompatSpinner;
36

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  void leaveState(RubikActivity act)
63
    {
64

    
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

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

    
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

    
85
    // BOT ////////////////////////////
86

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

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

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

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

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

    
127
          mPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
128
          }
129
        }
130
      });
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
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 AppCompatSpinner(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
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
154
    int maxLevel = RubikObjectList.getMaxLevel(mObject, sizeIndex);
155
    String[] levels = new String[maxLevel];
156

    
157
    for(int i=0; i<maxLevel; i++)
158
      {
159
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
160
      }
161

    
162
    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
    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
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  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

    
221
    mBackButton.setOnClickListener( new View.OnClickListener()
222
      {
223
      @Override
224
      public void onClick(View v)
225
        {
226
        if( act.getPostRender().canPlay() ) RubikState.goBack(act);
227
        }
228
      });
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  private void setupPopupWindow(final RubikActivity act, final float scale)
234
    {
235
    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

    
248
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
249
      {
250
      final RubikObjectList list = RubikObjectList.getObject(object);
251
      final int[] sizes = list.getSizes();
252
      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

    
266
        button.setBackgroundResource(icons[i]);
267
        button.setOnClickListener( new View.OnClickListener()
268
          {
269
          @Override
270
          public void onClick(View v)
271
            {
272
            if( act.getPostRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
273
              {
274
              mObject = obj;
275
              mSize   = sizes[size];
276
              act.changeObject(list,sizes[size],null);
277

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

    
292
            mPopup.dismiss();
293
            }
294
          });
295

    
296
        mLayout.addView(button);
297
        }
298
      }
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  public void savePreferences(SharedPreferences.Editor editor)
304
    {
305
    editor.putInt("statePlay_level" , mLevelValue);
306
    editor.putInt("statePlay_object", mObject);
307
    editor.putInt("statePlay_size"  , mSize);
308

    
309
    mObjButton   = null;
310
    mBackButton  = null;
311
    mSolveButton = null;
312
    mPlayButton  = null;
313
    mLevelSpinner= null;
314

    
315
    if( mPopup!=null )
316
      {
317
      mPopup.dismiss();
318
      mPopup = null;
319
      }
320
    }
321

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

    
324
  public void restorePreferences(SharedPreferences preferences)
325
    {
326
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
327
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
328
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  int getLevel()
356
    {
357
    return mLevelValue;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  public int getObject()
363
    {
364
    return mObject;
365
    }
366

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

    
369
  public int getSize()
370
    {
371
    return mSize;
372
    }
373

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