Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 0333d81e

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.network.RubikScoresDownloader;
35
import org.distorted.object.RubikObject;
36
import org.distorted.uistate.RubikState;
37
import org.distorted.uistate.RubikStateAbstract;
38
import org.distorted.uistate.RubikStatePlay;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
43
{
44
    @Override
45
    protected void onCreate(Bundle savedState)
46
      {
47
      super.onCreate(savedState);
48
      setTheme(R.style.CustomActivityThemeNoActionBar);
49
      setContentView(R.layout.main);
50
      }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onPause() 
56
      {
57
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
58
      view.onPause();
59
      DistortedLibrary.onPause();
60
      RubikScoresDownloader.onPause();
61
      savePreferences();
62
      super.onPause();
63
      }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
    
67
    @Override
68
    protected void onResume() 
69
      {
70
      super.onResume();
71
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
72
      view.onResume();
73
      restorePreferences();
74
      RubikState.setState(this);
75
      }
76
    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
    
79
    @Override
80
    protected void onDestroy() 
81
      {
82
      DistortedLibrary.onDestroy();
83
      super.onDestroy();
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
    @Override
89
    public void onClick(View v)
90
      {
91
      int id = v.getId();
92

    
93
      if( id>=0 && id< RubikObject.LENGTH )
94
        {
95
        int size = RubikObject.getObject(id).getObjectSize();
96

    
97
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
98
        boolean success = view.getRenderer().createCube(size);
99

    
100
        if( success )
101
          {
102
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
103
          play.markButton(this,id);
104
          }
105
        }
106

    
107
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
108
        {
109
        RubikState.goBack(this);
110
        }
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
    private void savePreferences()
116
      {
117
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
118
      SharedPreferences.Editor editor = preferences.edit();
119

    
120
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
121
        {
122
        BaseEffect.Type.getType(i).savePreferences(editor);
123
        }
124

    
125
      for (int i=0; i<RubikState.LENGTH; i++)
126
        {
127
        RubikState.getState(i).getStateClass().savePreferences(editor);
128
        }
129

    
130
      RubikState.savePreferences(editor);
131
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
132
      view.getRenderer().savePreferences(editor);
133

    
134
      editor.apply();
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    private void restorePreferences()
140
      {
141
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
142

    
143
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
144
        {
145
        BaseEffect.Type.getType(i).restorePreferences(preferences);
146
        }
147

    
148
      for (int i=0; i< RubikState.LENGTH; i++)
149
        {
150
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
151
        }
152

    
153
      RubikState.restorePreferences(preferences);
154

    
155
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
156
      view.getRenderer().restorePreferences(preferences);
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
// PUBLIC API
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    public void Play(View v)
164
      {
165
      RubikState.switchState(this,RubikState.PLAY);
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    public void Settings(View v)
171
      {
172
      RubikDialogSettings settings = new RubikDialogSettings();
173
      settings.show(getSupportFragmentManager(), null);
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
    public void Scores(View v)
179
      {
180
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
181
      int tab = play.getButton();
182

    
183
      Bundle bundle = new Bundle();
184
      bundle.putInt("tab", tab);
185

    
186
      RubikDialogScores scores = new RubikDialogScores();
187
      scores.setArguments(bundle);
188
      scores.show(getSupportFragmentManager(), null);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public void About(View v)
194
      {
195
      RubikDialogAbout about = new RubikDialogAbout();
196
      about.show(getSupportFragmentManager(), null);
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    public void Scramble(View v)
202
      {
203
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
204
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
205
      int scramble = play.getPicker();
206
      view.getRenderer().scrambleCube(scramble);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    public void Solve(View v)
212
      {
213
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
214
      view.getRenderer().solveCube();
215
      }
216
}
(1-1/3)