Commit | Line | Data |
---|---|---|
e0794d3a | 1 | interface PresetListener { |
2b068dd5 | 2 | public void onPresetSelected(LXDeck deck, Preset preset); |
42a424d7 | 3 | public void onPresetStored(LXDeck deck, Preset preset); |
2b068dd5 | 4 | public void onPresetDirty(LXDeck deck, Preset preset); |
e0794d3a MS |
5 | } |
6 | ||
2b068dd5 | 7 | class PresetManager { |
e0794d3a MS |
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 | ||
b8bb2748 | 13 | class DeckState implements LXParameterListener { |
2b068dd5 MS |
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()]; | |
e0794d3a MS |
72 | private final Preset[] presets = new Preset[NUM_PRESETS]; |
73 | private final List<PresetListener> listeners = new ArrayList<PresetListener>(); | |
74 | ||
e0794d3a MS |
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 | } | |
42a424d7 | 91 | for (LXDeck deck : lx.engine.getDecks()) { |
2b068dd5 | 92 | deckState[deck.index] = new DeckState(deck); |
1f974cbc MS |
93 | } |
94 | } | |
95 | ||
e0794d3a MS |
96 | public void addListener(PresetListener listener) { |
97 | listeners.add(listener); | |
98 | } | |
a8d1f57a | 99 | |
42a424d7 | 100 | public void select(LXDeck deck, int index) { |
f6394aaa | 101 | presets[index].select(deck); |
e0794d3a MS |
102 | } |
103 | ||
42a424d7 | 104 | public void store(LXDeck deck, int index) { |
2b068dd5 | 105 | presets[index].store(deck); |
e0794d3a MS |
106 | } |
107 | ||
2b068dd5 MS |
108 | public void dirty(LXDeck deck) { |
109 | deckState[deck.index].onDirty(); | |
c1d777c9 MS |
110 | } |
111 | ||
2b068dd5 MS |
112 | public void dirty(LXPattern pattern) { |
113 | dirty(pattern.getDeck()); | |
e0794d3a | 114 | } |
2b068dd5 MS |
115 | |
116 | public void onStore(LXDeck deck, Preset preset, LXPattern pattern) { | |
117 | deckState[deck.index].onStore(preset, pattern); | |
1f974cbc MS |
118 | } |
119 | ||
2b068dd5 MS |
120 | public void onSelect(LXDeck deck, Preset preset, LXPattern pattern) { |
121 | deckState[deck.index].onSelect(preset, pattern); | |
122 | } | |
123 | ||
e0794d3a MS |
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 | ||
2b068dd5 MS |
175 | public void store(LXDeck deck) { |
176 | LXPattern pattern = deck.getActivePattern(); | |
e0794d3a | 177 | className = pattern.getClass().getName(); |
2b068dd5 | 178 | parameters.clear(); |
e0794d3a MS |
179 | for (LXParameter p : pattern.getParameters()) { |
180 | parameters.put(p.getLabel(), p.getValuef()); | |
181 | } | |
a1396b9e MS |
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 | } | |
e0794d3a | 191 | manager.write(); |
2b068dd5 | 192 | manager.onStore(deck, this, pattern); |
e0794d3a MS |
193 | } |
194 | ||
42a424d7 | 195 | public void select(LXDeck deck) { |
e0794d3a MS |
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 | } | |
a1396b9e MS |
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 | } | |
e0794d3a MS |
218 | } |
219 | deck.goPattern(pattern); | |
a1396b9e MS |
220 | if (pattern instanceof DPat) { |
221 | ((DPat)pattern).updateLights(); | |
222 | } | |
2b068dd5 | 223 | manager.onSelect(deck, this, pattern); |
e0794d3a MS |
224 | break; |
225 | } | |
2b068dd5 | 226 | } |
e0794d3a MS |
227 | } |
228 | } | |
229 |