Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / ScreenList.java @ 85038b84

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_NOTHING, new RubikScreenPlay()     ),
35
  DETA ( PLAY , MODE_NOTHING, new RubikScreenDetails()  ),
36
  SOLV ( DETA , MODE_ROTATE , new RubikScreenSolving()  ),
37
  PATT ( DETA , MODE_DRAG   , new RubikScreenPattern()  ),
38
  SVER ( DETA , MODE_REPLACE, new RubikScreenSolver()   ),
39
  SOLU ( SVER , MODE_DRAG   , new RubikScreenSolution() ),
40
  READ ( DETA , MODE_ROTATE , new RubikScreenReady()    ),
41
  DONE ( DETA , MODE_DRAG   , new RubikScreenDone()     ),
42
  FREE ( DETA , MODE_ROTATE , new RubikScreenFreePlay() ),
43
  ;
44

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

    
51
  private static ScreenList mCurrScreen;
52

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
79
    return PLAY;
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

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

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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