Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / ScreenList.java @ fcd5b990

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.screens;
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 ScreenList
33
  {
34
  PLAY ( null , MODE_ROTATE , new RubikScreenPlay()     ),
35
  SOLV ( PLAY , MODE_ROTATE , new RubikScreenSolving()  ),
36
  PATT ( PLAY , MODE_DRAG   , new RubikScreenPattern()  ),
37
  SVER ( PLAY , MODE_REPLACE, new RubikScreenSolver()   ),
38
  SOLU ( SVER , MODE_DRAG   , new RubikScreenSolution() ),
39
  READ ( PLAY , MODE_ROTATE , new RubikScreenReady()    ),
40
  DONE ( PLAY , MODE_DRAG   , new RubikScreenDone()     ),
41
  ;
42

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

    
49
  private static ScreenList mCurrState;
50

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

    
56
    for(ScreenList state: ScreenList.values())
57
      {
58
      states[i] = state;
59
      i++;
60
      }
61
    }
62

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

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

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

    
72
  public static ScreenList getStateFromName(String name)
73
    {
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
    }
84

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

    
87
  public static ScreenList getCurrentState()
88
    {
89
    return mCurrState;
90
    }
91

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

    
94
  public static int getMode()
95
    {
96
    return mCurrState.mMode;
97
    }
98

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

    
101
  public static void savePreferences(SharedPreferences.Editor editor)
102
    {
103
    editor.putString("curr_state_name", mCurrState.name() );
104
    }
105

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

    
108
  public static void restorePreferences(SharedPreferences preferences)
109
    {
110
    String currStateName = preferences.getString("curr_state_name", ScreenList.PLAY.name() );
111
    mCurrState = getStateFromName(currStateName);
112
    }
113

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

    
116
  public static void goBack(RubikActivity act)
117
    {
118
    switchState(act, mCurrState.mBack );
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  public static void setState(RubikActivity act)
124
    {
125
    mCurrState.enterState(act);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public static void switchState(RubikActivity act, ScreenList next)
131
    {
132
    if( next!=null )
133
      {
134
      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
      if( mCurrState!=null ) mCurrState.leaveState(act);
144
      next.enterState(act);
145
      mCurrState = next;
146
      }
147
    else
148
      {
149
      act.finish();
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  ScreenList(ScreenList back, int mode, RubikScreenAbstract clazz)
156
    {
157
    mBack = back;
158
    mMode = mode;
159
    mClass= clazz;
160
    }
161

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

    
164
  public RubikScreenAbstract getStateClass()
165
    {
166
    return mClass;
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  public void leaveState(RubikActivity act)
172
    {
173
    mClass.leaveState(act);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public void enterState(RubikActivity act)
179
    {
180
    mClass.enterState(act);
181
    }
182
  }
(10-10/12)