Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikActivity.java @ 33499c56

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.v4.app.FragmentManager;
26
import android.support.v7.app.AppCompatActivity;
27
import android.view.View;
28

    
29
import org.distorted.effect.BaseEffect;
30
import org.distorted.library.main.DistortedLibrary;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
35
{
36
    RubikState mCurrentState;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
    @Override
41
    protected void onCreate(Bundle savedState)
42
      {
43
      super.onCreate(savedState);
44
      setTheme(R.style.CustomActivityThemeNoActionBar);
45
      setContentView(R.layout.main);
46

    
47
      if( savedState==null )
48
        {
49
        RubikDialogMain diag = new RubikDialogMain();
50
        diag.show(getSupportFragmentManager(), RubikDialogMain.getDialogTag() );
51
        }
52
      }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
    
56
    @Override
57
    protected void onPause() 
58
      {
59
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
60
      view.onPause();
61
      DistortedLibrary.onPause();
62
      RubikScoresDownloader.onPause();
63
      savePreferences();
64
      super.onPause();
65
      }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
    
69
    @Override
70
    protected void onResume() 
71
      {
72
      super.onResume();
73
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
74
      view.onResume();
75
      restorePreferences();
76
      view.enterState(this, mCurrentState);
77
      }
78
    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
    
81
    @Override
82
    protected void onDestroy() 
83
      {
84
      DistortedLibrary.onDestroy();
85
      super.onDestroy();
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
    @Override
91
    public void onClick(View v)
92
      {
93
      int id = v.getId();
94

    
95
      if( id>=0 && id<RubikSize.LENGTH )
96
        {
97
        int size = RubikSize.getSize(id).getCubeSize();
98

    
99
        RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
100
        boolean success = view.getRenderer().createCube(size);
101

    
102
        if( success )
103
          {
104
          view.markButton(id);
105
          }
106
        }
107

    
108
      if( id == RubikSurfaceView.BUTTON_ID_BACK )
109
        {
110
        mCurrentState = mCurrentState.getBack();
111

    
112
        if( mCurrentState!=null )
113
          {
114
          RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
115
          view.enterState(this, mCurrentState);
116

    
117
          RubikDialogMain diag = new RubikDialogMain();
118
          diag.show(getSupportFragmentManager(), RubikDialogMain.getDialogTag() );
119
          }
120
        else
121
          {
122
          mCurrentState = RubikState.MAIN;
123
          finish();
124
          }
125
        }
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    private void savePreferences()
131
      {
132
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
133
      SharedPreferences.Editor editor = preferences.edit();
134

    
135
      for (int i = 0; i< BaseEffect.Type.LENGTH; i++)
136
        {
137
        BaseEffect.Type.getType(i).savePreferences(editor);
138
        }
139

    
140
      editor.putInt("state", mCurrentState.ordinal() );
141

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

    
145
      editor.apply();
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

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

    
159
      int stateOrdinal = preferences.getInt("state", RubikState.MAIN.ordinal() );
160
      mCurrentState = RubikState.getState(stateOrdinal);
161

    
162
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
163
      view.restorePreferences(preferences);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// PUBLIC API
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    public void Play(View v)
171
      {
172
      FragmentManager mana = getSupportFragmentManager();
173
      RubikDialogMain diag = (RubikDialogMain) mana.findFragmentByTag(RubikDialogMain.getDialogTag());
174

    
175
      if( diag!=null )
176
        {
177
        diag.dismiss();
178
        }
179
      else
180
        {
181
        android.util.Log.e("act", "cannot find main dialog!");
182
        }
183

    
184
      mCurrentState = RubikState.PLAY;
185
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
186
      view.enterState(this,mCurrentState);
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public void Settings(View v)
192
      {
193
      RubikDialogSettings settings = new RubikDialogSettings();
194
      settings.show(getSupportFragmentManager(), null);
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    public void Scores(View v)
200
      {
201
      Bundle bundle = new Bundle();
202
      bundle.putInt("tab", RubikSurfaceView.getRedButton());
203

    
204
      RubikDialogScores scores = new RubikDialogScores();
205
      scores.setArguments(bundle);
206
      scores.show(getSupportFragmentManager(), null);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    public void About(View v)
212
      {
213
      RubikDialogAbout about = new RubikDialogAbout();
214
      about.show(getSupportFragmentManager(), null);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    public void Scramble(View v)
220
      {
221
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
222
      view.scramble();
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    public void Solve(View v)
228
      {
229
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
230
      view.getRenderer().solveCube();
231
      }
232
}
(1-1/12)