Project

General

Profile

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

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

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 static final ScreenList[] screens;
45
  private final ScreenList mBack;
46
  private final int mMode;
47
  private final RubikScreenAbstract mClass;
48

    
49
  private static ScreenList mCurrScreen;
50

    
51
  static
52
    {
53
    int i = 0;
54
    screens = new ScreenList[LENGTH];
55
    for(ScreenList state: ScreenList.values()) screens[i++] = state;
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public static ScreenList getScreen(int ordinal)
61
    {
62
    return ordinal>=0 && ordinal<LENGTH ?  screens[ordinal] : PLAY;
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  public static ScreenList getScreenFromName(String name)
68
    {
69
    for(int i=0; i<LENGTH; i++)
70
      {
71
      if( name.equals(screens[i].name()) )
72
        {
73
        return screens[i];
74
        }
75
      }
76

    
77
    return PLAY;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  public static ScreenList getCurrentScreen()
83
    {
84
    return mCurrScreen;
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  public static int getMode()
90
    {
91
    return mCurrScreen.mMode;
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  public static void savePreferences(SharedPreferences.Editor editor)
97
    {
98
    editor.putString("curr_state_name", mCurrScreen.name() );
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  public static void restorePreferences(SharedPreferences preferences)
104
    {
105
    String currScreenName = preferences.getString("curr_state_name", ScreenList.PLAY.name() );
106
    mCurrScreen = getScreenFromName(currScreenName);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  public static void goBack(RubikActivity act)
112
    {
113
    switchScreen(act, mCurrScreen.mBack );
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  public static void setScreen(RubikActivity act)
119
    {
120
    mCurrScreen.enterScreen(act);
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  public static void switchScreen(RubikActivity act, ScreenList next)
126
    {
127
    if( next!=null )
128
      {
129
      FirebaseAnalytics analytics = act.getAnalytics();
130

    
131
      if( analytics!=null )
132
        {
133
        Bundle bundle = new Bundle();
134
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, next.toString());
135
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_START, bundle);
136
        }
137

    
138
      if( mCurrScreen !=null ) mCurrScreen.leaveScreen(act);
139
      next.enterScreen(act);
140
      mCurrScreen = next;
141
      }
142
    else
143
      {
144
      act.finish();
145
      }
146
    }
147

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

    
150
  ScreenList(ScreenList back, int mode, RubikScreenAbstract clazz)
151
    {
152
    mBack = back;
153
    mMode = mode;
154
    mClass= clazz;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  public RubikScreenAbstract getScreenClass()
160
    {
161
    return mClass;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public void leaveScreen(RubikActivity act)
167
    {
168
    mClass.leaveScreen(act);
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  public void enterScreen(RubikActivity act)
174
    {
175
    mClass.enterScreen(act);
176
    }
177
  }
(10-10/12)