Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 8becce57

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.magic;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24
import android.preference.PreferenceManager;
25
import android.support.v7.app.AppCompatActivity;
26
import android.view.View;
27

    
28
import org.distorted.dialog.RubikDialogAbout;
29
import org.distorted.dialog.RubikDialogScores;
30
import org.distorted.dialog.RubikDialogEffects;
31
import org.distorted.effect.BaseEffect;
32
import org.distorted.library.main.DistortedLibrary;
33

    
34
import org.distorted.object.RubikObject;
35
import org.distorted.scores.RubikScores;
36
import org.distorted.scores.RubikScoresDownloader;
37
import org.distorted.object.RubikObjectList;
38
import org.distorted.uistate.RubikState;
39
import org.distorted.uistate.RubikStatePlay;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikActivity extends AppCompatActivity
44
{
45
    private boolean mJustStarted;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
    @Override
50
    protected void onCreate(Bundle savedState)
51
      {
52
      super.onCreate(savedState);
53
      setTheme(R.style.CustomActivityThemeNoActionBar);
54
      setContentView(R.layout.main);
55

    
56
      mJustStarted = true;
57
      }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onPause() 
63
      {
64
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
65
      view.onPause();
66
      DistortedLibrary.onPause();
67
      RubikScoresDownloader.onPause();
68
      savePreferences();
69
      super.onPause();
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onResume() 
76
      {
77
      super.onResume();
78
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
79
      view.onResume();
80
      restorePreferences();
81
      RubikState.setState(this);
82

    
83
      if( mJustStarted )
84
        {
85
        mJustStarted = false;
86
        RubikScores scores = RubikScores.getInstance();
87
        scores.incrementNumRuns();
88
        scores.setCountry(this);
89
        }
90

    
91
      boolean success = false;
92
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
93
      int object = play.getObject();
94
      int size   = play.getSize();
95

    
96
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
97
        {
98
        RubikObjectList obj = RubikObjectList.getObject(object);
99
        int[] sizes = obj.getSizes();
100
        int sizeIndex = RubikObjectList.getSizeIndex(object,size);
101

    
102
        if( sizeIndex>=0 && sizeIndex<sizes.length )
103
          {
104
          success = true;
105
          view.getPostRender().changeObject( obj, size, null );
106
          }
107

    
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
        view.getPostRender().changeObject(obj,s, null);
117
        }
118
      }
119
    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    @Override
123
    protected void onDestroy() 
124
      {
125
      DistortedLibrary.onDestroy();
126
      super.onDestroy();
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    private void savePreferences()
132
      {
133
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
134
      SharedPreferences.Editor editor = preferences.edit();
135

    
136
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
137
        {
138
        BaseEffect.Type.getType(i).savePreferences(editor);
139
        }
140

    
141
      for (int i=0; i<RubikState.LENGTH; i++)
142
        {
143
        RubikState.getState(i).getStateClass().savePreferences(editor);
144
        }
145

    
146
      RubikState.savePreferences(editor);
147
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
148
      view.getPostRender().savePreferences(editor);
149

    
150
      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
        {
161
        BaseEffect.Type.getType(i).restorePreferences(preferences);
162
        }
163

    
164
      for (int i=0; i< RubikState.LENGTH; i++)
165
        {
166
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
167
        }
168

    
169
      RubikState.restorePreferences(preferences);
170

    
171
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
172
      view.getPostRender().restorePreferences(preferences);
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// PUBLIC API
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
    public RubikObject getObject()
180
      {
181
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
182
      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
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public void changeObject(RubikObjectList object, int size, String moves)
197
      {
198
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
199
      RubikPostRender post = view.getPostRender();
200

    
201
      if( post.canDrag() )
202
        {
203
        post.changeObject(object,size,moves);
204
        }
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    public boolean isVertical()
210
      {
211
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
212
      return view.isVertical();
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    public void Play(View v)
218
      {
219
      RubikState.switchState(this,RubikState.PLAY);
220
      }
221

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

    
224
    public void Effects(View v)
225
      {
226
      RubikDialogEffects settings = new RubikDialogEffects();
227
      settings.show(getSupportFragmentManager(), null);
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    public void Scores(View v)
233
      {
234
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
235
      int object = play.getObject();
236
      int size   = play.getSize();
237
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
238

    
239
      Bundle bundle = new Bundle();
240
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
241
      bundle.putBoolean("submitting", false);
242

    
243
      RubikDialogScores scores = new RubikDialogScores();
244
      scores.setArguments(bundle);
245
      scores.show(getSupportFragmentManager(), null);
246
      }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
    public void Patterns(View v)
251
      {
252
      RubikState.switchState(this,RubikState.PATT);
253
      }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
    public void Solver(View v)
258
      {
259
      android.util.Log.e("act", "Not implemented yet");
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
    public void About(View v)
265
      {
266
      RubikDialogAbout diag = new RubikDialogAbout();
267
      diag.show(getSupportFragmentManager(), null);
268
      }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
    public void Scramble(View v)
273
      {
274
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
275
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
276
      int scramble = play.getPicker();
277
      view.getPostRender().scrambleObject(scramble);
278
      }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    public void Solve(View v)
283
      {
284
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
285
      view.getPostRender().solveObject();
286
      }
287
}
(1-1/4)