Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStatePlay.java @ 5a4d4fba

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

    
39
import com.google.firebase.analytics.FirebaseAnalytics;
40

    
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43
import org.distorted.objects.RubikObjectList;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  void leaveState(RubikActivity act)
67
    {
68

    
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

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

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

    
89
    // BOT ////////////////////////////
90

    
91
    setupSolveButton(act,scale);
92

    
93
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
94
    layoutLeft.removeAllViews();
95
    layoutLeft.addView(mSolveButton);
96

    
97
    setupBackButton(act,scale);
98

    
99
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
100
    layoutRight.removeAllViews();
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.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
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
    int spinnerLength  = (int)(scale*150 + 0.5f);
150
    LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(spinnerLength,LinearLayout.LayoutParams.MATCH_PARENT);
151
    spinnerLayoutParams.topMargin    =   spinnerMargin;
152
    spinnerLayoutParams.bottomMargin =   spinnerMargin;
153
    spinnerLayoutParams.leftMargin   =   spinnerMargin;
154
    spinnerLayoutParams.rightMargin  = 2*spinnerMargin;
155

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

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

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

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

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

    
212
    mSolveButton.setOnClickListener( new View.OnClickListener()
213
      {
214
      @Override
215
      public void onClick(View v)
216
        {
217
        act.getPreRender().solveObject();
218
        }
219
      });
220
    }
221

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

    
224
  private void setupBackButton(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
    mBackButton = new Button(act);
229
    mBackButton.setLayoutParams(backParams);
230
    mBackButton.setPadding(padding,0,padding,0);
231
    mBackButton.setText(R.string.back);
232

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

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

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

    
251
    mPopup = new PopupWindow(act);
252
    mPopup.setContentView(layout);
253
    mPopup.setFocusable(true);
254
    int margin = (int)(5*scale + 0.5f);
255

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

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

    
268
      for(int i=0; i<len; i++)
269
        {
270
        final int size = i;
271

    
272
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
273
        p.setMargins(margin, margin, margin, margin);
274

    
275
        ImageButton button = new ImageButton(act);
276
        button.setLayoutParams(p);
277

    
278
        button.setBackgroundResource(icons[i]);
279
        button.setOnClickListener( new View.OnClickListener()
280
          {
281
          @Override
282
          public void onClick(View v)
283
            {
284
            if( act.getPreRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
285
              {
286
              FirebaseAnalytics analytics = act.getAnalytics();
287

    
288
              if( analytics!=null )
289
                {
290
                String name = RubikObjectList.getObject(obj).name()+"_"+sizes[size];
291
                Bundle bundle = new Bundle();
292
                bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
293
                analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
294
                }
295

    
296
              mObject = obj;
297
              mSize   = sizes[size];
298
              act.changeObject(list,sizes[size]);
299
              adjustSpinner(act);
300
              }
301

    
302
            mPopup.dismiss();
303
            }
304
          });
305

    
306
        mLayout.addView(button);
307
        }
308
      }
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public void savePreferences(SharedPreferences.Editor editor)
314
    {
315
    editor.putInt("statePlay_level" , mLevelValue);
316
    editor.putInt("statePlay_object", mObject);
317
    editor.putInt("statePlay_size"  , mSize);
318

    
319
    if( mPopup!=null )
320
      {
321
      mPopup.dismiss();
322
      mPopup = null;
323
      }
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

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

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public boolean setObjectAndSize(RubikActivity act, RubikObjectList obj, int size)
338
    {
339
    if( mObject!=obj.ordinal() || mSize != size )
340
      {
341
      boolean success = false;
342

    
343
      for( int s: obj.getSizes() )
344
        if( s==size )
345
          {
346
          success = true;
347
          break;
348
          }
349

    
350
      if( success )
351
        {
352
        mObject = obj.ordinal();
353
        mSize   = size;
354

    
355
        if( mLevelSpinner!=null ) adjustSpinner(act);
356
        }
357

    
358
      return success;
359
      }
360

    
361
    return true;
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  private void adjustSpinner(RubikActivity act)
367
    {
368
    int sizeIndex = RubikObjectList.getSizeIndex(mObject,mSize);
369
    int maxLevel  = RubikObjectList.getMaxLevel(mObject, sizeIndex);
370
    String[] levels = new String[maxLevel];
371

    
372
    for(int i=0; i<maxLevel; i++)
373
      {
374
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
375
      }
376

    
377
    mSpinnerAdapter = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels);
378
    mSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
379
    mLevelSpinner.setAdapter(mSpinnerAdapter);
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  int getLevel()
385
    {
386
    return mLevelValue;
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  public int getObject()
392
    {
393
    return mObject;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public int getSize()
399
    {
400
    return mSize;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
406
    {
407
    mLevelValue = pos+1;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

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