Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 6d4d56cb

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

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

    
31
import org.distorted.dialogs.RubikDialogError;
32
import org.distorted.effects.BaseEffect;
33
import org.distorted.library.main.DistortedLibrary;
34

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikActivity extends AppCompatActivity
45
{
46
    public static final float PADDING             = 0.01f;
47
    public static final float MARGIN              = 0.004f;
48
    public static final float LARGE_MARGIN        = 0.025f;
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.05f;
52
    public static final float MENU_ITEM_SIZE      = 0.12f;
53
    public static final float PATTERN_GROUP_TEXT  = 0.03f;
54
    public static final float PATTERN_CHILD_TEXT  = 0.02f;
55
    public static final float SCORES_LEVEL_TEXT   = 0.035f;
56
    public static final float SCORES_ITEM_TEXT    = 0.030f;
57

    
58
    public static final float MENU_BIG_TEXT_SIZE   = 0.05f;
59
    public static final float MENU_MEDIUM_TEXT_SIZE= 0.04f;
60
    public static final float MENU_SMALL_TEXT_SIZE = 0.035f;
61

    
62
    private boolean mJustStarted;
63
    private FirebaseAnalytics mFirebaseAnalytics;
64
    private static int mScreenWidth, mScreenHeight;
65

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

    
68
    @Override
69
    protected void onCreate(Bundle savedState)
70
      {
71
      super.onCreate(savedState);
72
      setTheme(R.style.CustomActivityThemeNoActionBar);
73
      setContentView(R.layout.main);
74

    
75
      mJustStarted = true;
76
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
77

    
78
      DisplayMetrics displaymetrics = new DisplayMetrics();
79
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
80
      mScreenWidth =displaymetrics.widthPixels;
81
      mScreenHeight=displaymetrics.heightPixels;
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
    
86
    @Override
87
    protected void onPause() 
88
      {
89
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
90
      view.onPause();
91
      DistortedLibrary.onPause();
92
      RubikScoresDownloader.onPause();
93
      savePreferences();
94
      super.onPause();
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
    @Override
100
    protected void onResume() 
101
      {
102
      super.onResume();
103
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
104
      view.onResume();
105
      view.initialize();
106
      restorePreferences();
107
      RubikState.setState(this);
108

    
109
      if( mJustStarted )
110
        {
111
        mJustStarted = false;
112
        RubikScores scores = RubikScores.getInstance();
113
        scores.incrementNumRuns();
114
        scores.setCountry(this);
115
        }
116

    
117
      boolean success = false;
118
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
119
      int object = play.getObject();
120
      int size   = play.getSize();
121

    
122
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
123
        {
124
        RubikObjectList obj = RubikObjectList.getObject(object);
125
        int[] sizes = obj.getSizes();
126
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
127

    
128
        if( sizeIndex>=0 && sizeIndex<sizes.length )
129
          {
130
          success = true;
131
          view.getPreRender().changeObject(obj,size);
132
          }
133

    
134
        }
135

    
136
      if( !success )
137
        {
138
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
139
        int s = RubikStatePlay.DEF_SIZE;
140

    
141
        play.setObjectAndSize(this,obj,s);
142
        view.getPreRender().changeObject(obj,s);
143
        }
144
      }
145
    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
    
148
    @Override
149
    protected void onDestroy() 
150
      {
151
      DistortedLibrary.onDestroy();
152
      super.onDestroy();
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    private void savePreferences()
158
      {
159
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
160
      SharedPreferences.Editor editor = preferences.edit();
161

    
162
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
163
        {
164
        BaseEffect.Type.getType(i).savePreferences(editor);
165
        }
166

    
167
      for (int i=0; i<RubikState.LENGTH; i++)
168
        {
169
        RubikState.getState(i).getStateClass().savePreferences(editor);
170
        }
171

    
172
      RubikState.savePreferences(editor);
173
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
174
      view.getPreRender().savePreferences(editor);
175

    
176
      editor.apply();
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    private void restorePreferences()
182
      {
183
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
184

    
185
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
186
        {
187
        BaseEffect.Type.getType(i).restorePreferences(preferences);
188
        }
189

    
190
      for (int i=0; i< RubikState.LENGTH; i++)
191
        {
192
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
193
        }
194

    
195
      RubikState.restorePreferences(preferences);
196

    
197
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
198
      view.getPreRender().restorePreferences(preferences);
199
      }
200

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

    
203
    void OpenGLError(String message)
204
      {
205
      Bundle bundle = new Bundle();
206
      bundle.putString("error", message );
207

    
208
      RubikDialogError errDiag = new RubikDialogError();
209
      errDiag.setArguments(bundle);
210
      errDiag.show(getSupportFragmentManager(), null);
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// PUBLIC API
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    public FirebaseAnalytics getAnalytics()
218
      {
219
      return mFirebaseAnalytics;
220
      }
221

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

    
224
    public RubikObject getObject()
225
      {
226
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
227
      RubikPreRender pre = view.getPreRender();
228
      return pre.getObject();
229
      }
230

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

    
233
    public int getScreenWidthInPixels()
234
      {
235
      return mScreenWidth;
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
    public int getScreenHeightInPixels()
241
      {
242
      return mScreenHeight;
243
      }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
    public RubikPreRender getPreRender()
248
      {
249
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
250
      return view.getPreRender();
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    public void changeObject(RubikObjectList newObject, int newSize, boolean reportChange)
256
      {
257
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
258
      RubikPreRender pre = view.getPreRender();
259

    
260
      if( reportChange )
261
        {
262
        RubikObject oldObject = pre.getObject();
263
        RubikObjectList oldList = oldObject.getObjectList();
264
        int oldSize = oldObject.getSize();
265
        float fps = view.getRenderer().getFPS();
266
        StringBuilder name = new StringBuilder();
267
        name.append(oldList.name());
268
        name.append('_');
269
        name.append(oldSize);
270
        name.append(' ');
271
        name.append(fps);
272
        name.append(" --> ");
273
        name.append(newObject.name());
274
        name.append('_');
275
        name.append(newSize);
276

    
277
        if( BuildConfig.DEBUG )
278
          {
279
          android.util.Log.e("rubik", name.toString());
280
          }
281
        else
282
          {
283
          FirebaseAnalytics analytics = getAnalytics();
284

    
285
          if( analytics!=null )
286
            {
287
            Bundle bundle = new Bundle();
288
            bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name.toString());
289
            analytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM, bundle);
290
            }
291
          }
292
        }
293

    
294
      pre.changeObject(newObject,newSize);
295
      }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
    public void setupObject(RubikObjectList object, int size, int[][] moves)
300
      {
301
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
302
      RubikPreRender pre = view.getPreRender();
303
      pre.setupObject(object,size,moves);
304
      }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
    public static int getDrawableSize()
309
      {
310
      if( mScreenHeight<1000 )
311
        {
312
        //android.util.Log.e("view", "returning small, mScrH="+mScreenHeight);
313
        return 0;
314
        }
315
      if( mScreenHeight<1600 )
316
        {
317
        //android.util.Log.e("view", "returning medium, mScrH="+mScreenHeight);
318
        return 1;
319
        }
320
      if( mScreenHeight<1900 )
321
        {
322
        //android.util.Log.e("view", "returning bug, mScrH="+mScreenHeight);
323
        return 2;
324
        }
325

    
326
      //android.util.Log.e("view", "returning huge, mScrH="+mScreenHeight);
327
      return 3;
328
      }
329

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

    
332
    public static int getDrawable(int small, int medium, int big, int huge)
333
      {
334
      int size = getDrawableSize();
335

    
336
      switch(size)
337
        {
338
        case 0 : return small;
339
        case 1 : return medium;
340
        case 2 : return big;
341
        default: return huge;
342
        }
343
      }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
    public boolean isVertical()
348
      {
349
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
350
      return view.isVertical();
351
      }
352
}
(1-1/4)