Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 2f1b15da

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.effect.BaseEffect;
29
import org.distorted.library.main.DistortedLibrary;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
34
{
35
    RubikState mCurrentState;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
    @Override
40
    protected void onCreate(Bundle savedState)
41
      {
42
      super.onCreate(savedState);
43
      setTheme(R.style.CustomActivityThemeNoActionBar);
44
      setContentView(R.layout.main);
45
      }
46

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
    
62
    @Override
63
    protected void onResume() 
64
      {
65
      super.onResume();
66
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
67
      view.onResume();
68
      restorePreferences();
69
      view.switchState(this, mCurrentState);
70
      }
71
    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onDestroy() 
76
      {
77
      DistortedLibrary.onDestroy();
78
      super.onDestroy();
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
    @Override
84
    public void onClick(View v)
85
      {
86
      int id = v.getId();
87

    
88
      if( id>=0 && id<RubikSize.LENGTH )
89
        {
90
        int size = RubikSize.getSize(id).getCubeSize();
91

    
92
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
93
        boolean success = view.getRenderer().createCube(size);
94

    
95
        if( success )
96
          {
97
          view.markButton(id);
98
          }
99
        }
100

    
101
      if( id == RubikSurfaceView.BUTTON_ID_BACK )
102
        {
103
        mCurrentState = mCurrentState.getBack();
104

    
105
        if( mCurrentState!=null )
106
          {
107
          RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
108
          view.switchState(this, mCurrentState);
109
          }
110
        else
111
          {
112
          mCurrentState = RubikState.MAIN;
113
          finish();
114
          }
115
        }
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    private void savePreferences()
121
      {
122
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
123
      SharedPreferences.Editor editor = preferences.edit();
124

    
125
      for (int i = 0; i< BaseEffect.Type.LENGTH; i++)
126
        {
127
        BaseEffect.Type.getType(i).savePreferences(editor);
128
        }
129

    
130
      editor.putInt("state", mCurrentState.ordinal() );
131

    
132
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
133
      view.savePreferences(editor);
134

    
135
      editor.apply();
136
      }
137

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

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

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

    
149
      int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() );
150
      mCurrentState = RubikState.getState(stateOrdinal);
151

    
152
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
153
      view.restorePreferences(preferences);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
// PUBLIC API
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    public void Play(View v)
161
      {
162
      mCurrentState = RubikState.PLAY;
163
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
164
      view.switchState(this,mCurrentState);
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    public void Scores(View v)
178
      {
179
      Bundle bundle = new Bundle();
180
      bundle.putInt("tab", RubikSurfaceView.getRedButton());
181

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

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

    
197
    public void Scramble(View v)
198
      {
199
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
200
      view.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/12)