Project

General

Profile

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

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

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.RubikDialogPattern;
30
import org.distorted.dialog.RubikDialogScores;
31
import org.distorted.dialog.RubikDialogEffects;
32
import org.distorted.effect.BaseEffect;
33
import org.distorted.library.main.DistortedLibrary;
34

    
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
      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

    
100
        if( size>=0 && size<sizes.length )
101
          {
102
          view.getRenderer().createObject( obj, sizes[size] );
103
          }
104
        }
105
      }
106
    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    @Override
110
    protected void onDestroy() 
111
      {
112
      DistortedLibrary.onDestroy();
113
      super.onDestroy();
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    private void savePreferences()
119
      {
120
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
121
      SharedPreferences.Editor editor = preferences.edit();
122

    
123
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
124
        {
125
        BaseEffect.Type.getType(i).savePreferences(editor);
126
        }
127

    
128
      for (int i=0; i<RubikState.LENGTH; i++)
129
        {
130
        RubikState.getState(i).getStateClass().savePreferences(editor);
131
        }
132

    
133
      RubikState.savePreferences(editor);
134
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.getRenderer().savePreferences(editor);
136

    
137
      editor.apply();
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    private void restorePreferences()
143
      {
144
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
145

    
146
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
147
        {
148
        BaseEffect.Type.getType(i).restorePreferences(preferences);
149
        }
150

    
151
      for (int i=0; i< RubikState.LENGTH; i++)
152
        {
153
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
154
        }
155

    
156
      RubikState.restorePreferences(preferences);
157

    
158
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
159
      view.getRenderer().restorePreferences(preferences);
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// PUBLIC API
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    public void setCanRotate(boolean can)
167
      {
168
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
169
      RubikRenderer renderer = view.getRenderer();
170

    
171
      renderer.setCanRotate(can);
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
    public void changeObject(int object, int size)
177
      {
178
      RubikObjectList obj = RubikObjectList.getObject(object);
179
      int objectSize = obj.getSizes()[size];
180

    
181
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
182
      RubikRenderer renderer = view.getRenderer();
183

    
184
      if( renderer.canDrag() )
185
        {
186
        renderer.createObject(obj,objectSize);
187
        }
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

    
221
      Bundle bundle = new Bundle();
222
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
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
      RubikDialogPattern diag = new RubikDialogPattern();
235
      diag.show(getSupportFragmentManager(), null);
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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