Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 4888e97c

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

    
77
      int object = play.getObject();
78
      int size   = play.getSize();
79
      RubikObjectList obj = RubikObjectList.getObject(object);
80
      int objectSize = obj.getSizes()[size];
81

    
82
      view.getRenderer().createObject( obj, objectSize );
83
      }
84
    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
    @Override
88
    protected void onDestroy() 
89
      {
90
      DistortedLibrary.onDestroy();
91
      super.onDestroy();
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    @Override
97
    public void onClick(View v)
98
      {
99
      int id = v.getId();
100

    
101
      if( id>=0 && id< RubikObjectList.getTotal() )
102
        {
103
        int object = RubikObjectList.unpackObject(id);
104
        int size= RubikObjectList.unpackSize(id);
105

    
106
        RubikObjectList obj = RubikObjectList.getObject(object);
107
        int objectSize = obj.getSizes()[size];
108

    
109
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
110
        boolean success = view.getRenderer().createObject(obj,objectSize);
111

    
112
        if( success )
113
          {
114
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
115
          play.markButton(this,object,size);
116
          }
117
        }
118

    
119
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
120
        {
121
        RubikState.goBack(this);
122
        }
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    private void savePreferences()
128
      {
129
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
130
      SharedPreferences.Editor editor = preferences.edit();
131

    
132
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
133
        {
134
        BaseEffect.Type.getType(i).savePreferences(editor);
135
        }
136

    
137
      for (int i=0; i<RubikState.LENGTH; i++)
138
        {
139
        RubikState.getState(i).getStateClass().savePreferences(editor);
140
        }
141

    
142
      RubikState.savePreferences(editor);
143
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
144
      view.getRenderer().savePreferences(editor);
145

    
146
      editor.apply();
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
    private void restorePreferences()
152
      {
153
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
154

    
155
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
156
        {
157
        BaseEffect.Type.getType(i).restorePreferences(preferences);
158
        }
159

    
160
      for (int i=0; i< RubikState.LENGTH; i++)
161
        {
162
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
163
        }
164

    
165
      RubikState.restorePreferences(preferences);
166

    
167
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
168
      view.getRenderer().restorePreferences(preferences);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
// PUBLIC API
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    public void Play(View v)
176
      {
177
      RubikState.switchState(this,RubikState.PLAY);
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    public void Settings(View v)
183
      {
184
      RubikDialogSettings settings = new RubikDialogSettings();
185
      settings.show(getSupportFragmentManager(), null);
186
      }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
    public void Scores(View v)
191
      {
192
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
193
      int object = play.getObject();
194
      int size   = play.getSize();
195

    
196
      Bundle bundle = new Bundle();
197
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
198

    
199
      RubikDialogScores scores = new RubikDialogScores();
200
      scores.setArguments(bundle);
201
      scores.show(getSupportFragmentManager(), null);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    public void About(View v)
207
      {
208
      RubikDialogAbout about = new RubikDialogAbout();
209
      about.show(getSupportFragmentManager(), null);
210
      }
211

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

    
214
    public void Scramble(View v)
215
      {
216
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
217
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
218
      int scramble = play.getPicker();
219
      view.getRenderer().scrambleObject(scramble);
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    public void Solve(View v)
225
      {
226
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
227
      view.getRenderer().solveObject();
228
      }
229
}
(1-1/3)