Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikState.java @ 7289fd6c

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