Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 054fbee1

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.09f;
52
    public static final float MENU_ITEM_SIZE      = 0.12f;
53
    public static final float PATTERN_GROUP_TEXT  = 0.012f;
54
    public static final float PATTERN_CHILD_TEXT  = 0.008f;
55

    
56
    public static final float MENU_BIG_TEXT_SIZE   = 0.05f;
57
    public static final float MENU_MEDIUM_TEXT_SIZE= 0.04f;
58
    public static final float MENU_SMALL_TEXT_SIZE = 0.035f;
59

    
60
    private boolean mJustStarted;
61
    private FirebaseAnalytics mFirebaseAnalytics;
62
    private static int mScreenWidth, mScreenHeight;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
73
      mJustStarted = true;
74
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
75

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

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

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

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

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

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

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

    
132
        }
133

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

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

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

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

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

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

    
174
      editor.apply();
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

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

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

    
193
      RubikState.restorePreferences(preferences);
194

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
// PUBLIC API
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    public FirebaseAnalytics getAnalytics()
216
      {
217
      return mFirebaseAnalytics;
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

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

    
231
    public int getScreenWidthInPixels()
232
      {
233
      return mScreenWidth;
234
      }
235

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

    
238
    public int getScreenHeightInPixels()
239
      {
240
      return mScreenHeight;
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

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

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

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

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

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

    
292
      pre.changeObject(newObject,newSize);
293
      }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

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

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

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

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

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

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