Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ a31d25de

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
package org.distorted.magic;
21
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 f6fcf06a Leszek Koltunski
import android.support.v7.app.AppCompatActivity;
26 0c52af30 Leszek Koltunski
import android.view.View;
27
28 211b48f2 Leszek Koltunski
import org.distorted.dialog.RubikDialogAbout;
29
import org.distorted.dialog.RubikDialogScores;
30 4c0cd600 Leszek Koltunski
import org.distorted.dialog.RubikDialogEffects;
31 33499c56 Leszek Koltunski
import org.distorted.effect.BaseEffect;
32 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 0c52af30 Leszek Koltunski
34 aa171dee Leszek Koltunski
import org.distorted.object.RubikObject;
35 c3ffcf58 Leszek Koltunski
import org.distorted.scores.RubikScores;
36 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScoresDownloader;
37 27a70eae Leszek Koltunski
import org.distorted.object.RubikObjectList;
38 211b48f2 Leszek Koltunski
import org.distorted.uistate.RubikState;
39
import org.distorted.uistate.RubikStatePlay;
40
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 a6d3b158 Leszek Koltunski
201 8becce57 Leszek Koltunski
      if( post.canDrag() )
202 a6d3b158 Leszek Koltunski
        {
203 8becce57 Leszek Koltunski
        post.changeObject(object,size,moves);
204 a6d3b158 Leszek Koltunski
        }
205 769d7b9f Leszek Koltunski
      }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
    public boolean isVertical()
210
      {
211
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
212
      return view.isVertical();
213
      }
214
215 4235de9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
    public void Play(View v)
218
      {
219 211b48f2 Leszek Koltunski
      RubikState.switchState(this,RubikState.PLAY);
220 4235de9b Leszek Koltunski
      }
221
222 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224 4ed3823a Leszek Koltunski
    public void Effects(View v)
225 0c52af30 Leszek Koltunski
      {
226 4c0cd600 Leszek Koltunski
      RubikDialogEffects settings = new RubikDialogEffects();
227 f6fcf06a Leszek Koltunski
      settings.show(getSupportFragmentManager(), null);
228
      }
229
230 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232
    public void Scores(View v)
233
      {
234 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
235 4888e97c Leszek Koltunski
      int object = play.getObject();
236
      int size   = play.getSize();
237 53f23b64 Leszek Koltunski
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
238 211b48f2 Leszek Koltunski
239 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
240 53f23b64 Leszek Koltunski
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
241 4895fff6 Leszek Koltunski
      bundle.putBoolean("submitting", false);
242 4918f19c Leszek Koltunski
243 0fd3ac1f Leszek Koltunski
      RubikDialogScores scores = new RubikDialogScores();
244 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
245 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
246
      }
247
248 4ed3823a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
    public void Patterns(View v)
251
      {
252 53f23b64 Leszek Koltunski
      RubikState.switchState(this,RubikState.PATT);
253 4ed3823a Leszek Koltunski
      }
254
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257
    public void Solver(View v)
258
      {
259
      android.util.Log.e("act", "Not implemented yet");
260
      }
261
262 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264 5560eea9 Leszek Koltunski
    public void About(View v)
265 1cbcc6bf Leszek Koltunski
      {
266 d18993ac Leszek Koltunski
      RubikDialogAbout diag = new RubikDialogAbout();
267
      diag.show(getSupportFragmentManager(), null);
268 1cbcc6bf Leszek Koltunski
      }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
    public void Scramble(View v)
273
      {
274
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
275 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
276
      int scramble = play.getPicker();
277 8becce57 Leszek Koltunski
      view.getPostRender().scrambleObject(scramble);
278 1cbcc6bf Leszek Koltunski
      }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
283 1cbcc6bf Leszek Koltunski
      {
284 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
285 8becce57 Leszek Koltunski
      view.getPostRender().solveObject();
286 1cbcc6bf Leszek Koltunski
      }
287 0c52af30 Leszek Koltunski
}