Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 27a70eae

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.RubikObjectList;
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
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
76
      int ordinal = play.getButton();
77
      view.getRenderer().createObject(RubikObjectList.getObject(ordinal));
78
      }
79
    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
    
82
    @Override
83
    protected void onDestroy() 
84
      {
85
      DistortedLibrary.onDestroy();
86
      super.onDestroy();
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
    @Override
92
    public void onClick(View v)
93
      {
94
      int id = v.getId();
95

    
96
      if( id>=0 && id< RubikObjectList.LENGTH )
97
        {
98
        RubikObjectList object = RubikObjectList.getObject(id);
99

    
100
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
101
        boolean success = view.getRenderer().createObject(object);
102

    
103
        if( success )
104
          {
105
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
106
          play.markButton(this,id);
107
          }
108
        }
109

    
110
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
111
        {
112
        RubikState.goBack(this);
113
        }
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

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

    
128
      for (int i=0; i<RubikState.LENGTH; i++)
129
        {
130
        RubikState.getState(i).getStateClass().savePreferences(editor);
131
        }
132

    
133
      RubikState.savePreferences(editor);
134
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.getRenderer().savePreferences(editor);
136

    
137
      editor.apply();
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

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

    
151
      for (int i=0; i< RubikState.LENGTH; i++)
152
        {
153
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
154
        }
155

    
156
      RubikState.restorePreferences(preferences);
157

    
158
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
159
      view.getRenderer().restorePreferences(preferences);
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// PUBLIC API
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    public void Play(View v)
167
      {
168
      RubikState.switchState(this,RubikState.PLAY);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    public void Settings(View v)
174
      {
175
      RubikDialogSettings settings = new RubikDialogSettings();
176
      settings.show(getSupportFragmentManager(), null);
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    public void Scores(View v)
182
      {
183
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
184
      int tab = play.getButton();
185

    
186
      Bundle bundle = new Bundle();
187
      bundle.putInt("tab", tab);
188

    
189
      RubikDialogScores scores = new RubikDialogScores();
190
      scores.setArguments(bundle);
191
      scores.show(getSupportFragmentManager(), null);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public void About(View v)
197
      {
198
      RubikDialogAbout about = new RubikDialogAbout();
199
      about.show(getSupportFragmentManager(), null);
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    public void Solve(View v)
215
      {
216
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
217
      view.getRenderer().solveObject();
218
      }
219
}
(1-1/3)