forked from connordelacruz/ChannelShiftGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.pde
120 lines (93 loc) · 3.33 KB
/
channels.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
// =============================================================================
// Globals, logic, and event handlers related to source/target color channels
// =============================================================================
// Constants ===================================================================
// Maps index 0-2 to corresponding color channel. Used as a shorthand when
// making operations more human readable
String[] CHANNELS = new String[]{"R","G","B"};
// Manager Class ===============================================================
/**
* Manage the state of selected color channels
*/
public class ChannelManager {
// Source and target channels
public int sourceChannel, targetChannel;
public ChannelManager() {
sourceChannel = targetChannel = 0;
}
public String stringifyStep() {
String step = CHANNELS[sourceChannel];
// Only show what channel was shifted if not swapped
if (sourceChannel != targetChannel)
step += CHANNELS[targetChannel];
return step;
}
// Getter/Setter Methods
public void setSourceChannel(int channel) {
sourceChannel = channel;
}
public void setTargetChannel(int channel) {
targetChannel = channel;
}
public void setChannel(boolean source, int channel) {
if (source)
sourceChannel = channel;
else
targetChannel = channel;
}
public int getChannel(boolean source) {
return source ? sourceChannel : targetChannel;
}
public void setChannels(int source, int target) {
sourceChannel = source;
targetChannel = target;
}
public int[] getChannels() {
return new int[]{ sourceChannel, targetChannel };
}
// Set target to match source (i.e. don't swap)
public void linkTargetToSource() { targetChannel = sourceChannel; }
// Return true if channels match
public boolean channelsMatch() { return sourceChannel == targetChannel; }
// Reset channels to default
public void resetChannels() { sourceChannel = targetChannel = 0; }
// Randomize channels
public void randomize(boolean source, boolean target) {
if (source)
sourceChannel = int(random(3));
if (target)
targetChannel = int(random(3));
}
}
// Event Handlers ==============================================================
// Source/Target Channel -------------------------------------------------------
/**
* Select a source/target channel toggle
* @param source If true, set sourceChannel, else set targetChannel
* @param channel Channel to set (Index into CHANNELS)
*/
void setChannelToggle(boolean source, int channel) {
ChannelOption toggle = source ? srcToggles[channel] : targToggles[channel];
toggle.setSelected(true);
}
/**
* Update the selected source/target channel toggle to match the corresponding
* global variable
* @param source If true, set sourceChannel, else set targetChannel
*/
void updateChannelToggle(boolean source) {
setChannelToggle(source, channelManager.getChannel(source));
}
/**
* Update the selected source and target channel toggles to match global
* variables. Wrapper that calls updateChannelToggle() for both source and
* target
*/
void updateChannelToggles() {
updateChannelToggle(true);
updateChannelToggle(false);
}
public void channelOption_clicked(ChannelOption source, GEvent event) {
channelManager.setChannel(source.isSource(), source.getChannel());
showPreview();
}