Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 4c0cd600

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 4c0cd600 Leszek Koltunski
import org.distorted.dialog.RubikDialogEffects;
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 4888e97c Leszek Koltunski
83 c3ffcf58 Leszek Koltunski
      if( mJustStarted )
84
        {
85
        mJustStarted = false;
86 82ce8e64 Leszek Koltunski
        RubikScores scores = RubikScores.getInstance();
87
        scores.incrementNumRuns();
88
        scores.setCountry(this);
89 c3ffcf58 Leszek Koltunski
        }
90
91 e41e7dc3 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
92 4888e97c Leszek Koltunski
      int object = play.getObject();
93
      int size   = play.getSize();
94
95 4c0cd600 Leszek Koltunski
      if( object>=0 && object<RubikObjectList.NUM_OBJECTS )
96
        {
97
        RubikObjectList obj = RubikObjectList.getObject(object);
98
        int[] sizes = obj.getSizes();
99
100
        if( size>=0 && size<sizes.length )
101
          {
102
          view.getRenderer().createObject( obj, sizes[size] );
103
          }
104
        }
105 0c52af30 Leszek Koltunski
      }
106
    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    @Override
110
    protected void onDestroy() 
111
      {
112 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
113 0c52af30 Leszek Koltunski
      super.onDestroy();
114
      }
115
116 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
    @Override
119
    public void onClick(View v)
120
      {
121 4c0cd600 Leszek Koltunski
      switch(v.getId())
122 f3e12931 Leszek Koltunski
        {
123 4c0cd600 Leszek Koltunski
        case RubikStateAbstract.BUTTON_ID_OBJECT: RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
124
                                                  play.bringUpPopup(this,v);
125
                                                  break;
126
        case RubikStateAbstract.BUTTON_ID_BACK:   RubikState.goBack(this);
127
                                                  break;
128
        }
129
      }
130 4888e97c Leszek Koltunski
131 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132 f3e12931 Leszek Koltunski
133 4c0cd600 Leszek Koltunski
    public void changeObject(int object, int size)
134
      {
135
      RubikObjectList obj = RubikObjectList.getObject(object);
136
      int objectSize = obj.getSizes()[size];
137 f3e12931 Leszek Koltunski
138 4c0cd600 Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
139
      view.getRenderer().createObject(obj,objectSize);
140
      }
141 4235de9b Leszek Koltunski
142 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
    public boolean isVertical()
145
      {
146
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
147
      return view.isVertical();
148 33499c56 Leszek Koltunski
      }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152
    private void savePreferences()
153
      {
154
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
155
      SharedPreferences.Editor editor = preferences.edit();
156
157 211b48f2 Leszek Koltunski
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
158 33499c56 Leszek Koltunski
        {
159
        BaseEffect.Type.getType(i).savePreferences(editor);
160
        }
161
162 211b48f2 Leszek Koltunski
      for (int i=0; i<RubikState.LENGTH; i++)
163
        {
164
        RubikState.getState(i).getStateClass().savePreferences(editor);
165
        }
166 33499c56 Leszek Koltunski
167 211b48f2 Leszek Koltunski
      RubikState.savePreferences(editor);
168 0333d81e Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
169
      view.getRenderer().savePreferences(editor);
170
171 33499c56 Leszek Koltunski
      editor.apply();
172
      }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
176
    private void restorePreferences()
177
      {
178
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
179
180
      for (int i=0; i< BaseEffect.Type.LENGTH; i++)
181 4235de9b Leszek Koltunski
        {
182 33499c56 Leszek Koltunski
        BaseEffect.Type.getType(i).restorePreferences(preferences);
183 4235de9b Leszek Koltunski
        }
184 33499c56 Leszek Koltunski
185 211b48f2 Leszek Koltunski
      for (int i=0; i< RubikState.LENGTH; i++)
186
        {
187
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
188
        }
189 33499c56 Leszek Koltunski
190 211b48f2 Leszek Koltunski
      RubikState.restorePreferences(preferences);
191 0333d81e Leszek Koltunski
192
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
193
      view.getRenderer().restorePreferences(preferences);
194 f3e12931 Leszek Koltunski
      }
195
196 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
197 47ba5ddc Leszek Koltunski
// PUBLIC API
198 4235de9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200
    public void Play(View v)
201
      {
202 211b48f2 Leszek Koltunski
      RubikState.switchState(this,RubikState.PLAY);
203 4235de9b Leszek Koltunski
      }
204
205 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207 a47ee432 Leszek Koltunski
    public void Settings(View v)
208 0c52af30 Leszek Koltunski
      {
209 4c0cd600 Leszek Koltunski
      RubikDialogEffects settings = new RubikDialogEffects();
210 f6fcf06a Leszek Koltunski
      settings.show(getSupportFragmentManager(), null);
211
      }
212
213 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
    public void Scores(View v)
216
      {
217 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
218 4888e97c Leszek Koltunski
      int object = play.getObject();
219
      int size   = play.getSize();
220 211b48f2 Leszek Koltunski
221 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
222 4888e97c Leszek Koltunski
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
223 4895fff6 Leszek Koltunski
      bundle.putBoolean("submitting", false);
224 4918f19c Leszek Koltunski
225 0fd3ac1f Leszek Koltunski
      RubikDialogScores scores = new RubikDialogScores();
226 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
227 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
228
      }
229
230 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
231
232 5560eea9 Leszek Koltunski
    public void About(View v)
233 1cbcc6bf Leszek Koltunski
      {
234 0fd3ac1f Leszek Koltunski
      RubikDialogAbout about = new RubikDialogAbout();
235 5560eea9 Leszek Koltunski
      about.show(getSupportFragmentManager(), null);
236 1cbcc6bf Leszek Koltunski
      }
237
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
    public void Scramble(View v)
241
      {
242
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
243 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
244
      int scramble = play.getPicker();
245 27a70eae Leszek Koltunski
      view.getRenderer().scrambleObject(scramble);
246 1cbcc6bf Leszek Koltunski
      }
247
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
251 1cbcc6bf Leszek Koltunski
      {
252 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
253 27a70eae Leszek Koltunski
      view.getRenderer().solveObject();
254 1cbcc6bf Leszek Koltunski
      }
255 0c52af30 Leszek Koltunski
}