Project

General

Profile

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

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

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.objectlib.main.ObjectControl.*;
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
  FREE ( PLAY , MODE_ROTATE , new RubikScreenFreePlay() ),
42
  ;
43

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

    
50
  private static ScreenList mCurrScreen;
51

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
78
    return PLAY;
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

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

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

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public void enterScreen(RubikActivity act)
175
    {
176
    mClass.enterScreen(act);
177
    }
178
  }
(11-11/11)