Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.states;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24

    
25
import com.google.firebase.analytics.FirebaseAnalytics;
26

    
27
import org.distorted.main.RubikActivity;
28
import static org.distorted.main.RubikSurfaceView.*;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public enum RubikState
33
  {
34
  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
  READ ( PLAY , MODE_ROTATE , new RubikStateReady()    ),
41
  DONE ( PLAY , MODE_DRAG   , new RubikStateDone()    ),
42
  ;
43

    
44
  public static final int LENGTH = values().length;
45
  private final RubikState mBack;
46
  private int mMode;
47
  private final RubikStateAbstract mClass;
48
  private static final RubikState[] sizes;
49

    
50
  private static RubikState mCurrState;
51

    
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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  public static RubikState getState(int ordinal)
67
    {
68
    return sizes[ordinal];
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  public static RubikState getCurrentState()
74
    {
75
    return mCurrState;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  public static int getMode()
81
    {
82
    return mCurrState.mMode;
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  public static void savePreferences(SharedPreferences.Editor editor)
88
    {
89
    editor.putInt("curr_state", mCurrState.ordinal() );
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  public static void restorePreferences(SharedPreferences preferences)
95
    {
96
    int currState = preferences.getInt("curr_state", RubikState.MAIN.ordinal() );
97
    mCurrState = getState(currState);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  public static void goBack(RubikActivity act)
103
    {
104
    switchState(act, mCurrState.mBack );
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  public static void setState(RubikActivity act)
110
    {
111
    mCurrState.enterState(act);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  public static void switchState(RubikActivity act, RubikState next)
117
    {
118
    if( next!=null )
119
      {
120
      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
      if( mCurrState!=null ) mCurrState.leaveState(act);
130
      next.enterState(act);
131
      mCurrState = next;
132
      }
133
    else
134
      {
135
      act.finish();
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  RubikState(RubikState back, int mode, RubikStateAbstract clazz)
142
    {
143
    mBack = back;
144
    mMode = mode;
145
    mClass= clazz;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public RubikStateAbstract getStateClass()
151
    {
152
    return mClass;
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  public void leaveState(RubikActivity act)
158
    {
159
    mClass.leaveState(act);
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public void enterState(RubikActivity act)
165
    {
166
    mClass.enterState(act);
167
    }
168
  }
(1-1/10)