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.SharedPreferences;
|
23
|
import org.distorted.magic.RubikActivity;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
public enum RubikState
|
28
|
{
|
29
|
MAIN ( null , new RubikStateMain() ),
|
30
|
PLAY ( MAIN , new RubikStatePlay() ),
|
31
|
SOLV ( PLAY , new RubikStateSolving() ),
|
32
|
;
|
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
|
private static RubikState mCurrentState;
|
40
|
|
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
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
public static RubikState getState(int ordinal)
|
56
|
{
|
57
|
return sizes[ordinal];
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
public static RubikState getCurrentState()
|
63
|
{
|
64
|
return mCurrentState;
|
65
|
}
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
public static void savePreferences(SharedPreferences.Editor editor)
|
70
|
{
|
71
|
editor.putInt("state", mCurrentState.ordinal() );
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
public static void restorePreferences(SharedPreferences preferences)
|
77
|
{
|
78
|
int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() );
|
79
|
mCurrentState = getState(stateOrdinal);
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
|
84
|
public static void goBack(RubikActivity act)
|
85
|
{
|
86
|
switchState(act, mCurrentState.mBack );
|
87
|
}
|
88
|
|
89
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
90
|
|
91
|
public static void setState(RubikActivity act)
|
92
|
{
|
93
|
mCurrentState.enterState(act);
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
public static void switchState(RubikActivity act, RubikState state)
|
99
|
{
|
100
|
if( state!=null )
|
101
|
{
|
102
|
if( mCurrentState!=null ) mCurrentState.leaveState(act);
|
103
|
state.enterState(act);
|
104
|
mCurrentState = state;
|
105
|
}
|
106
|
else
|
107
|
{
|
108
|
act.finish();
|
109
|
}
|
110
|
}
|
111
|
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
RubikState(RubikState back, RubikStateAbstract clazz)
|
115
|
{
|
116
|
mBack = back;
|
117
|
mClass = clazz;
|
118
|
}
|
119
|
|
120
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
|
122
|
public RubikStateAbstract getStateClass()
|
123
|
{
|
124
|
return mClass;
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
public void leaveState(RubikActivity act)
|
130
|
{
|
131
|
mClass.leaveState(act);
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
public void enterState(RubikActivity act)
|
137
|
{
|
138
|
mClass.enterState(act);
|
139
|
}
|
140
|
}
|