Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikActivity.java @ 66e777b0

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

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

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

    
34
import org.distorted.objects.RubikObject;
35
import org.distorted.scores.RubikScores;
36
import org.distorted.scores.RubikScoresDownloader;
37
import org.distorted.objects.RubikObjectList;
38
import org.distorted.states.RubikState;
39
import org.distorted.states.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, int[][] moves)
197
      {
198
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
199
      RubikPostRender post = view.getPostRender();
200
      post.changeObject(object,size,moves);
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    public boolean isVertical()
206
      {
207
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
208
      return view.isVertical();
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
    public void Play(View v)
214
      {
215
      RubikState.switchState(this,RubikState.PLAY);
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    public void Effects(View v)
221
      {
222
      RubikDialogEffects settings = new RubikDialogEffects();
223
      settings.show(getSupportFragmentManager(), null);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    public void Scores(View v)
229
      {
230
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
231
      int object = play.getObject();
232
      int size   = play.getSize();
233
      int sizeIndex = RubikObjectList.getSizeIndex(object,size);
234

    
235
      Bundle bundle = new Bundle();
236
      bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
237
      bundle.putBoolean("submitting", false);
238

    
239
      RubikDialogScores scores = new RubikDialogScores();
240
      scores.setArguments(bundle);
241
      scores.show(getSupportFragmentManager(), null);
242
      }
243

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

    
246
    public void Patterns(View v)
247
      {
248
      RubikState.switchState(this,RubikState.PATT);
249
      }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    public void Solver(View v)
254
      {
255
      RubikState.switchState(this,RubikState.SVER);
256
      }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
    public void About(View v)
261
      {
262
      RubikDialogAbout diag = new RubikDialogAbout();
263
      diag.show(getSupportFragmentManager(), null);
264
      }
265
}
(1-1/4)