From 216e4b49dfc14abf24454ac20defa58980881b77 Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Thu, 13 Jun 2024 18:48:20 -0400 Subject: [PATCH] improve editor state construction --- Source/LumatoneEditorState.cpp | 69 +++++++++++++++---- Source/LumatoneEditorState.h | 43 +++++++++--- Source/Main.cpp | 2 +- Source/data/LumatoneEditSelectionState.cpp | 40 +++++++---- Source/data/LumatoneEditSelectionState.h | 1 - Source/mapping_editors/KeyEditorPanel.cpp | 2 +- .../mapping_editors/MappingSettingsPanel.cpp | 2 +- .../mapping_editors/MultiSelectControls.cpp | 2 +- 8 files changed, 117 insertions(+), 44 deletions(-) diff --git a/Source/LumatoneEditorState.cpp b/Source/LumatoneEditorState.cpp index e412d0c..db3e2da 100644 --- a/Source/LumatoneEditorState.cpp +++ b/Source/LumatoneEditorState.cpp @@ -11,7 +11,6 @@ #include "LumatoneEditorState.h" #include "./LumatoneEditorLookAndFeel.h" -#include "./KeyEditComponent.h" #include "./lumatone_editor_library/device/lumatone_controller.h" #include "./lumatone_editor_library/listeners/editor_listener.h" @@ -48,9 +47,9 @@ juce::Array GetLumatoneEditorProperty() return properties; } - -LumatoneEditorState::LumatoneEditorState(juce::String name, LumatoneFirmwareDriver& driverIn, juce::UndoManager *undoManagerIn) - : LumatoneApplicationState("LumatoneEditor", driverIn, juce::ValueTree(), undoManagerIn) +LumatoneEditorState::LumatoneEditorState(juce::ValueTree stateIn, LumatoneFirmwareDriver &driverIn, juce::UndoManager *undoManagerIn) + : LumatoneApplicationState(stateIn, driverIn, undoManagerIn) + , editSelectionState(name + "_EditSelection", state) { appFonts = std::make_shared(); lookAndFeel = std::make_shared(*appFonts, true); @@ -62,6 +61,7 @@ LumatoneEditorState::LumatoneEditorState(juce::String name, LumatoneFirmwareDriv LumatoneEditorState::LumatoneEditorState(juce::String name, const LumatoneEditorState &stateIn) : LumatoneApplicationState(name, stateIn) + , editSelectionState(name, state) , appFonts(stateIn.appFonts) , lookAndFeel(stateIn.lookAndFeel) , recentFiles(stateIn.recentFiles) @@ -70,8 +70,16 @@ LumatoneEditorState::LumatoneEditorState(juce::String name, const LumatoneEditor { } +LumatoneEditorState::LumatoneEditorState(const LumatoneEditorState &stateIn) + : LumatoneEditorState(stateIn.name + "Copy", stateIn) +{ +} + LumatoneEditorState::~LumatoneEditorState() { + if (name == LumatoneEditorProperty::StateTree.toString()) + DBG(state.toXmlString()); + recentFiles = nullptr; propertiesFile = nullptr; lookAndFeel = nullptr; @@ -93,7 +101,12 @@ juce::RecentlyOpenedFilesList& LumatoneEditorState::getRecentFiles() return *recentFiles; } -const juce::Array& LumatoneEditorState::getColourPalettes() +LumatoneEditSelectionState::Data LumatoneEditorState::getEditSelectionData() const +{ + return editSelectionState.getData(); +} + +const juce::Array &LumatoneEditorState::getColourPalettes() { return *colourPalettes; } @@ -181,6 +194,31 @@ void LumatoneEditorState::Controller::setEditMode(EditorMode editMode) editorState.setStateProperty(LumatoneEditorProperty::EditorMode, (int)editorState.editorMode); } +void LumatoneEditorState::Controller::setAssignKeyColour(bool set, juce::Colour colourIn) +{ + editorState.editSelectionState.setKeyColour(set, colourIn); +} + +void LumatoneEditorState::Controller::setAssignKeyType(bool set, LumatoneKeyType typeIn) +{ + editorState.editSelectionState.setKeyType(set, typeIn); +} + +void LumatoneEditorState::Controller::setAssignKeyNote(bool set, int noteIn) +{ + editorState.editSelectionState.setKeyNote(set, noteIn); +} + +void LumatoneEditorState::Controller::setAssignKeyChannel(bool set, int channelIn) +{ + editorState.editSelectionState.setKeyChannel(set, channelIn); +} + +void LumatoneEditorState::Controller::setAssignCCFader(bool set, bool ccFaderDefaultIn) +{ + editorState.editSelectionState.setCCFader(set, ccFaderDefaultIn); +} + void LumatoneEditorState::Controller::setWindowState(const juce::Rectangle &windowBounds, juce::String stateString) { editorState.windowBounds = windowBounds; @@ -210,6 +248,9 @@ juce::ValueTree LumatoneEditorState::loadStateProperties(juce::ValueTree stateIn void LumatoneEditorState::handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier &property) { + // DBG(name + " state changed"); + // DBG(state.toXmlString()); + LumatoneApplicationState::handleStatePropertyChange(stateIn, property); if (property == LumatoneEditorProperty::MainWindowBounds) @@ -276,16 +317,16 @@ void LumatoneEditorState::loadPropertiesFile(juce::PropertiesFile *propertiesIn) setStateProperty(LumatoneEditorProperty::AutoConnectDevice, propertiesFile->getBoolValue(LumatoneEditorProperty::AutoConnectDevice.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteKeyTypeSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteKeyTypeSetActive.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteNoteSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteNoteSetActive.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteChannelSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteChannelSetActive.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteColourSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteColourSetActive.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteCCFaderIsDefault, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteCCFaderIsDefault.toString(), false)); - setStateProperty(LumatoneEditorProperty::SingleNoteAutoIncNoteActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteAutoIncNoteActive.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteAutoIncChannelActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteAutoIncChannelActive.toString(), true)); - setStateProperty(LumatoneEditorProperty::SingleNoteAutoIncChannelAfterNumNotes, propertiesFile->getIntValue(LumatoneEditorProperty::SingleNoteAutoIncChannelAfterNumNotes.toString(), 127)); + // setStateProperty(LumatoneEditorProperty::SingleNoteKeyTypeSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteKeyTypeSetActive.toString(), true)); + // setStateProperty(LumatoneEditorProperty::SingleNoteNoteSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteNoteSetActive.toString(), true)); + // setStateProperty(LumatoneEditorProperty::SingleNoteChannelSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteChannelSetActive.toString(), true)); + // setStateProperty(LumatoneEditorProperty::SingleNoteColourSetActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteColourSetActive.toString(), true)); + // setStateProperty(LumatoneEditorProperty::SingleNoteCCFaderIsDefault, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteCCFaderIsDefault.toString(), false)); + // setStateProperty(LumatoneEditorProperty::SingleNoteAutoIncNoteActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteAutoIncNoteActive.toString(), true)); + // setStateProperty(LumatoneEditorProperty::SingleNoteAutoIncChannelActive, propertiesFile->getBoolValue(LumatoneEditorProperty::SingleNoteAutoIncChannelActive.toString(), true)); + // setStateProperty(LumatoneEditorProperty::SingleNoteAutoIncChannelAfterNumNotes, propertiesFile->getIntValue(LumatoneEditorProperty::SingleNoteAutoIncChannelAfterNumNotes.toString(), 127)); - setStateProperty(LumatoneEditorProperty::IsomorphicMassAssign, propertiesFile->getBoolValue(LumatoneEditorProperty::IsomorphicMassAssign.toString(), false)); + // setStateProperty(LumatoneEditorProperty::IsomorphicMassAssign, propertiesFile->getBoolValue(LumatoneEditorProperty::IsomorphicMassAssign.toString(), false)) setStateProperty(LumatoneEditorProperty::LastSettingsPanel, propertiesFile->getIntValue(LumatoneEditorProperty::LastSettingsPanel.toString(), 1)); setStateProperty(LumatoneEditorProperty::LastColourWindowTab, propertiesFile->getIntValue(LumatoneEditorProperty::LastColourWindowTab.toString(), 1)); diff --git a/Source/LumatoneEditorState.h b/Source/LumatoneEditorState.h index ca7b742..8a71d55 100644 --- a/Source/LumatoneEditorState.h +++ b/Source/LumatoneEditorState.h @@ -14,6 +14,8 @@ #include "./lumatone_editor_library/data/application_state.h" #include "./lumatone_editor_library/palettes/colour_palette_file.h" +#include "./data/LumatoneEditSelectionState.h" + #define CHOOSE_FILE_NOOP [](bool) -> void {} struct LumatoneEditorFontLibrary; @@ -50,16 +52,18 @@ namespace LumatoneEditorProperty static const juce::Identifier EditorMode = juce::Identifier("EditorMode"); - static const juce::Identifier SingleNoteNoteSetActive = juce::Identifier("SingleNoteNoteSetActive"); - static const juce::Identifier SingleNoteChannelSetActive = juce::Identifier("SingleNoteChannelSetActive"); - static const juce::Identifier SingleNoteColourSetActive = juce::Identifier("SingleNoteColourSetActive"); - static const juce::Identifier SingleNoteKeyTypeSetActive = juce::Identifier("SingleNoteKeyTypeSetActive"); - static const juce::Identifier SingleNoteAutoIncNoteActive = juce::Identifier("SingleNoteAutoIncNoteActive"); - static const juce::Identifier SingleNoteAutoIncChannelActive = juce::Identifier("SingleNoteAutoIncChannelActive"); - static const juce::Identifier SingleNoteAutoIncChannelAfterNumNotes = juce::Identifier("SingleNoteAutoIncChannelAfterNumNotes"); - static const juce::Identifier SingleNoteCCFaderIsDefault = juce::Identifier("SingleNoteCCFaderIsDefault"); + // static const juce::Identifier SingleNoteNoteSetActive = juce::Identifier("SingleNoteNoteSetActive"); + // static const juce::Identifier SingleNoteChannelSetActive = juce::Identifier("SingleNoteChannelSetActive"); + // static const juce::Identifier SingleNoteColourSetActive = juce::Identifier("SingleNoteColourSetActive"); + // static const juce::Identifier SingleNoteKeyTypeSetActive = juce::Identifier("SingleNoteKeyTypeSetActive"); + // static const juce::Identifier SingleNoteAutoIncNoteActive = juce::Identifier("SingleNoteAutoIncNoteActive"); + // static const juce::Identifier SingleNoteAutoIncChannelActive = juce::Identifier("SingleNoteAutoIncChannelActive"); + // static const juce::Identifier SingleNoteAutoIncChannelAfterNumNotes = juce::Identifier("SingleNoteAutoIncChannelAfterNumNotes"); + // static const juce::Identifier SingleNoteCCFaderIsDefault = juce::Identifier("SingleNoteCCFaderIsDefault"); + + // static const juce::Identifier IsomorphicMassAssign = juce::Identifier("IsomorphicMassAssign"); - static const juce::Identifier IsomorphicMassAssign = juce::Identifier("IsomorphicMassAssign"); + static const juce::Identifier SelectedKeys = juce::Identifier("SelectedKeys"); static const juce::Identifier LastSettingsPanel = juce::Identifier("LastSettingsPanel"); static const juce::Identifier LastColourWindowTab = juce::Identifier("LastColourWindowTab"); @@ -77,8 +81,11 @@ static juce::Array GetLumatoneEditorProperties(); class LumatoneEditorState : public LumatoneApplicationState { public: - LumatoneEditorState(juce::String name, LumatoneFirmwareDriver& driverIn, juce::UndoManager* undoManagerIn); + // Only for "top level" state instance + LumatoneEditorState(juce::ValueTree stateIn, LumatoneFirmwareDriver& driverIn, juce::UndoManager* undoManagerIn); + LumatoneEditorState(juce::String name, const LumatoneEditorState& stateIn); + LumatoneEditorState(const LumatoneEditorState& stateIn); ~LumatoneEditorState() override; @@ -95,6 +102,10 @@ class LumatoneEditorState : public LumatoneApplicationState EditorMode getEditorMode() const { return editorMode; } + juce::Array getSelectedKeys() const; + + LumatoneEditSelectionState::Data getEditSelectionData() const; + LumatoneEditorLookAndFeel& getEditorLookAndFeel() { return *lookAndFeel; } virtual const juce::Array& getColourPalettes(); @@ -134,6 +145,10 @@ class LumatoneEditorState : public LumatoneApplicationState EditorMode editorMode = EditorMode::OFFLINE; + juce::Array selectedKeys; + + LumatoneEditSelectionState editSelectionState; + private: std::shared_ptr appFonts; std::shared_ptr lookAndFeel; @@ -148,7 +163,7 @@ class LumatoneEditorState : public LumatoneApplicationState //================================================================================ public: - class Controller : protected LumatoneApplicationState::Controller + class Controller : public LumatoneApplicationState::Controller { public: Controller(LumatoneEditorState& stateIn) @@ -184,6 +199,12 @@ class LumatoneEditorState : public LumatoneApplicationState void setDeveloperMode(bool developerModeOn); void setEditMode(EditorMode editMode); + void setAssignKeyColour(bool set, juce::Colour colourIn); + void setAssignKeyType(bool set, LumatoneKeyType typeIn); + void setAssignKeyNote(bool set, int noteIn); + void setAssignKeyChannel(bool set, int channelIn); + void setAssignCCFader(bool set, bool ccFaderDefaultIn); + void setWindowState(const juce::Rectangle& windowBounds, juce::String stateString); private: diff --git a/Source/Main.cpp b/Source/Main.cpp index 5989860..ecb040d 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -37,7 +37,7 @@ TerpstraSysExApplication::TerpstraSysExApplication() : firmwareDriver(LumatoneFirmwareDriver::HostMode::Driver) - , state("LumatoneEditor", firmwareDriver, &undoManager) + , state(juce::ValueTree(LumatoneEditorProperty::StateTree), firmwareDriver, &undoManager) , LumatoneEditorState::Controller(state) { // Localisation diff --git a/Source/data/LumatoneEditSelectionState.cpp b/Source/data/LumatoneEditSelectionState.cpp index fe3710e..e6a4757 100644 --- a/Source/data/LumatoneEditSelectionState.cpp +++ b/Source/data/LumatoneEditSelectionState.cpp @@ -1,4 +1,6 @@ #include "LumatoneEditSelectionState.h" +#include "../lumatone_editor_library/data/lumatone_layout.h" +#include "../LumatoneEditorState.h" static juce::Array getEditSelectionProperties() { @@ -11,23 +13,27 @@ static juce::Array getEditSelectionProperties() return properties; } -LumatoneEditSelectionState::LumatoneEditSelectionState(juce::String nameIn) - : LumatoneStateBase(nameIn) -{ - state = juce::ValueTree(LumatoneEditSelectionProperty::AssignKeyEdits); -} - LumatoneEditSelectionState::LumatoneEditSelectionState(juce::String nameIn, juce::ValueTree parentStateIn) : LumatoneStateBase(nameIn) { - state = juce::ValueTree(LumatoneEditSelectionProperty::AssignKeyEdits); - parentStateIn.addChild(state, -1, nullptr); + state = parentStateIn.getChildWithName(LumatoneEditSelectionProperty::AssignKeyEdits); + + if (!state.isValid()) + { + state = juce::ValueTree(LumatoneEditSelectionProperty::AssignKeyEdits); + parentStateIn.addChild(state, -1, nullptr); + } + + state.addListener(this); } LumatoneEditSelectionState::LumatoneEditSelectionState(juce::String nameIn, juce::ValueTree parentStateIn, const LumatoneEditSelectionState &stateToCopy) : LumatoneStateBase(nameIn) { - state = loadStateProperties(stateToCopy.state.getChildWithName(LumatoneEditSelectionProperty::AssignKeyEdits)); + juce::ValueTree stateData = stateToCopy.state.getChildWithName(LumatoneEditSelectionProperty::AssignKeyEdits); + state = loadStateProperties(stateData); + state.addListener(this); + parentStateIn.addChild(state, -1, nullptr); } @@ -47,6 +53,12 @@ juce::ValueTree LumatoneEditSelectionState::loadStateProperties(juce::ValueTree void LumatoneEditSelectionState::handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier &property) { + // DBG(name + " edit selection state changed"); + // DBG(state.toXmlString()); + + if (stateIn != state) + return; + bool setProperty = stateIn.hasProperty(property); juce::var value = stateIn.getProperty(property); @@ -119,7 +131,7 @@ void LumatoneEditSelectionState::setKeyColour(bool set, juce::Colour colourIn) if (set) { - state.setProperty(LumatoneEditSelectionProperty::AssignKeyColour, colourIn.toString(), nullptr); + state.setPropertyExcludingListener(this, LumatoneEditSelectionProperty::AssignKeyColour, colourIn.toString(), nullptr); } else { @@ -134,7 +146,7 @@ void LumatoneEditSelectionState::setKeyType(bool set, LumatoneKeyType typeIn) if (set) { - state.setProperty(LumatoneEditSelectionProperty::AssignKeyType, juce::var((int)typeIn), nullptr); + state.setPropertyExcludingListener(this, LumatoneEditSelectionProperty::AssignKeyType, juce::var((int)typeIn), nullptr); } else { @@ -149,7 +161,7 @@ void LumatoneEditSelectionState::setKeyNote(bool set, int noteIn) if (set) { - state.setProperty(LumatoneEditSelectionProperty::AssignKeyNote, juce::var(noteIn), nullptr); + state.setPropertyExcludingListener(this, LumatoneEditSelectionProperty::AssignKeyNote, juce::var(noteIn), nullptr); } else { @@ -164,7 +176,7 @@ void LumatoneEditSelectionState::setKeyChannel(bool set, int channelIn) if (set) { - state.setProperty(LumatoneEditSelectionProperty::AssignKeyChannel, juce::var(channelIn), nullptr); + state.setPropertyExcludingListener(this, LumatoneEditSelectionProperty::AssignKeyChannel, juce::var(channelIn), nullptr); } else { @@ -179,7 +191,7 @@ void LumatoneEditSelectionState::setCCFader(bool set, bool ccFaderDefaultIn) if (set) { - state.setProperty(LumatoneEditSelectionProperty::AssignKeyCCFader, juce::var(ccFaderDefaultIn), nullptr); + state.setPropertyExcludingListener(this, LumatoneEditSelectionProperty::AssignKeyCCFader, juce::var(ccFaderDefaultIn), nullptr); } else { diff --git a/Source/data/LumatoneEditSelectionState.h b/Source/data/LumatoneEditSelectionState.h index 4030c18..f2cc567 100644 --- a/Source/data/LumatoneEditSelectionState.h +++ b/Source/data/LumatoneEditSelectionState.h @@ -35,7 +35,6 @@ class LumatoneEditSelectionState : public LumatoneStateBase public: - LumatoneEditSelectionState(juce::String nameIn); LumatoneEditSelectionState(juce::String nameIn, juce::ValueTree parentStateIn); LumatoneEditSelectionState(juce::String nameIn, juce::ValueTree parentStateIn, const LumatoneEditSelectionState& stateToCopy); diff --git a/Source/mapping_editors/KeyEditorPanel.cpp b/Source/mapping_editors/KeyEditorPanel.cpp index f8c0291..a281eba 100644 --- a/Source/mapping_editors/KeyEditorPanel.cpp +++ b/Source/mapping_editors/KeyEditorPanel.cpp @@ -6,7 +6,7 @@ KeyEditorPanel::KeyEditorPanel(const LumatoneEditorState &stateIn) : juce::Component("KeyEditorPanel") - , LumatoneEditorState(stateIn) + , LumatoneEditorState("KeyEditorPanel", stateIn) { keyEditorControls = std::make_unique(stateIn); addAndMakeVisible(keyEditorControls.get()); diff --git a/Source/mapping_editors/MappingSettingsPanel.cpp b/Source/mapping_editors/MappingSettingsPanel.cpp index 0042b31..22fa8a8 100644 --- a/Source/mapping_editors/MappingSettingsPanel.cpp +++ b/Source/mapping_editors/MappingSettingsPanel.cpp @@ -14,7 +14,7 @@ MappingSettingsPanel::MappingSettingsPanel(const LumatoneEditorState& stateIn) : juce::Component("MappingSettingsPanel") - , LumatoneEditorState(stateIn) + , LumatoneEditorState("MappingSettingsPanel", stateIn) { settingsControls = std::make_unique(stateIn); addAndMakeVisible(settingsControls.get()); diff --git a/Source/mapping_editors/MultiSelectControls.cpp b/Source/mapping_editors/MultiSelectControls.cpp index 4555e87..c915e62 100644 --- a/Source/mapping_editors/MultiSelectControls.cpp +++ b/Source/mapping_editors/MultiSelectControls.cpp @@ -15,7 +15,7 @@ #include "../lumatone_editor_library/palettes/colour_edit_textbox.h" MultiSelectControls::MultiSelectControls(const LumatoneEditorState& stateIn) - : LumatoneEditorState(stateIn) + : LumatoneEditorState("MultiSelectControls", stateIn) , juce::Component("MultiSelectControls") { lblMultiSelect = std::make_unique("lblMultiSelect", "Multi-Select");