forked from visigoth/SugarCubes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_Presets.pde
229 lines (201 loc) · 6.25 KB
/
_Presets.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
interface PresetListener {
public void onPresetSelected(LXDeck deck, Preset preset);
public void onPresetStored(LXDeck deck, Preset preset);
public void onPresetDirty(LXDeck deck, Preset preset);
}
class PresetManager {
public static final int NUM_PRESETS = 8;
public static final String FILENAME = "data/presets.txt";
public static final String DELIMITER = "\t";
class DeckState implements LXParameterListener {
final LXDeck deck;
LXPattern selectedPattern = null;
Preset selectedPreset = null;
boolean isDirty = false;
DeckState(LXDeck deck) {
this.deck = deck;
deck.addListener(new LXDeck.AbstractListener() {
public void patternDidChange(LXDeck deck, LXPattern pattern) {
if (selectedPattern != pattern) {
onDirty();
}
}
});
}
private void onSelect(Preset preset, LXPattern pattern) {
if ((selectedPattern != pattern) && (selectedPattern != null)) {
for (LXParameter p : selectedPattern.getParameters()) {
((LXListenableParameter) p).removeListener(this);
}
}
selectedPreset = preset;
selectedPattern = pattern;
isDirty = false;
for (LXParameter p : pattern.getParameters()) {
((LXListenableParameter) p).addListener(this);
}
for (PresetListener listener : listeners) {
listener.onPresetSelected(deck, preset);
}
}
private void onStore(Preset preset, LXPattern pattern) {
selectedPreset = preset;
selectedPattern = pattern;
isDirty = false;
for (PresetListener listener : listeners) {
listener.onPresetStored(deck, preset);
}
}
private void onDirty() {
if (selectedPreset != null) {
isDirty = true;
for (PresetListener listener : listeners) {
listener.onPresetDirty(deck, selectedPreset);
}
}
}
public void onParameterChanged(LXParameter parameter) {
onDirty();
}
}
private final DeckState[] deckState = new DeckState[lx.engine.getDecks().size()];
private final Preset[] presets = new Preset[NUM_PRESETS];
private final List<PresetListener> listeners = new ArrayList<PresetListener>();
PresetManager() {
for (int i = 0; i < presets.length; ++i) {
presets[i] = new Preset(this, i);
}
String[] values = loadStrings(FILENAME);
if (values == null) {
write();
} else {
int i = 0;
for (String serialized : values) {
presets[i++].load(serialized);
if (i >= NUM_PRESETS) {
break;
}
}
}
for (LXDeck deck : lx.engine.getDecks()) {
deckState[deck.index] = new DeckState(deck);
}
}
public void addListener(PresetListener listener) {
listeners.add(listener);
}
public void select(LXDeck deck, int index) {
presets[index].select(deck);
}
public void store(LXDeck deck, int index) {
presets[index].store(deck);
}
public void dirty(LXDeck deck) {
deckState[deck.index].onDirty();
}
public void dirty(LXPattern pattern) {
dirty(pattern.getDeck());
}
public void onStore(LXDeck deck, Preset preset, LXPattern pattern) {
deckState[deck.index].onStore(preset, pattern);
}
public void onSelect(LXDeck deck, Preset preset, LXPattern pattern) {
deckState[deck.index].onSelect(preset, pattern);
}
public void write() {
String[] lines = new String[NUM_PRESETS];
int i = 0;
for (Preset preset : presets) {
lines[i++] = preset.serialize();
}
saveStrings(FILENAME, lines);
}
}
class Preset {
final PresetManager manager;
final int index;
String className;
final Map<String, Float> parameters = new HashMap<String, Float>();
Preset(PresetManager manager, int index) {
this.manager = manager;
this.index = index;
}
public void load(String serialized) {
className = null;
parameters.clear();
try {
String[] parts = serialized.split(PresetManager.DELIMITER);
className = parts[0];
int i = 1;
while (i < parts.length - 1) {
parameters.put(parts[i], Float.parseFloat(parts[i+1]));
i += 2;
}
} catch (Exception x) {
className = null;
parameters.clear();
}
}
public String serialize() {
if (className == null) {
return "null";
}
String val = className + PresetManager.DELIMITER;
for (String pKey : parameters.keySet()) {
val += pKey + PresetManager.DELIMITER + parameters.get(pKey) + PresetManager.DELIMITER;
}
return val;
}
public void store(LXDeck deck) {
LXPattern pattern = deck.getActivePattern();
className = pattern.getClass().getName();
parameters.clear();
for (LXParameter p : pattern.getParameters()) {
parameters.put(p.getLabel(), p.getValuef());
}
if (pattern instanceof DPat) {
DPat dpattern = (DPat) pattern;
for (DBool bool : dpattern.bools) {
parameters.put(bool.tag, bool.b ? 1.f : 0.f);
}
for (Pick pick : dpattern.picks) {
parameters.put(pick.tag, pick.CurRow + pick.CurCol/100.f);
}
}
manager.write();
manager.onStore(deck, this, pattern);
}
public void select(LXDeck deck) {
for (LXPattern pattern : deck.getPatterns()) {
if (pattern.getClass().getName().equals(className)) {
for (String pLabel : parameters.keySet()) {
for (LXParameter p : pattern.getParameters()) {
if (p.getLabel().equals(pLabel)) {
p.setValue(parameters.get(pLabel));
}
}
if (pattern instanceof DPat) {
DPat dpattern = (DPat) pattern;
for (DBool bool : dpattern.bools) {
if (bool.tag.equals(pLabel)) {
bool.set(bool.row, bool.col, parameters.get(pLabel) > 0);
}
}
for (Pick pick : dpattern.picks) {
if (pick.tag.equals(pLabel)) {
float f = parameters.get(pLabel);
pick.set((int) floor(f), (int) round((f%1)*100.));
}
}
}
}
deck.goPattern(pattern);
if (pattern instanceof DPat) {
((DPat)pattern).updateLights();
}
manager.onSelect(deck, this, pattern);
break;
}
}
}
}