1 |
f2a9be6d
|
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 |
211b48f2
|
Leszek Koltunski
|
package org.distorted.uistate;
|
21 |
f2a9be6d
|
Leszek Koltunski
|
|
22 |
211b48f2
|
Leszek Koltunski
|
import android.content.SharedPreferences;
|
23 |
|
|
import org.distorted.magic.RubikActivity;
|
24 |
f2a9be6d
|
Leszek Koltunski
|
|
25 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
|
|
|
27 |
211b48f2
|
Leszek Koltunski
|
public enum RubikState
|
28 |
f2a9be6d
|
Leszek Koltunski
|
{
|
29 |
ad9e8bb3
|
Leszek Koltunski
|
MAIN ( null , new RubikStateMain() ),
|
30 |
|
|
PLAY ( MAIN , new RubikStatePlay() ),
|
31 |
|
|
SOLV ( PLAY , new RubikStateSolving() ),
|
32 |
211b48f2
|
Leszek Koltunski
|
;
|
33 |
|
|
|
34 |
|
|
public static final int LENGTH = values().length;
|
35 |
|
|
private final RubikState mBack;
|
36 |
|
|
private final RubikStateAbstract mClass;
|
37 |
|
|
private static final RubikState[] sizes;
|
38 |
|
|
|
39 |
d18993ac
|
Leszek Koltunski
|
private static RubikState mCurrState, mPrevState;
|
40 |
211b48f2
|
Leszek Koltunski
|
|
41 |
|
|
static
|
42 |
|
|
{
|
43 |
|
|
int i = 0;
|
44 |
|
|
sizes = new RubikState[LENGTH];
|
45 |
|
|
|
46 |
|
|
for(RubikState size: RubikState.values())
|
47 |
|
|
{
|
48 |
|
|
sizes[i] = size;
|
49 |
|
|
i++;
|
50 |
|
|
}
|
51 |
|
|
}
|
52 |
f2a9be6d
|
Leszek Koltunski
|
|
53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
211b48f2
|
Leszek Koltunski
|
public static RubikState getState(int ordinal)
|
56 |
f2a9be6d
|
Leszek Koltunski
|
{
|
57 |
211b48f2
|
Leszek Koltunski
|
return sizes[ordinal];
|
58 |
f2a9be6d
|
Leszek Koltunski
|
}
|
59 |
|
|
|
60 |
ad9e8bb3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61 |
|
|
|
62 |
|
|
public static RubikState getCurrentState()
|
63 |
|
|
{
|
64 |
d18993ac
|
Leszek Koltunski
|
return mCurrState;
|
65 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
66 |
|
|
|
67 |
f2a9be6d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68 |
|
|
|
69 |
211b48f2
|
Leszek Koltunski
|
public static void savePreferences(SharedPreferences.Editor editor)
|
70 |
f2a9be6d
|
Leszek Koltunski
|
{
|
71 |
d18993ac
|
Leszek Koltunski
|
editor.putInt("curr_state", mCurrState.ordinal() );
|
72 |
|
|
editor.putInt("prev_state", mPrevState.ordinal() );
|
73 |
f2a9be6d
|
Leszek Koltunski
|
}
|
74 |
|
|
|
75 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
|
77 |
211b48f2
|
Leszek Koltunski
|
public static void restorePreferences(SharedPreferences preferences)
|
78 |
f2a9be6d
|
Leszek Koltunski
|
{
|
79 |
d18993ac
|
Leszek Koltunski
|
int currState = preferences.getInt("curr_state", RubikState.MAIN.ordinal() );
|
80 |
|
|
int prevState = preferences.getInt("prev_state", RubikState.MAIN.ordinal() );
|
81 |
|
|
|
82 |
|
|
mCurrState = getState(currState);
|
83 |
|
|
mPrevState = getState(prevState);
|
84 |
211b48f2
|
Leszek Koltunski
|
}
|
85 |
|
|
|
86 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87 |
f2a9be6d
|
Leszek Koltunski
|
|
88 |
211b48f2
|
Leszek Koltunski
|
public static void goBack(RubikActivity act)
|
89 |
|
|
{
|
90 |
d18993ac
|
Leszek Koltunski
|
switchState(act, mCurrState.mBack );
|
91 |
f2a9be6d
|
Leszek Koltunski
|
}
|
92 |
|
|
|
93 |
a675474f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94 |
|
|
|
95 |
211b48f2
|
Leszek Koltunski
|
public static void setState(RubikActivity act)
|
96 |
a675474f
|
Leszek Koltunski
|
{
|
97 |
d18993ac
|
Leszek Koltunski
|
mCurrState.enterState(act,mPrevState);
|
98 |
a675474f
|
Leszek Koltunski
|
}
|
99 |
|
|
|
100 |
f2a9be6d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101 |
|
|
|
102 |
211b48f2
|
Leszek Koltunski
|
public static void switchState(RubikActivity act, RubikState state)
|
103 |
b8b38548
|
Leszek Koltunski
|
{
|
104 |
211b48f2
|
Leszek Koltunski
|
if( state!=null )
|
105 |
|
|
{
|
106 |
d18993ac
|
Leszek Koltunski
|
if( mCurrState!=null ) mCurrState.leaveState(act);
|
107 |
|
|
state.enterState(act,mCurrState);
|
108 |
|
|
mPrevState = mCurrState;
|
109 |
|
|
mCurrState = state;
|
110 |
211b48f2
|
Leszek Koltunski
|
}
|
111 |
|
|
else
|
112 |
|
|
{
|
113 |
|
|
act.finish();
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
b8b38548
|
Leszek Koltunski
|
|
119 |
211b48f2
|
Leszek Koltunski
|
RubikState(RubikState back, RubikStateAbstract clazz)
|
120 |
|
|
{
|
121 |
|
|
mBack = back;
|
122 |
|
|
mClass = clazz;
|
123 |
946d9762
|
Leszek Koltunski
|
}
|
124 |
b8b38548
|
Leszek Koltunski
|
|
125 |
211b48f2
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126 |
|
|
|
127 |
|
|
public RubikStateAbstract getStateClass()
|
128 |
|
|
{
|
129 |
|
|
return mClass;
|
130 |
|
|
}
|
131 |
b8b38548
|
Leszek Koltunski
|
|
132 |
211b48f2
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133 |
b8b38548
|
Leszek Koltunski
|
|
134 |
211b48f2
|
Leszek Koltunski
|
public void leaveState(RubikActivity act)
|
135 |
|
|
{
|
136 |
|
|
mClass.leaveState(act);
|
137 |
|
|
}
|
138 |
b8b38548
|
Leszek Koltunski
|
|
139 |
211b48f2
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140 |
cc5ec229
|
Leszek Koltunski
|
|
141 |
d18993ac
|
Leszek Koltunski
|
public void enterState(RubikActivity act, RubikState prevState)
|
142 |
211b48f2
|
Leszek Koltunski
|
{
|
143 |
d18993ac
|
Leszek Koltunski
|
mClass.enterState(act,prevState);
|
144 |
f2a9be6d
|
Leszek Koltunski
|
}
|
145 |
211b48f2
|
Leszek Koltunski
|
}
|