Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikState.java @ 714292f1

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 ad9e8bb3 Leszek Koltunski
  MAIN ( null , new RubikStateMain()    ),
30
  PLAY ( MAIN , new RubikStatePlay()    ),
31
  SOLV ( PLAY , new RubikStateSolving() ),
32 211b48f2 Leszek Koltunski
  ;
33
34
  public static final int LENGTH = values().length;
35
  private final RubikState mBack;
36
  private final RubikStateAbstract mClass;
37
  private static final RubikState[] sizes;
38
39
  private static RubikState mCurrentState;
40
41
  static
42
    {
43
    int i = 0;
44
    sizes = new RubikState[LENGTH];
45
46
    for(RubikState size: RubikState.values())
47
      {
48
      sizes[i] = size;
49
      i++;
50
      }
51
    }
52 f2a9be6d Leszek Koltunski
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 211b48f2 Leszek Koltunski
  public static RubikState getState(int ordinal)
56 f2a9be6d Leszek Koltunski
    {
57 211b48f2 Leszek Koltunski
    return sizes[ordinal];
58 f2a9be6d Leszek Koltunski
    }
59
60 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
  public static RubikState getCurrentState()
63
    {
64
    return mCurrentState;
65
    }
66
67 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 211b48f2 Leszek Koltunski
  public static void savePreferences(SharedPreferences.Editor editor)
70 f2a9be6d Leszek Koltunski
    {
71 211b48f2 Leszek Koltunski
    editor.putInt("state", mCurrentState.ordinal() );
72 f2a9be6d Leszek Koltunski
    }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 211b48f2 Leszek Koltunski
  public static void restorePreferences(SharedPreferences preferences)
77 f2a9be6d Leszek Koltunski
    {
78 211b48f2 Leszek Koltunski
    int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() );
79
    mCurrentState = getState(stateOrdinal);
80
    }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83 f2a9be6d Leszek Koltunski
84 211b48f2 Leszek Koltunski
  public static void goBack(RubikActivity act)
85
    {
86 ad9e8bb3 Leszek Koltunski
    switchState(act, mCurrentState.mBack );
87 f2a9be6d Leszek Koltunski
    }
88
89 a675474f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 211b48f2 Leszek Koltunski
  public static void setState(RubikActivity act)
92 a675474f Leszek Koltunski
    {
93 211b48f2 Leszek Koltunski
    mCurrentState.enterState(act);
94 a675474f Leszek Koltunski
    }
95
96 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 211b48f2 Leszek Koltunski
  public static void switchState(RubikActivity act, RubikState state)
99 b8b38548 Leszek Koltunski
    {
100 211b48f2 Leszek Koltunski
    if( state!=null )
101
      {
102
      if( mCurrentState!=null ) mCurrentState.leaveState(act);
103
      state.enterState(act);
104
      mCurrentState = state;
105
      }
106
    else
107
      {
108
      act.finish();
109
      }
110
    }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113 b8b38548 Leszek Koltunski
114 211b48f2 Leszek Koltunski
  RubikState(RubikState back, RubikStateAbstract clazz)
115
    {
116
    mBack = back;
117
    mClass = clazz;
118 946d9762 Leszek Koltunski
    }
119 b8b38548 Leszek Koltunski
120 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
  public RubikStateAbstract getStateClass()
123
    {
124
    return mClass;
125
    }
126 b8b38548 Leszek Koltunski
127 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128 b8b38548 Leszek Koltunski
129 211b48f2 Leszek Koltunski
  public void leaveState(RubikActivity act)
130
    {
131
    mClass.leaveState(act);
132
    }
133 b8b38548 Leszek Koltunski
134 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
135 cc5ec229 Leszek Koltunski
136 211b48f2 Leszek Koltunski
  public void enterState(RubikActivity act)
137
    {
138
    mClass.enterState(act);
139 f2a9be6d Leszek Koltunski
    }
140 211b48f2 Leszek Koltunski
  }