-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreferencesActivity.java
360 lines (295 loc) · 11.5 KB
/
PreferencesActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.dpanayotov.gameoflife.preferences;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import com.azeesoft.lib.colorpicker.ColorPickerDialog;
import com.dpanayotov.gameoflife.R;
import com.dpanayotov.gameoflife.life.Life;
import com.dpanayotov.gameoflife.preferences.custom.ColorNamesAdapter;
import com.dpanayotov.gameoflife.preferences.custom.ColorValuesAdapter;
import com.dpanayotov.gameoflife.preferences.custom.SwitchPreference;
import com.dpanayotov.gameoflife.preferences.custom.ValueSetSeekBarPreference;
import com.dpanayotov.gameoflife.util.Resolution;
import com.dpanayotov.gameoflife.util.ScreenUtil;
import com.woxthebox.draglistview.DragListView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by Dean Panayotov on 9/24/2016
*/
public class PreferencesActivity extends Activity implements SurfaceHolder.Callback {
@BindView(R.id.grid_width_height)
ValueSetSeekBarPreference<Resolution> gridWidthHeight;
@BindView(R.id.cell_size)
ValueSetSeekBarPreference<Integer> cellSize;
@BindView(R.id.tick_rate)
ValueSetSeekBarPreference<Integer> tickRate;
@BindView(R.id.isometric_projection)
SwitchPreference isometricProjection;
@BindView(R.id.highlife)
SwitchPreference highlife;
@BindView(R.id.initial_population_density)
ValueSetSeekBarPreference<Integer> initialPopulationDensity;
@BindView(R.id.min_population_density)
ValueSetSeekBarPreference<Integer> minPopulationDensity;
@BindView(R.id.surface_view)
SurfaceView surfaceView;
@BindView(R.id.progress_overlay)
View progressOverlay;
//-------------
@BindView(R.id.list_color_names)
RecyclerView listColorNames;
@BindView(R.id.list_color_values)
DragListView listColorValues;
ColorValuesAdapter colorValuesAdapter;
List<Pair<Integer, Integer>> colorValues;
private Life life;
private int canvasWidth;
private int canvasHeight;
ColorValuesAdapter.OnItemClickedListener onItemClickedListener = new ColorValuesAdapter
.OnItemClickedListener() {
@Override
public void onItemClicked(int position) {
showColorPickerDialog(Preferences.Color.values()[position]);
}
};
private void calculateDimensions() {
Rect surfaceFrame = surfaceView.getHolder().getSurfaceFrame();
canvasWidth = surfaceFrame.width();
canvasHeight = surfaceFrame.height();
}
private void initDemo() {
if (life != null) {
life.destroy();
}
life = new Life(canvasWidth, canvasHeight, surfaceView.getHolder(), true);
life.start();
}
private void calculateAndInitDemo() {
calculateDimensions();
initDemo();
}
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
ButterKnife.bind(this);
List<Resolution> resolutions = ScreenUtil.getAvailableResolutions();
List<Integer> cellSizes = new ArrayList<>();
for (Resolution resolution : resolutions) {
cellSizes.add(resolution.cellSize);
}
gridWidthHeight.setValues(resolutions);
cellSize.setValues(cellSizes);
cellSize.setPosition(Preferences.getResolution());
cellSize.setOnValueChangeListener(new ValueSetSeekBarPreference
.OnValueChangeListener<Integer>() {
@Override
public void onValueChange(Integer value, int position) {
gridWidthHeight.setPosition(position);
Preferences.setResolution(position);
calculateAndInitDemo();
}
});
tickRate.setValues(Preferences.getTickRates());
tickRate.setPosition(Preferences.getTickRate());
tickRate.setOnValueChangeListener(new ValueSetSeekBarPreference
.OnValueChangeListener<Integer>() {
@Override
public void onValueChange(Integer value, int position) {
Preferences.setTickRate(position);
initDemo();
}
});
isometricProjection.setChecked(Preferences.getIsometricProjection());
isometricProjection.setOnCheckedChangeListener(new SwitchPreference.OnCheckedChangeListener
() {
@Override
public void onCheckedChanged(boolean checked) {
Preferences.setIsometricProjection(checked);
initDemo();
}
});
highlife.setChecked(Preferences.getHighlife());
highlife.setOnCheckedChangeListener(new SwitchPreference.OnCheckedChangeListener
() {
@Override
public void onCheckedChanged(boolean checked) {
Preferences.setHighlife(checked);
initDemo();
}
});
initialPopulationDensity.setValues(Preferences.getInitialPopulationDensityOptions());
initialPopulationDensity.setPosition(Preferences.getInitialPopulationDensity());
initialPopulationDensity.setOnValueChangeListener(new ValueSetSeekBarPreference
.OnValueChangeListener<Integer>() {
@Override
public void onValueChange(Integer value, int position) {
Preferences.setInitialPopulationDensity(position);
initDemo();
}
});
minPopulationDensity.setValues(Preferences.getMinPopulationDensityOptions());
minPopulationDensity.setPosition(Preferences.getMinPopulationDensity());
minPopulationDensity.setOnValueChangeListener(new ValueSetSeekBarPreference
.OnValueChangeListener<Integer>() {
@Override
public void onValueChange(Integer value, int position) {
Preferences.setMinPopulationDensity(position);
initDemo();
}
});
surfaceView.getHolder().addCallback(this);
listColorNames.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager
.VERTICAL, false));
listColorNames.setAdapter(new ColorNamesAdapter());
listColorValues.setLayoutManager(new LinearLayoutManager(this));
listColorValues.setCanDragHorizontally(false);
listColorValues.setDragListListener(new DragListView.DragListListener() {
@Override
public void onItemDragStarted(int position) {
}
@Override
public void onItemDragging(int itemPosition, float x, float y) {
}
@Override
public void onItemDragEnded(int fromPosition, int toPosition) {
if (fromPosition != toPosition) {
colorItemDragged(fromPosition, toPosition);
colorValues = constructColorValues();
initDemo();
}
}
});
initFadeAnimations();
initColorValues();
}
private void initColorValues() {
colorValues = constructColorValues();
colorValuesAdapter = new ColorValuesAdapter(new ArrayList<>(colorValues),
onItemClickedListener);
listColorValues.post(new Runnable() {
@Override
public void run() {
fadeColorsList(false);
}
});
listColorValues.setAdapter(colorValuesAdapter, true);
}
private void colorItemDragged(int from, int to) {
setColorValueByPosition(to, from);
if (from < to) {
for (int i = from; i < to; i++) {
setColorValueByPosition(i, i + 1);
}
} else {
for (int i = to + 1; i <= from; i++) {
setColorValueByPosition(i, i - 1);
}
}
}
private void setColorValueByPosition(int target, int source) {
Preferences.setColor(Preferences.Color.values()[target], colorValues.get(source).second);
}
private List<Pair<Integer, Integer>> constructColorValues() {
Preferences.Color[] colors = Preferences.Color.values();
List<Pair<Integer, Integer>> pairs = new ArrayList<>();
for (int i = 0; i < colors.length; i++) {
pairs.add(new Pair<>(i, Preferences.getColor(colors[i])));
}
return pairs;
}
private void showColorPickerDialog(final Preferences.Color color) {
progressOverlay.setVisibility(View.VISIBLE);
ColorPickerDialog colorPickerDialog = ColorPickerDialog.createColorPickerDialog(this);
colorPickerDialog.hideOpacityBar();
colorPickerDialog.setOnColorPickedListener(new ColorPickerDialog.OnColorPickedListener() {
@Override
public void onColorPicked(int value, String hexVal) {
Preferences.setColor(color, value);
updateColors();
initDemo();
}
});
colorPickerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
progressOverlay.setVisibility(View.INVISIBLE);
}
});
colorPickerDialog.setInitialColor(Preferences.getColor(color));
colorPickerDialog.show();
}
private Animation fadeIn;
private Animation fadeOut;
private static final long FADE_ANIMATION_DURATION = 500;
private void initFadeAnimations() {
fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(1000);
fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(FADE_ANIMATION_DURATION);
fadeOut.setDuration(FADE_ANIMATION_DURATION);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
initColorValues();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void fadeColorsList(boolean out) {
listColorValues.startAnimation(out ? fadeOut : fadeIn);
}
private void updateColors() {
fadeColorsList(true);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
calculateAndInitDemo();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
life.destroy();
}
@Override
protected void onStop() {
super.onStop();
if (life != null) {
life.stop();
}
}
@Override
protected void onStart() {
super.onStart();
if (life != null) {
life.start();
}
}
}