From d18c35a62adffab9ad28b83a0a2948acb6528d88 Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Thu, 13 Jun 2024 18:47:11 -0400 Subject: [PATCH] improve app/layout state construction, fix inherited state listening --- .../data/application_state.cpp | 30 +++++----- .../data/application_state.h | 11 ++-- .../data/lumatone_state.cpp | 57 +++++++++---------- .../data/lumatone_state.h | 8 +-- 4 files changed, 52 insertions(+), 54 deletions(-) diff --git a/Source/lumatone_editor_library/data/application_state.cpp b/Source/lumatone_editor_library/data/application_state.cpp index aea4c6e..c1915d9 100644 --- a/Source/lumatone_editor_library/data/application_state.cpp +++ b/Source/lumatone_editor_library/data/application_state.cpp @@ -20,8 +20,8 @@ juce::Array getLumatoneApplicationProperties() return properties; } -LumatoneApplicationState::LumatoneApplicationState(juce::String nameIn, LumatoneFirmwareDriver& driverIn, juce::ValueTree stateIn, juce::UndoManager *undoManagerIn) - : LumatoneState(nameIn, stateIn, undoManagerIn) +LumatoneApplicationState::LumatoneApplicationState(juce::ValueTree stateIn, LumatoneFirmwareDriver& driverIn, juce::UndoManager *undoManagerIn) + : LumatoneState(stateIn, undoManagerIn) , firmwareDriver(driverIn) { editorListeners = std::make_shared>(); @@ -37,14 +37,6 @@ LumatoneApplicationState::LumatoneApplicationState(juce::String nameIn, Lumatone loadStateProperties(stateIn); } -// LumatoneApplicationState::LumatoneApplicationState(juce::String nameIn, const LumatoneState &stateIn, juce::UndoManager *undoManagerIn) -// : LumatoneState(nameIn, stateIn, undoManagerIn) -// { -// colourModel = std::make_shared(); -// layoutContext = std::make_shared(*mappingData); -// loadStateProperties(state); -// } - LumatoneApplicationState::LumatoneApplicationState(juce::String nameIn, const LumatoneApplicationState &stateIn) : LumatoneState(nameIn, (const LumatoneState&)stateIn) , firmwareDriver(stateIn.firmwareDriver) @@ -60,6 +52,11 @@ LumatoneApplicationState::LumatoneApplicationState(juce::String nameIn, const Lu loadStateProperties(state); } +LumatoneApplicationState::LumatoneApplicationState(const LumatoneApplicationState &stateIn) + : LumatoneApplicationState(stateIn.name + "Copy", stateIn) +{ +} + LumatoneApplicationState::~LumatoneApplicationState() { layoutContext = nullptr; @@ -158,9 +155,11 @@ void LumatoneApplicationState::setActiveMacroButtonColour(juce::Colour buttonCol juce::ValueTree LumatoneApplicationState::loadStateProperties(juce::ValueTree stateIn) { - juce::ValueTree newState = (stateIn.hasType(LumatoneStateProperty::StateTree)) + juce::ValueTree newState = (stateIn.hasType(LumatoneStateProperty::LumatoneState)) ? stateIn - : juce::ValueTree(LumatoneStateProperty::StateTree); + : juce::ValueTree(LumatoneStateProperty::LumatoneState); + + LumatoneState::loadStateProperties(newState); // DBG("LumatoneApplicationState::loadStateProperties:\n" + newState.toXmlString()); for (auto property : getLumatoneApplicationProperties()) @@ -169,7 +168,8 @@ juce::ValueTree LumatoneApplicationState::loadStateProperties(juce::ValueTree st handleStatePropertyChange(newState, property); } - LumatoneState::loadStateProperties(newState); + // if (name.contains("Copy")) + // DBG(juce::String("Loaded ") + name + juce::String(" properties")); return newState; } @@ -378,7 +378,7 @@ void LumatoneApplicationState::setInvertExpression(bool invert) void LumatoneApplicationState::setInvertSustain(bool invert) { LumatoneState::setInvertSustain(invert); - + if (doSendChangesToDevice()) { controller->invertSustainPedal(invert); @@ -514,7 +514,7 @@ bool LumatoneApplicationState::Controller::requestSettingsFromDevice() // Macro button colours appState.controller->requestMacroButtonColours(); - + // General options appState.controller->requestPresetFlags(); appState.controller->requestExpressionPedalSensitivity(); diff --git a/Source/lumatone_editor_library/data/application_state.h b/Source/lumatone_editor_library/data/application_state.h index 893c28e..f314256 100644 --- a/Source/lumatone_editor_library/data/application_state.h +++ b/Source/lumatone_editor_library/data/application_state.h @@ -53,8 +53,11 @@ class DeviceActivityMonitor; class LumatoneApplicationState : public LumatoneState { public: - LumatoneApplicationState(juce::String nameIn, LumatoneFirmwareDriver& driverIn, juce::ValueTree stateIn=juce::ValueTree(), juce::UndoManager* undoManager=nullptr); + // Top-level constructor + LumatoneApplicationState(juce::ValueTree stateIn, LumatoneFirmwareDriver& driverIn, juce::UndoManager* undoManager); + LumatoneApplicationState(juce::String nameIn, const LumatoneApplicationState& stateIn); + LumatoneApplicationState(const LumatoneApplicationState& stateIn); virtual ~LumatoneApplicationState() override; @@ -84,19 +87,19 @@ class LumatoneApplicationState : public LumatoneState virtual void setCompleteConfig(const LumatoneLayout& layoutIn) override; virtual void setLayout(const LumatoneLayout& layoutIn) override; virtual void setBoard(const LumatoneBoard& boardIn, int boardId) override; - + virtual void setKey(const LumatoneKey& keyIn, int boardId, int keyIndex) override; virtual void setKeyConfig(const LumatoneKey& keyIn, int boardId, int keyIndex) override; virtual void setKeyColour(juce::Colour colour, int boardId, int keyIndex) override; virtual void sendSelectionParam(const juce::Array& selection, bool signalEditorListeners = true, bool bufferKeyUpdates = false) override; virtual void sendSelectionColours(const juce::Array& selection, bool signalEditorListeners = true, bool bufferKeyUpdates = false) override; - + virtual void setAftertouchEnabled(bool enabled) override; virtual void setLightOnKeyStrokes(bool enabled) override; virtual void setInvertExpression(bool invert) override; virtual void setInvertSustain(bool invert) override; virtual void setExpressionSensitivity(juce::uint8 sensitivity) override; - + virtual void setConfigTable(LumatoneConfigTable::TableType type, const LumatoneConfigTable& table) override; private: diff --git a/Source/lumatone_editor_library/data/lumatone_state.cpp b/Source/lumatone_editor_library/data/lumatone_state.cpp index 31b8862..0db5128 100644 --- a/Source/lumatone_editor_library/data/lumatone_state.cpp +++ b/Source/lumatone_editor_library/data/lumatone_state.cpp @@ -23,9 +23,9 @@ juce::Array LumatoneState::getLumatoneStateProperties() properties.add(LumatoneStateProperty::AftertouchEnabled); properties.add(LumatoneStateProperty::InvertExpression); properties.add(LumatoneStateProperty::InvertSustain); - + properties.add(LumatoneStateProperty::ExpressionSensitivity); - + properties.add(LumatoneStateProperty::InactiveMacroButtonColour); properties.add(LumatoneStateProperty::ActiveMacroButtonColour); @@ -34,29 +34,34 @@ juce::Array LumatoneState::getLumatoneStateProperties() return properties; } -LumatoneState::LumatoneState(juce::String nameIn, juce::ValueTree stateIn, juce::UndoManager* undoManagerIn) - : LumatoneStateBase(nameIn) +LumatoneState::LumatoneState(juce::ValueTree stateIn, juce::UndoManager *undoManagerIn) + : LumatoneStateBase(stateIn.getType().toString()) , undoManager(undoManagerIn) { - state = loadStateProperties(stateIn); - state.addListener(this); - mappingData = std::make_shared(); midiKeyMap = std::make_shared(mappingData.get()); - juce::ValueTree layoutState = juce::ValueTree(juce::Identifier(nameIn)); - layoutState.appendChild(mappingData->getState(), nullptr); - state.appendChild(layoutState, nullptr); + state = loadStateProperties(stateIn); + state.addListener(this); + + auto mappingState = mappingData->getState(); + state.appendChild(mappingState, nullptr); } -LumatoneState::LumatoneState(juce::String nameIn, const LumatoneState& stateToCopy) - : LumatoneState(nameIn, stateToCopy.state, stateToCopy.undoManager) +LumatoneState::LumatoneState(juce::String nameIn, const LumatoneState &stateIn) + : LumatoneStateBase(nameIn) + , undoManager(stateIn.undoManager) + , mappingData(stateIn.mappingData) + , midiKeyMap(stateIn.midiKeyMap) { - mappingData = stateToCopy.mappingData; - midiKeyMap = stateToCopy.midiKeyMap; + state = loadStateProperties(stateIn.state); + state.addListener(this); } -LumatoneState::LumatoneState(const LumatoneState &stateIn) : LumatoneState(stateIn.name + "Copy", stateIn) {} +LumatoneState::LumatoneState(const LumatoneState &stateIn) + : LumatoneState(stateIn.name + "Copy", stateIn) +{ +} LumatoneState::~LumatoneState() { @@ -68,9 +73,9 @@ LumatoneState::~LumatoneState() juce::ValueTree LumatoneState::loadStateProperties(juce::ValueTree stateIn) { - juce::ValueTree newState = (stateIn.hasType(LumatoneStateProperty::StateTree)) + juce::ValueTree newState = stateIn.isValid() ? stateIn - : juce::ValueTree(LumatoneStateProperty::StateTree); + : juce::ValueTree(LumatoneStateProperty::DefaultState); for (auto property : getLumatoneStateProperties()) { @@ -83,6 +88,10 @@ juce::ValueTree LumatoneState::loadStateProperties(juce::ValueTree stateIn) void LumatoneState::handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier& property) { + // Ignore any changes from parents + if (stateIn != state || !stateIn.isAChildOf(state)) + return; + if (property == LumatoneStateProperty::LastConnectedSerialNumber) { connectedSerialNumber = stateIn.getProperty(property).toString(); @@ -203,20 +212,6 @@ void LumatoneState::setLightOnKeyStrokes(bool enabled) mappingData->setLightOnKeyStrokes(enabled); } -void LumatoneState::valueTreePropertyChanged(juce::ValueTree& treeWhosePropertyHasChanged, const juce::Identifier& property) -{ - //if (treeWhosePropertyHasChanged == state) - //{ - DBG("LumatoneState::valueTreePropertyChanged(" - + treeWhosePropertyHasChanged.getType().toString() + ", " - + property.toString() + ")"); - - if (treeWhosePropertyHasChanged == state) - { - handleStatePropertyChange(state, property); - } -} - LumatoneFirmware::ReleaseVersion LumatoneState::getLumatoneVersion() const { return determinedVersion; diff --git a/Source/lumatone_editor_library/data/lumatone_state.h b/Source/lumatone_editor_library/data/lumatone_state.h index af73b16..b3d6f42 100644 --- a/Source/lumatone_editor_library/data/lumatone_state.h +++ b/Source/lumatone_editor_library/data/lumatone_state.h @@ -21,7 +21,8 @@ class LumatoneOutputMap; namespace LumatoneStateProperty { - static const juce::Identifier StateTree = juce::Identifier("LumatoneEditorStateTree"); + static const juce::Identifier DefaultState = juce::Identifier("LumatoneStateTree"); + static const juce::Identifier LumatoneState = juce::Identifier("LumatoneState"); // Lumatone Data static const juce::Identifier LastConnectedSerialNumber = juce::Identifier("LastConnectedSerialNumber"); @@ -43,8 +44,9 @@ namespace LumatoneStateProperty class LumatoneState : public LumatoneStateBase { public: + // Top-level constructor + LumatoneState(juce::ValueTree stateIn, juce::UndoManager* undoManager); - LumatoneState(juce::String nameIn, juce::ValueTree stateIn=juce::ValueTree(), juce::UndoManager* undoManager=nullptr); LumatoneState(juce::String nameIn, const LumatoneState& stateToCopy); LumatoneState(const LumatoneState& stateIn); @@ -115,8 +117,6 @@ class LumatoneState : public LumatoneStateBase virtual juce::ValueTree loadStateProperties(juce::ValueTree stateIn) override; - virtual void valueTreePropertyChanged(juce::ValueTree& treeWhosePropertyHasChanged, const juce::Identifier& property) override; - virtual void handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier& property) override; virtual void loadPropertiesFile(juce::PropertiesFile* properties) override;