Revision 85b09df4
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/states/RubikStatePlay.java | ||
---|---|---|
26 | 26 |
import android.view.Gravity; |
27 | 27 |
import android.view.LayoutInflater; |
28 | 28 |
import android.view.View; |
29 |
import android.widget.AdapterView; |
|
30 |
import android.widget.ArrayAdapter; |
|
29 | 31 |
import android.widget.Button; |
30 | 32 |
import android.widget.ImageButton; |
31 | 33 |
import android.widget.LinearLayout; |
32 | 34 |
import android.widget.PopupWindow; |
35 |
import android.widget.Spinner; |
|
33 | 36 |
|
34 |
import org.distorted.component.HorizontalNumberPicker; |
|
35 | 37 |
import org.distorted.main.R; |
36 | 38 |
import org.distorted.main.RubikActivity; |
37 | 39 |
import org.distorted.objects.RubikObjectList; |
38 | 40 |
|
39 | 41 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
40 | 42 |
|
41 |
public class RubikStatePlay extends RubikStateAbstract |
|
43 |
public class RubikStatePlay extends RubikStateAbstract implements AdapterView.OnItemSelectedListener
|
|
42 | 44 |
{ |
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 |
public static final int DEF_OBJECT = RubikObjectList.CUBE.ordinal(); |
|
47 |
public static final int DEF_SIZE = 3; |
|
45 |
private static final int DEF_LEVEL = 1; |
|
46 |
public static final int MAX_LEVEL = 18; |
|
47 |
public static final int DEF_OBJECT= RubikObjectList.CUBE.ordinal(); |
|
48 |
public static final int DEF_SIZE = 3; |
|
48 | 49 |
|
49 | 50 |
private ImageButton mObjButton; |
50 |
private Button mBackButton; |
|
51 |
private Button mBackButton, mSolveButton, mPlayButton;
|
|
51 | 52 |
private PopupWindow mPopup; |
52 |
private HorizontalNumberPicker mPicker; |
|
53 |
private int mPickerValue; |
|
54 | 53 |
private int mObject = DEF_OBJECT; |
55 | 54 |
private int mSize = DEF_SIZE; |
56 |
private int mLayoutWidth, mLayoutHeight;
|
|
55 |
private int mLayoutWidth; |
|
57 | 56 |
private LinearLayout mLayout; |
57 |
private Spinner mLevelSpinner; |
|
58 |
private int mLevelValue; |
|
58 | 59 |
|
59 | 60 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
60 | 61 |
|
61 | 62 |
void leaveState(RubikActivity act) |
62 | 63 |
{ |
63 |
mPickerValue = mPicker.getValue(); |
|
64 |
|
|
64 | 65 |
} |
65 | 66 |
|
66 | 67 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
67 | 68 |
|
68 | 69 |
void enterState(final RubikActivity act) |
69 | 70 |
{ |
70 |
LayoutInflater inflater = act.getLayoutInflater(); |
|
71 |
DisplayMetrics metrics = act.getResources().getDisplayMetrics(); |
|
72 |
final float scale = metrics.density; |
|
71 | 73 |
|
72 | 74 |
// TOP //////////////////////////// |
73 |
final View viewTop = inflater.inflate(R.layout.play_title, null); |
|
74 |
|
|
75 | 75 |
LinearLayout layoutTop = act.findViewById(R.id.upperBar); |
76 | 76 |
layoutTop.removeAllViews(); |
77 |
layoutTop.addView(viewTop); |
|
77 |
|
|
78 |
if( mObjButton ==null ) setupObjectButton(act,scale); |
|
79 |
layoutTop.addView(mObjButton); |
|
80 |
if( mLevelSpinner==null ) setupLevelSpinner(act,scale); |
|
81 |
layoutTop.addView(mLevelSpinner); |
|
82 |
if( mPlayButton ==null ) setupPlayButton(act,scale); |
|
83 |
layoutTop.addView(mPlayButton); |
|
78 | 84 |
|
79 | 85 |
// BOT //////////////////////////// |
80 |
DisplayMetrics metrics = act.getResources().getDisplayMetrics(); |
|
81 |
final float scale = metrics.density; |
|
82 | 86 |
|
83 |
if( mObjButton==null ) setupObjectButton(act,scale);
|
|
87 |
if( mSolveButton==null ) setupSolveButton(act,scale);
|
|
84 | 88 |
|
85 | 89 |
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft); |
86 | 90 |
layoutLeft.removeAllViews(); |
87 |
layoutLeft.addView(mObjButton);
|
|
91 |
layoutLeft.addView(mSolveButton);
|
|
88 | 92 |
|
89 | 93 |
if( mBackButton==null ) setupBackButton(act,scale); |
90 | 94 |
|
... | ... | |
92 | 96 |
layoutRight.removeAllViews(); |
93 | 97 |
layoutRight.addView(mBackButton); |
94 | 98 |
|
95 |
mPicker = act.findViewById(R.id.rubikNumberPicker); |
|
96 |
mPicker.setMin(MIN_SCRAMBLE); |
|
97 |
mPicker.setMax(MAX_SCRAMBLE); |
|
98 |
mPicker.setValue(mPickerValue); |
|
99 |
|
|
100 | 99 |
if( mPopup==null ) setupPopupWindow(act, scale); |
101 | 100 |
} |
102 | 101 |
|
... | ... | |
122 | 121 |
boolean vertical = act.isVertical(); |
123 | 122 |
mLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL); |
124 | 123 |
|
125 |
int height = view.getHeight(); |
|
126 | 124 |
int width = view.getWidth(); |
127 | 125 |
int laywid = mLayoutWidth * (vertical? 1:total); |
128 |
int layhei = mLayoutHeight* (vertical? total:1); |
|
129 | 126 |
|
130 |
mPopup.showAsDropDown(view, (width-laywid)/2, -height-layhei, Gravity.LEFT);
|
|
127 |
mPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
|
|
131 | 128 |
} |
132 | 129 |
} |
133 | 130 |
}); |
134 | 131 |
} |
135 | 132 |
|
133 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
134 |
|
|
135 |
private void setupLevelSpinner(final RubikActivity act, final float scale) |
|
136 |
{ |
|
137 |
int spinnerPadding = (int)(scale* 10 + 0.5f); |
|
138 |
int spinnerMargin = (int)(scale* 3 + 0.5f); |
|
139 |
int spinnerLength = (int)(scale*150 + 0.5f); |
|
140 |
LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(spinnerLength,LinearLayout.LayoutParams.MATCH_PARENT); |
|
141 |
spinnerLayoutParams.topMargin = spinnerMargin; |
|
142 |
spinnerLayoutParams.bottomMargin = spinnerMargin; |
|
143 |
spinnerLayoutParams.leftMargin = spinnerMargin; |
|
144 |
spinnerLayoutParams.rightMargin = 2*spinnerMargin; |
|
145 |
|
|
146 |
mLevelSpinner = new Spinner(act); |
|
147 |
mLevelSpinner.setLayoutParams(spinnerLayoutParams); |
|
148 |
mLevelSpinner.setPadding(spinnerPadding,0,spinnerPadding,0); |
|
149 |
mLevelSpinner.setBackgroundResource(R.drawable.spinner); |
|
150 |
mLevelSpinner.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); |
|
151 |
|
|
152 |
mLevelSpinner.setOnItemSelectedListener(this); |
|
153 |
String[] levels = new String[MAX_LEVEL]; |
|
154 |
|
|
155 |
for(int i=0; i<MAX_LEVEL; i++) |
|
156 |
{ |
|
157 |
levels[i] = act.getString(R.string.lv_placeholder,i+1); |
|
158 |
} |
|
159 |
|
|
160 |
ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, levels); |
|
161 |
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
|
162 |
mLevelSpinner.setAdapter(adapterType); |
|
163 |
mLevelSpinner.setSelection(mLevelValue-1); |
|
164 |
} |
|
165 |
|
|
166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
167 |
|
|
168 |
private void setupPlayButton(final RubikActivity act, final float scale) |
|
169 |
{ |
|
170 |
int padding = (int)(3*scale + 0.5f); |
|
171 |
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT); |
|
172 |
mPlayButton = new Button(act); |
|
173 |
mPlayButton.setLayoutParams(backParams); |
|
174 |
mPlayButton.setPadding(padding,0,padding,0); |
|
175 |
mPlayButton.setText(R.string.play); |
|
176 |
|
|
177 |
mPlayButton.setOnClickListener( new View.OnClickListener() |
|
178 |
{ |
|
179 |
@Override |
|
180 |
public void onClick(View v) |
|
181 |
{ |
|
182 |
act.getPostRender().scrambleObject(mLevelValue); |
|
183 |
} |
|
184 |
}); |
|
185 |
} |
|
186 |
|
|
187 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
188 |
|
|
189 |
private void setupSolveButton(final RubikActivity act, final float scale) |
|
190 |
{ |
|
191 |
int padding = (int)(3*scale + 0.5f); |
|
192 |
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); |
|
193 |
mSolveButton = new Button(act); |
|
194 |
mSolveButton.setLayoutParams(backParams); |
|
195 |
mSolveButton.setPadding(padding,0,padding,0); |
|
196 |
mSolveButton.setText(R.string.solve); |
|
197 |
|
|
198 |
mSolveButton.setOnClickListener( new View.OnClickListener() |
|
199 |
{ |
|
200 |
@Override |
|
201 |
public void onClick(View v) |
|
202 |
{ |
|
203 |
act.getPostRender().solveObject(); |
|
204 |
} |
|
205 |
}); |
|
206 |
} |
|
207 |
|
|
136 | 208 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
137 | 209 |
|
138 | 210 |
private void setupBackButton(final RubikActivity act, final float scale) |
... | ... | |
169 | 241 |
|
170 | 242 |
BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(R.drawable.cube2); |
171 | 243 |
int cubeWidth = bd.getIntrinsicWidth(); |
172 |
int cubeHeight = bd.getIntrinsicHeight(); |
|
173 |
|
|
174 | 244 |
mLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f); |
175 |
mLayoutHeight= (int)(cubeHeight+ 2*margin + 0.5f); |
|
176 | 245 |
|
177 | 246 |
for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++) |
178 | 247 |
{ |
... | ... | |
217 | 286 |
|
218 | 287 |
public void savePreferences(SharedPreferences.Editor editor) |
219 | 288 |
{ |
220 |
if( mPicker!=null ) |
|
221 |
{ |
|
222 |
editor.putInt("statePlay_scramble", mPicker.getValue() ); |
|
223 |
} |
|
224 |
|
|
289 |
editor.putInt("statePlay_level" , mLevelValue); |
|
225 | 290 |
editor.putInt("statePlay_object", mObject); |
226 | 291 |
editor.putInt("statePlay_size" , mSize); |
227 | 292 |
|
228 |
mObjButton = null; |
|
229 |
mBackButton= null; |
|
293 |
mObjButton = null; |
|
294 |
mBackButton = null; |
|
295 |
mSolveButton = null; |
|
296 |
mPlayButton = null; |
|
297 |
mLevelSpinner= null; |
|
230 | 298 |
|
231 | 299 |
if( mPopup!=null ) |
232 | 300 |
{ |
... | ... | |
239 | 307 |
|
240 | 308 |
public void restorePreferences(SharedPreferences preferences) |
241 | 309 |
{ |
242 |
mPickerValue= preferences.getInt("statePlay_scramble", DEF_SCRAMBLE);
|
|
243 |
mObject = preferences.getInt("statePlay_object" , DEF_OBJECT );
|
|
244 |
mSize = preferences.getInt("statePlay_size" , DEF_SIZE );
|
|
310 |
mLevelValue = preferences.getInt("statePlay_level" , DEF_LEVEL );
|
|
311 |
mObject = preferences.getInt("statePlay_object", DEF_OBJECT);
|
|
312 |
mSize = preferences.getInt("statePlay_size" , DEF_SIZE );
|
|
245 | 313 |
} |
246 | 314 |
|
247 | 315 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
268 | 336 |
|
269 | 337 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
270 | 338 |
|
271 |
public int getPicker()
|
|
339 |
int getLevel()
|
|
272 | 340 |
{ |
273 |
return mPicker!=null ? mPicker.getValue() : 1;
|
|
341 |
return mLevelValue;
|
|
274 | 342 |
} |
275 | 343 |
|
276 | 344 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
286 | 354 |
{ |
287 | 355 |
return mSize; |
288 | 356 |
} |
357 |
|
|
358 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
359 |
|
|
360 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) |
|
361 |
{ |
|
362 |
mLevelValue = pos+1; |
|
363 |
} |
|
364 |
|
|
365 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
366 |
|
|
367 |
public void onNothingSelected(AdapterView<?> parent) { } |
|
289 | 368 |
} |
Also available in: Unified diff
Reorganize UI of the Play state.