Project

General

Profile

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

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

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)
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
    int padding = (int)(3*scale + 0.5f);
83

    
84
    if( mObjButton==null )
85
      {
86
      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

    
98
    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
      }
108
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
109
    layoutRight.removeAllViews();
110
    layoutRight.addView(mBackButton);
111

    
112
    mPicker = act.findViewById(R.id.rubikNumberPicker);
113
    mPicker.setMin(MIN_SCRAMBLE);
114
    mPicker.setMax(MAX_SCRAMBLE);
115
    mPicker.setValue(mPickerValue);
116

    
117
    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

    
123
      mPopup = new PopupWindow(act);
124
      mPopup.setContentView(layout);
125
      mPopup.setFocusable(true);
126
      int margin = (int)(5*scale + 0.5f);
127

    
128
      BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2);
129
      int cubeWidth  = bd.getIntrinsicWidth();
130
      int cubeHeight = bd.getIntrinsicHeight();
131

    
132
      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
      }
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  public void savePreferences(SharedPreferences.Editor editor)
191
    {
192
    if( mPicker!=null )
193
      {
194
      editor.putInt("statePlay_scramble", mPicker.getValue() );
195
      }
196

    
197
    editor.putInt("statePlay_object", mObject);
198
    editor.putInt("statePlay_size"  , mSize);
199

    
200
    mObjButton = null;
201
    mBackButton= null;
202

    
203
    if( mPopup!=null )
204
      {
205
      mPopup.dismiss();
206
      mPopup     = null;
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void restorePreferences(SharedPreferences preferences)
213
    {
214
    mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
215
    mObject     = preferences.getInt("statePlay_object"  , DEF_OBJECT  );
216
    mSize       = preferences.getInt("statePlay_size"    , DEF_SIZE    );
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  public int getPicker()
222
    {
223
    return mPicker!=null ? mPicker.getValue() : 1;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public int getObject()
229
    {
230
    return mObject;
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  public int getSize()
236
    {
237
    return mSize;
238
    }
239
  }
(4-4/5)