Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePlay.java @ a42e25a6

1 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.uistate;
21
22 4c0cd600 Leszek Koltunski
import android.content.Context;
23 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
24 4c0cd600 Leszek Koltunski
import android.graphics.drawable.BitmapDrawable;
25 211b48f2 Leszek Koltunski
import android.util.DisplayMetrics;
26 4c0cd600 Leszek Koltunski
import android.view.Gravity;
27 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.widget.Button;
30
import android.widget.ImageButton;
31
import android.widget.LinearLayout;
32 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
33 211b48f2 Leszek Koltunski
34
import org.distorted.component.HorizontalNumberPicker;
35
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37 27a70eae Leszek Koltunski
import org.distorted.object.RubikObjectList;
38 211b48f2 Leszek Koltunski
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
public class RubikStatePlay extends RubikStateAbstract
42
  {
43
  private static final int MIN_SCRAMBLE =  1;
44
  private static final int DEF_SCRAMBLE =  1;
45
  public  static final int MAX_SCRAMBLE = 18;
46 53f23b64 Leszek Koltunski
  public  static final int DEF_OBJECT   = RubikObjectList.CUBE.ordinal();
47
  public  static final int DEF_SIZE     =  3;
48 211b48f2 Leszek Koltunski
49 4c0cd600 Leszek Koltunski
  private ImageButton mObjButton;
50
  private Button mBackButton;
51
  private PopupWindow mPopup;
52 211b48f2 Leszek Koltunski
  private HorizontalNumberPicker mPicker;
53
  private int mPickerValue;
54 4888e97c Leszek Koltunski
  private int mObject = DEF_OBJECT;
55
  private int mSize   = DEF_SIZE;
56 4c0cd600 Leszek Koltunski
  private int mLayoutWidth, mLayoutHeight;
57
  private LinearLayout mLayout;
58 211b48f2 Leszek Koltunski
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 329c0aeb Leszek Koltunski
  void leaveState(RubikActivity act)
62 211b48f2 Leszek Koltunski
    {
63
    mPickerValue = mPicker.getValue();
64
    }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
69 211b48f2 Leszek Koltunski
    {
70
    LayoutInflater inflater = act.getLayoutInflater();
71
72
    // TOP ////////////////////////////
73 4c0cd600 Leszek Koltunski
    final View viewTop = inflater.inflate(R.layout.play_title, null);
74
75 211b48f2 Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
76
    layoutTop.removeAllViews();
77
    layoutTop.addView(viewTop);
78
79
    // BOT ////////////////////////////
80
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
81 4c0cd600 Leszek Koltunski
    final float scale = metrics.density;
82 211b48f2 Leszek Koltunski
83 769d7b9f Leszek Koltunski
    if( mObjButton==null ) setupObjectButton(act,scale);
84
85 4c0cd600 Leszek Koltunski
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
86
    layoutLeft.removeAllViews();
87
    layoutLeft.addView(mObjButton);
88 4888e97c Leszek Koltunski
89 769d7b9f Leszek Koltunski
    if( mBackButton==null ) setupBackButton(act,scale);
90
91 4c0cd600 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
92
    layoutRight.removeAllViews();
93
    layoutRight.addView(mBackButton);
94 211b48f2 Leszek Koltunski
95 4c0cd600 Leszek Koltunski
    mPicker = act.findViewById(R.id.rubikNumberPicker);
96
    mPicker.setMin(MIN_SCRAMBLE);
97
    mPicker.setMax(MAX_SCRAMBLE);
98
    mPicker.setValue(mPickerValue);
99 211b48f2 Leszek Koltunski
100 769d7b9f Leszek Koltunski
    if( mPopup==null ) setupPopupWindow(act, scale);
101
    }
102 211b48f2 Leszek Koltunski
103 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104 4c0cd600 Leszek Koltunski
105 769d7b9f Leszek Koltunski
  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 4c0cd600 Leszek Koltunski
        {
119 a42e25a6 Leszek Koltunski
        if( act.getPostRender().canPlay() )
120
          {
121
          int total = RubikObjectList.getTotal();
122
          boolean vertical = act.isVertical();
123
          mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
124 4c0cd600 Leszek Koltunski
125 a42e25a6 Leszek Koltunski
          int height = view.getHeight();
126
          int width  = view.getWidth();
127
          int laywid = mLayoutWidth * (vertical? 1:total);
128
          int layhei = mLayoutHeight* (vertical? total:1);
129 4c0cd600 Leszek Koltunski
130 a42e25a6 Leszek Koltunski
          mPopup.showAsDropDown(view, (width-laywid)/2, -height-layhei, Gravity.LEFT);
131
          }
132 769d7b9f Leszek Koltunski
        }
133
      });
134
    }
135 4c0cd600 Leszek Koltunski
136 769d7b9f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137 4c0cd600 Leszek Koltunski
138 769d7b9f Leszek Koltunski
  private void setupBackButton(final RubikActivity act, final float scale)
139
    {
140
    int padding = (int)(3*scale + 0.5f);
141
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
142
    mBackButton = new Button(act);
143
    mBackButton.setLayoutParams(backParams);
144
    mBackButton.setPadding(padding,0,padding,0);
145
    mBackButton.setText(R.string.back);
146 a6d3b158 Leszek Koltunski
147
    mBackButton.setOnClickListener( new View.OnClickListener()
148
      {
149
      @Override
150
      public void onClick(View v)
151
        {
152 a42e25a6 Leszek Koltunski
        if( act.getPostRender().canPlay() ) RubikState.goBack(act);
153 a6d3b158 Leszek Koltunski
        }
154
      });
155 211b48f2 Leszek Koltunski
    }
156
157 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159 769d7b9f Leszek Koltunski
  private void setupPopupWindow(final RubikActivity act, final float scale)
160 4c0cd600 Leszek Koltunski
    {
161 769d7b9f Leszek Koltunski
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
162
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
163
    mLayout = layout.findViewById(R.id.popup);
164
165
    mPopup = new PopupWindow(act);
166
    mPopup.setContentView(layout);
167
    mPopup.setFocusable(true);
168
    int margin = (int)(5*scale + 0.5f);
169
170
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
171
    int cubeWidth  = bd.getIntrinsicWidth();
172
    int cubeHeight = bd.getIntrinsicHeight();
173
174
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
175
    mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f);
176 4c0cd600 Leszek Koltunski
177 769d7b9f Leszek Koltunski
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
178
      {
179 53f23b64 Leszek Koltunski
      final RubikObjectList list = RubikObjectList.getObject(object);
180
      final int[] sizes = list.getSizes();
181 769d7b9f Leszek Koltunski
      int[] icons = list.getIconIDs();
182
      int len = sizes.length;
183
      final int obj = object;
184
185
      for(int i=0; i<len; i++)
186
        {
187
        final int size = i;
188
189
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
190
        p.setMargins(margin, margin, margin, margin);
191
192
        ImageButton button = new ImageButton(act);
193
        button.setLayoutParams(p);
194 4c0cd600 Leszek Koltunski
195 769d7b9f Leszek Koltunski
        button.setBackgroundResource(icons[i]);
196
        button.setOnClickListener( new View.OnClickListener()
197
          {
198
          @Override
199
          public void onClick(View v)
200
            {
201 a42e25a6 Leszek Koltunski
            if( act.getPostRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
202
              {
203
              mObject = obj;
204
              mSize   = sizes[size];
205
              act.changeObject(list,sizes[size],null);
206
              }
207 769d7b9f Leszek Koltunski
            mPopup.dismiss();
208
            }
209
          });
210
211
        mLayout.addView(button);
212
        }
213
      }
214 4c0cd600 Leszek Koltunski
    }
215
216 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  public void savePreferences(SharedPreferences.Editor editor)
219
    {
220
    if( mPicker!=null )
221
      {
222 4888e97c Leszek Koltunski
      editor.putInt("statePlay_scramble", mPicker.getValue() );
223 211b48f2 Leszek Koltunski
      }
224 8e1231ae Leszek Koltunski
225 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
226
    editor.putInt("statePlay_size"  , mSize);
227 4c0cd600 Leszek Koltunski
228
    mObjButton = null;
229
    mBackButton= null;
230
231
    if( mPopup!=null )
232
      {
233
      mPopup.dismiss();
234 53f23b64 Leszek Koltunski
      mPopup = null;
235 4c0cd600 Leszek Koltunski
      }
236 211b48f2 Leszek Koltunski
    }
237
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
  public void restorePreferences(SharedPreferences preferences)
241
    {
242 4888e97c Leszek Koltunski
    mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
243
    mObject     = preferences.getInt("statePlay_object"  , DEF_OBJECT  );
244
    mSize       = preferences.getInt("statePlay_size"    , DEF_SIZE    );
245 211b48f2 Leszek Koltunski
    }
246
247 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249
  public boolean setObjectAndSize(RubikObjectList obj, int size)
250
    {
251
    boolean success = false;
252
253
    for( int s: obj.getSizes() )
254
      if( s==size )
255
        {
256
        success = true;
257
        break;
258
        }
259
260
    if( success )
261
      {
262
      mObject = obj.ordinal();
263
      mSize   = size;
264
      }
265
266
    return success;
267
    }
268
269 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
270
271
  public int getPicker()
272
    {
273
    return mPicker!=null ? mPicker.getValue() : 1;
274
    }
275
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278 4888e97c Leszek Koltunski
  public int getObject()
279 211b48f2 Leszek Koltunski
    {
280 4888e97c Leszek Koltunski
    return mObject;
281 211b48f2 Leszek Koltunski
    }
282
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
285 4888e97c Leszek Koltunski
  public int getSize()
286 211b48f2 Leszek Koltunski
    {
287 4888e97c Leszek Koltunski
    return mSize;
288
    }
289 211b48f2 Leszek Koltunski
  }