Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ f6a9c538

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

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

    
58
    private boolean mJustStarted;
59
    private FirebaseAnalytics mFirebaseAnalytics;
60
    private static int mScreenWidth, mScreenHeight;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

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

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

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

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

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

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

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

    
130
        }
131

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

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

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

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

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

    
172
      editor.apply();
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

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

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

    
191
      RubikState.restorePreferences(preferences);
192

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

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

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

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

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
// PUBLIC API
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
    public FirebaseAnalytics getAnalytics()
214
      {
215
      return mFirebaseAnalytics;
216
      }
217

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

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

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

    
229
    public int getScreenWidthInPixels()
230
      {
231
      return mScreenWidth;
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
    public int getScreenHeightInPixels()
237
      {
238
      return mScreenHeight;
239
      }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

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

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

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

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

    
290
      pre.changeObject(newObject,newSize);
291
      }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

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

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
    public static int getDrawableSize()
305
      {
306
      if( mScreenHeight<1000 )
307
        {
308
        //android.util.Log.e("view", "returning small, mScrH="+mScreenHeight);
309
        return 0;
310
        }
311
      if( mScreenHeight<1600 )
312
        {
313
        //android.util.Log.e("view", "returning medium, mScrH="+mScreenHeight);
314
        return 1;
315
        }
316

    
317
      //android.util.Log.e("view", "returning big, mScrH="+mScreenHeight);
318
      return 2;
319
      }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
    public static int getDrawable(int small, int medium, int big)
324
      {
325
      int size = getDrawableSize();
326

    
327
      switch(size)
328
        {
329
        case 0 : return small;
330
        case 1 : return medium;
331
        default: return big;
332
        }
333
      }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
    public boolean isVertical()
338
      {
339
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
340
      return view.isVertical();
341
      }
342
}
(1-1/4)