Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikState.java @ 211b48f2

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
  ;
32

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

    
38
  private static RubikState mCurrentState;
39

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

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

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public static void savePreferences(SharedPreferences.Editor editor)
62
    {
63
    editor.putInt("state", mCurrentState.ordinal() );
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  public static void restorePreferences(SharedPreferences preferences)
69
    {
70
    int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() );
71
    mCurrentState = getState(stateOrdinal);
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  public static void goBack(RubikActivity act)
77
    {
78
    switchState(act, mCurrentState.getBack() );
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  public static void setState(RubikActivity act)
84
    {
85
    mCurrentState.enterState(act);
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  public static void switchState(RubikActivity act, RubikState state)
91
    {
92
    if( state!=null )
93
      {
94
      if( mCurrentState!=null ) mCurrentState.leaveState(act);
95
      state.enterState(act);
96
      mCurrentState = state;
97
      }
98
    else
99
      {
100
      act.finish();
101
      }
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  RubikState(RubikState back, RubikStateAbstract clazz)
107
    {
108
    mBack = back;
109
    mClass = clazz;
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  public RubikState getBack()
115
    {
116
    return mBack;
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public RubikStateAbstract getStateClass()
122
    {
123
    return mClass;
124
    }
125

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

    
128
  public void leaveState(RubikActivity act)
129
    {
130
    mClass.leaveState(act);
131
    }
132

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

    
135
  public void enterState(RubikActivity act)
136
    {
137
    mClass.enterState(act);
138
    }
139
  }
(1-1/4)