Project

General

Profile

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

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

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.uistate;
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.Button;
30
import android.widget.ImageButton;
31
import android.widget.LinearLayout;
32
import android.widget.PopupWindow;
33

    
34
import org.distorted.component.HorizontalNumberPicker;
35
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37
import org.distorted.object.RubikObjectList;
38

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

    
49
  private ImageButton mObjButton;
50
  private Button mBackButton;
51
  private PopupWindow mPopup;
52
  private HorizontalNumberPicker mPicker;
53
  private int mPickerValue;
54
  private int mObject = DEF_OBJECT;
55
  private int mSize   = DEF_SIZE;
56
  private int mLayoutWidth, mLayoutHeight;
57
  private LinearLayout mLayout;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  void leaveState(RubikActivity act)
62
    {
63
    mPickerValue = mPicker.getValue();
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  void enterState(final RubikActivity act, RubikState prev)
69
    {
70
    LayoutInflater inflater = act.getLayoutInflater();
71

    
72
    // TOP ////////////////////////////
73
    final View viewTop = inflater.inflate(R.layout.play_title, null);
74

    
75
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
76
    layoutTop.removeAllViews();
77
    layoutTop.addView(viewTop);
78

    
79
    // BOT ////////////////////////////
80
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
81
    final float scale = metrics.density;
82

    
83
    if( mObjButton==null ) setupObjectButton(act,scale);
84

    
85
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
86
    layoutLeft.removeAllViews();
87
    layoutLeft.addView(mObjButton);
88

    
89
    if( mBackButton==null ) setupBackButton(act,scale);
90

    
91
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
92
    layoutRight.removeAllViews();
93
    layoutRight.addView(mBackButton);
94

    
95
    mPicker = act.findViewById(R.id.rubikNumberPicker);
96
    mPicker.setMin(MIN_SCRAMBLE);
97
    mPicker.setMax(MAX_SCRAMBLE);
98
    mPicker.setValue(mPickerValue);
99

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
123
        int height = view.getHeight();
124
        int width  = view.getWidth();
125
        int laywid = mLayoutWidth * (vertical? 1:total);
126
        int layhei = mLayoutHeight* (vertical? total:1);
127

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private void setupBackButton(final RubikActivity act, final float scale)
136
    {
137
    int padding = (int)(3*scale + 0.5f);
138
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
139
    mBackButton = new Button(act);
140
    mBackButton.setLayoutParams(backParams);
141
    mBackButton.setId(BUTTON_ID_BACK);
142
    mBackButton.setPadding(padding,0,padding,0);
143
    mBackButton.setText(R.string.back);
144
    mBackButton.setOnClickListener(act);
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void setupPopupWindow(final RubikActivity act, final float scale)
150
    {
151
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
152
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
153
    mLayout = layout.findViewById(R.id.popup);
154

    
155
    mPopup = new PopupWindow(act);
156
    mPopup.setContentView(layout);
157
    mPopup.setFocusable(true);
158
    int margin = (int)(5*scale + 0.5f);
159

    
160
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
161
    int cubeWidth  = bd.getIntrinsicWidth();
162
    int cubeHeight = bd.getIntrinsicHeight();
163

    
164
    mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
165
    mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f);
166

    
167
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
168
      {
169
      RubikObjectList list = RubikObjectList.getObject(object);
170
      int[] sizes = list.getSizes();
171
      int[] icons = list.getIconIDs();
172
      int len = sizes.length;
173
      final int obj = object;
174

    
175
      for(int i=0; i<len; i++)
176
        {
177
        final int size = i;
178

    
179
        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
180
        p.setMargins(margin, margin, margin, margin);
181

    
182
        ImageButton button = new ImageButton(act);
183
        button.setLayoutParams(p);
184

    
185
        button.setBackgroundResource(icons[i]);
186
        button.setOnClickListener( new View.OnClickListener()
187
          {
188
          @Override
189
          public void onClick(View v)
190
            {
191
            mObject = obj;
192
            mSize   = size;
193
            act.changeObject(obj,size);
194
            mPopup.dismiss();
195
            }
196
          });
197

    
198
        mLayout.addView(button);
199
        }
200
      }
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  public void savePreferences(SharedPreferences.Editor editor)
206
    {
207
    if( mPicker!=null )
208
      {
209
      editor.putInt("statePlay_scramble", mPicker.getValue() );
210
      }
211

    
212
    editor.putInt("statePlay_object", mObject);
213
    editor.putInt("statePlay_size"  , mSize);
214

    
215
    mObjButton = null;
216
    mBackButton= null;
217

    
218
    if( mPopup!=null )
219
      {
220
      mPopup.dismiss();
221
      mPopup     = null;
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  public void restorePreferences(SharedPreferences preferences)
228
    {
229
    mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
230
    mObject     = preferences.getInt("statePlay_object"  , DEF_OBJECT  );
231
    mSize       = preferences.getInt("statePlay_size"    , DEF_SIZE    );
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public int getPicker()
237
    {
238
    return mPicker!=null ? mPicker.getValue() : 1;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public int getObject()
244
    {
245
    return mObject;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public int getSize()
251
    {
252
    return mSize;
253
    }
254
  }
(4-4/5)