Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ c3ffcf58

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 c3ffcf58 Leszek Koltunski
import org.distorted.scores.RubikScores;
35 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScoresDownloader;
36 27a70eae Leszek Koltunski
import org.distorted.object.RubikObjectList;
37 211b48f2 Leszek Koltunski
import org.distorted.uistate.RubikState;
38
import org.distorted.uistate.RubikStateAbstract;
39
import org.distorted.uistate.RubikStatePlay;
40
41 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43 f3e12931 Leszek Koltunski
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
44 0c52af30 Leszek Koltunski
{
45 c3ffcf58 Leszek Koltunski
    private boolean mJustStarted;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 0c52af30 Leszek Koltunski
    @Override
50 34747dd1 Leszek Koltunski
    protected void onCreate(Bundle savedState)
51 0c52af30 Leszek Koltunski
      {
52 34747dd1 Leszek Koltunski
      super.onCreate(savedState);
53 f6fcf06a Leszek Koltunski
      setTheme(R.style.CustomActivityThemeNoActionBar);
54 1cbcc6bf Leszek Koltunski
      setContentView(R.layout.main);
55 c3ffcf58 Leszek Koltunski
56
      mJustStarted = true;
57 0c52af30 Leszek Koltunski
      }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onPause() 
63
      {
64 4235de9b Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
65 0c52af30 Leszek Koltunski
      view.onPause();
66 e1111500 Leszek Koltunski
      DistortedLibrary.onPause();
67 f3e12931 Leszek Koltunski
      RubikScoresDownloader.onPause();
68 33499c56 Leszek Koltunski
      savePreferences();
69 0c52af30 Leszek Koltunski
      super.onPause();
70
      }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onResume() 
76
      {
77
      super.onResume();
78 4235de9b Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
79 0c52af30 Leszek Koltunski
      view.onResume();
80 33499c56 Leszek Koltunski
      restorePreferences();
81 211b48f2 Leszek Koltunski
      RubikState.setState(this);
82 27a70eae Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
83 4888e97c Leszek Koltunski
84 c3ffcf58 Leszek Koltunski
      if( mJustStarted )
85
        {
86
        mJustStarted = false;
87
        RubikScores.getInstance().incrementNumRuns();
88
        }
89
90 4888e97c Leszek Koltunski
      int object = play.getObject();
91
      int size   = play.getSize();
92
      RubikObjectList obj = RubikObjectList.getObject(object);
93
      int objectSize = obj.getSizes()[size];
94
95
      view.getRenderer().createObject( obj, objectSize );
96 0c52af30 Leszek Koltunski
      }
97
    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
    @Override
101
    protected void onDestroy() 
102
      {
103 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
104 0c52af30 Leszek Koltunski
      super.onDestroy();
105
      }
106
107 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
    @Override
110
    public void onClick(View v)
111
      {
112
      int id = v.getId();
113
114 4888e97c Leszek Koltunski
      if( id>=0 && id< RubikObjectList.getTotal() )
115 f3e12931 Leszek Koltunski
        {
116 4888e97c Leszek Koltunski
        int object = RubikObjectList.unpackObject(id);
117
        int size= RubikObjectList.unpackSize(id);
118
119
        RubikObjectList obj = RubikObjectList.getObject(object);
120
        int objectSize = obj.getSizes()[size];
121 f3e12931 Leszek Koltunski
122
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
123 4888e97c Leszek Koltunski
        boolean success = view.getRenderer().createObject(obj,objectSize);
124 f3e12931 Leszek Koltunski
125
        if( success )
126
          {
127 211b48f2 Leszek Koltunski
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
128 4888e97c Leszek Koltunski
          play.markButton(this,object,size);
129 f3e12931 Leszek Koltunski
          }
130
        }
131 4235de9b Leszek Koltunski
132 211b48f2 Leszek Koltunski
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
133 4235de9b Leszek Koltunski
        {
134 211b48f2 Leszek Koltunski
        RubikState.goBack(this);
135 4235de9b Leszek Koltunski
        }
136 33499c56 Leszek Koltunski
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
    private void savePreferences()
141
      {
142
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
143
      SharedPreferences.Editor editor = preferences.edit();
144
145 211b48f2 Leszek Koltunski
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
146 33499c56 Leszek Koltunski
        {
147
        BaseEffect.Type.getType(i).savePreferences(editor);
148
        }
149
150 211b48f2 Leszek Koltunski
      for (int i=0; i<RubikState.LENGTH; i++)
151
        {
152
        RubikState.getState(i).getStateClass().savePreferences(editor);
153
        }
154 33499c56 Leszek Koltunski
155 211b48f2 Leszek Koltunski
      RubikState.savePreferences(editor);
156 0333d81e Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
157
      view.getRenderer().savePreferences(editor);
158
159 33499c56 Leszek Koltunski
      editor.apply();
160
      }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164
    private void restorePreferences()
165
      {
166
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
167
168
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
169 4235de9b Leszek Koltunski
        {
170 33499c56 Leszek Koltunski
        BaseEffect.Type.getType(i).restorePreferences(preferences);
171 4235de9b Leszek Koltunski
        }
172 33499c56 Leszek Koltunski
173 211b48f2 Leszek Koltunski
      for (int i=0; i< RubikState.LENGTH; i++)
174
        {
175
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
176
        }
177 33499c56 Leszek Koltunski
178 211b48f2 Leszek Koltunski
      RubikState.restorePreferences(preferences);
179 0333d81e Leszek Koltunski
180
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
181
      view.getRenderer().restorePreferences(preferences);
182 f3e12931 Leszek Koltunski
      }
183
184 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
185 47ba5ddc Leszek Koltunski
// PUBLIC API
186 4235de9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
    public void Play(View v)
189
      {
190 211b48f2 Leszek Koltunski
      RubikState.switchState(this,RubikState.PLAY);
191 4235de9b Leszek Koltunski
      }
192
193 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195 a47ee432 Leszek Koltunski
    public void Settings(View v)
196 0c52af30 Leszek Koltunski
      {
197 0fd3ac1f Leszek Koltunski
      RubikDialogSettings settings = new RubikDialogSettings();
198 f6fcf06a Leszek Koltunski
      settings.show(getSupportFragmentManager(), null);
199
      }
200
201 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
    public void Scores(View v)
204
      {
205 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
206 4888e97c Leszek Koltunski
      int object = play.getObject();
207
      int size   = play.getSize();
208 211b48f2 Leszek Koltunski
209 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
210 4888e97c Leszek Koltunski
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
211 4918f19c Leszek Koltunski
212 0fd3ac1f Leszek Koltunski
      RubikDialogScores scores = new RubikDialogScores();
213 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
214 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
215
      }
216
217 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219 5560eea9 Leszek Koltunski
    public void About(View v)
220 1cbcc6bf Leszek Koltunski
      {
221 0fd3ac1f Leszek Koltunski
      RubikDialogAbout about = new RubikDialogAbout();
222 5560eea9 Leszek Koltunski
      about.show(getSupportFragmentManager(), null);
223 1cbcc6bf Leszek Koltunski
      }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
    public void Scramble(View v)
228
      {
229
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
230 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
231
      int scramble = play.getPicker();
232 27a70eae Leszek Koltunski
      view.getRenderer().scrambleObject(scramble);
233 1cbcc6bf Leszek Koltunski
      }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
238 1cbcc6bf Leszek Koltunski
      {
239 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
240 27a70eae Leszek Koltunski
      view.getRenderer().solveObject();
241 1cbcc6bf Leszek Koltunski
      }
242 0c52af30 Leszek Koltunski
}