Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikState.java @ 4f470e00

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
  PATT ( MAIN , new RubikStatePattern() ),
33
  ;
34

    
35
  public static final int LENGTH = values().length;
36
  private final RubikState mBack;
37
  private final RubikStateAbstract mClass;
38
  private static final RubikState[] sizes;
39

    
40
  private static RubikState mCurrState, mPrevState;
41

    
42
  static
43
    {
44
    int i = 0;
45
    sizes = new RubikState[LENGTH];
46

    
47
    for(RubikState size: RubikState.values())
48
      {
49
      sizes[i] = size;
50
      i++;
51
      }
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  public static RubikState getState(int ordinal)
57
    {
58
    return sizes[ordinal];
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  public static RubikState getCurrentState()
64
    {
65
    return mCurrState;
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  public static void restorePreferences(SharedPreferences preferences)
79
    {
80
    int currState = preferences.getInt("curr_state", RubikState.MAIN.ordinal() );
81
    int prevState = preferences.getInt("prev_state", RubikState.MAIN.ordinal() );
82

    
83
    mCurrState = getState(currState);
84
    mPrevState = getState(prevState);
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

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

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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