-
Notifications
You must be signed in to change notification settings - Fork 3
/
randomize.pde
153 lines (124 loc) · 4.57 KB
/
randomize.pde
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
// =============================================================================
// Globals, logic, and event handlers related to randomizing values
// =============================================================================
// Manager =====================================================================
public class RandomizeManager {
// TODO: more informative names now that these are public
// If true, randomize the corresponding settings
public boolean src, targ, xShift, yShift;
// Maximum percent channels can be shifted
public int xShiftMax, yShiftMax;
public RandomizeManager() {
src = targ = xShift = yShift = true;
xShiftMax = yShiftMax = 100;
}
// Getter/Setter Methods
// Source channel
public void toggleSource(boolean val) { src = val; }
// Target channel
public void toggleTarget(boolean val) { targ = val; }
// Either channel (let ChannelManager determine which one(s))
public boolean randomizeChannel() { return src || targ; }
// Horizontal shift
public void toggleXShift(boolean val) { xShift = val; }
// Horizontal shift max percent
public void setXShiftMaxPercent(int percent) {
xShiftMax = getPercentWithinBounds(percent);
}
// Vertical shift
public void toggleYShift(boolean val) { yShift = val; }
// Vertical shift max percent
public void setYShiftMaxPercent(int percent) {
yShiftMax = getPercentWithinBounds(percent);
}
// Conditional max shift getter/setters
public void setShiftMaxPercent(int percent, boolean horizontal) {
if (horizontal)
setXShiftMaxPercent(percent);
else
setYShiftMaxPercent(percent);
}
public int shiftMaxPercent(boolean horizontal) {
return horizontal ? xShiftMax : yShiftMax;
}
// Helpers
int getPercentWithinBounds(int percent) {
if (percent > 100)
percent = 100;
else if (percent < 0)
percent = 0;
return percent;
}
}
// Event Handlers ==============================================================
// Randomize Button ------------------------------------------------------------
/**
* Calls the randomize() methods of managers based on the configuration of
* randomizeManager. Updates GUI on change
*/
void randomizeValues() {
// Channels
if (randomizeManager.randomizeChannel()) {
channelManager.randomize(randomizeManager.src, randomizeManager.targ);
updateChannelToggles();
}
// Shift
if (randomizeManager.xShift) {
xShiftManager.randomize(randomizeManager.xShiftMax);
updateShiftSlider(true);
}
if (randomizeManager.yShift) {
yShiftManager.randomize(randomizeManager.yShiftMax);
updateShiftSlider(false);
}
}
public void randomizeBtn_click(GButton source, GEvent event) {
randomizeValues();
showPreview();
}
// Randomize Checkboxes --------------------------------------------------------
public void randSrcCheckbox_click(GCheckbox source, GEvent event) {
randomizeManager.toggleSource(source.isSelected());
}
public void randTargCheckbox_click(GCheckbox source, GEvent event) {
randomizeManager.toggleTarget(source.isSelected());
}
// TODO: enable/disable corresponding input
public void randXShiftCheckbox_click(GCheckbox source, GEvent event) {
randomizeManager.toggleXShift(source.isSelected());
}
// TODO: enable/disable corresponding input
public void randYShiftCheckbox_click(GCheckbox source, GEvent event) {
randomizeManager.toggleYShift(source.isSelected());
}
// Max Shift Percent Inputs ----------------------------------------------------
// TODO: doc, merge common w/ slider inputs
void randMaxInputEventHandler(GTextField source, GEvent event, boolean horizontal) {
switch(event) {
case ENTERED:
// Unfocus on enter, then do same actions as LOST_FOCUS case
source.setFocus(false);
case LOST_FOCUS:
// Sanitize and update manager
// NOTE: RandomizeManager percent setter methods ensure the value is
// between 0 and 100
int val = sanitizeIntegerInputValue(source);
if (val > -1) {
randomizeManager.setShiftMaxPercent(val, horizontal);
}
// Update input text to match sanitized input (after RandomizeManager
// ensures it's a valid percentage)
// Also reverts input text in the event that it was not a valid numeric
// value after parsing
source.setText("" + randomizeManager.shiftMaxPercent(horizontal));
break;
default:
break;
}
}
public void randXMaxInput_change(GTextField source, GEvent event) {
randMaxInputEventHandler(source, event, true);
}
public void randYMaxInput_change(GTextField source, GEvent event) {
randMaxInputEventHandler(source, event, false);
}