Project

General

Profile

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

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

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

    
27
import android.util.DisplayMetrics;
28
import android.view.View;
29

    
30
import com.google.firebase.analytics.FirebaseAnalytics;
31

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class RubikActivity extends AppCompatActivity
48
{
49
    public static final float BUTTON_TEXT_SIZE = 0.05f;
50
    public static final float TITLE_TEXT_SIZE  = 0.06f;
51
    public static final float BITMAP_TEXT_SIZE = 0.09f;
52

    
53
    public static final float MENU_BIG_TEXT_SIZE   = 0.05f;
54
    public static final float MENU_MEDIUM_TEXT_SIZE= 0.04f;
55
    public static final float MENU_SMALL_TEXT_SIZE = 0.035f;
56

    
57
    private boolean mJustStarted;
58
    private FirebaseAnalytics mFirebaseAnalytics;
59
    private float mScreenWidth;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
    @Override
64
    protected void onCreate(Bundle savedState)
65
      {
66
      super.onCreate(savedState);
67
      setTheme(R.style.CustomActivityThemeNoActionBar);
68
      setContentView(R.layout.main);
69

    
70
      mJustStarted = true;
71
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
72

    
73
      DisplayMetrics displaymetrics = new DisplayMetrics();
74
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
75
      mScreenWidth=displaymetrics.widthPixels;
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
    @Override
81
    protected void onPause() 
82
      {
83
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
84
      view.onPause();
85
      DistortedLibrary.onPause();
86
      RubikScoresDownloader.onPause();
87
      savePreferences();
88
      super.onPause();
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    @Override
94
    protected void onResume() 
95
      {
96
      super.onResume();
97
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
98
      view.onResume();
99
      restorePreferences();
100
      RubikState.setState(this);
101

    
102
      if( mJustStarted )
103
        {
104
        mJustStarted = false;
105
        RubikScores scores = RubikScores.getInstance();
106
        scores.incrementNumRuns();
107
        scores.setCountry(this);
108
        }
109

    
110
      boolean success = false;
111
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
112
      int object = play.getObject();
113
      int size   = play.getSize();
114

    
115
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
116
        {
117
        RubikObjectList obj = RubikObjectList.getObject(object);
118
        int[] sizes = obj.getSizes();
119
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
120

    
121
        if( sizeIndex>=0 && sizeIndex<sizes.length )
122
          {
123
          success = true;
124
          view.getPreRender().changeObject(obj,size);
125
          }
126

    
127
        }
128

    
129
      if( !success )
130
        {
131
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
132
        int s = RubikStatePlay.DEF_SIZE;
133

    
134
        play.setObjectAndSize(this,obj,s);
135
        view.getPreRender().changeObject(obj,s);
136
        }
137
      }
138
    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141
    @Override
142
    protected void onDestroy() 
143
      {
144
      DistortedLibrary.onDestroy();
145
      super.onDestroy();
146
      }
147

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

    
150
    private void savePreferences()
151
      {
152
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
153
      SharedPreferences.Editor editor = preferences.edit();
154

    
155
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
156
        {
157
        BaseEffect.Type.getType(i).savePreferences(editor);
158
        }
159

    
160
      for (int i=0; i<RubikState.LENGTH; i++)
161
        {
162
        RubikState.getState(i).getStateClass().savePreferences(editor);
163
        }
164

    
165
      RubikState.savePreferences(editor);
166
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
167
      view.getPreRender().savePreferences(editor);
168

    
169
      editor.apply();
170
      }
171

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

    
174
    private void restorePreferences()
175
      {
176
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
177

    
178
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
179
        {
180
        BaseEffect.Type.getType(i).restorePreferences(preferences);
181
        }
182

    
183
      for (int i=0; i< RubikState.LENGTH; i++)
184
        {
185
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
186
        }
187

    
188
      RubikState.restorePreferences(preferences);
189

    
190
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
191
      view.getPreRender().restorePreferences(preferences);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    void OpenGLError(String message)
197
      {
198
      Bundle bundle = new Bundle();
199
      bundle.putString("error", message );
200

    
201
      RubikDialogError errDiag = new RubikDialogError();
202
      errDiag.setArguments(bundle);
203
      errDiag.show(getSupportFragmentManager(), null);
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
// PUBLIC API
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    public FirebaseAnalytics getAnalytics()
211
      {
212
      return mFirebaseAnalytics;
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    public RubikObject getObject()
218
      {
219
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
220
      RubikPreRender pre = view.getPreRender();
221
      return pre.getObject();
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
    public float getScreenWidthInPixels()
227
      {
228
      return mScreenWidth;
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    public RubikPreRender getPreRender()
234
      {
235
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
236
      return view.getPreRender();
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
242
      {
243
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
244
      RubikPreRender pre = view.getPreRender();
245

    
246
      if( reportChange )
247
        {
248
        RubikObject oldObject = pre.getObject();
249
        RubikObjectList oldList = oldObject.getObjectList();
250
        int oldSize = oldObject.getSize();
251
        float fps = view.getRenderer().getFPS();
252
        StringBuilder name = new StringBuilder();
253
        name.append(oldList.name());
254
        name.append('_');
255
        name.append(oldSize);
256
        name.append(' ');
257
        name.append(fps);
258
        name.append(" --> ");
259
        name.append(newObject.name());
260
        name.append('_');
261
        name.append(newSize);
262

    
263
        if( BuildConfig.DEBUG )
264
          {
265
          android.util.Log.e("rubik", name.toString());
266
          }
267
        else
268
          {
269
          FirebaseAnalytics analytics = getAnalytics();
270

    
271
          if( analytics!=null )
272
            {
273
            Bundle bundle = new Bundle();
274
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
275
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
276
            }
277
          }
278
        }
279

    
280
      pre.changeObject(newObject,newSize);
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
    public void setupObject(RubikObjectList object, int size, int[][] moves)
286
      {
287
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
288
      RubikPreRender pre = view.getPreRender();
289
      pre.setupObject(object,size,moves);
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
    public boolean isVertical()
295
      {
296
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
297
      return view.isVertical();
298
      }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
    public void Play(View v)
303
      {
304
      RubikState.switchState(this,RubikState.PLAY);
305
      }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
    public void Scores(View v)
310
      {
311
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
312
      int object = play.getObject();
313
      int size   = play.getSize();
314
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
315

    
316
      Bundle bundle = new Bundle();
317
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
318
      bundle.putBoolean("submitting", false);
319

    
320
      RubikDialogScores scores = new RubikDialogScores();
321
      scores.setArguments(bundle);
322
      scores.show(getSupportFragmentManager(), null);
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    public void Patterns(View v)
328
      {
329
      RubikState.switchState(this,RubikState.PATT);
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
    public void Solver(View v)
335
      {
336
      RubikState.switchState(this,RubikState.SVER);
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
    public void About(View v)
342
      {
343
      RubikDialogAbout diag = new RubikDialogAbout();
344
      diag.show(getSupportFragmentManager(), null);
345
      }
346
}
(1-1/4)