Project

General

Profile

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

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

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.magic;
21
22 33499c56 Leszek Koltunski
import android.content.SharedPreferences;
23 0c52af30 Leszek Koltunski
import android.os.Bundle;
24 33499c56 Leszek Koltunski
import android.preference.PreferenceManager;
25 f6fcf06a Leszek Koltunski
import android.support.v7.app.AppCompatActivity;
26 0c52af30 Leszek Koltunski
import android.view.View;
27
28 211b48f2 Leszek Koltunski
import org.distorted.dialog.RubikDialogAbout;
29
import org.distorted.dialog.RubikDialogScores;
30
import org.distorted.dialog.RubikDialogSettings;
31 33499c56 Leszek Koltunski
import org.distorted.effect.BaseEffect;
32 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 0c52af30 Leszek Koltunski
34 4f9f99a2 Leszek Koltunski
import org.distorted.network.RubikScoresDownloader;
35 27a70eae Leszek Koltunski
import org.distorted.object.RubikObjectList;
36 211b48f2 Leszek Koltunski
import org.distorted.uistate.RubikState;
37
import org.distorted.uistate.RubikStateAbstract;
38
import org.distorted.uistate.RubikStatePlay;
39
40 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 f3e12931 Leszek Koltunski
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
43 0c52af30 Leszek Koltunski
{
44
    @Override
45 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
46 0c52af30 Leszek Koltunski
      {
47 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
48 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
49 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
50 0c52af30 Leszek Koltunski
      }
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onPause() 
56
      {
57 4235de9b Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
58 0c52af30 Leszek Koltunski
      view.onPause();
59 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
60 f3e12931 Leszek Koltunski
      RubikScoresDownloader.onPause();
61 33499c56 Leszek Koltunski
      savePreferences();
62 0c52af30 Leszek Koltunski
      super.onPause();
63
      }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
    
67
    @Override
68
    protected void onResume() 
69
      {
70
      super.onResume();
71 4235de9b Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
72 0c52af30 Leszek Koltunski
      view.onResume();
73 33499c56 Leszek Koltunski
      restorePreferences();
74 211b48f2 Leszek Koltunski
      RubikState.setState(this);
75 27a70eae Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
76
      int ordinal = play.getButton();
77
      view.getRenderer().createObject(RubikObjectList.getObject(ordinal));
78 0c52af30 Leszek Koltunski
      }
79
    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
    
82
    @Override
83
    protected void onDestroy() 
84
      {
85 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
86 0c52af30 Leszek Koltunski
      super.onDestroy();
87
      }
88
89 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
    @Override
92
    public void onClick(View v)
93
      {
94
      int id = v.getId();
95
96 27a70eae Leszek Koltunski
      if( id>=0 && id< RubikObjectList.LENGTH )
97 f3e12931 Leszek Koltunski
        {
98 27a70eae Leszek Koltunski
        RubikObjectList object = RubikObjectList.getObject(id);
99 f3e12931 Leszek Koltunski
100
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
101 27a70eae Leszek Koltunski
        boolean success = view.getRenderer().createObject(object);
102 f3e12931 Leszek Koltunski
103
        if( success )
104
          {
105 211b48f2 Leszek Koltunski
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
106
          play.markButton(this,id);
107 f3e12931 Leszek Koltunski
          }
108
        }
109 4235de9b Leszek Koltunski
110 211b48f2 Leszek Koltunski
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
111 4235de9b Leszek Koltunski
        {
112 211b48f2 Leszek Koltunski
        RubikState.goBack(this);
113 4235de9b Leszek Koltunski
        }
114 33499c56 Leszek Koltunski
      }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
    private void savePreferences()
119
      {
120
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
121
      SharedPreferences.Editor editor = preferences.edit();
122
123 211b48f2 Leszek Koltunski
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
124 33499c56 Leszek Koltunski
        {
125
        BaseEffect.Type.getType(i).savePreferences(editor);
126
        }
127
128 211b48f2 Leszek Koltunski
      for (int i=0; i<RubikState.LENGTH; i++)
129
        {
130
        RubikState.getState(i).getStateClass().savePreferences(editor);
131
        }
132 33499c56 Leszek Koltunski
133 211b48f2 Leszek Koltunski
      RubikState.savePreferences(editor);
134 0333d81e Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
135
      view.getRenderer().savePreferences(editor);
136
137 33499c56 Leszek Koltunski
      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 4235de9b Leszek Koltunski
        {
148 33499c56 Leszek Koltunski
        BaseEffect.Type.getType(i).restorePreferences(preferences);
149 4235de9b Leszek Koltunski
        }
150 33499c56 Leszek Koltunski
151 211b48f2 Leszek Koltunski
      for (int i=0; i< RubikState.LENGTH; i++)
152
        {
153
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
154
        }
155 33499c56 Leszek Koltunski
156 211b48f2 Leszek Koltunski
      RubikState.restorePreferences(preferences);
157 0333d81e Leszek Koltunski
158
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
159
      view.getRenderer().restorePreferences(preferences);
160 f3e12931 Leszek Koltunski
      }
161
162 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
163 47ba5ddc Leszek Koltunski
// PUBLIC API
164 4235de9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
    public void Play(View v)
167
      {
168 211b48f2 Leszek Koltunski
      RubikState.switchState(this,RubikState.PLAY);
169 4235de9b Leszek Koltunski
      }
170
171 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173 a47ee432 Leszek Koltunski
    public void Settings(View v)
174 0c52af30 Leszek Koltunski
      {
175 0fd3ac1f Leszek Koltunski
      RubikDialogSettings settings = new RubikDialogSettings();
176 f6fcf06a Leszek Koltunski
      settings.show(getSupportFragmentManager(), null);
177
      }
178
179 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
    public void Scores(View v)
182
      {
183 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
184
      int tab = play.getButton();
185
186 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
187 211b48f2 Leszek Koltunski
      bundle.putInt("tab", tab);
188 4918f19c Leszek Koltunski
189 0fd3ac1f Leszek Koltunski
      RubikDialogScores scores = new RubikDialogScores();
190 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
191 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
192
      }
193
194 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196 5560eea9 Leszek Koltunski
    public void About(View v)
197 1cbcc6bf Leszek Koltunski
      {
198 0fd3ac1f Leszek Koltunski
      RubikDialogAbout about = new RubikDialogAbout();
199 5560eea9 Leszek Koltunski
      about.show(getSupportFragmentManager(), null);
200 1cbcc6bf Leszek Koltunski
      }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204
    public void Scramble(View v)
205
      {
206
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
207 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
208
      int scramble = play.getPicker();
209 27a70eae Leszek Koltunski
      view.getRenderer().scrambleObject(scramble);
210 1cbcc6bf Leszek Koltunski
      }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
215 1cbcc6bf Leszek Koltunski
      {
216 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
217 27a70eae Leszek Koltunski
      view.getRenderer().solveObject();
218 1cbcc6bf Leszek Koltunski
      }
219 0c52af30 Leszek Koltunski
}