Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikState.java @ 473611ee

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

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

    
44
  private static RubikState mCurrState;
45

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

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

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  public static RubikState getCurrentState()
68
    {
69
    return mCurrState;
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

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

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

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

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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

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

    
135
  public RubikStateAbstract getStateClass()
136
    {
137
    return mClass;
138
    }
139

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

    
142
  public void leaveState(RubikActivity act)
143
    {
144
    mClass.leaveState(act);
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  public void enterState(RubikActivity act)
150
    {
151
    mClass.enterState(act);
152
    }
153
  }
(1-1/8)