Project

General

Profile

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

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

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

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

    
43
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
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
      RubikObjectList obj = RubikObjectList.getObject(object);
95
      int objectSize = obj.getSizes()[size];
96

    
97
      view.getRenderer().createObject( obj, objectSize );
98
      }
99
    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
    @Override
103
    protected void onDestroy() 
104
      {
105
      DistortedLibrary.onDestroy();
106
      super.onDestroy();
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    @Override
112
    public void onClick(View v)
113
      {
114
      int id = v.getId();
115

    
116
      if( id>=0 && id< RubikObjectList.getTotal() )
117
        {
118
        int object = RubikObjectList.unpackObject(id);
119
        int size= RubikObjectList.unpackSize(id);
120

    
121
        RubikObjectList obj = RubikObjectList.getObject(object);
122
        int objectSize = obj.getSizes()[size];
123

    
124
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
125
        boolean success = view.getRenderer().createObject(obj,objectSize);
126

    
127
        if( success )
128
          {
129
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
130
          play.markButton(this,object,size);
131
          }
132
        }
133

    
134
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
135
        {
136
        RubikState.goBack(this);
137
        }
138
      }
139

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

    
142
    private void savePreferences()
143
      {
144
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
145
      SharedPreferences.Editor editor = preferences.edit();
146

    
147
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
148
        {
149
        BaseEffect.Type.getType(i).savePreferences(editor);
150
        }
151

    
152
      for (int i=0; i<RubikState.LENGTH; i++)
153
        {
154
        RubikState.getState(i).getStateClass().savePreferences(editor);
155
        }
156

    
157
      RubikState.savePreferences(editor);
158
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
159
      view.getRenderer().savePreferences(editor);
160

    
161
      editor.apply();
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    private void restorePreferences()
167
      {
168
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
169

    
170
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
171
        {
172
        BaseEffect.Type.getType(i).restorePreferences(preferences);
173
        }
174

    
175
      for (int i=0; i< RubikState.LENGTH; i++)
176
        {
177
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
178
        }
179

    
180
      RubikState.restorePreferences(preferences);
181

    
182
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
183
      view.getRenderer().restorePreferences(preferences);
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
// PUBLIC API
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    public void Play(View v)
191
      {
192
      RubikState.switchState(this,RubikState.PLAY);
193
      }
194

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

    
197
    public void Settings(View v)
198
      {
199
      RubikDialogSettings settings = new RubikDialogSettings();
200
      settings.show(getSupportFragmentManager(), null);
201
      }
202

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

    
205
    public void Scores(View v)
206
      {
207
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
208
      int object = play.getObject();
209
      int size   = play.getSize();
210

    
211
      Bundle bundle = new Bundle();
212
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
213

    
214
      RubikDialogScores scores = new RubikDialogScores();
215
      scores.setArguments(bundle);
216
      scores.show(getSupportFragmentManager(), null);
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    public void About(View v)
222
      {
223
      RubikDialogAbout about = new RubikDialogAbout();
224
      about.show(getSupportFragmentManager(), null);
225
      }
226

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

    
229
    public void Scramble(View v)
230
      {
231
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
232
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
233
      int scramble = play.getPicker();
234
      view.getRenderer().scrambleObject(scramble);
235
      }
236

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

    
239
    public void Solve(View v)
240
      {
241
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
242
      view.getRenderer().solveObject();
243
      }
244
}
(1-1/3)