Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / StateList.java @ eaf87d1d

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 1b3cbd5b Leszek Koltunski
import android.os.Bundle;
24
25
import com.google.firebase.analytics.FirebaseAnalytics;
26
27 1f9772f3 Leszek Koltunski
import org.distorted.main.RubikActivity;
28 473611ee Leszek Koltunski
import static org.distorted.main.RubikSurfaceView.*;
29 f2a9be6d Leszek Koltunski
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 be576d14 Leszek Koltunski
public enum StateList
33 f2a9be6d Leszek Koltunski
  {
34 e03e0352 Leszek Koltunski
  PLAY ( null , MODE_ROTATE , new RubikStatePlay()     ),
35 473611ee Leszek Koltunski
  SOLV ( PLAY , MODE_ROTATE , new RubikStateSolving()  ),
36 e03e0352 Leszek Koltunski
  PATT ( PLAY , MODE_DRAG   , new RubikStatePattern()  ),
37
  SVER ( PLAY , MODE_REPLACE, new RubikStateSolver()   ),
38 473611ee Leszek Koltunski
  SOLU ( SVER , MODE_DRAG   , new RubikStateSolution() ),
39 807d82b7 Leszek Koltunski
  READ ( PLAY , MODE_ROTATE , new RubikStateReady()    ),
40 e03e0352 Leszek Koltunski
  DONE ( PLAY , MODE_DRAG   , new RubikStateDone()     ),
41 211b48f2 Leszek Koltunski
  ;
42
43
  public static final int LENGTH = values().length;
44 be576d14 Leszek Koltunski
  private final StateList mBack;
45 473611ee Leszek Koltunski
  private int mMode;
46 211b48f2 Leszek Koltunski
  private final RubikStateAbstract mClass;
47 be576d14 Leszek Koltunski
  private static final StateList[] states;
48 211b48f2 Leszek Koltunski
49 be576d14 Leszek Koltunski
  private static StateList mCurrState;
50 211b48f2 Leszek Koltunski
51
  static
52
    {
53
    int i = 0;
54 be576d14 Leszek Koltunski
    states = new StateList[LENGTH];
55 211b48f2 Leszek Koltunski
56 be576d14 Leszek Koltunski
    for(StateList state: StateList.values())
57 211b48f2 Leszek Koltunski
      {
58 59535491 Leszek Koltunski
      states[i] = state;
59 211b48f2 Leszek Koltunski
      i++;
60
      }
61
    }
62 f2a9be6d Leszek Koltunski
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 be576d14 Leszek Koltunski
  public static StateList getState(int ordinal)
66 f2a9be6d Leszek Koltunski
    {
67 59535491 Leszek Koltunski
    return ordinal>=0 && ordinal<LENGTH ?  states[ordinal] : PLAY;
68
    }
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 be576d14 Leszek Koltunski
  public static StateList getStateFromName(String name)
73 59535491 Leszek Koltunski
    {
74
    for(int i=0; i<LENGTH; i++)
75
      {
76
      if( name.equals(states[i].name()) )
77
        {
78
        return states[i];
79
        }
80
      }
81
82
    return PLAY;
83 f2a9be6d Leszek Koltunski
    }
84
85 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 be576d14 Leszek Koltunski
  public static StateList getCurrentState()
88 ad9e8bb3 Leszek Koltunski
    {
89 d18993ac Leszek Koltunski
    return mCurrState;
90 ad9e8bb3 Leszek Koltunski
    }
91
92 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94 473611ee Leszek Koltunski
  public static int getMode()
95 53f23b64 Leszek Koltunski
    {
96 473611ee Leszek Koltunski
    return mCurrState.mMode;
97 53f23b64 Leszek Koltunski
    }
98
99 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101 211b48f2 Leszek Koltunski
  public static void savePreferences(SharedPreferences.Editor editor)
102 f2a9be6d Leszek Koltunski
    {
103 59535491 Leszek Koltunski
    editor.putString("curr_state_name", mCurrState.name() );
104 f2a9be6d Leszek Koltunski
    }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 211b48f2 Leszek Koltunski
  public static void restorePreferences(SharedPreferences preferences)
109 f2a9be6d Leszek Koltunski
    {
110 be576d14 Leszek Koltunski
    String currStateName = preferences.getString("curr_state_name", StateList.PLAY.name() );
111 59535491 Leszek Koltunski
    mCurrState = getStateFromName(currStateName);
112 211b48f2 Leszek Koltunski
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115 f2a9be6d Leszek Koltunski
116 211b48f2 Leszek Koltunski
  public static void goBack(RubikActivity act)
117
    {
118 d18993ac Leszek Koltunski
    switchState(act, mCurrState.mBack );
119 f2a9be6d Leszek Koltunski
    }
120
121 a675474f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123 211b48f2 Leszek Koltunski
  public static void setState(RubikActivity act)
124 a675474f Leszek Koltunski
    {
125 a6d3b158 Leszek Koltunski
    mCurrState.enterState(act);
126 a675474f Leszek Koltunski
    }
127
128 f2a9be6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 be576d14 Leszek Koltunski
  public static void switchState(RubikActivity act, StateList next)
131 b8b38548 Leszek Koltunski
    {
132 4f470e00 Leszek Koltunski
    if( next!=null )
133 211b48f2 Leszek Koltunski
      {
134 1b3cbd5b Leszek Koltunski
      FirebaseAnalytics analytics = act.getAnalytics();
135
136
      if( analytics!=null )
137
        {
138
        Bundle bundle = new Bundle();
139
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, next.toString());
140
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_START, bundle);
141
        }
142
143 d18993ac Leszek Koltunski
      if( mCurrState!=null ) mCurrState.leaveState(act);
144 a6d3b158 Leszek Koltunski
      next.enterState(act);
145 4f470e00 Leszek Koltunski
      mCurrState = next;
146 211b48f2 Leszek Koltunski
      }
147
    else
148
      {
149
      act.finish();
150
      }
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154 b8b38548 Leszek Koltunski
155 be576d14 Leszek Koltunski
  StateList(StateList back, int mode, RubikStateAbstract clazz)
156 211b48f2 Leszek Koltunski
    {
157 473611ee Leszek Koltunski
    mBack = back;
158
    mMode = mode;
159
    mClass= clazz;
160 946d9762 Leszek Koltunski
    }
161 b8b38548 Leszek Koltunski
162 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164
  public RubikStateAbstract getStateClass()
165
    {
166
    return mClass;
167
    }
168 b8b38548 Leszek Koltunski
169 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170 b8b38548 Leszek Koltunski
171 211b48f2 Leszek Koltunski
  public void leaveState(RubikActivity act)
172
    {
173
    mClass.leaveState(act);
174
    }
175 b8b38548 Leszek Koltunski
176 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
177 cc5ec229 Leszek Koltunski
178 a6d3b158 Leszek Koltunski
  public void enterState(RubikActivity act)
179 211b48f2 Leszek Koltunski
    {
180 a6d3b158 Leszek Koltunski
    mClass.enterState(act);
181 f2a9be6d Leszek Koltunski
    }
182 211b48f2 Leszek Koltunski
  }