Skip to content

Commit

Permalink
fix state copying issues, move event manager to application state
Browse files Browse the repository at this point in the history
  • Loading branch information
vsicurella committed Aug 4, 2024
1 parent 9b5030f commit 94e1e69
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
11 changes: 5 additions & 6 deletions Source/LumatoneEditorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "./lumatone_editor_library/palettes/colour_selection_group.h"


static juce::File getDefaultUserDocumentsDirectory()
{
return File::getSpecialLocation(File::userDocumentsDirectory).getChildFile("Lumatone Editor");
Expand All @@ -31,7 +30,7 @@ static juce::File getDefaultUserPalettesDirectory()
return getDefaultUserDocumentsDirectory().getChildFile("Palettes");
}

juce::Array<juce::Identifier> GetLumatoneEditorProperty()
juce::Array<juce::Identifier> GetLumatoneEditorProperties()
{
juce::Array<juce::Identifier> properties;
properties.add(LumatoneEditorProperty::HasChangesToSave);
Expand Down Expand Up @@ -78,6 +77,7 @@ LumatoneEditorState::LumatoneEditorState(juce::String name, const LumatoneEditor
, colourSelectionGroup(stateIn.colourSelectionGroup)
{
// DBG(name + " LumatoneEditorState created");
loadStateProperties(stateIn.state);
}

LumatoneEditorState::LumatoneEditorState(const LumatoneEditorState &stateIn)
Expand Down Expand Up @@ -287,17 +287,15 @@ juce::ValueTree LumatoneEditorState::loadStateProperties(juce::ValueTree stateIn
? stateIn
: juce::ValueTree(LumatoneEditorProperty::StateTree);

// TODO load editor properties
// LumatoneApplicationState::loadStateProperties(newState);

// DBG("LumatoneApplicationState::loadStateProperties:\n" + newState.toXmlString());
for (auto property : getLumatoneApplicationProperties())
for (auto property : GetLumatoneEditorProperties())
{
if (newState.hasProperty(property))
handleStatePropertyChange(newState, property);
}

LumatoneState::loadStateProperties(newState);

return newState;
}

Expand Down Expand Up @@ -596,3 +594,4 @@ void LumatoneEditorState::Controller::setDeveloperMode(bool developerModeOn)
editorState.setStateProperty(LumatoneEditorProperty::DeveloperModeOn, editorState.inDeveloperMode);
savePropertyBoolValue(LumatoneEditorProperty::DeveloperModeOn, developerModeOn);
}

43 changes: 27 additions & 16 deletions Source/lumatone_editor_library/data/application_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "../device/lumatone_controller.h"
#include "../device/activity_monitor.h"
#include "../device/lumatone_event_manager.h"

#include "../color/colour_model.h"
#include "../data/lumatone_context.h"
#include "../actions/lumatone_action.h"
Expand Down Expand Up @@ -39,8 +41,9 @@ LumatoneApplicationState::LumatoneApplicationState(juce::ValueTree stateIn, Luma
layoutContext = std::make_shared<LumatoneContext>(*mappingData);
colourModel = std::make_shared<LumatoneColourModel>();

eventManager = std::make_shared<LumatoneEventManager>(*this, driverIn);
controller = std::make_shared<LumatoneController>(*this, driverIn);
activityMonitor = std::make_shared<DeviceActivityMonitor>(*this, &driverIn);
activityMonitor = std::make_shared<DeviceActivityMonitor>(*this, driverIn);

loadStateProperties(stateIn);
}
Expand All @@ -52,13 +55,14 @@ LumatoneApplicationState::LumatoneApplicationState(juce::String nameIn, const Lu
, statusListeners(stateIn.statusListeners)
, firmwareListeners(stateIn.firmwareListeners)
, midiListeners(stateIn.midiListeners)
, layoutContext(stateIn.layoutContext)
, controller(stateIn.controller)
, activityMonitor(stateIn.activityMonitor)
, colourModel(stateIn.colourModel)
, selectedKeys(stateIn.selectedKeys)
, receiveSettingsStatus(stateIn.receiveSettingsStatus)
, receiveLayoutStatus(stateIn.receiveLayoutStatus)
, layoutContext(stateIn.layoutContext)
, colourModel(stateIn.colourModel)
, eventManager(stateIn.eventManager)
, controller(stateIn.controller)
, activityMonitor(stateIn.activityMonitor)
{
loadStateProperties(state);
}
Expand All @@ -70,10 +74,17 @@ LumatoneApplicationState::LumatoneApplicationState(const LumatoneApplicationStat

LumatoneApplicationState::~LumatoneApplicationState()
{
layoutContext = nullptr;
activityMonitor = nullptr;
controller = nullptr;
eventManager = nullptr;
colourModel = nullptr;
layoutContext = nullptr;
receiveLayoutStatus = nullptr;
receiveSettingsStatus = nullptr;
midiListeners = nullptr;
firmwareListeners = nullptr;
statusListeners = nullptr;
editorListeners = nullptr;
}

ConnectionState LumatoneApplicationState::getConnectionState() const
Expand Down Expand Up @@ -171,23 +182,23 @@ void LumatoneApplicationState::setActiveMacroButtonColour(juce::Colour buttonCol

juce::ValueTree LumatoneApplicationState::loadStateProperties(juce::ValueTree stateIn)
{
juce::ValueTree newState = (stateIn.hasType(LumatoneStateProperty::LumatoneState))
? stateIn
: juce::ValueTree(LumatoneStateProperty::LumatoneState);
// juce::ValueTree newState = stateIn.isValid()
// ? stateIn
// : juce::ValueTree(LumatoneStateProperty::DefaultState);

LumatoneState::loadStateProperties(newState);
// LumatoneState::loadStateProperties(newState);

// DBG("LumatoneApplicationState::loadStateProperties:\n" + newState.toXmlString());
for (auto property : getLumatoneApplicationProperties())
{
if (newState.hasProperty(property))
handleStatePropertyChange(newState, property);
if (stateIn.hasProperty(property))
handleStatePropertyChange(stateIn, property);
}

// if (name.contains("Copy"))
// DBG(juce::String("Loaded ") + name + juce::String(" properties"));

return newState;
return stateIn;
}

void LumatoneApplicationState::handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier &property)
Expand Down Expand Up @@ -274,7 +285,7 @@ void LumatoneApplicationState::setLayout(const LumatoneLayout &layoutIn)

if (doSendChangesToDevice())
{
controller->sendCompleteMapping(layoutIn);
controller->sendCompleteMapping(layoutIn, false, false);
}

clearContext();
Expand Down Expand Up @@ -721,12 +732,12 @@ void LumatoneApplicationState::DeviceController::setMidiInput(int deviceIndex, b
{
auto deviceInfo = getMidiInputList()[deviceIndex];
deviceAppState.setStateProperty(LumatoneApplicationProperty::LastInputDeviceId, deviceInfo.identifier);
deviceAppState.controller->setDriverMidiInput(deviceIndex, test);
deviceAppState.activityMonitor->setMidiInput(deviceIndex, test);
}

void LumatoneApplicationState::DeviceController::setMidiOutput(int deviceIndex, bool test)
{
auto deviceInfo = getMidiOutputList()[deviceIndex];
deviceAppState.setStateProperty(LumatoneApplicationProperty::LastOutputDeviceId, deviceInfo.identifier);
deviceAppState.controller->setDriverMidiOutput(deviceIndex, test);
deviceAppState.activityMonitor->setMidiOutput(deviceIndex, test);
}
11 changes: 7 additions & 4 deletions Source/lumatone_editor_library/data/application_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace LumatoneEditor
class MidiListener;
}

class LumatoneEventManager;

namespace LumatoneApplicationProperty
{
// Device Management
Expand Down Expand Up @@ -162,12 +164,13 @@ class LumatoneApplicationState : public LumatoneState
std::shared_ptr<FirmwareSupport::ReceiveSettingsStatus> receiveSettingsStatus;
std::shared_ptr<FirmwareSupport::ReceiveLayoutStatus> receiveLayoutStatus;

std::shared_ptr<LumatoneContext> layoutContext;
std::shared_ptr<LumatoneColourModel> colourModel;
std::shared_ptr<LumatoneContext> layoutContext;
std::shared_ptr<LumatoneColourModel> colourModel;

// Anything inheriting from this state should come after shared data
std::shared_ptr<LumatoneController> controller;
std::shared_ptr<DeviceActivityMonitor> activityMonitor;
std::shared_ptr<LumatoneEventManager> eventManager;
std::shared_ptr<LumatoneController> controller;
std::shared_ptr<DeviceActivityMonitor> activityMonitor;


bool contextIsSet = false;
Expand Down
21 changes: 15 additions & 6 deletions Source/lumatone_editor_library/data/lumatone_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ LumatoneState::LumatoneState(juce::ValueTree stateIn, juce::UndoManager *undoMan
mappingData = std::make_shared<LumatoneLayout>();
midiKeyMap = std::make_shared<LumatoneOutputMap>(mappingData.get());

if (!stateIn.isValid())
state = juce::ValueTree(LumatoneStateProperty::DefaultState);

state = loadStateProperties(stateIn);
state.addListener(this);

Expand Down Expand Up @@ -73,17 +76,17 @@ LumatoneState::~LumatoneState()

juce::ValueTree LumatoneState::loadStateProperties(juce::ValueTree stateIn)
{
juce::ValueTree newState = stateIn.isValid()
? stateIn
: juce::ValueTree(LumatoneStateProperty::DefaultState);
// juce::ValueTree newState = stateIn.isValid()
// ? stateIn
// : juce::ValueTree(LumatoneStateProperty::DefaultState);

for (auto property : getLumatoneStateProperties())
{
if (newState.hasProperty(property))
handleStatePropertyChange(newState, property);
if (stateIn.hasProperty(property))
handleStatePropertyChange(stateIn, property);
}

return newState;
return stateIn;
}

void LumatoneState::handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier& property)
Expand Down Expand Up @@ -205,11 +208,13 @@ void LumatoneState::sendSelectionColours(const juce::Array<MappedLumatoneKey>& s
void LumatoneState::setAftertouchEnabled(bool enabled)
{
mappingData->setAftertouchEnabled(enabled);
setStateProperty(LumatoneStateProperty::AftertouchEnabled, enabled);
}

void LumatoneState::setLightOnKeyStrokes(bool enabled)
{
mappingData->setLightOnKeyStrokes(enabled);
setStateProperty(LumatoneStateProperty::LightsOnAfterKeystroke, enabled);
}

LumatoneFirmware::ReleaseVersion LumatoneState::getLumatoneVersion() const
Expand Down Expand Up @@ -259,21 +264,25 @@ const FirmwareSupport& LumatoneState::getFirmwareSupport() const
void LumatoneState::setInvertExpression(bool invert)
{
mappingData->setInvertExpression(invert);
setStateProperty(LumatoneStateProperty::InvertExpression, invert);
}

void LumatoneState::setInvertSustain(bool invert)
{
mappingData->setInvertSustain(invert);
setStateProperty(LumatoneStateProperty::InvertSustain, invert);
}

void LumatoneState::setExpressionSensitivity(juce::uint8 sensitivity)
{
mappingData->setExpressionSensitivity(sensitivity);
setStateProperty(LumatoneStateProperty::ExpressionSensitivity, sensitivity);
}

void LumatoneState::setConfigTable(LumatoneConfigTable::TableType type, const LumatoneConfigTable& table)
{
mappingData->setConfigTable(type, table.velocityValues);
// todo update value tree state
}

void LumatoneState::setInactiveMacroButtonColour(juce::Colour buttonColour)
Expand Down

0 comments on commit 94e1e69

Please sign in to comment.