Project

General

Profile

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

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

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.RubikStateAbstract;
40
import org.distorted.uistate.RubikStatePlay;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
45
{
46
    private boolean mJustStarted;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
57
      mJustStarted = true;
58
      }
59

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

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

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

    
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

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
    @Override
120
    public void onClick(View v)
121
      {
122
      if( v.getId() == RubikStateAbstract.BUTTON_ID_BACK )
123
        {
124
        RubikState.goBack(this);
125
        }
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(int object, int size)
179
      {
180
      RubikObjectList obj = RubikObjectList.getObject(object);
181
      int objectSize = obj.getSizes()[size];
182

    
183
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
184
      view.getRenderer().createObject(obj,objectSize);
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

    
218
      Bundle bundle = new Bundle();
219
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
220
      bundle.putBoolean("submitting", false);
221

    
222
      RubikDialogScores scores = new RubikDialogScores();
223
      scores.setArguments(bundle);
224
      scores.show(getSupportFragmentManager(), null);
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    public void Patterns(View v)
230
      {
231
      RubikDialogPattern diag = new RubikDialogPattern();
232
      diag.show(getSupportFragmentManager(), null);
233
      }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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