1 |
0c52af30
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2019 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
ffd744be
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
0c52af30
|
Leszek Koltunski
|
// //
|
6 |
ffd744be
|
Leszek Koltunski
|
// Distorted 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 |
ffd744be
|
Leszek Koltunski
|
// Distorted 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 |
ffd744be
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
0c52af30
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.magic;
|
21 |
|
|
|
22 |
fe381d8e
|
Leszek Koltunski
|
import android.content.SharedPreferences;
|
23 |
0c52af30
|
Leszek Koltunski
|
import android.graphics.PorterDuff;
|
24 |
|
|
import android.graphics.drawable.Drawable;
|
25 |
|
|
import android.opengl.GLSurfaceView;
|
26 |
|
|
import android.os.Bundle;
|
27 |
fe381d8e
|
Leszek Koltunski
|
import android.preference.PreferenceManager;
|
28 |
0c52af30
|
Leszek Koltunski
|
import android.support.v4.content.ContextCompat;
|
29 |
f6fcf06a
|
Leszek Koltunski
|
import android.support.v7.app.AppCompatActivity;
|
30 |
0c52af30
|
Leszek Koltunski
|
import android.view.View;
|
31 |
|
|
|
32 |
b0a2ce63
|
Leszek Koltunski
|
import org.distorted.effect.AppearEffect;
|
33 |
|
|
import org.distorted.effect.DisappearEffect;
|
34 |
e1111500
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedLibrary;
|
35 |
0c52af30
|
Leszek Koltunski
|
|
36 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
|
38 |
f6fcf06a
|
Leszek Koltunski
|
public class RubikActivity extends AppCompatActivity implements RubikSettings.OnCompleteListener
|
39 |
0c52af30
|
Leszek Koltunski
|
{
|
40 |
|
|
static final int DEFAULT_SIZE = 3;
|
41 |
34998c9d
|
Leszek Koltunski
|
private static final int SMALLEST_SIZE = 2;
|
42 |
0c52af30
|
Leszek Koltunski
|
private static final int[] button_ids = {R.id.rubikSize2, R.id.rubikSize3, R.id.rubikSize4};
|
43 |
|
|
|
44 |
1cbcc6bf
|
Leszek Koltunski
|
public static final int DEFAULT_APPEAR_POS = 10;
|
45 |
|
|
public static final int DEFAULT_DISAPPEAR_POS = 10;
|
46 |
|
|
public static final int DEFAULT_APPEAR_TYPE = 1;
|
47 |
|
|
public static final int DEFAULT_DISAPPEAR_TYPE = 1;
|
48 |
fe381d8e
|
Leszek Koltunski
|
|
49 |
ee5c2ae1
|
Leszek Koltunski
|
private static int mSize = DEFAULT_SIZE;
|
50 |
34747dd1
|
Leszek Koltunski
|
|
51 |
f6fcf06a
|
Leszek Koltunski
|
private int mAppearPos, mDisappearPos;
|
52 |
|
|
private int mAppearType, mDisappearType;
|
53 |
|
|
|
54 |
0c52af30
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55 |
|
|
|
56 |
|
|
@Override
|
57 |
34747dd1
|
Leszek Koltunski
|
protected void onCreate(Bundle savedState)
|
58 |
0c52af30
|
Leszek Koltunski
|
{
|
59 |
34747dd1
|
Leszek Koltunski
|
super.onCreate(savedState);
|
60 |
f6fcf06a
|
Leszek Koltunski
|
setTheme(R.style.CustomActivityThemeNoActionBar);
|
61 |
1cbcc6bf
|
Leszek Koltunski
|
setContentView(R.layout.main);
|
62 |
5e307a01
|
Leszek Koltunski
|
markButton(mSize);
|
63 |
f6fcf06a
|
Leszek Koltunski
|
|
64 |
fe381d8e
|
Leszek Koltunski
|
restorePreferences();
|
65 |
|
|
applyPreferences();
|
66 |
0c52af30
|
Leszek Koltunski
|
}
|
67 |
|
|
|
68 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69 |
|
|
|
70 |
|
|
@Override
|
71 |
|
|
protected void onPause()
|
72 |
|
|
{
|
73 |
|
|
GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
74 |
|
|
view.onPause();
|
75 |
e1111500
|
Leszek Koltunski
|
DistortedLibrary.onPause();
|
76 |
fe381d8e
|
Leszek Koltunski
|
savePreferences();
|
77 |
0c52af30
|
Leszek Koltunski
|
super.onPause();
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81 |
|
|
|
82 |
|
|
@Override
|
83 |
|
|
protected void onResume()
|
84 |
|
|
{
|
85 |
|
|
super.onResume();
|
86 |
|
|
GLSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
87 |
|
|
view.onResume();
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91 |
|
|
|
92 |
|
|
@Override
|
93 |
|
|
protected void onDestroy()
|
94 |
|
|
{
|
95 |
e1111500
|
Leszek Koltunski
|
DistortedLibrary.onDestroy();
|
96 |
0c52af30
|
Leszek Koltunski
|
super.onDestroy();
|
97 |
|
|
}
|
98 |
|
|
|
99 |
34747dd1
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100 |
|
|
|
101 |
|
|
static int getSize()
|
102 |
|
|
{
|
103 |
|
|
return mSize;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
0c52af30
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107 |
|
|
|
108 |
a47ee432
|
Leszek Koltunski
|
public void Settings(View v)
|
109 |
0c52af30
|
Leszek Koltunski
|
{
|
110 |
f6fcf06a
|
Leszek Koltunski
|
Bundle args = new Bundle();
|
111 |
|
|
|
112 |
|
|
args.putInt("appearPos" , mAppearPos );
|
113 |
|
|
args.putInt("disappearPos" , mDisappearPos );
|
114 |
|
|
args.putInt("appearType" , mAppearType );
|
115 |
|
|
args.putInt("disappearType", mDisappearType);
|
116 |
|
|
|
117 |
|
|
RubikSettings settings = new RubikSettings();
|
118 |
|
|
settings.setArguments(args);
|
119 |
|
|
settings.show(getSupportFragmentManager(), null);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
1cbcc6bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123 |
|
|
|
124 |
|
|
public void Credits(View v)
|
125 |
|
|
{
|
126 |
|
|
RubikCredits credits = new RubikCredits();
|
127 |
|
|
credits.show(getSupportFragmentManager(), null);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131 |
|
|
|
132 |
|
|
public void Scramble(View v)
|
133 |
|
|
{
|
134 |
|
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
135 |
|
|
view.getRenderer().scrambleCube();
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139 |
|
|
|
140 |
|
|
public void Solve(View v)
|
141 |
|
|
{
|
142 |
|
|
|
143 |
|
|
}
|
144 |
|
|
|
145 |
f6fcf06a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146 |
|
|
|
147 |
|
|
public void onComplete(int aP, int dP, int aT, int dT)
|
148 |
|
|
{
|
149 |
|
|
mAppearPos = aP;
|
150 |
|
|
mDisappearPos = dP;
|
151 |
|
|
mAppearType = aT;
|
152 |
|
|
mDisappearType= dT;
|
153 |
b0a2ce63
|
Leszek Koltunski
|
|
154 |
fe381d8e
|
Leszek Koltunski
|
applyPreferences();
|
155 |
0c52af30
|
Leszek Koltunski
|
}
|
156 |
|
|
|
157 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158 |
|
|
|
159 |
|
|
public void setSize(View v)
|
160 |
|
|
{
|
161 |
|
|
int size=0, id = v.getId();
|
162 |
|
|
|
163 |
|
|
for(int b=0; b<button_ids.length; b++)
|
164 |
|
|
if( button_ids[b] == id )
|
165 |
|
|
{
|
166 |
34998c9d
|
Leszek Koltunski
|
size = b+SMALLEST_SIZE;
|
167 |
0c52af30
|
Leszek Koltunski
|
break;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
171 |
aa8b36aa
|
Leszek Koltunski
|
boolean success = view.getRenderer().createCube(size);
|
172 |
f291130e
|
Leszek Koltunski
|
|
173 |
|
|
if( success )
|
174 |
|
|
{
|
175 |
|
|
markButton(size);
|
176 |
|
|
}
|
177 |
0c52af30
|
Leszek Koltunski
|
}
|
178 |
|
|
|
179 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
180 |
|
|
|
181 |
|
|
private void markButton(int size)
|
182 |
|
|
{
|
183 |
34747dd1
|
Leszek Koltunski
|
mSize = size;
|
184 |
|
|
|
185 |
0c52af30
|
Leszek Koltunski
|
for(int b=0; b<button_ids.length; b++)
|
186 |
|
|
{
|
187 |
|
|
Drawable d = findViewById(button_ids[b]).getBackground();
|
188 |
|
|
|
189 |
34998c9d
|
Leszek Koltunski
|
if( size == b+SMALLEST_SIZE )
|
190 |
0c52af30
|
Leszek Koltunski
|
{
|
191 |
|
|
d.setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.MULTIPLY);
|
192 |
|
|
}
|
193 |
|
|
else
|
194 |
|
|
{
|
195 |
|
|
d.clearColorFilter();
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
fe381d8e
|
Leszek Koltunski
|
|
200 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
201 |
|
|
|
202 |
|
|
private void savePreferences()
|
203 |
|
|
{
|
204 |
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
205 |
|
|
SharedPreferences.Editor editor = preferences.edit();
|
206 |
|
|
|
207 |
|
|
editor.putInt("appearPos" , mAppearPos );
|
208 |
|
|
editor.putInt("disappearPos" , mDisappearPos );
|
209 |
|
|
editor.putInt("appearType" , mAppearType );
|
210 |
|
|
editor.putInt("disappearType", mDisappearType);
|
211 |
|
|
|
212 |
|
|
editor.apply();
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216 |
|
|
|
217 |
|
|
private void restorePreferences()
|
218 |
|
|
{
|
219 |
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
220 |
|
|
|
221 |
|
|
mAppearPos = preferences.getInt("appearPos" , DEFAULT_APPEAR_POS );
|
222 |
|
|
mDisappearPos = preferences.getInt("disappearPos" , DEFAULT_DISAPPEAR_POS );
|
223 |
|
|
mAppearType = preferences.getInt("appearType" , DEFAULT_APPEAR_TYPE );
|
224 |
|
|
mDisappearType = preferences.getInt("disappearType", DEFAULT_DISAPPEAR_TYPE);
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228 |
|
|
|
229 |
|
|
private void applyPreferences()
|
230 |
|
|
{
|
231 |
|
|
RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
|
232 |
|
|
RubikRenderer renderer = view.getRenderer();
|
233 |
|
|
|
234 |
1cbcc6bf
|
Leszek Koltunski
|
renderer.setAppearDuration(translateDuration(mAppearPos)+1);
|
235 |
|
|
renderer.setDisappearDuration(translateDuration(mDisappearPos) +1);
|
236 |
fe381d8e
|
Leszek Koltunski
|
renderer.setAppearType(AppearEffect.getType(mAppearType));
|
237 |
|
|
renderer.setDisappearType(DisappearEffect.getType(mDisappearType));
|
238 |
|
|
}
|
239 |
1cbcc6bf
|
Leszek Koltunski
|
|
240 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241 |
|
|
|
242 |
|
|
public static int translateDuration(int pos)
|
243 |
|
|
{
|
244 |
|
|
return (pos/2)*100;
|
245 |
|
|
}
|
246 |
0c52af30
|
Leszek Koltunski
|
}
|