Revision ce0c7ca8
Added by Leszek Koltunski almost 5 years ago
src/main/java/org/distorted/magic/RubikActivity.java | ||
---|---|---|
34 | 34 |
|
35 | 35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
36 | 36 |
|
37 |
public class RubikActivity extends AppCompatActivity implements RubikSettings.OnCompleteListener
|
|
37 |
public class RubikActivity extends AppCompatActivity |
|
38 | 38 |
{ |
39 | 39 |
static final int DEFAULT_SIZE = 3; |
40 | 40 |
private static final int SMALLEST_SIZE = 2; |
... | ... | |
44 | 44 |
public static final int MAX_SCRAMBLE = 10; |
45 | 45 |
|
46 | 46 |
private static int mSize = DEFAULT_SIZE; |
47 |
|
|
48 |
private int[] mPos; |
|
49 |
private int[] mType; |
|
50 |
|
|
51 | 47 |
private HorizontalNumberPicker mPicker; |
52 | 48 |
|
53 | 49 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
60 | 56 |
setContentView(R.layout.main); |
61 | 57 |
markButton(mSize); |
62 | 58 |
|
63 |
mPos = new int[RubikSettingsEnum.LENGTH]; |
|
64 |
mType= new int[RubikSettingsEnum.LENGTH]; |
|
65 |
|
|
66 | 59 |
mPicker = findViewById(R.id.rubikNumberPicker); |
67 | 60 |
mPicker.setMin(MIN_SCRAMBLE); |
68 | 61 |
mPicker.setMax(MAX_SCRAMBLE); |
69 | 62 |
|
70 | 63 |
restorePreferences(); |
71 |
|
|
72 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
|
73 |
{ |
|
74 |
applyPreferences(i); |
|
75 |
} |
|
76 | 64 |
} |
77 | 65 |
|
78 | 66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
117 | 105 |
|
118 | 106 |
public void Settings(View v) |
119 | 107 |
{ |
120 |
Bundle args = new Bundle(); |
|
121 |
|
|
122 |
String name; |
|
123 |
|
|
124 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
|
125 |
{ |
|
126 |
RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i); |
|
127 |
name = rse.name(); |
|
128 |
|
|
129 |
args.putInt(name+"_Pos" , mPos[i]); |
|
130 |
args.putInt(name+"_Type", mType[i]); |
|
131 |
} |
|
132 |
|
|
133 | 108 |
RubikSettings settings = new RubikSettings(); |
134 |
settings.setArguments(args); |
|
135 | 109 |
settings.show(getSupportFragmentManager(), null); |
136 | 110 |
} |
137 | 111 |
|
... | ... | |
161 | 135 |
view.getRenderer().solveCube(); |
162 | 136 |
} |
163 | 137 |
|
164 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
165 |
|
|
166 |
public void onComplete(int index, int position, int type ) |
|
167 |
{ |
|
168 |
if( index>=0 && index<RubikSettingsEnum.LENGTH ) |
|
169 |
{ |
|
170 |
mPos[index] = position; |
|
171 |
mType[index]= type; |
|
172 |
|
|
173 |
applyPreferences(index); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 | 138 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
178 | 139 |
|
179 | 140 |
public void setSize(View v) |
... | ... | |
223 | 184 |
{ |
224 | 185 |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); |
225 | 186 |
SharedPreferences.Editor editor = preferences.edit(); |
226 |
String name; |
|
227 | 187 |
|
228 | 188 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
229 | 189 |
{ |
230 |
RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i); |
|
231 |
name = rse.name(); |
|
232 |
|
|
233 |
editor.putInt(name+"_Pos" , mPos[i]); |
|
234 |
editor.putInt(name+"_Type", mType[i]); |
|
190 |
RubikSettingsEnum.getEnum(i).savePreferences(editor); |
|
235 | 191 |
} |
236 | 192 |
|
237 | 193 |
editor.putInt("scramble", mPicker.getValue() ); |
... | ... | |
245 | 201 |
{ |
246 | 202 |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); |
247 | 203 |
|
248 |
String name; |
|
249 |
|
|
250 | 204 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
251 | 205 |
{ |
252 |
RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i); |
|
253 |
name = rse.name(); |
|
254 |
|
|
255 |
mPos[i] = preferences.getInt(name+"_Pos" , rse.getDefaultPos() ); |
|
256 |
mType[i] = preferences.getInt(name+"_Type", rse.getDefaultType()); |
|
206 |
RubikSettingsEnum.getEnum(i).restorePreferences(preferences); |
|
257 | 207 |
} |
258 | 208 |
|
259 | 209 |
int scramble= preferences.getInt("scramble", MIN_SCRAMBLE); |
260 | 210 |
|
261 | 211 |
mPicker.setValue(scramble); |
262 | 212 |
} |
263 |
|
|
264 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
265 |
|
|
266 |
private void applyPreferences(int index) |
|
267 |
{ |
|
268 |
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView); |
|
269 |
RubikRenderer renderer = view.getRenderer(); |
|
270 |
|
|
271 |
renderer.setPos (index, translateDuration(mPos[index])+1); |
|
272 |
renderer.setType(index, mType[index] ); |
|
273 |
} |
|
274 |
|
|
275 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
276 |
|
|
277 |
public static int translateDuration(int pos) |
|
278 |
{ |
|
279 |
return (pos/2)*100; |
|
280 |
} |
|
281 | 213 |
} |
src/main/java/org/distorted/magic/RubikRenderer.java | ||
---|---|---|
56 | 56 |
private RubikCube mOldCube, mNewCube; |
57 | 57 |
private int mScreenWidth, mScreenHeight; |
58 | 58 |
private MeshFlat mMesh; |
59 |
private int[] mPos; |
|
60 |
private int[] mType; |
|
61 | 59 |
|
62 | 60 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
63 | 61 |
|
... | ... | |
90 | 88 |
mCanScramble = true; |
91 | 89 |
mCanSolve = true; |
92 | 90 |
|
93 |
mPos = new int[RubikSettingsEnum.LENGTH]; |
|
94 |
mType = new int[RubikSettingsEnum.LENGTH]; |
|
95 |
|
|
96 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
|
97 |
{ |
|
98 |
mPos[i] = 1000; |
|
99 |
} |
|
100 |
|
|
101 | 91 |
mMesh= new MeshFlat(20,20); |
102 | 92 |
mNextCubeSize =RubikActivity.getSize(); |
103 | 93 |
} |
... | ... | |
144 | 134 |
mCanDrag = false; |
145 | 135 |
mCanRotate = false; |
146 | 136 |
mNextCubeSize = 0; |
147 |
|
|
148 |
sizeChangeEffect(); |
|
137 |
changeSizeNow(); |
|
149 | 138 |
} |
150 | 139 |
|
151 | 140 |
if( mSolveCube ) |
... | ... | |
239 | 228 |
} |
240 | 229 |
} |
241 | 230 |
|
242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
243 |
|
|
244 |
private void sizeChangeEffect() |
|
245 |
{ |
|
246 |
try |
|
247 |
{ |
|
248 |
SizeChangeEffect effect = SizeChangeEffect.create(mType[0]); |
|
249 |
mSizeChangeEffectID = effect.start(mPos[0],mScreen,mOldCube,mNewCube,this); |
|
250 |
} |
|
251 |
catch(Exception ex) |
|
252 |
{ |
|
253 |
android.util.Log.e("Renderer", "failed to create SizeChangeEffect, exception: "+ex.getMessage()); |
|
254 |
|
|
255 |
if( mOldCube!=null ) mScreen.detach(mOldCube); |
|
256 |
mScreen.attach(mNewCube); |
|
257 |
mCanRotate = true; |
|
258 |
mCanDrag = true; |
|
259 |
} |
|
260 |
} |
|
261 |
|
|
262 | 231 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
263 | 232 |
|
264 | 233 |
private float computeFOV(float cameraDistance, int screenHeight) |
... | ... | |
275 | 244 |
mFinishRotation = true; |
276 | 245 |
} |
277 | 246 |
|
278 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
279 |
|
|
280 |
void setPos(int index, int pos) |
|
281 |
{ |
|
282 |
if( index>=0 && index<RubikSettingsEnum.LENGTH ) mPos[index] = pos; |
|
283 |
} |
|
284 |
|
|
285 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
286 |
|
|
287 |
void setType(int index, int type) |
|
288 |
{ |
|
289 |
if( index>=0 && index<RubikSettingsEnum.LENGTH ) mType[index] = type; |
|
290 |
} |
|
291 |
|
|
292 | 247 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
293 | 248 |
|
294 | 249 |
boolean createCube(int newSize) |
... | ... | |
304 | 259 |
|
305 | 260 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
306 | 261 |
|
307 |
private void createCubeNow(int newSize)
|
|
262 |
private void recomputeScaleFactor(int screenWidth, int screenHeight)
|
|
308 | 263 |
{ |
309 |
if( mOldCube!=null ) mOldCube.releaseResources(); |
|
310 |
mOldCube = mNewCube; |
|
264 |
mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth); |
|
311 | 265 |
|
312 |
DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE); |
|
313 |
DistortedEffects effects = new DistortedEffects(); |
|
266 |
if( mNewCube!=null ) |
|
267 |
{ |
|
268 |
mNewCube.recomputeScaleFactor(screenWidth, screenHeight, mCubeSizeInScreenSpace); |
|
269 |
} |
|
270 |
} |
|
314 | 271 |
|
315 |
mNewCube = new RubikCube(newSize, mQuatCurrent, mQuatAccumulated, texture, mMesh, effects); |
|
316 |
mNewCube.createTexture(); |
|
272 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
317 | 273 |
|
318 |
if( mScreenWidth!=0 ) |
|
274 |
void scrambleCube(int num) |
|
275 |
{ |
|
276 |
if( mCanScramble ) |
|
319 | 277 |
{ |
320 |
recomputeScaleFactor(mScreenWidth,mScreenHeight); |
|
278 |
mScrambleCube = true; |
|
279 |
mScrambleCubeNum = num; |
|
321 | 280 |
} |
322 | 281 |
} |
323 | 282 |
|
324 | 283 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
325 | 284 |
|
326 |
private void recomputeScaleFactor(int screenWidth, int screenHeight)
|
|
285 |
void solveCube()
|
|
327 | 286 |
{ |
328 |
mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth); |
|
329 |
|
|
330 |
if( mNewCube!=null ) |
|
287 |
if( mCanSolve ) |
|
331 | 288 |
{ |
332 |
mNewCube.recomputeScaleFactor(screenWidth, screenHeight, mCubeSizeInScreenSpace);
|
|
289 |
mSolveCube = true;
|
|
333 | 290 |
} |
334 | 291 |
} |
335 | 292 |
|
336 | 293 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
337 | 294 |
|
338 |
void scrambleCube(int num)
|
|
295 |
private void createCubeNow(int newSize)
|
|
339 | 296 |
{ |
340 |
if( mCanScramble ) |
|
297 |
if( mOldCube!=null ) mOldCube.releaseResources(); |
|
298 |
mOldCube = mNewCube; |
|
299 |
|
|
300 |
DistortedTexture texture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE); |
|
301 |
DistortedEffects effects = new DistortedEffects(); |
|
302 |
|
|
303 |
mNewCube = new RubikCube(newSize, mQuatCurrent, mQuatAccumulated, texture, mMesh, effects); |
|
304 |
mNewCube.createTexture(); |
|
305 |
|
|
306 |
if( mScreenWidth!=0 ) |
|
341 | 307 |
{ |
342 |
mScrambleCube = true; |
|
343 |
mScrambleCubeNum = num; |
|
308 |
recomputeScaleFactor(mScreenWidth,mScreenHeight); |
|
344 | 309 |
} |
345 | 310 |
} |
346 | 311 |
|
347 | 312 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
348 | 313 |
|
349 |
private void scrambleCubeNow()
|
|
314 |
private void changeSizeNow()
|
|
350 | 315 |
{ |
351 | 316 |
try |
352 | 317 |
{ |
353 |
ScrambleEffect effect = ScrambleEffect.create(mType[2]); |
|
354 |
mScrambleEffectID = effect.start(mPos[2],mNewCube,mScrambleCubeNum,this); |
|
318 |
int pos0 = RubikSettingsEnum.getEnum(0).getCurrentPos(); |
|
319 |
int typ0 = RubikSettingsEnum.getEnum(0).getCurrentType(); |
|
320 |
int pos = RubikSettingsEnum.translatePos(pos0)+1; |
|
321 |
SizeChangeEffect effect = SizeChangeEffect.create(typ0); |
|
322 |
mSizeChangeEffectID = effect.start(pos,mScreen,mOldCube,mNewCube,this); |
|
355 | 323 |
} |
356 | 324 |
catch(Exception ex) |
357 | 325 |
{ |
358 |
android.util.Log.e("Renderer", "failed to create ScrambleEffect, exception: "+ex.getMessage());
|
|
326 |
android.util.Log.e("Renderer", "failed to create SizeChangeEffect, exception: "+ex.getMessage());
|
|
359 | 327 |
|
328 |
if( mOldCube!=null ) mScreen.detach(mOldCube); |
|
329 |
mScreen.attach(mNewCube); |
|
360 | 330 |
mCanRotate = true; |
361 | 331 |
mCanDrag = true; |
362 | 332 |
} |
... | ... | |
364 | 334 |
|
365 | 335 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
366 | 336 |
|
367 |
void solveCube()
|
|
337 |
private void solveCubeNow()
|
|
368 | 338 |
{ |
369 |
if( mCanSolve )
|
|
339 |
try
|
|
370 | 340 |
{ |
371 |
mSolveCube = true; |
|
341 |
int pos1 = RubikSettingsEnum.getEnum(1).getCurrentPos(); |
|
342 |
int typ1 = RubikSettingsEnum.getEnum(1).getCurrentType(); |
|
343 |
int pos = RubikSettingsEnum.translatePos(pos1)+1; |
|
344 |
SolveEffect effect = SolveEffect.create(typ1); |
|
345 |
mSolveEffectID = effect.start(pos,mScreen,mNewCube,this); |
|
346 |
} |
|
347 |
catch(Exception ex) |
|
348 |
{ |
|
349 |
android.util.Log.e("Renderer", "failed to create SolveEffect, exception: "+ex.getMessage()); |
|
350 |
|
|
351 |
mNewCube.solve(); // |
|
352 |
mCanRotate = true; // just solve the cube |
|
353 |
mCanDrag = true; // |
|
372 | 354 |
} |
373 | 355 |
} |
374 | 356 |
|
375 | 357 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
376 | 358 |
|
377 |
private void solveCubeNow()
|
|
359 |
private void scrambleCubeNow()
|
|
378 | 360 |
{ |
379 | 361 |
try |
380 | 362 |
{ |
381 |
SolveEffect effect = SolveEffect.create(mType[1]); |
|
382 |
mSolveEffectID = effect.start(mPos[1],mScreen,mNewCube,this); |
|
363 |
int pos2 = RubikSettingsEnum.getEnum(2).getCurrentPos(); |
|
364 |
int typ2 = RubikSettingsEnum.getEnum(2).getCurrentType(); |
|
365 |
int pos = RubikSettingsEnum.translatePos(pos2)+1; |
|
366 |
ScrambleEffect effect = ScrambleEffect.create(typ2); |
|
367 |
mScrambleEffectID = effect.start(pos,mNewCube,mScrambleCubeNum,this); |
|
383 | 368 |
} |
384 | 369 |
catch(Exception ex) |
385 | 370 |
{ |
386 |
android.util.Log.e("Renderer", "failed to create SolveEffect, exception: "+ex.getMessage());
|
|
371 |
android.util.Log.e("Renderer", "failed to create ScrambleEffect, exception: "+ex.getMessage());
|
|
387 | 372 |
|
388 |
mNewCube.solve(); // |
|
389 |
mCanRotate = true; // just solve the cube |
|
390 |
mCanDrag = true; // |
|
373 |
mCanRotate = true; |
|
374 |
mCanDrag = true; |
|
391 | 375 |
} |
392 | 376 |
} |
393 | 377 |
|
src/main/java/org/distorted/magic/RubikSettings.java | ||
---|---|---|
20 | 20 |
package org.distorted.magic; |
21 | 21 |
|
22 | 22 |
import android.app.Dialog; |
23 |
import android.content.Context; |
|
24 |
import android.content.DialogInterface; |
|
25 | 23 |
import android.os.Bundle; |
26 | 24 |
import android.support.annotation.NonNull; |
27 |
import android.support.annotation.Nullable; |
|
28 | 25 |
import android.support.v4.app.FragmentActivity; |
29 | 26 |
import android.support.v7.app.AlertDialog; |
30 | 27 |
import android.support.v7.app.AppCompatDialogFragment; |
... | ... | |
42 | 39 |
|
43 | 40 |
public class RubikSettings extends AppCompatDialogFragment implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener |
44 | 41 |
{ |
45 |
public interface OnCompleteListener |
|
46 |
{ |
|
47 |
void onComplete(int index, int position, int type); |
|
48 |
} |
|
49 |
|
|
50 |
private OnCompleteListener mListener; |
|
51 |
|
|
52 | 42 |
private TextView[] mDurationText; |
53 |
private int[] mSeekBarID; |
|
54 |
private int[] mSpinnerID; |
|
55 |
private int[] mPos; |
|
56 |
private int[] mType; |
|
57 | 43 |
|
58 | 44 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
59 | 45 |
|
60 | 46 |
public RubikSettings() |
61 | 47 |
{ |
62 | 48 |
mDurationText = new TextView[RubikSettingsEnum.LENGTH]; |
63 |
mSeekBarID = new int[RubikSettingsEnum.LENGTH]; |
|
64 |
mSpinnerID = new int[RubikSettingsEnum.LENGTH]; |
|
65 |
mPos = new int[RubikSettingsEnum.LENGTH]; |
|
66 |
mType = new int[RubikSettingsEnum.LENGTH]; |
|
67 |
} |
|
68 |
|
|
69 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
70 |
|
|
71 |
@Override |
|
72 |
public void onAttach(Context context) |
|
73 |
{ |
|
74 |
super.onAttach(context); |
|
75 |
|
|
76 |
try |
|
77 |
{ |
|
78 |
mListener = (OnCompleteListener)context; |
|
79 |
} |
|
80 |
catch (final ClassCastException e) |
|
81 |
{ |
|
82 |
throw new ClassCastException(context.toString() + " must implement OnCompleteListener"); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
87 |
|
|
88 |
@Override |
|
89 |
public void onCreate(@Nullable Bundle savedInstanceState) |
|
90 |
{ |
|
91 |
super.onCreate(savedInstanceState); |
|
92 |
|
|
93 |
Bundle args = getArguments(); |
|
94 |
|
|
95 |
String name; |
|
96 |
|
|
97 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
|
98 |
{ |
|
99 |
RubikSettingsEnum rse = RubikSettingsEnum.getEnum(i); |
|
100 |
name = rse.name(); |
|
101 |
|
|
102 |
try |
|
103 |
{ |
|
104 |
mPos[i] = args.getInt(name+"_Pos" ); |
|
105 |
mType[i] = args.getInt(name+"_Type"); |
|
106 |
} |
|
107 |
catch(NullPointerException ex) |
|
108 |
{ |
|
109 |
mPos[i] = rse.getDefaultPos(); |
|
110 |
mType[i] = rse.getDefaultType(); |
|
111 |
} |
|
112 |
} |
|
113 | 49 |
} |
114 | 50 |
|
115 | 51 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
120 | 56 |
{ |
121 | 57 |
FragmentActivity act = getActivity(); |
122 | 58 |
AlertDialog.Builder builder = new AlertDialog.Builder(act); |
123 |
|
|
124 | 59 |
builder.setCancelable(false); |
125 |
builder.setPositiveButton( R.string.save, new DialogInterface.OnClickListener() |
|
126 |
{ |
|
127 |
@Override |
|
128 |
public void onClick(DialogInterface dialog, int which) |
|
129 |
{ |
|
130 |
saveOptions(); |
|
131 |
} |
|
132 |
}); |
|
133 | 60 |
|
134 | 61 |
LayoutInflater inflater = act.getLayoutInflater(); |
135 | 62 |
final View view = inflater.inflate(R.layout.settings, null); |
... | ... | |
162 | 89 |
///// TEXT /////////////////////////////////////////////////////////////////////////// |
163 | 90 |
|
164 | 91 |
int layoutHeight = (int)(scale*48 + 0.5f); |
165 |
int padding = (int)(scale*15 + 0.5f);
|
|
92 |
int padding = (int)(scale*10 + 0.5f);
|
|
166 | 93 |
|
167 | 94 |
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,layoutHeight); |
168 | 95 |
|
... | ... | |
233 | 160 |
seekBar.setId(index); |
234 | 161 |
innerLayout1.addView(seekBar); |
235 | 162 |
|
236 |
mSeekBarID[index] = seekBar.getId(); |
|
237 |
|
|
238 | 163 |
seekBar.setOnSeekBarChangeListener(this); |
239 |
seekBar.setProgress(mPos[index]);
|
|
164 |
seekBar.setProgress(rsEnum.getCurrentPos());
|
|
240 | 165 |
|
241 | 166 |
///// INNER LAYOUT2 ////////////////////////////////////////////////////////////////// |
242 | 167 |
|
... | ... | |
272 | 197 |
spinner.setId(index); |
273 | 198 |
innerLayout2.addView(spinner); |
274 | 199 |
|
275 |
mSpinnerID[index] = spinner.getId(); |
|
276 |
|
|
277 | 200 |
spinner.setOnItemSelectedListener(this); |
278 | 201 |
String[] appear = RubikSettingsEnum.getNames(index); |
279 | 202 |
ArrayAdapter<String> adapterType = new ArrayAdapter<>(act,android.R.layout.simple_spinner_item, appear); |
280 | 203 |
adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
281 | 204 |
spinner.setAdapter(adapterType); |
282 |
|
|
283 |
int type = mType[index]; |
|
284 |
|
|
285 |
if(type>=0 && type<appear.length) |
|
286 |
{ |
|
287 |
spinner.setSelection(type); |
|
288 |
} |
|
289 |
} |
|
290 |
|
|
291 |
|
|
292 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
293 |
|
|
294 |
private void saveOptions() |
|
295 |
{ |
|
296 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++) |
|
297 |
{ |
|
298 |
mListener.onComplete(i, mPos[i], mType[i]); |
|
299 |
} |
|
205 |
spinner.setSelection(rsEnum.getCurrentType()); |
|
300 | 206 |
} |
301 | 207 |
|
302 | 208 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
305 | 211 |
{ |
306 | 212 |
int parentID = parent.getId(); |
307 | 213 |
|
308 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
|
214 |
if( parentID>=0 && parentID< RubikSettingsEnum.LENGTH) // ith spinner's ID is equal to i (see createSettingSection)
|
|
309 | 215 |
{ |
310 |
if( mSpinnerID[i] == parentID ) |
|
311 |
{ |
|
312 |
mType[i] = pos; |
|
313 |
break; |
|
314 |
} |
|
216 |
RubikSettingsEnum.getEnum(parentID).setCurrentType(pos); |
|
315 | 217 |
} |
316 | 218 |
} |
317 | 219 |
|
... | ... | |
321 | 223 |
{ |
322 | 224 |
int barID = bar.getId(); |
323 | 225 |
|
324 |
for (int i=0; i<RubikSettingsEnum.LENGTH; i++)
|
|
226 |
if( barID>=0 && barID< RubikSettingsEnum.LENGTH) // ith seekbar's ID is equal to i (see createSettingSection)
|
|
325 | 227 |
{ |
326 |
if( mSeekBarID[i] == barID ) |
|
327 |
{ |
|
328 |
mPos[i] = progress; |
|
329 |
int ms = RubikActivity.translateDuration(mPos[i]); |
|
330 |
mDurationText[i].setText(getString(R.string.ms_placeholder,ms)); |
|
331 |
break; |
|
332 |
} |
|
228 |
RubikSettingsEnum.getEnum(barID).setCurrentPos(progress); |
|
229 |
int ms = RubikSettingsEnum.translatePos(progress); |
|
230 |
mDurationText[barID].setText(getString(R.string.ms_placeholder,ms)); |
|
333 | 231 |
} |
334 | 232 |
} |
335 | 233 |
|
src/main/java/org/distorted/magic/RubikSettingsEnum.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.magic; |
21 | 21 |
|
22 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
22 |
import android.content.SharedPreferences;
|
|
23 | 23 |
|
24 | 24 |
import org.distorted.effect.ScrambleEffect; |
25 | 25 |
import org.distorted.effect.SizeChangeEffect; |
26 | 26 |
import org.distorted.effect.SolveEffect; |
27 | 27 |
|
28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
29 |
|
|
28 | 30 |
enum RubikSettingsEnum |
29 | 31 |
{ |
30 |
SIZECHANGE ( 20, 1, R.string.sizechange_effect ),
|
|
32 |
SIZECHANGE ( 20, 1, R.string.sizechange_effect), |
|
31 | 33 |
SOLVE ( 20, 1, R.string.solve_effect ), |
32 |
SCRAMBLE ( 100, 0, R.string.scramble_effect );
|
|
34 |
SCRAMBLE ( 100, 1, R.string.scramble_effect );
|
|
33 | 35 |
|
34 | 36 |
public static final int LENGTH = values().length; |
35 |
private int mDefaultPos, mDefaultType; |
|
37 |
private final int mDefaultPos, mDefaultType; |
|
38 |
private int mCurrentPos, mCurrentType; |
|
36 | 39 |
private int mText; |
37 | 40 |
|
38 | 41 |
private static final RubikSettingsEnum[] enums; // copy the values() to a local variable so that we |
... | ... | |
54 | 57 |
|
55 | 58 |
RubikSettingsEnum( int dPos, int dType, int text ) |
56 | 59 |
{ |
57 |
mDefaultPos = dPos; |
|
58 |
mDefaultType = dType; |
|
60 |
mDefaultPos = mCurrentPos = dPos;
|
|
61 |
mDefaultType = mCurrentType= dType;
|
|
59 | 62 |
mText = text; |
60 | 63 |
} |
61 | 64 |
|
62 | 65 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
63 | 66 |
|
64 |
int getDefaultPos()
|
|
67 |
int getText()
|
|
65 | 68 |
{ |
66 |
return mDefaultPos;
|
|
69 |
return mText;
|
|
67 | 70 |
} |
68 | 71 |
|
69 | 72 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
70 | 73 |
|
71 |
int getDefaultType()
|
|
74 |
int getCurrentPos()
|
|
72 | 75 |
{ |
73 |
return mDefaultType;
|
|
76 |
return mCurrentPos;
|
|
74 | 77 |
} |
75 | 78 |
|
76 | 79 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
77 | 80 |
|
78 |
int getText()
|
|
81 |
int getCurrentType()
|
|
79 | 82 |
{ |
80 |
return mText; |
|
83 |
return mCurrentType; |
|
84 |
} |
|
85 |
|
|
86 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
87 |
|
|
88 |
void setCurrentPos(int pos) |
|
89 |
{ |
|
90 |
mCurrentPos = pos; |
|
91 |
} |
|
92 |
|
|
93 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
94 |
|
|
95 |
void setCurrentType(int type) |
|
96 |
{ |
|
97 |
mCurrentType = type; |
|
98 |
} |
|
99 |
|
|
100 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
101 |
|
|
102 |
void savePreferences(SharedPreferences.Editor editor) |
|
103 |
{ |
|
104 |
String name = name(); |
|
105 |
|
|
106 |
editor.putInt(name+"_Pos" , mCurrentPos ); |
|
107 |
editor.putInt(name+"_Type", mCurrentType); |
|
108 |
} |
|
109 |
|
|
110 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
111 |
|
|
112 |
void restorePreferences(SharedPreferences preferences) |
|
113 |
{ |
|
114 |
String name = name(); |
|
115 |
|
|
116 |
mCurrentPos = preferences.getInt(name+"_Pos" , mDefaultPos ); |
|
117 |
mCurrentType = preferences.getInt(name+"_Type", mDefaultType); |
|
81 | 118 |
} |
82 | 119 |
|
83 | 120 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
100 | 137 |
|
101 | 138 |
return null; |
102 | 139 |
} |
140 |
|
|
141 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
142 |
|
|
143 |
static int translatePos(int pos) |
|
144 |
{ |
|
145 |
return (pos/2)*100; |
|
146 |
} |
|
103 | 147 |
} |
Also available in: Unified diff
RubikCube: major progress with a separate RubikSettingsEnum enum - almost finished.