Project

General

Profile

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

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

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 4888e97c Leszek Koltunski
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 0c52af30 Leszek Koltunski
      }
84
    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
    @Override
88
    protected void onDestroy() 
89
      {
90 e1111500 Leszek Koltunski
      DistortedLibrary.onDestroy();
91 0c52af30 Leszek Koltunski
      super.onDestroy();
92
      }
93
94 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
    @Override
97
    public void onClick(View v)
98
      {
99
      int id = v.getId();
100
101 4888e97c Leszek Koltunski
      if( id>=0 && id< RubikObjectList.getTotal() )
102 f3e12931 Leszek Koltunski
        {
103 4888e97c Leszek Koltunski
        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 f3e12931 Leszek Koltunski
109
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
110 4888e97c Leszek Koltunski
        boolean success = view.getRenderer().createObject(obj,objectSize);
111 f3e12931 Leszek Koltunski
112
        if( success )
113
          {
114 211b48f2 Leszek Koltunski
          RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
115 4888e97c Leszek Koltunski
          play.markButton(this,object,size);
116 f3e12931 Leszek Koltunski
          }
117
        }
118 4235de9b Leszek Koltunski
119 211b48f2 Leszek Koltunski
      if( id == RubikStateAbstract.BUTTON_ID_BACK )
120 4235de9b Leszek Koltunski
        {
121 211b48f2 Leszek Koltunski
        RubikState.goBack(this);
122 4235de9b Leszek Koltunski
        }
123 33499c56 Leszek Koltunski
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
    private void savePreferences()
128
      {
129
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
130
      SharedPreferences.Editor editor = preferences.edit();
131
132 211b48f2 Leszek Koltunski
      for (int i=0; i<BaseEffect.Type.LENGTH; i++)
133 33499c56 Leszek Koltunski
        {
134
        BaseEffect.Type.getType(i).savePreferences(editor);
135
        }
136
137 211b48f2 Leszek Koltunski
      for (int i=0; i<RubikState.LENGTH; i++)
138
        {
139
        RubikState.getState(i).getStateClass().savePreferences(editor);
140
        }
141 33499c56 Leszek Koltunski
142 211b48f2 Leszek Koltunski
      RubikState.savePreferences(editor);
143 0333d81e Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
144
      view.getRenderer().savePreferences(editor);
145
146 33499c56 Leszek Koltunski
      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 4235de9b Leszek Koltunski
        {
157 33499c56 Leszek Koltunski
        BaseEffect.Type.getType(i).restorePreferences(preferences);
158 4235de9b Leszek Koltunski
        }
159 33499c56 Leszek Koltunski
160 211b48f2 Leszek Koltunski
      for (int i=0; i< RubikState.LENGTH; i++)
161
        {
162
        RubikState.getState(i).getStateClass().restorePreferences(preferences);
163
        }
164 33499c56 Leszek Koltunski
165 211b48f2 Leszek Koltunski
      RubikState.restorePreferences(preferences);
166 0333d81e Leszek Koltunski
167
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
168
      view.getRenderer().restorePreferences(preferences);
169 f3e12931 Leszek Koltunski
      }
170
171 34747dd1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
172 47ba5ddc Leszek Koltunski
// PUBLIC API
173 4235de9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
    public void Play(View v)
176
      {
177 211b48f2 Leszek Koltunski
      RubikState.switchState(this,RubikState.PLAY);
178 4235de9b Leszek Koltunski
      }
179
180 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182 a47ee432 Leszek Koltunski
    public void Settings(View v)
183 0c52af30 Leszek Koltunski
      {
184 0fd3ac1f Leszek Koltunski
      RubikDialogSettings settings = new RubikDialogSettings();
185 f6fcf06a Leszek Koltunski
      settings.show(getSupportFragmentManager(), null);
186
      }
187
188 a7a7cc9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190
    public void Scores(View v)
191
      {
192 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay) RubikState.PLAY.getStateClass();
193 4888e97c Leszek Koltunski
      int object = play.getObject();
194
      int size   = play.getSize();
195 211b48f2 Leszek Koltunski
196 32f60dec Leszek Koltunski
      Bundle bundle = new Bundle();
197 4888e97c Leszek Koltunski
      bundle.putInt("tab", RubikObjectList.pack(object,size) );
198 4918f19c Leszek Koltunski
199 0fd3ac1f Leszek Koltunski
      RubikDialogScores scores = new RubikDialogScores();
200 32f60dec Leszek Koltunski
      scores.setArguments(bundle);
201 a7a7cc9c Leszek Koltunski
      scores.show(getSupportFragmentManager(), null);
202
      }
203
204 1cbcc6bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206 5560eea9 Leszek Koltunski
    public void About(View v)
207 1cbcc6bf Leszek Koltunski
      {
208 0fd3ac1f Leszek Koltunski
      RubikDialogAbout about = new RubikDialogAbout();
209 5560eea9 Leszek Koltunski
      about.show(getSupportFragmentManager(), null);
210 1cbcc6bf Leszek Koltunski
      }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214
    public void Scramble(View v)
215
      {
216
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
217 211b48f2 Leszek Koltunski
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
218
      int scramble = play.getPicker();
219 27a70eae Leszek Koltunski
      view.getRenderer().scrambleObject(scramble);
220 1cbcc6bf Leszek Koltunski
      }
221
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224 2ecf0c21 Leszek Koltunski
    public void Solve(View v)
225 1cbcc6bf Leszek Koltunski
      {
226 f548942f Leszek Koltunski
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
227 27a70eae Leszek Koltunski
      view.getRenderer().solveObject();
228 1cbcc6bf Leszek Koltunski
      }
229 0c52af30 Leszek Koltunski
}