From: Mark Slee Date: Tue, 22 Oct 2013 01:05:38 +0000 (-0700) Subject: New preset listening code and keyboard buttons for presets X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2b068dd5d83e070da10318a9ab4017a7ce86d2ca;p=SugarCubes.git New preset listening code and keyboard buttons for presets --- diff --git a/_Internals.pde b/_Internals.pde index 2e91381..79d12e5 100644 --- a/_Internals.pde +++ b/_Internals.pde @@ -140,7 +140,6 @@ void setup() { // MIDI devices midiEngine = new MidiEngine(); - presetManager.setMidiEngine(midiEngine); logTime("Setup MIDI devices"); // Build output driver @@ -425,6 +424,44 @@ void keyPressed() { mappingTool.keyPressed(uiMapping); } switch (key) { + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + if (!midiEngine.isQwertyEnabled()) { + presetManager.select(midiEngine.getFocusedDeck(), key - '1'); + } + break; + + case '!': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0); + break; + case '@': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1); + break; + case '#': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2); + break; + case '$': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3); + break; + case '%': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4); + break; + case '^': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5); + break; + case '&': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6); + break; + case '*': + if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7); + break; + case '-': case '_': frameRate(--targetFramerate); diff --git a/_MIDI.pde b/_MIDI.pde index 8f71457..5747a1f 100644 --- a/_MIDI.pde +++ b/_MIDI.pde @@ -677,6 +677,9 @@ class APC40MidiOutput implements LXParameter.Listener, GridOutput { midiEngine.addListener(new MidiEngineListener() { public void onFocusedDeck(int deckIndex) { resetPatternParameters(); + for (int i = 0; i < 8; ++i) { + output.sendNoteOn(i, 52, 0); + } } }); } @@ -687,7 +690,9 @@ class APC40MidiOutput implements LXParameter.Listener, GridOutput { }); LXDeck.Listener deckListener = new LXDeck.AbstractListener() { public void patternDidChange(LXDeck deck, LXPattern pattern) { - resetPatternParameters(); + if (deck == getTargetDeck()) { + resetPatternParameters(); + } } }; for (LXDeck d : lx.engine.getDecks()) { @@ -696,7 +701,7 @@ class APC40MidiOutput implements LXParameter.Listener, GridOutput { } } presetManager.addListener(new PresetListener() { - public void onPresetLoaded(LXDeck deck, Preset preset) { + public void onPresetSelected(LXDeck deck, Preset preset) { if (deck == getTargetDeck()) { for (int i = 0; i < 8; ++i) { output.sendNoteOn(i, 52, (preset.index == i) ? 1 : 0); @@ -710,12 +715,7 @@ class APC40MidiOutput implements LXParameter.Listener, GridOutput { } public void onPresetStored(LXDeck deck, Preset preset) { if (deck == getTargetDeck()) { - onPresetLoaded(deck, preset); - } - } - public void onPresetUnloaded() { - for (int i = 0; i < 8; ++i) { - output.sendNoteOn(i, 52, 0); + onPresetStored(deck, preset); } } }); @@ -748,10 +748,7 @@ class APC40MidiOutput implements LXParameter.Listener, GridOutput { } protected LXDeck getTargetDeck() { - if (targetDeck != null) { - return targetDeck; - } - return midiEngine.getFocusedDeck(); + return (targetDeck != null) ? targetDeck : midiEngine.getFocusedDeck(); } private void resetParameters() { diff --git a/_Presets.pde b/_Presets.pde index 2c61125..9d5c305 100644 --- a/_Presets.pde +++ b/_Presets.pde @@ -1,22 +1,77 @@ interface PresetListener { - public void onPresetLoaded(LXDeck deck, Preset preset); - public void onPresetDirty(LXDeck deck, Preset preset); + public void onPresetSelected(LXDeck deck, Preset preset); public void onPresetStored(LXDeck deck, Preset preset); - public void onPresetUnloaded(); + public void onPresetDirty(LXDeck deck, Preset preset); } -class PresetManager implements LXParameter.Listener { +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 LXParameter.Listener { + + 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 listeners = new ArrayList(); - private Preset loadedPreset = null; - private LXPattern loadedPattern = null; - PresetManager() { for (int i = 0; i < presets.length; ++i) { presets[i] = new Preset(this, i); @@ -34,88 +89,38 @@ class PresetManager implements LXParameter.Listener { } } for (LXDeck deck : lx.engine.getDecks()) { - deck.addListener(new LXDeck.AbstractListener() { - public void patternDidChange(LXDeck deck, LXPattern pattern) { - if (pattern != loadedPattern) { - onPresetDirty(deck); - } - } - }); + deckState[deck.index] = new DeckState(deck); } } - public void setMidiEngine(MidiEngine midiEngine) { - midiEngine.addListener(new MidiEngineListener() { - public void onFocusedDeck(int deckIndex) { - loadedPreset = null; - for (PresetListener listener : listeners) { - listener.onPresetUnloaded(); - } - } - }); - } - public void addListener(PresetListener listener) { listeners.add(listener); } - public LXDeck deckForPattern(LXPattern pattern) { - for (LXDeck deck : lx.engine.getDecks()) { - for (LXPattern p : deck.getPatterns()) { - if (p == pattern) { - return deck; - } - } - } - return null; - } - - public void dirty(LXPattern pattern) { - onPresetDirty(deckForPattern(pattern)); - } - public void select(LXDeck deck, int index) { presets[index].select(deck); } public void store(LXDeck deck, int index) { - presets[index].store(midiEngine.getFocusedPattern()); - for (PresetListener listener : listeners) { - listener.onPresetStored(deck, presets[index]); - } - select(deck, index); + presets[index].store(deck); } - public void onPresetLoaded(LXDeck deck, Preset preset, LXPattern pattern) { - if (loadedPattern != pattern) { - if (loadedPattern != null) { - for (LXParameter p : loadedPattern.getParameters()) { - ((LXListenableParameter) p).removeListener(this); - } - } - } - for (PresetListener listener : listeners) { - listener.onPresetLoaded(deck, preset); - } - loadedPreset = preset; - loadedPattern = pattern; - for (LXParameter p : loadedPattern.getParameters()) { - ((LXListenableParameter) p).addListener(this); - } + public void dirty(LXDeck deck) { + deckState[deck.index].onDirty(); } - private void onPresetDirty(LXDeck deck) { - if (loadedPreset != null) { - for (PresetListener listener : listeners) { - listener.onPresetDirty(deck, loadedPreset); - } - } + public void dirty(LXPattern pattern) { + dirty(pattern.getDeck()); } - - public void onParameterChanged(LXParameter p) { - onPresetDirty(deckForPattern(loadedPattern)); + + 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; @@ -167,10 +172,10 @@ class Preset { return val; } - public void store(LXPattern pattern) { - className = null; - parameters.clear(); + 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()); } @@ -184,6 +189,7 @@ class Preset { } } manager.write(); + manager.onStore(deck, this, pattern); } public void select(LXDeck deck) { @@ -214,10 +220,10 @@ class Preset { if (pattern instanceof DPat) { ((DPat)pattern).updateLights(); } - manager.onPresetLoaded(deck, this, pattern); + manager.onSelect(deck, this, pattern); break; } - } + } } } diff --git a/code/HeronLX.jar b/code/HeronLX.jar index eea9627..c407dc8 100755 Binary files a/code/HeronLX.jar and b/code/HeronLX.jar differ