Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikState.java @ 4f36e418

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.states;
21

    
22
import android.content.SharedPreferences;
23
import org.distorted.main.RubikActivity;
24
import static org.distorted.main.RubikSurfaceView.*;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public enum RubikState
29
  {
30
  MAIN ( null , MODE_DRAG   , new RubikStateMain()     ),
31
  PLAY ( MAIN , MODE_ROTATE , new RubikStatePlay()     ),
32
  SOLV ( PLAY , MODE_ROTATE , new RubikStateSolving()  ),
33
  PATT ( MAIN , MODE_DRAG   , new RubikStatePattern()  ),
34
  SVER ( MAIN , MODE_REPLACE, new RubikStateSolver()   ),
35
  SOLU ( SVER , MODE_DRAG   , new RubikStateSolution() ),
36
  READ ( PLAY , MODE_ROTATE , new RubikStateReady()    ),
37
  DONE ( PLAY , MODE_DRAG   , new RubikStateDone()    ),
38
  ;
39

    
40
  public static final int LENGTH = values().length;
41
  private final RubikState mBack;
42
  private int mMode;
43
  private final RubikStateAbstract mClass;
44
  private static final RubikState[] sizes;
45

    
46
  private static RubikState mCurrState;
47

    
48
  static
49
    {
50
    int i = 0;
51
    sizes = new RubikState[LENGTH];
52

    
53
    for(RubikState size: RubikState.values())
54
      {
55
      sizes[i] = size;
56
      i++;
57
      }
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  public static RubikState getState(int ordinal)
63
    {
64
    return sizes[ordinal];
65
    }
66

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

    
69
  public static RubikState getCurrentState()
70
    {
71
    return mCurrState;
72
    }
73

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

    
76
  public static int getMode()
77
    {
78
    return mCurrState.mMode;
79
    }
80

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

    
83
  public static void savePreferences(SharedPreferences.Editor editor)
84
    {
85
    editor.putInt("curr_state", mCurrState.ordinal() );
86
    }
87

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

    
90
  public static void restorePreferences(SharedPreferences preferences)
91
    {
92
    int currState = preferences.getInt("curr_state", RubikState.MAIN.ordinal() );
93
    mCurrState = getState(currState);
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public static void goBack(RubikActivity act)
99
    {
100
    switchState(act, mCurrState.mBack );
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  public static void setState(RubikActivity act)
106
    {
107
    mCurrState.enterState(act);
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public static void switchState(RubikActivity act, RubikState next)
113
    {
114
    if( next!=null )
115
      {
116
      if( mCurrState!=null ) mCurrState.leaveState(act);
117
      next.enterState(act);
118
      mCurrState = next;
119
      }
120
    else
121
      {
122
      act.finish();
123
      }
124
    }
125

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

    
128
  RubikState(RubikState back, int mode, RubikStateAbstract clazz)
129
    {
130
    mBack = back;
131
    mMode = mode;
132
    mClass= clazz;
133
    }
134

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

    
137
  public RubikStateAbstract getStateClass()
138
    {
139
    return mClass;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public void leaveState(RubikActivity act)
145
    {
146
    mClass.leaveState(act);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public void enterState(RubikActivity act)
152
    {
153
    mClass.enterState(act);
154
    }
155
  }
(1-1/10)