-
Notifications
You must be signed in to change notification settings - Fork 3
/
shift.pde
284 lines (241 loc) · 8.96 KB
/
shift.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
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
// =============================================================================
// Globals, logic, and event handlers related to channel shift values
// =============================================================================
// Globals =====================================================================
// Set to true if a slider was changed. Window mouse event listener checks this
// when the mouse is released and updates the preview image. This is to avoid
// re-drawing the preview every time the slider value changes
boolean sliderChanged = false;
// Whether the sliders are using percentages of dimensions or exact pixel
// values. Default is percentages. [0] is x slider and [1] is y slider
boolean[] sliderPercentValue = new boolean[]{true, true};
// Manager =====================================================================
/**
* Manage the state of channel shift values
*/
public class ShiftManager {
// Shift amounts (pixels and percentage)
public int shiftAmount, shiftPercent;
// Corresponding image dimension (used for percent and max calculations)
public int imgDimension;
// True if shifting on x axis, false if on y axis (for stringifying step)
public boolean horizontal;
public ShiftManager(boolean horizontal) {
shiftAmount = shiftPercent = imgDimension = 0;
this.horizontal = horizontal;
}
public ShiftManager(boolean horizontal, int imgDimension) {
this(horizontal);
this.imgDimension = imgDimension;
}
public String stringifyStep() {
String step = "";
// Only show shift if not 0
if (shiftAmount > 0)
step += "-" + (horizontal ? "x" : "y") + shiftAmount;
return step;
}
// Percent/Pixel Conversion
private int shiftPercentToPixels(int percent) {
return (int)(imgDimension * percent / 100);
}
private int shiftPixelsToPercent(int amount) {
return (int)(100 * amount / imgDimension);
}
// Getter/Setter Methods
public void setShiftAmount(int amount) {
// Upper bound
if (amount > imgDimension)
amount = imgDimension;
shiftAmount = amount;
shiftPercent = shiftPixelsToPercent(amount);
}
public void setShiftPercent(int percent) {
// Upper bound
if (percent > 100)
percent = 100;
shiftPercent = percent;
shiftAmount = shiftPercentToPixels(percent);
}
public void resetShift() { shiftAmount = shiftPercent = 0; }
// Randomize shift value
public void randomize(int maxPercent) {
setShiftAmount(int(random(imgDimension * maxPercent / 100)));
}
public void randomize() { randomize(100); }
// Return true if shift is 0
public boolean shiftIsZero() { return shiftAmount == 0; }
public void setImgDimension(int dimension) {
// Skip if dimension is unchanged
if (dimension == imgDimension)
return;
imgDimension = dimension;
// Recalculate shift amount based on new dimension
shiftAmount = shiftPercentToPixels(shiftPercent);
}
public int getImgDimension() { return imgDimension; }
}
// Event Handlers ==============================================================
// TODO organize
// Horizontal/Vertical Shift ---------------------------------------------------
/**
* Shorthand function for checking whether a slider is set to use percent value
*/
boolean sliderPercentValue(boolean horizontal) {
return sliderPercentValue[horizontal ? 0 : 1];
}
/**
* Set whether a shift slider is using a percentage or exact pixel values.
* Converts the existing value of the slider accordingly
* @param horizontal If true, set horizontal slider, else set vertical slider
* @param setPercentValue If true, use a percentage for the slider, else use
* exact pixel values
*/
void setSliderValueType(boolean horizontal, boolean setPercentValue) {
int configIndex = horizontal ? 0 : 1;
// Don't update if nothing changed
if (sliderPercentValue(horizontal) == setPercentValue)
return;
ShiftManager manager = horizontal ? xShiftManager : yShiftManager;
GSlider target = horizontal ? xSlider : ySlider;
int updatedValue, upperBound;
if (setPercentValue) {
updatedValue = manager.shiftPercent;
upperBound = 100;
} else {
updatedValue = manager.shiftAmount;
upperBound = manager.getImgDimension();
}
// Set bounds and current value
target.setLimits(updatedValue, 0, upperBound);
// Update text input
updateShiftSliderInput(horizontal);
// Update globals
sliderPercentValue[configIndex] = setPercentValue;
}
/**
* Set horizontal or vertical shift
* @param horizontal If true, set horizontal shift, else set vertical shift
* @param shiftAmount Number of pixels to shift by. If the specified slider is
* set to use percent values, it will be converted
*/
void setShift(boolean horizontal, int shiftAmount) {
ShiftManager manager = horizontal ? xShiftManager : yShiftManager;
if (sliderPercentValue(horizontal))
manager.setShiftPercent(shiftAmount);
else
manager.setShiftAmount(shiftAmount);
}
/**
* Update the shift slider value to match the corresponding global variable
* @param horizontal If true, set horizontal shift, else set vertical shift
*/
void updateShiftSlider(boolean horizontal) {
GSlider slider = horizontal ? xSlider : ySlider;
GTextField sliderInput = horizontal ? xSliderInput : ySliderInput;
ShiftManager manager = horizontal ? xShiftManager : yShiftManager;
boolean percentValue = sliderPercentValue(horizontal);
int val = percentValue ? manager.shiftPercent : manager.shiftAmount;
// TODO: extract upperBound calc to method and update usages?
int upperBound = percentValue ? 100 : manager.getImgDimension();
slider.setLimits(val, 0, upperBound);
// Update text input
updateShiftSliderInput(horizontal);
}
/**
* Update the x/y shift slider values to match global variables. Wrapper that
* calls updateShiftSlider() for both horizontal and vertical
*/
void updateShiftSliders() {
updateShiftSlider(true);
updateShiftSlider(false);
}
public void xSlider_change(GSlider source, GEvent event) {
setShift(true, source.getValueI());
xSliderInput.setText("" + source.getValueI());
sliderChanged = true;
}
public void ySlider_change(GSlider source, GEvent event) {
setShift(false, source.getValueI());
ySliderInput.setText("" + source.getValueI());
sliderChanged = true;
}
// Listens for mouse events and updates preview if a slider was changed
public void controlsWindow_mouse(PApplet appc, GWinData data, MouseEvent event) {
switch(event.getAction()) {
case MouseEvent.RELEASE:
// Update preview if a slider value was changed
if (sliderChanged) {
sliderChanged = false;
showPreview();
}
break;
default:
break;
}
}
// Slider Text Inputs ----------------------------------------------------------
// TODO: walk thru what's getting updated and pulling values and reduce any redundancy
/**
* Event handler for slider inputs
* @param source The GTextField object
* @param event The GEvent fired
* @param horizontal true if horizontal shift input, false if vertical shift
* input
*/
void sliderInputEventHandler(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 slider
// NOTE: setShift() wraps manager methods that will handle val > upper
// bound of slider. updateShiftSlider() will call
// updateShiftSliderInput() after setting the slider to match the
// ShiftManager, so we don't have to worry about going over the bounds at
// this time
int val = sanitizeIntegerInputValue(source);
if (val > -1) {
setShift(horizontal, val);
updateShiftSlider(horizontal);
showPreview();
} else {
// Set to match slider if empty
updateShiftSliderInput(horizontal);
}
break;
default:
break;
}
}
/**
* Update slider input text to match slider value
* @param horizontal true if horizontal shift input, false if vertical shift
* input
*/
void updateShiftSliderInput(boolean horizontal) {
GTextField input = horizontal ? xSliderInput : ySliderInput;
GSlider slider = horizontal ? xSlider : ySlider;
input.setText("" + slider.getValueI());
}
public void xSliderInput_change(GTextField source, GEvent event) {
sliderInputEventHandler(source, event, true);
}
public void ySliderInput_change(GTextField source, GEvent event) {
sliderInputEventHandler(source, event, false);
}
// Slider Toggles --------------------------------------------------------------
// TODO: rename related items to indicate that this sets the units of the sliders?
public void xSliderPercent_clicked(GOption source, GEvent event) {
setSliderValueType(true, true);
}
public void xSliderPixels_clicked(GOption source, GEvent event) {
setSliderValueType(true, false);
}
public void ySliderPercent_clicked(GOption source, GEvent event) {
setSliderValueType(false, true);
}
public void ySliderPixels_clicked(GOption source, GEvent event) {
setSliderValueType(false, false);
}