Project

General

Profile

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

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

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