Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikState.java @ 1f9772f3

1 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.states;
21 f2a9be6d Leszek Koltunski
22 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
23 1f9772f3 Leszek Koltunski
import org.distorted.main.RubikActivity;
24 f2a9be6d Leszek Koltunski
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
27 211b48f2 Leszek Koltunski
public enum RubikState
28 f2a9be6d Leszek Koltunski
  {
29 264af0ad Leszek Koltunski
  MAIN ( null , false, new RubikStateMain()      ),
30
  PLAY ( MAIN , true , new RubikStatePlay()      ),
31
  SOLV ( PLAY , true , new RubikStateSolving()   ),
32
  PATT ( MAIN , false, new RubikStatePattern()   ),
33
  SVER ( MAIN , false, new RubikStateSolver()    ),
34
  SOLU ( SVER , false, new RubikStateSolution()  ),
35 211b48f2 Leszek Koltunski
  ;
36
37
  public static final int LENGTH = values().length;
38
  private final RubikState mBack;
39 53f23b64 Leszek Koltunski
  private boolean mCanRotate;
40 211b48f2 Leszek Koltunski
  private final RubikStateAbstract mClass;
41
  private static final RubikState[] sizes;
42
43 a6d3b158 Leszek Koltunski
  private static RubikState mCurrState;
44 211b48f2 Leszek Koltunski
45
  static
46
    {
47
    int i = 0;
48
    sizes = new RubikState[LENGTH];
49
50
    for(RubikState size: RubikState.values())
51
      {
52
      sizes[i] = size;
53
      i++;
54
      }
55
    }
56 f2a9be6d Leszek Koltunski
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 211b48f2 Leszek Koltunski
  public static RubikState getState(int ordinal)
60 f2a9be6d Leszek Koltunski
    {
61 211b48f2 Leszek Koltunski
    return sizes[ordinal];
62 f2a9be6d Leszek Koltunski
    }
63
64 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
  public static RubikState getCurrentState()
67
    {
68 d18993ac Leszek Koltunski
    return mCurrState;
69 ad9e8bb3 Leszek Koltunski
    }
70
71 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
  public static boolean canRotate()
74
    {
75
    return mCurrState.mCanRotate;
76
    }
77
78 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 211b48f2 Leszek Koltunski
  public static void savePreferences(SharedPreferences.Editor editor)
81 f2a9be6d Leszek Koltunski
    {
82 d18993ac Leszek Koltunski
    editor.putInt("curr_state", mCurrState.ordinal() );
83 f2a9be6d Leszek Koltunski
    }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 211b48f2 Leszek Koltunski
  public static void restorePreferences(SharedPreferences preferences)
88 f2a9be6d Leszek Koltunski
    {
89 d18993ac Leszek Koltunski
    int currState = preferences.getInt("curr_state", RubikState.MAIN.ordinal() );
90
    mCurrState = getState(currState);
91 211b48f2 Leszek Koltunski
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94 f2a9be6d Leszek Koltunski
95 211b48f2 Leszek Koltunski
  public static void goBack(RubikActivity act)
96
    {
97 d18993ac Leszek Koltunski
    switchState(act, mCurrState.mBack );
98 f2a9be6d Leszek Koltunski
    }
99
100 a675474f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102 211b48f2 Leszek Koltunski
  public static void setState(RubikActivity act)
103 a675474f Leszek Koltunski
    {
104 a6d3b158 Leszek Koltunski
    mCurrState.enterState(act);
105 a675474f Leszek Koltunski
    }
106
107 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109 4f470e00 Leszek Koltunski
  public static void switchState(RubikActivity act, RubikState next)
110 b8b38548 Leszek Koltunski
    {
111 4f470e00 Leszek Koltunski
    if( next!=null )
112 211b48f2 Leszek Koltunski
      {
113 d18993ac Leszek Koltunski
      if( mCurrState!=null ) mCurrState.leaveState(act);
114 a6d3b158 Leszek Koltunski
      next.enterState(act);
115 4f470e00 Leszek Koltunski
      mCurrState = next;
116 211b48f2 Leszek Koltunski
      }
117
    else
118
      {
119
      act.finish();
120
      }
121
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124 b8b38548 Leszek Koltunski
125 53f23b64 Leszek Koltunski
  RubikState(RubikState back, boolean canRotate, RubikStateAbstract clazz)
126 211b48f2 Leszek Koltunski
    {
127 53f23b64 Leszek Koltunski
    mBack      = back;
128
    mCanRotate = canRotate;
129
    mClass     = clazz;
130 946d9762 Leszek Koltunski
    }
131 b8b38548 Leszek Koltunski
132 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134
  public RubikStateAbstract getStateClass()
135
    {
136
    return mClass;
137
    }
138 b8b38548 Leszek Koltunski
139 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140 b8b38548 Leszek Koltunski
141 211b48f2 Leszek Koltunski
  public void leaveState(RubikActivity act)
142
    {
143
    mClass.leaveState(act);
144
    }
145 b8b38548 Leszek Koltunski
146 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
147 cc5ec229 Leszek Koltunski
148 a6d3b158 Leszek Koltunski
  public void enterState(RubikActivity act)
149 211b48f2 Leszek Koltunski
    {
150 a6d3b158 Leszek Koltunski
    mClass.enterState(act);
151 f2a9be6d Leszek Koltunski
    }
152 211b48f2 Leszek Koltunski
  }