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);
9 public static final int NUM_PRESETS = 8;
10 public static final String FILENAME = "data/presets.txt";
11 public static final String DELIMITER = "\t";
13 class DeckState implements LXParameterListener {
16 LXPattern selectedPattern = null;
17 Preset selectedPreset = null;
18 boolean isDirty = false;
20 DeckState(LXDeck deck) {
22 deck.addListener(new LXDeck.AbstractListener() {
23 public void patternDidChange(LXDeck deck, LXPattern pattern) {
24 if (selectedPattern != pattern) {
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);
37 selectedPreset = preset;
38 selectedPattern = pattern;
40 for (LXParameter p : pattern.getParameters()) {
41 ((LXListenableParameter) p).addListener(this);
43 for (PresetListener listener : listeners) {
44 listener.onPresetSelected(deck, preset);
48 private void onStore(Preset preset, LXPattern pattern) {
49 selectedPreset = preset;
50 selectedPattern = pattern;
52 for (PresetListener listener : listeners) {
53 listener.onPresetStored(deck, preset);
57 private void onDirty() {
58 if (selectedPreset != null) {
60 for (PresetListener listener : listeners) {
61 listener.onPresetDirty(deck, selectedPreset);
66 public void onParameterChanged(LXParameter parameter) {
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>();
76 for (int i = 0; i < presets.length; ++i) {
77 presets[i] = new Preset(this, i);
79 String[] values = loadStrings(FILENAME);
84 for (String serialized : values) {
85 presets[i++].load(serialized);
86 if (i >= NUM_PRESETS) {
91 for (LXDeck deck : lx.engine.getDecks()) {
92 deckState[deck.index] = new DeckState(deck);
96 public void addListener(PresetListener listener) {
97 listeners.add(listener);
100 public void select(LXDeck deck, int index) {
101 presets[index].select(deck);
104 public void store(LXDeck deck, int index) {
105 presets[index].store(deck);
108 public void dirty(LXDeck deck) {
109 deckState[deck.index].onDirty();
112 public void dirty(LXPattern pattern) {
113 dirty(pattern.getDeck());
116 public void onStore(LXDeck deck, Preset preset, LXPattern pattern) {
117 deckState[deck.index].onStore(preset, pattern);
120 public void onSelect(LXDeck deck, Preset preset, LXPattern pattern) {
121 deckState[deck.index].onSelect(preset, pattern);
124 public void write() {
125 String[] lines = new String[NUM_PRESETS];
127 for (Preset preset : presets) {
128 lines[i++] = preset.serialize();
130 saveStrings(FILENAME, lines);
136 final PresetManager manager;
140 final Map<String, Float> parameters = new HashMap<String, Float>();
142 Preset(PresetManager manager, int index) {
143 this.manager = manager;
147 public void load(String serialized) {
151 String[] parts = serialized.split(PresetManager.DELIMITER);
152 className = parts[0];
154 while (i < parts.length - 1) {
155 parameters.put(parts[i], Float.parseFloat(parts[i+1]));
158 } catch (Exception x) {
164 public String serialize() {
165 if (className == null) {
168 String val = className + PresetManager.DELIMITER;
169 for (String pKey : parameters.keySet()) {
170 val += pKey + PresetManager.DELIMITER + parameters.get(pKey) + PresetManager.DELIMITER;
175 public void store(LXDeck deck) {
176 LXPattern pattern = deck.getActivePattern();
177 className = pattern.getClass().getName();
179 for (LXParameter p : pattern.getParameters()) {
180 parameters.put(p.getLabel(), p.getValuef());
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);
187 for (Pick pick : dpattern.picks) {
188 parameters.put(pick.tag, pick.CurRow + pick.CurCol/100.f);
192 manager.onStore(deck, this, pattern);
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));
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);
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.));
219 deck.goPattern(pattern);
220 if (pattern instanceof DPat) {
221 ((DPat)pattern).updateLights();
223 manager.onSelect(deck, this, pattern);