Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikState.java @ e03e0352

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
  PLAY ( null , MODE_ROTATE , new RubikStatePlay()     ),
35
  SOLV ( PLAY , MODE_ROTATE , new RubikStateSolving()  ),
36
  PATT ( PLAY , MODE_DRAG   , new RubikStatePattern()  ),
37
  SVER ( PLAY , MODE_REPLACE, new RubikStateSolver()   ),
38
  SOLU ( SVER , MODE_DRAG   , new RubikStateSolution() ),
39
  READ ( PLAY , MODE_ROTATE , new RubikStateReady()    ),
40
  DONE ( PLAY , MODE_DRAG   , new RubikStateDone()     ),
41
  ;
42

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

    
49
  private static RubikState mCurrState;
50

    
51
  static
52
    {
53
    int i = 0;
54
    sizes = new RubikState[LENGTH];
55

    
56
    for(RubikState size: RubikState.values())
57
      {
58
      sizes[i] = size;
59
      i++;
60
      }
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public static RubikState getState(int ordinal)
66
    {
67
    return ordinal>=0 && ordinal<LENGTH ?  sizes[ordinal] : PLAY;
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public static void switchState(RubikActivity act, RubikState next)
116
    {
117
    if( next!=null )
118
      {
119
      FirebaseAnalytics analytics = act.getAnalytics();
120

    
121
      if( analytics!=null )
122
        {
123
        Bundle bundle = new Bundle();
124
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, next.toString());
125
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_START, bundle);
126
        }
127

    
128
      if( mCurrState!=null ) mCurrState.leaveState(act);
129
      next.enterState(act);
130
      mCurrState = next;
131
      }
132
    else
133
      {
134
      act.finish();
135
      }
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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