Project

General

Profile

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

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

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
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
83

    
84
      if( mJustStarted )
85
        {
86
        mJustStarted = false;
87
        RubikScores.getInstance().incrementNumRuns();
88
        }
89

    
90
      int object = play.getObject();
91
      int size   = play.getSize();
92
      RubikObjectList obj = RubikObjectList.getObject(object);
93
      int objectSize = obj.getSizes()[size];
94

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

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

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

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

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

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

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

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

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

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

    
159
      editor.apply();
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

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

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

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

    
178
      RubikState.restorePreferences(preferences);
179

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// PUBLIC API
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

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

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

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

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

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