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.opengl.GLSurfaceView;
|
24
|
import android.os.Bundle;
|
25
|
import android.preference.PreferenceManager;
|
26
|
import android.support.v7.app.AppCompatActivity;
|
27
|
import android.view.View;
|
28
|
|
29
|
import org.distorted.component.HorizontalNumberPicker;
|
30
|
import org.distorted.library.main.DistortedLibrary;
|
31
|
import org.distorted.effect.BaseEffect;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
public class RubikActivity extends AppCompatActivity implements View.OnClickListener
|
36
|
{
|
37
|
public static final int MIN_SCRAMBLE = 1;
|
38
|
public static final int DEF_SCRAMBLE = 1;
|
39
|
public static final int MAX_SCRAMBLE = 18;
|
40
|
|
41
|
private HorizontalNumberPicker mPicker;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
private void savePreferences()
|
46
|
{
|
47
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
48
|
SharedPreferences.Editor editor = preferences.edit();
|
49
|
|
50
|
for (int i=0; i< BaseEffect.Type.LENGTH; i++)
|
51
|
{
|
52
|
BaseEffect.Type.getType(i).savePreferences(editor);
|
53
|
}
|
54
|
|
55
|
editor.putInt("scramble", mPicker.getValue() );
|
56
|
|
57
|
editor.apply();
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
private void restorePreferences()
|
63
|
{
|
64
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
65
|
|
66
|
for (int i=0; i< BaseEffect.Type.LENGTH; i++)
|
67
|
{
|
68
|
BaseEffect.Type.getType(i).restorePreferences(preferences);
|
69
|
}
|
70
|
|
71
|
int scramble= preferences.getInt("scramble", DEF_SCRAMBLE);
|
72
|
|
73
|
mPicker.setValue(scramble);
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
@Override
|
79
|
protected void onCreate(Bundle savedState)
|
80
|
{
|
81
|
super.onCreate(savedState);
|
82
|
setTheme(R.style.CustomActivityThemeNoActionBar);
|
83
|
setContentView(R.layout.main);
|
84
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
85
|
view.addSizeButtons(this);
|
86
|
view.markButton(RubikSurfaceView.getRedButton());
|
87
|
|
88
|
mPicker = findViewById(R.id.rubikNumberPicker);
|
89
|
mPicker.setMin(MIN_SCRAMBLE);
|
90
|
mPicker.setMax(MAX_SCRAMBLE);
|
91
|
|
92
|
restorePreferences();
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
@Override
|
98
|
protected void onPause()
|
99
|
{
|
100
|
GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
101
|
view.onPause();
|
102
|
DistortedLibrary.onPause();
|
103
|
RubikScoresDownloader.onPause();
|
104
|
savePreferences();
|
105
|
super.onPause();
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
@Override
|
111
|
protected void onResume()
|
112
|
{
|
113
|
super.onResume();
|
114
|
GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
115
|
view.onResume();
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
@Override
|
121
|
protected void onDestroy()
|
122
|
{
|
123
|
DistortedLibrary.onDestroy();
|
124
|
super.onDestroy();
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
@Override
|
130
|
public void onClick(View v)
|
131
|
{
|
132
|
int id = v.getId();
|
133
|
|
134
|
if( id>=0 && id<RubikSize.LENGTH )
|
135
|
{
|
136
|
int size = RubikSize.getSize(id).getCubeSize();
|
137
|
|
138
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
139
|
boolean success = view.getRenderer().createCube(size);
|
140
|
|
141
|
if( success )
|
142
|
{
|
143
|
view.markButton(id);
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
// PUBLIC API
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
public void Settings(View v)
|
153
|
{
|
154
|
RubikSettings settings = new RubikSettings();
|
155
|
settings.show(getSupportFragmentManager(), null);
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void Scores(View v)
|
161
|
{
|
162
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
163
|
Bundle bundle = new Bundle();
|
164
|
bundle.putInt("tab", view.getRedButton());
|
165
|
|
166
|
RubikScores scores = new RubikScores();
|
167
|
scores.setArguments(bundle);
|
168
|
scores.show(getSupportFragmentManager(), null);
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
public void About(View v)
|
174
|
{
|
175
|
RubikAbout about = new RubikAbout();
|
176
|
about.show(getSupportFragmentManager(), null);
|
177
|
}
|
178
|
|
179
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180
|
|
181
|
public void Scramble(View v)
|
182
|
{
|
183
|
int scramble = mPicker.getValue();
|
184
|
|
185
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
186
|
view.getRenderer().scrambleCube(scramble);
|
187
|
view.enterScrambleMode();
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
public void Solve(View v)
|
193
|
{
|
194
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
195
|
view.getRenderer().solveCube();
|
196
|
view.leaveScrambleMode();
|
197
|
}
|
198
|
}
|