Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 66e777b0

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22 33499c56 Leszek Koltunski
import android.content.SharedPreferences;
23 0c52af30 Leszek Koltunski
import android.os.Bundle;
24 33499c56 Leszek Koltunski
import android.preference.PreferenceManager;
25 66e777b0 Leszek Koltunski
import androidx.appcompat.app.AppCompatActivity;
26 0c52af30 Leszek Koltunski
import android.view.View;
27
28 1f9772f3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
29
import org.distorted.dialogs.RubikDialogScores;
30
import org.distorted.dialogs.RubikDialogEffects;
31
import org.distorted.effects.BaseEffect;
32 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 0c52af30 Leszek Koltunski
34 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObject;
35 c3ffcf58 Leszek Koltunski
import org.distorted.scores.RubikScores;
36 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScoresDownloader;
37 1f9772f3 Leszek Koltunski
import org.distorted.objects.RubikObjectList;
38
import org.distorted.states.RubikState;
39
import org.distorted.states.RubikStatePlay;
40 211b48f2 Leszek Koltunski
41 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 a6d3b158 Leszek Koltunski
public class RubikActivity extends AppCompatActivity
44 0c52af30 Leszek Koltunski
{
45 c3ffcf58 Leszek Koltunski
    private boolean mJustStarted;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 0c52af30 Leszek Koltunski
    @Override
50 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
51 0c52af30 Leszek Koltunski
      {
52 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
53 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
54 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
55 c3ffcf58 Leszek Koltunski
56
      mJustStarted = true;
57 0c52af30 Leszek Koltunski
      }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onPause() 
63
      {
64 4235de9b Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
65 0c52af30 Leszek Koltunski
      view.onPause();
66 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
67 f3e12931 Leszek Koltunski
      RubikScoresDownloader.onPause();
68 33499c56 Leszek Koltunski
      savePreferences();
69 0c52af30 Leszek Koltunski
      super.onPause();
70
      }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onResume() 
76
      {
77
      super.onResume();
78 4235de9b Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
79 0c52af30 Leszek Koltunski
      view.onResume();
80 33499c56 Leszek Koltunski
      restorePreferences();
81 211b48f2 Leszek Koltunski
      RubikState.setState(this);
82 4888e97c Leszek Koltunski
83 c3ffcf58 Leszek Koltunski
      if( mJustStarted )
84
        {
85
        mJustStarted = false;
86 82ce8e64 Leszek Koltunski
        RubikScores scores = RubikScores.getInstance();
87
        scores.incrementNumRuns();
88
        scores.setCountry(this);
89 c3ffcf58 Leszek Koltunski
        }
90
91 53f23b64 Leszek Koltunski
      boolean success = false;
92 e41e7dc3 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
93 4888e97c Leszek Koltunski
      int object = play.getObject();
94
      int size   = play.getSize();
95
96 4c0cd600 Leszek Koltunski
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
97
        {
98
        RubikObjectList obj = RubikObjectList.getObject(object);
99
        int[] sizes = obj.getSizes();
100 53f23b64 Leszek Koltunski
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
101 4c0cd600 Leszek Koltunski
102 53f23b64 Leszek Koltunski
        if( sizeIndex>=0 && sizeIndex<sizes.length )
103 4c0cd600 Leszek Koltunski
          {
104 53f23b64 Leszek Koltunski
          success = true;
105 8becce57 Leszek Koltunski
          view.getPostRender().changeObject( obj, size, null );
106 4c0cd600 Leszek Koltunski
          }
107 53f23b64 Leszek Koltunski
108
        }
109
110
      if( !success )
111
        {
112
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
113
        int s = RubikStatePlay.DEF_SIZE;
114
115
        play.setObjectAndSize(obj,s);
116 8becce57 Leszek Koltunski
        view.getPostRender().changeObject(obj,s, null);
117 4c0cd600 Leszek Koltunski
        }
118 0c52af30 Leszek Koltunski
      }
119
    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    @Override
123
    protected void onDestroy() 
124
      {
125 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
126 0c52af30 Leszek Koltunski
      super.onDestroy();
127
      }
128
129 33499c56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131
    private void savePreferences()
132
      {
133
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
134
      SharedPreferences.Editor editor = preferences.edit();
135
136 211b48f2 Leszek Koltunski
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
137 33499c56 Leszek Koltunski
        {
138
        BaseEffect.Type.getType(i).savePreferences(editor);
139
        }
140
141 211b48f2 Leszek Koltunski
      for (int i=0; i<RubikState.LENGTH; i++)
142
        {
143
        RubikState.getState(i).getStateClass().savePreferences(editor);
144
        }
145 33499c56 Leszek Koltunski
146 211b48f2 Leszek Koltunski
      RubikState.savePreferences(editor);
147 0333d81e Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
148 8becce57 Leszek Koltunski
      view.getPostRender().savePreferences(editor);
149 0333d81e Leszek Koltunski
150 33499c56 Leszek Koltunski
      editor.apply();
151
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
    private void restorePreferences()
156
      {
157
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
158
159
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
160 4235de9b Leszek Koltunski
        {
161 33499c56 Leszek Koltunski
        BaseEffect.Type.getType(i).restorePreferences(preferences);
162 4235de9b Leszek Koltunski
        }
163 33499c56 Leszek Koltunski
164 211b48f2 Leszek Koltunski
      for (int i=0; i< RubikState.LENGTH; i++)
165
        {
166
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
167
        }
168 33499c56 Leszek Koltunski
169 211b48f2 Leszek Koltunski
      RubikState.restorePreferences(preferences);
170 0333d81e Leszek Koltunski
171
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
172 8becce57 Leszek Koltunski
      view.getPostRender().restorePreferences(preferences);
173 f3e12931 Leszek Koltunski
      }
174
175 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176 47ba5ddc Leszek Koltunski
// PUBLIC API
177 a6d3b158 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179 aa171dee Leszek Koltunski
    public RubikObject getObject()
180
      {
181
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
182 8becce57 Leszek Koltunski
      RubikPostRender post = view.getPostRender();
183
      return post.getObject();
184
      }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
    public RubikPostRender getPostRender()
189
      {
190
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
191
      return view.getPostRender();
192 aa171dee Leszek Koltunski
      }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196 a31d25de Leszek Koltunski
    public void changeObject(RubikObjectList object, int size, int[][] moves)
197 769d7b9f Leszek Koltunski
      {
198
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
199 8becce57 Leszek Koltunski
      RubikPostRender post = view.getPostRender();
200 a42e25a6 Leszek Koltunski
      post.changeObject(object,size,moves);
201 769d7b9f Leszek Koltunski
      }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
    public boolean isVertical()
206
      {
207
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
208
      return view.isVertical();
209
      }
210
211 4235de9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
    public void Play(View v)
214
      {
215 211b48f2 Leszek Koltunski
      RubikState.switchState(this,RubikState.PLAY);
216 4235de9b Leszek Koltunski
      }
217
218 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220 4ed3823a Leszek Koltunski
    public void Effects(View v)
221 0c52af30 Leszek Koltunski
      {
222 4c0cd600 Leszek Koltunski
      RubikDialogEffects settings = new RubikDialogEffects();
223 f6fcf06a Leszek Koltunski
      settings.show(getSupportFragmentManager(), null);
224
      }
225
226 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228
    public void Scores(View v)
229
      {
230 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
231 4888e97c Leszek Koltunski
      int object = play.getObject();
232
      int size   = play.getSize();
233 53f23b64 Leszek Koltunski
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
234 211b48f2 Leszek Koltunski
235 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
236 53f23b64 Leszek Koltunski
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
237 4895fff6 Leszek Koltunski
      bundle.putBoolean("submitting", false);
238 4918f19c Leszek Koltunski
239 0fd3ac1f Leszek Koltunski
      RubikDialogScores scores = new RubikDialogScores();
240 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
241 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
242
      }
243
244 4ed3823a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246
    public void Patterns(View v)
247
      {
248 53f23b64 Leszek Koltunski
      RubikState.switchState(this,RubikState.PATT);
249 4ed3823a Leszek Koltunski
      }
250
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
253
    public void Solver(View v)
254
      {
255 7289fd6c Leszek Koltunski
      RubikState.switchState(this,RubikState.SVER);
256 4ed3823a Leszek Koltunski
      }
257
258 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260 5560eea9 Leszek Koltunski
    public void About(View v)
261 1cbcc6bf Leszek Koltunski
      {
262 d18993ac Leszek Koltunski
      RubikDialogAbout diag = new RubikDialogAbout();
263
      diag.show(getSupportFragmentManager(), null);
264 1cbcc6bf Leszek Koltunski
      }
265 0c52af30 Leszek Koltunski
}