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