Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikState.java @ d18993ac

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 mCurrState, mPrevState;
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 mCurrState;
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  public static void savePreferences(SharedPreferences.Editor editor)
70
    {
71
    editor.putInt("curr_state", mCurrState.ordinal() );
72
    editor.putInt("prev_state", mPrevState.ordinal() );
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  public static void restorePreferences(SharedPreferences preferences)
78
    {
79
    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
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  public static void goBack(RubikActivity act)
89
    {
90
    switchState(act, mCurrState.mBack );
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  public static void setState(RubikActivity act)
96
    {
97
    mCurrState.enterState(act,mPrevState);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  public static void switchState(RubikActivity act, RubikState state)
103
    {
104
    if( state!=null )
105
      {
106
      if( mCurrState!=null ) mCurrState.leaveState(act);
107
      state.enterState(act,mCurrState);
108
      mPrevState = mCurrState;
109
      mCurrState = state;
110
      }
111
    else
112
      {
113
      act.finish();
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  RubikState(RubikState back, RubikStateAbstract clazz)
120
    {
121
    mBack = back;
122
    mClass = clazz;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  public RubikStateAbstract getStateClass()
128
    {
129
    return mClass;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public void leaveState(RubikActivity act)
135
    {
136
    mClass.leaveState(act);
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  public void enterState(RubikActivity act, RubikState prevState)
142
    {
143
    mClass.enterState(act,prevState);
144
    }
145
  }
(1-1/5)