Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 211b48f2

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.uistate.RubikState;
35
import org.distorted.uistate.RubikStateAbstract;
36
import org.distorted.uistate.RubikStatePlay;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

    
91
      if( id>=0 && id<RubikSize.LENGTH )
92
        {
93
        int size = RubikSize.getSize(id).getCubeSize();
94

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

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

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

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

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

    
128
      RubikState.savePreferences(editor);
129
      editor.apply();
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

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

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

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

    
148
      RubikState.restorePreferences(preferences);
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// PUBLIC API
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

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

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

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

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

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

    
175
      Bundle bundle = new Bundle();
176
      bundle.putInt("tab", tab);
177

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

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

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

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