Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 4f9f99a2

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
      editor.apply();
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
    private void restorePreferences()
137
      {
138
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
139

    
140
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
141
        {
142
        BaseEffect.Type.getType(i).restorePreferences(preferences);
143
        }
144

    
145
      for (int i=0; i< RubikState.LENGTH; i++)
146
        {
147
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
148
        }
149

    
150
      RubikState.restorePreferences(preferences);
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// PUBLIC API
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    public void Play(View v)
158
      {
159
      RubikState.switchState(this,RubikState.PLAY);
160
      }
161

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

    
164
    public void Settings(View v)
165
      {
166
      RubikDialogSettings settings = new RubikDialogSettings();
167
      settings.show(getSupportFragmentManager(), null);
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    public void Scores(View v)
173
      {
174
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
175
      int tab = play.getButton();
176

    
177
      Bundle bundle = new Bundle();
178
      bundle.putInt("tab", tab);
179

    
180
      RubikDialogScores scores = new RubikDialogScores();
181
      scores.setArguments(bundle);
182
      scores.show(getSupportFragmentManager(), null);
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public void About(View v)
188
      {
189
      RubikDialogAbout about = new RubikDialogAbout();
190
      about.show(getSupportFragmentManager(), null);
191
      }
192

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

    
195
    public void Scramble(View v)
196
      {
197
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
198
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
199
      int scramble = play.getPicker();
200
      view.getRenderer().scrambleCube(scramble);
201
      }
202

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

    
205
    public void Solve(View v)
206
      {
207
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
208
      view.getRenderer().solveCube();
209
      }
210
}
(1-1/3)