Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 1bd07548

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
    private boolean mJustStarted;
54
    private FirebaseAnalytics mFirebaseAnalytics;
55
    private float mScreenWidth;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
    @Override
60
    protected void onCreate(Bundle savedState)
61
      {
62
      super.onCreate(savedState);
63
      setTheme(R.style.CustomActivityThemeNoActionBar);
64
      setContentView(R.layout.main);
65

    
66
      mJustStarted = true;
67
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
68

    
69
      DisplayMetrics displaymetrics = new DisplayMetrics();
70
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
71
      mScreenWidth=displaymetrics.widthPixels;
72

    
73
      android.util.Log.e("act", "screenWidth="+mScreenWidth);
74
      }
75

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

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

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

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

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

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

    
125
        }
126

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

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

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

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

    
167
      editor.apply();
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

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

    
186
      RubikState.restorePreferences(preferences);
187

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

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

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// PUBLIC API
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    public FirebaseAnalytics getAnalytics()
209
      {
210
      return mFirebaseAnalytics;
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    public float getScreenWidthInPixels()
225
      {
226
      return mScreenWidth;
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

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

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

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

    
278
      pre.changeObject(newObject,newSize);
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

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

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

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

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

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

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

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

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

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

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

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