Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePlay.java @ 4c0cd600

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 4888e97c Leszek Koltunski
  private static final int DEF_OBJECT   = RubikObjectList.CUBE.ordinal();
47
  private static final int DEF_SIZE     =  1;  // i.e. the second from the list of CUBE's sizes
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 4c0cd600 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
    int padding = (int)(3*scale + 0.5f);
83 211b48f2 Leszek Koltunski
84 4c0cd600 Leszek Koltunski
    if( mObjButton==null )
85 211b48f2 Leszek Koltunski
      {
86 4c0cd600 Leszek Koltunski
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
87
      mObjButton = new ImageButton(act);
88
      mObjButton.setLayoutParams(objectParams);
89
      mObjButton.setId(BUTTON_ID_OBJECT);
90
      mObjButton.setPadding(padding,0,padding,0);
91
      mObjButton.setImageResource(R.drawable.cube_menu);
92
      mObjButton.setOnClickListener(act);
93
      }
94
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
95
    layoutLeft.removeAllViews();
96
    layoutLeft.addView(mObjButton);
97 4888e97c Leszek Koltunski
98 4c0cd600 Leszek Koltunski
    if( mBackButton==null )
99
      {
100
      LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
101
      mBackButton = new Button(act);
102
      mBackButton.setLayoutParams(backParams);
103
      mBackButton.setId(BUTTON_ID_BACK);
104
      mBackButton.setPadding(padding,0,padding,0);
105
      mBackButton.setText(R.string.back);
106
      mBackButton.setOnClickListener(act);
107 211b48f2 Leszek Koltunski
      }
108 4c0cd600 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
109
    layoutRight.removeAllViews();
110
    layoutRight.addView(mBackButton);
111 211b48f2 Leszek Koltunski
112 4c0cd600 Leszek Koltunski
    mPicker = act.findViewById(R.id.rubikNumberPicker);
113
    mPicker.setMin(MIN_SCRAMBLE);
114
    mPicker.setMax(MAX_SCRAMBLE);
115
    mPicker.setValue(mPickerValue);
116 211b48f2 Leszek Koltunski
117 4c0cd600 Leszek Koltunski
    if( mPopup==null )
118
      {
119
      LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
120
      final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
121
      mLayout = layout.findViewById(R.id.popup);
122 211b48f2 Leszek Koltunski
123 4c0cd600 Leszek Koltunski
      mPopup = new PopupWindow(act);
124
      mPopup.setContentView(layout);
125
      mPopup.setFocusable(true);
126
      int margin = (int)(5*scale + 0.5f);
127 211b48f2 Leszek Koltunski
128 4c0cd600 Leszek Koltunski
      BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
129
      int cubeWidth  = bd.getIntrinsicWidth();
130
      int cubeHeight = bd.getIntrinsicHeight();
131 211b48f2 Leszek Koltunski
132 4c0cd600 Leszek Koltunski
      mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
133
      mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f);
134
135
      for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
136
        {
137
        RubikObjectList list = RubikObjectList.getObject(object);
138
        int[] sizes = list.getSizes();
139
        int[] icons = list.getIconIDs();
140
        int len = sizes.length;
141
        final int obj = object;
142
143
        for(int i=0; i<len; i++)
144
          {
145
          final int size = i;
146
147
          LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
148
          p.setMargins(margin, margin, margin, margin);
149
150
          ImageButton button = new ImageButton(act);
151
          button.setLayoutParams(p);
152
153
          button.setBackgroundResource(icons[i]);
154
          button.setOnClickListener( new View.OnClickListener()
155
            {
156
            @Override
157
            public void onClick(View v)
158
              {
159
              mObject = obj;
160
              mSize   = size;
161
              act.changeObject(obj,size);
162
              mPopup.dismiss();
163
              }
164
            });
165
166
          mLayout.addView(button);
167
          }
168
        }
169 211b48f2 Leszek Koltunski
      }
170
    }
171
172 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
  public void bringUpPopup(RubikActivity act, View view)
175
    {
176
    int total = RubikObjectList.getTotal();
177
    boolean vertical = act.isVertical();
178
    mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
179
180
    int height = view.getHeight();
181
    int width  = view.getWidth();
182
    int laywid = mLayoutWidth * (vertical? 1:total);
183
    int layhei = mLayoutHeight* (vertical? total:1);
184
185
    mPopup.showAsDropDown(view, (width-laywid)/2, -height-layhei, Gravity.LEFT);
186
    }
187
188 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190
  public void savePreferences(SharedPreferences.Editor editor)
191
    {
192
    if( mPicker!=null )
193
      {
194 4888e97c Leszek Koltunski
      editor.putInt("statePlay_scramble", mPicker.getValue() );
195 211b48f2 Leszek Koltunski
      }
196 8e1231ae Leszek Koltunski
197 4888e97c Leszek Koltunski
    editor.putInt("statePlay_object", mObject);
198
    editor.putInt("statePlay_size"  , mSize);
199 4c0cd600 Leszek Koltunski
200
    mObjButton = null;
201
    mBackButton= null;
202
203
    if( mPopup!=null )
204
      {
205
      mPopup.dismiss();
206
      mPopup     = null;
207
      }
208 211b48f2 Leszek Koltunski
    }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
  public void restorePreferences(SharedPreferences preferences)
213
    {
214 4888e97c Leszek Koltunski
    mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
215
    mObject     = preferences.getInt("statePlay_object"  , DEF_OBJECT  );
216
    mSize       = preferences.getInt("statePlay_size"    , DEF_SIZE    );
217 211b48f2 Leszek Koltunski
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
  public int getPicker()
222
    {
223
    return mPicker!=null ? mPicker.getValue() : 1;
224
    }
225
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228 4888e97c Leszek Koltunski
  public int getObject()
229 211b48f2 Leszek Koltunski
    {
230 4888e97c Leszek Koltunski
    return mObject;
231 211b48f2 Leszek Koltunski
    }
232
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235 4888e97c Leszek Koltunski
  public int getSize()
236 211b48f2 Leszek Koltunski
    {
237 4888e97c Leszek Koltunski
    return mSize;
238
    }
239 211b48f2 Leszek Koltunski
  }