Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 85b09df4

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 android.widget.Spinner;
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 MAX_LEVEL = 18;
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 Spinner mLevelSpinner;
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 Spinner(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
    String[] levels = new String[MAX_LEVEL];
154

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

    
160
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
161
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
162
    mLevelSpinner.setAdapter(adapterType);
163
    mLevelSpinner.setSelection(mLevelValue-1);
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  private void setupPlayButton(final RubikActivity act, final float scale)
169
    {
170
    int padding = (int)(3*scale + 0.5f);
171
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
172
    mPlayButton = new Button(act);
173
    mPlayButton.setLayoutParams(backParams);
174
    mPlayButton.setPadding(padding,0,padding,0);
175
    mPlayButton.setText(R.string.play);
176

    
177
    mPlayButton.setOnClickListener( new View.OnClickListener()
178
      {
179
      @Override
180
      public void onClick(View v)
181
        {
182
        act.getPostRender().scrambleObject(mLevelValue);
183
        }
184
      });
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void setupSolveButton(final RubikActivity act, final float scale)
190
    {
191
    int padding = (int)(3*scale + 0.5f);
192
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
193
    mSolveButton = new Button(act);
194
    mSolveButton.setLayoutParams(backParams);
195
    mSolveButton.setPadding(padding,0,padding,0);
196
    mSolveButton.setText(R.string.solve);
197

    
198
    mSolveButton.setOnClickListener( new View.OnClickListener()
199
      {
200
      @Override
201
      public void onClick(View v)
202
        {
203
        act.getPostRender().solveObject();
204
        }
205
      });
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  private void setupBackButton(final RubikActivity act, final float scale)
211
    {
212
    int padding = (int)(3*scale + 0.5f);
213
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
214
    mBackButton = new Button(act);
215
    mBackButton.setLayoutParams(backParams);
216
    mBackButton.setPadding(padding,0,padding,0);
217
    mBackButton.setText(R.string.back);
218

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

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  private void setupPopupWindow(final RubikActivity act, final float scale)
232
    {
233
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
234
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
235
    mLayout = layout.findViewById(R.id.popup);
236

    
237
    mPopup = new PopupWindow(act);
238
    mPopup.setContentView(layout);
239
    mPopup.setFocusable(true);
240
    int margin = (int)(5*scale + 0.5f);
241

    
242
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
243
    int cubeWidth  = bd.getIntrinsicWidth();
244
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
245

    
246
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
247
      {
248
      final RubikObjectList list = RubikObjectList.getObject(object);
249
      final int[] sizes = list.getSizes();
250
      int[] icons = list.getIconIDs();
251
      int len = sizes.length;
252
      final int obj = object;
253

    
254
      for(int i=0; i<len; i++)
255
        {
256
        final int size = i;
257

    
258
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
259
        p.setMargins(margin, margin, margin, margin);
260

    
261
        ImageButton button = new ImageButton(act);
262
        button.setLayoutParams(p);
263

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

    
280
        mLayout.addView(button);
281
        }
282
      }
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public void savePreferences(SharedPreferences.Editor editor)
288
    {
289
    editor.putInt("statePlay_level" , mLevelValue);
290
    editor.putInt("statePlay_object", mObject);
291
    editor.putInt("statePlay_size"  , mSize);
292

    
293
    mObjButton   = null;
294
    mBackButton  = null;
295
    mSolveButton = null;
296
    mPlayButton  = null;
297
    mLevelSpinner= null;
298

    
299
    if( mPopup!=null )
300
      {
301
      mPopup.dismiss();
302
      mPopup = null;
303
      }
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public void restorePreferences(SharedPreferences preferences)
309
    {
310
    mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
311
    mObject     = preferences.getInt("statePlay_object", DEF_OBJECT);
312
    mSize       = preferences.getInt("statePlay_size"  , DEF_SIZE  );
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public boolean setObjectAndSize(RubikObjectList obj, int size)
318
    {
319
    boolean success = false;
320

    
321
    for( int s: obj.getSizes() )
322
      if( s==size )
323
        {
324
        success = true;
325
        break;
326
        }
327

    
328
    if( success )
329
      {
330
      mObject = obj.ordinal();
331
      mSize   = size;
332
      }
333

    
334
    return success;
335
    }
336

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

    
339
  int getLevel()
340
    {
341
    return mLevelValue;
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  public int getObject()
347
    {
348
    return mObject;
349
    }
350

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

    
353
  public int getSize()
354
    {
355
    return mSize;
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

    
360
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
361
    {
362
    mLevelValue = pos+1;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public void onNothingSelected(AdapterView<?> parent) { }
368
  }
(5-5/8)