Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 53f23b64

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.scores.RubikScores;
35
import org.distorted.scores.RubikScoresDownloader;
36
import org.distorted.object.RubikObjectList;
37
import org.distorted.uistate.RubikState;
38
import org.distorted.uistate.RubikStatePlay;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

    
55
      mJustStarted = true;
56
      }
57

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

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

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

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

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

    
101
        if( sizeIndex>=0 && sizeIndex<sizes.length )
102
          {
103
          success = true;
104
          view.getRenderer().createObject( obj, size );
105
          }
106

    
107
        }
108

    
109
      if( !success )
110
        {
111
        RubikObjectList obj = RubikObjectList.getObject(RubikStatePlay.DEF_OBJECT);
112
        int s = RubikStatePlay.DEF_SIZE;
113

    
114
        play.setObjectAndSize(obj,s);
115
        view.getRenderer().createObject(obj,s);
116
        }
117
      }
118
    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    @Override
122
    protected void onDestroy() 
123
      {
124
      DistortedLibrary.onDestroy();
125
      super.onDestroy();
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

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

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

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

    
149
      editor.apply();
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    private void restorePreferences()
155
      {
156
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
157

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

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

    
168
      RubikState.restorePreferences(preferences);
169

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// PUBLIC API
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
    public void changeObject(RubikObjectList object, int size)
179
      {
180
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
181
      RubikRenderer renderer = view.getRenderer();
182

    
183
      if( renderer.canDrag() )
184
        {
185
        renderer.createObject(object,size);
186
        }
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public boolean isVertical()
192
      {
193
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
194
      return view.isVertical();
195
      }
196

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

    
199
    public void Play(View v)
200
      {
201
      RubikState.switchState(this,RubikState.PLAY);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    public void Effects(View v)
207
      {
208
      RubikDialogEffects settings = new RubikDialogEffects();
209
      settings.show(getSupportFragmentManager(), null);
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    public void Scores(View v)
215
      {
216
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
217
      int object = play.getObject();
218
      int size   = play.getSize();
219
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
220

    
221
      Bundle bundle = new Bundle();
222
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
223
      bundle.putBoolean("submitting", false);
224

    
225
      RubikDialogScores scores = new RubikDialogScores();
226
      scores.setArguments(bundle);
227
      scores.show(getSupportFragmentManager(), null);
228
      }
229

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

    
232
    public void Patterns(View v)
233
      {
234
      RubikState.switchState(this,RubikState.PATT);
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
    public void Solver(View v)
240
      {
241
      android.util.Log.e("act", "Not implemented yet");
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    public void About(View v)
247
      {
248
      RubikDialogAbout diag = new RubikDialogAbout();
249
      diag.show(getSupportFragmentManager(), null);
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public void Scramble(View v)
255
      {
256
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
257
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
258
      int scramble = play.getPicker();
259
      view.getRenderer().scrambleObject(scramble);
260
      }
261

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

    
264
    public void Solve(View v)
265
      {
266
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
267
      view.getRenderer().solveObject();
268
      }
269
}
(1-1/3)