Project

General

Profile

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

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

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;
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
    }
74

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

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  public static void goBack(RubikActivity act)
86
    {
87
    switchState(act, mCurrState.mBack );
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  public static void setState(RubikActivity act)
93
    {
94
    mCurrState.enterState(act);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public static void switchState(RubikActivity act, RubikState next)
100
    {
101
    if( next!=null )
102
      {
103
      if( mCurrState!=null ) mCurrState.leaveState(act);
104
      next.enterState(act);
105
      mCurrState = next;
106
      }
107
    else
108
      {
109
      act.finish();
110
      }
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  RubikState(RubikState back, RubikStateAbstract clazz)
116
    {
117
    mBack = back;
118
    mClass = clazz;
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  public RubikStateAbstract getStateClass()
124
    {
125
    return mClass;
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public void leaveState(RubikActivity act)
131
    {
132
    mClass.leaveState(act);
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  public void enterState(RubikActivity act)
138
    {
139
    mClass.enterState(act);
140
    }
141
  }
(1-1/6)