Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikState.java @ 53f23b64

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 , true , new RubikStateMain()    ),
30
  PLAY ( MAIN , true , new RubikStatePlay()    ),
31
  SOLV ( PLAY , true , new RubikStateSolving() ),
32
  PATT ( MAIN , false, new RubikStatePattern() ),
33
  ;
34

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

    
41
  private static RubikState mCurrState;
42

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  public static boolean canRotate()
72
    {
73
    return mCurrState.mCanRotate;
74
    }
75

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

    
78
  public static void savePreferences(SharedPreferences.Editor editor)
79
    {
80
    editor.putInt("curr_state", mCurrState.ordinal() );
81
    }
82

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

    
85
  public static void restorePreferences(SharedPreferences preferences)
86
    {
87
    int currState = preferences.getInt("curr_state", RubikState.MAIN.ordinal() );
88
    mCurrState = getState(currState);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  public static void goBack(RubikActivity act)
94
    {
95
    switchState(act, mCurrState.mBack );
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public static void setState(RubikActivity act)
101
    {
102
    mCurrState.enterState(act);
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

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

    
123
  RubikState(RubikState back, boolean canRotate, RubikStateAbstract clazz)
124
    {
125
    mBack      = back;
126
    mCanRotate = canRotate;
127
    mClass     = clazz;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  public RubikStateAbstract getStateClass()
133
    {
134
    return mClass;
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public void leaveState(RubikActivity act)
140
    {
141
    mClass.leaveState(act);
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  public void enterState(RubikActivity act)
147
    {
148
    mClass.enterState(act);
149
    }
150
  }
(1-1/6)