Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 2d9fc972

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.main;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24
import android.preference.PreferenceManager;
25
import androidx.appcompat.app.AppCompatActivity;
26
import android.view.View;
27

    
28
import com.google.firebase.analytics.FirebaseAnalytics;
29

    
30
import org.distorted.dialogs.RubikDialogAbout;
31
import org.distorted.dialogs.RubikDialogError;
32
import org.distorted.dialogs.RubikDialogScores;
33
import org.distorted.dialogs.RubikDialogEffects;
34
import org.distorted.effects.BaseEffect;
35
import org.distorted.library.main.DistortedLibrary;
36

    
37
import org.distorted.objects.RubikObject;
38
import org.distorted.scores.RubikScores;
39
import org.distorted.scores.RubikScoresDownloader;
40
import org.distorted.objects.RubikObjectList;
41
import org.distorted.states.RubikState;
42
import org.distorted.states.RubikStatePlay;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public class RubikActivity extends AppCompatActivity
47
{
48
    private boolean mJustStarted;
49
    private FirebaseAnalytics mFirebaseAnalytics;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    @Override
54
    protected void onCreate(Bundle savedState)
55
      {
56
      super.onCreate(savedState);
57
      setTheme(R.style.CustomActivityThemeNoActionBar);
58
      setContentView(R.layout.main);
59

    
60
      mJustStarted = true;
61
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
62
      }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
    
66
    @Override
67
    protected void onPause() 
68
      {
69
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
70
      view.onPause();
71
      DistortedLibrary.onPause();
72
      RubikScoresDownloader.onPause();
73
      savePreferences();
74
      super.onPause();
75
      }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
    
79
    @Override
80
    protected void onResume() 
81
      {
82
      super.onResume();
83
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
84
      view.onResume();
85
      restorePreferences();
86
      RubikState.setState(this);
87

    
88
      if( mJustStarted )
89
        {
90
        mJustStarted = false;
91
        RubikScores scores = RubikScores.getInstance();
92
        scores.incrementNumRuns();
93
        scores.setCountry(this);
94
        }
95

    
96
      boolean success = false;
97
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
98
      int object = play.getObject();
99
      int size   = play.getSize();
100

    
101
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
102
        {
103
        RubikObjectList obj = RubikObjectList.getObject(object);
104
        int[] sizes = obj.getSizes();
105
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
106

    
107
        if( sizeIndex>=0 && sizeIndex<sizes.length )
108
          {
109
          success = true;
110
          view.getPostRender().changeObject(obj,size);
111
          }
112

    
113
        }
114

    
115
      if( !success )
116
        {
117
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
118
        int s = RubikStatePlay.DEF_SIZE;
119

    
120
        play.setObjectAndSize(this,obj,s);
121
        view.getPostRender().changeObject(obj,s);
122
        }
123
      }
124
    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
    @Override
128
    protected void onDestroy() 
129
      {
130
      DistortedLibrary.onDestroy();
131
      super.onDestroy();
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
    private void savePreferences()
137
      {
138
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
139
      SharedPreferences.Editor editor = preferences.edit();
140

    
141
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
142
        {
143
        BaseEffect.Type.getType(i).savePreferences(editor);
144
        }
145

    
146
      for (int i=0; i<RubikState.LENGTH; i++)
147
        {
148
        RubikState.getState(i).getStateClass().savePreferences(editor);
149
        }
150

    
151
      RubikState.savePreferences(editor);
152
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
153
      view.getPostRender().savePreferences(editor);
154

    
155
      editor.apply();
156
      }
157

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

    
160
    private void restorePreferences()
161
      {
162
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
163

    
164
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
165
        {
166
        BaseEffect.Type.getType(i).restorePreferences(preferences);
167
        }
168

    
169
      for (int i=0; i< RubikState.LENGTH; i++)
170
        {
171
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
172
        }
173

    
174
      RubikState.restorePreferences(preferences);
175

    
176
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
177
      view.getPostRender().restorePreferences(preferences);
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    void OpenGLError(String message)
183
      {
184
      Bundle bundle = new Bundle();
185
      bundle.putString("error", message );
186

    
187
      RubikDialogError errDiag = new RubikDialogError();
188
      errDiag.setArguments(bundle);
189
      errDiag.show(getSupportFragmentManager(), null);
190
      }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// PUBLIC API
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public FirebaseAnalytics getAnalytics()
197
      {
198
      return mFirebaseAnalytics;
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public RubikObject getObject()
204
      {
205
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
206
      RubikPostRender post = view.getPostRender();
207
      return post.getObject();
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    public RubikPostRender getPostRender()
213
      {
214
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
215
      return view.getPostRender();
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    public void changeObject(RubikObjectList object, int size)
221
      {
222
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
223
      RubikPostRender post = view.getPostRender();
224
      post.changeObject(object,size);
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    public void setupObject(RubikObjectList object, int size, int[][] moves)
230
      {
231
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
232
      RubikPostRender post = view.getPostRender();
233
      post.setupObject(object,size,moves);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public boolean isVertical()
239
      {
240
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
241
      return view.isVertical();
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    public void Play(View v)
247
      {
248
      RubikState.switchState(this,RubikState.PLAY);
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    public void Effects(View v)
254
      {
255
      RubikDialogEffects settings = new RubikDialogEffects();
256
      settings.show(getSupportFragmentManager(), null);
257
      }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
    public void Scores(View v)
262
      {
263
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
264
      int object = play.getObject();
265
      int size   = play.getSize();
266
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
267

    
268
      Bundle bundle = new Bundle();
269
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
270
      bundle.putBoolean("submitting", false);
271

    
272
      RubikDialogScores scores = new RubikDialogScores();
273
      scores.setArguments(bundle);
274
      scores.show(getSupportFragmentManager(), null);
275
      }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
    public void Patterns(View v)
280
      {
281
      RubikState.switchState(this,RubikState.PATT);
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public void Solver(View v)
287
      {
288
      RubikState.switchState(this,RubikState.SVER);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public void About(View v)
294
      {
295
      RubikDialogAbout diag = new RubikDialogAbout();
296
      diag.show(getSupportFragmentManager(), null);
297
      }
298
}
(1-1/4)