Send normalized values back to APC40
[SugarCubes.git] / _Presets.pde
1 interface PresetListener {
2 public void onPresetSelected(LXDeck deck, Preset preset);
3 public void onPresetStored(LXDeck deck, Preset preset);
4 public void onPresetDirty(LXDeck deck, Preset preset);
5 }
6
7 class PresetManager {
8
9 public static final int NUM_PRESETS = 8;
10 public static final String FILENAME = "data/presets.txt";
11 public static final String DELIMITER = "\t";
12
13 class DeckState implements LXParameterListener {
14
15 final LXDeck deck;
16 LXPattern selectedPattern = null;
17 Preset selectedPreset = null;
18 boolean isDirty = false;
19
20 DeckState(LXDeck deck) {
21 this.deck = deck;
22 deck.addListener(new LXDeck.AbstractListener() {
23 public void patternDidChange(LXDeck deck, LXPattern pattern) {
24 if (selectedPattern != pattern) {
25 onDirty();
26 }
27 }
28 });
29 }
30
31 private void onSelect(Preset preset, LXPattern pattern) {
32 if ((selectedPattern != pattern) && (selectedPattern != null)) {
33 for (LXParameter p : selectedPattern.getParameters()) {
34 ((LXListenableParameter) p).removeListener(this);
35 }
36 }
37 selectedPreset = preset;
38 selectedPattern = pattern;
39 isDirty = false;
40 for (LXParameter p : pattern.getParameters()) {
41 ((LXListenableParameter) p).addListener(this);
42 }
43 for (PresetListener listener : listeners) {
44 listener.onPresetSelected(deck, preset);
45 }
46 }
47
48 private void onStore(Preset preset, LXPattern pattern) {
49 selectedPreset = preset;
50 selectedPattern = pattern;
51 isDirty = false;
52 for (PresetListener listener : listeners) {
53 listener.onPresetStored(deck, preset);
54 }
55 }
56
57 private void onDirty() {
58 if (selectedPreset != null) {
59 isDirty = true;
60 for (PresetListener listener : listeners) {
61 listener.onPresetDirty(deck, selectedPreset);
62 }
63 }
64 }
65
66 public void onParameterChanged(LXParameter parameter) {
67 onDirty();
68 }
69 }
70
71 private final DeckState[] deckState = new DeckState[lx.engine.getDecks().size()];
72 private final Preset[] presets = new Preset[NUM_PRESETS];
73 private final List<PresetListener> listeners = new ArrayList<PresetListener>();
74
75 PresetManager() {
76 for (int i = 0; i < presets.length; ++i) {
77 presets[i] = new Preset(this, i);
78 }
79 String[] values = loadStrings(FILENAME);
80 if (values == null) {
81 write();
82 } else {
83 int i = 0;
84 for (String serialized : values) {
85 presets[i++].load(serialized);
86 if (i >= NUM_PRESETS) {
87 break;
88 }
89 }
90 }
91 for (LXDeck deck : lx.engine.getDecks()) {
92 deckState[deck.index] = new DeckState(deck);
93 }
94 }
95
96 public void addListener(PresetListener listener) {
97 listeners.add(listener);
98 }
99
100 public void select(LXDeck deck, int index) {
101 presets[index].select(deck);
102 }
103
104 public void store(LXDeck deck, int index) {
105 presets[index].store(deck);
106 }
107
108 public void dirty(LXDeck deck) {
109 deckState[deck.index].onDirty();
110 }
111
112 public void dirty(LXPattern pattern) {
113 dirty(pattern.getDeck());
114 }
115
116 public void onStore(LXDeck deck, Preset preset, LXPattern pattern) {
117 deckState[deck.index].onStore(preset, pattern);
118 }
119
120 public void onSelect(LXDeck deck, Preset preset, LXPattern pattern) {
121 deckState[deck.index].onSelect(preset, pattern);
122 }
123
124 public void write() {
125 String[] lines = new String[NUM_PRESETS];
126 int i = 0;
127 for (Preset preset : presets) {
128 lines[i++] = preset.serialize();
129 }
130 saveStrings(FILENAME, lines);
131 }
132 }
133
134 class Preset {
135
136 final PresetManager manager;
137 final int index;
138
139 String className;
140 final Map<String, Float> parameters = new HashMap<String, Float>();
141
142 Preset(PresetManager manager, int index) {
143 this.manager = manager;
144 this.index = index;
145 }
146
147 public void load(String serialized) {
148 className = null;
149 parameters.clear();
150 try {
151 String[] parts = serialized.split(PresetManager.DELIMITER);
152 className = parts[0];
153 int i = 1;
154 while (i < parts.length - 1) {
155 parameters.put(parts[i], Float.parseFloat(parts[i+1]));
156 i += 2;
157 }
158 } catch (Exception x) {
159 className = null;
160 parameters.clear();
161 }
162 }
163
164 public String serialize() {
165 if (className == null) {
166 return "null";
167 }
168 String val = className + PresetManager.DELIMITER;
169 for (String pKey : parameters.keySet()) {
170 val += pKey + PresetManager.DELIMITER + parameters.get(pKey) + PresetManager.DELIMITER;
171 }
172 return val;
173 }
174
175 public void store(LXDeck deck) {
176 LXPattern pattern = deck.getActivePattern();
177 className = pattern.getClass().getName();
178 parameters.clear();
179 for (LXParameter p : pattern.getParameters()) {
180 parameters.put(p.getLabel(), p.getValuef());
181 }
182 if (pattern instanceof DPat) {
183 DPat dpattern = (DPat) pattern;
184 for (DBool bool : dpattern.bools) {
185 parameters.put(bool.tag, bool.b ? 1.f : 0.f);
186 }
187 for (Pick pick : dpattern.picks) {
188 parameters.put(pick.tag, pick.CurRow + pick.CurCol/100.f);
189 }
190 }
191 manager.write();
192 manager.onStore(deck, this, pattern);
193 }
194
195 public void select(LXDeck deck) {
196 for (LXPattern pattern : deck.getPatterns()) {
197 if (pattern.getClass().getName().equals(className)) {
198 for (String pLabel : parameters.keySet()) {
199 for (LXParameter p : pattern.getParameters()) {
200 if (p.getLabel().equals(pLabel)) {
201 p.setValue(parameters.get(pLabel));
202 }
203 }
204 if (pattern instanceof DPat) {
205 DPat dpattern = (DPat) pattern;
206 for (DBool bool : dpattern.bools) {
207 if (bool.tag.equals(pLabel)) {
208 bool.set(bool.row, bool.col, parameters.get(pLabel) > 0);
209 }
210 }
211 for (Pick pick : dpattern.picks) {
212 if (pick.tag.equals(pLabel)) {
213 float f = parameters.get(pLabel);
214 pick.set((int) floor(f), (int) round((f%1)*100.));
215 }
216 }
217 }
218 }
219 deck.goPattern(pattern);
220 if (pattern instanceof DPat) {
221 ((DPat)pattern).updateLights();
222 }
223 manager.onSelect(deck, this, pattern);
224 break;
225 }
226 }
227 }
228 }
229