diff --git a/include/element/session.hpp b/include/element/session.hpp index d834f1213..4ecc5e0ff 100644 --- a/include/element/session.hpp +++ b/include/element/session.hpp @@ -139,10 +139,4 @@ class Session : public Model, typedef juce::ReferenceCountedObjectPtr SessionPtr; typedef SessionPtr SessionRef; -/** Convert legacy + data into flat . - Operates directly on a session value tree so it can be unit tested. - Appends one MidiMapping per resolvable ControllerMap; legacy trees are - left untouched. Safe to call repeatedly only on freshly loaded data. */ -void migrateControllerMaps (juce::ValueTree session); - } // namespace element diff --git a/src/session.cpp b/src/session.cpp index 0ed001cd4..744a75f90 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -13,6 +13,51 @@ using namespace juce; namespace element { +namespace detail { + +void migrateControllerMaps (ValueTree session) +{ + if (! session.isValid()) + return; + + auto controllers = session.getChildWithName (tags::controllers); + auto maps = session.getChildWithName (tags::maps); + auto mappings = session.getOrCreateChildWithName (tags::midiMappings, nullptr); + + for (int i = 0; i < maps.getNumChildren(); ++i) + { + const auto map = maps.getChild (i); + if (! map.hasType (types::ControllerMap)) + continue; + + const auto controllerId = map.getProperty (tags::controller).toString(); + const auto controlId = map.getProperty (tags::control).toString(); + + auto controller = controllers.getChildWithProperty (tags::uuid, controllerId); + if (! controller.isValid()) + continue; + auto control = controller.getChildWithProperty (tags::uuid, controlId); + if (! control.isValid()) + continue; + + MidiMapping mapping (String {}); + mapping.setProperty (tags::device, controller.getProperty (tags::inputDevice).toString()); + mapping.setProperty (tags::name, control.getProperty (tags::name).toString()); + mapping.setProperty (tags::eventType, control.getProperty ("eventType").toString()); + mapping.setProperty (tags::eventId, (int) control.getProperty ("eventId", 0)); + mapping.setProperty (tags::midiChannel, (int) control.getProperty (tags::midiChannel, 0)); + mapping.setProperty (tags::toggle, ! (bool) control.getProperty ("momentary", false)); + mapping.setProperty (tags::targetType, "parameter"); + mapping.setProperty (tags::node, map.getProperty (tags::node).toString()); + mapping.setProperty (tags::parameter, (int) map.getProperty (tags::parameter, -1)); + + mappings.addChild (mapping.data(), -1, nullptr); + } + + session.removeChild (controllers, nullptr); + session.removeChild (maps, nullptr); +} + static Node findNodeRecursive (const Node& start, const Uuid& uuid) { if (! uuid.isNull() && uuid == start.getUuid()) @@ -28,6 +73,8 @@ static Node findNodeRecursive (const Node& start, const Uuid& uuid) return Node(); } +} // namespace detail + class Session::Impl { public: @@ -98,7 +145,7 @@ bool Session::loadData (const ValueTree& data) objectData.removeListener (this); objectData = data; setMissingProperties(); - migrateControllerMaps (objectData); + detail::migrateControllerMaps (objectData); cleanOrphanMidiMappings(); objectData.addListener (this); return true; @@ -183,7 +230,7 @@ Node Session::findNodeById (const Uuid& uuid) for (int i = getNumGraphs(); --i >= 0;) { - node = findNodeRecursive (getGraph (i), uuid); + node = detail::findNodeRecursive (getGraph (i), uuid); if (node.isValid()) break; } @@ -260,46 +307,6 @@ void Session::forEach (const ValueTree tree, ValueTreeFunction handler) const forEach (tree.getChild (i), handler); } -void migrateControllerMaps (ValueTree session) -{ - if (! session.isValid()) - return; - - auto controllers = session.getChildWithName (tags::controllers); - auto maps = session.getChildWithName (tags::maps); - auto mappings = session.getOrCreateChildWithName (tags::midiMappings, nullptr); - - for (int i = 0; i < maps.getNumChildren(); ++i) - { - const auto map = maps.getChild (i); - if (! map.hasType (types::ControllerMap)) - continue; - - const auto controllerId = map.getProperty (tags::controller).toString(); - const auto controlId = map.getProperty (tags::control).toString(); - - auto controller = controllers.getChildWithProperty (tags::uuid, controllerId); - if (! controller.isValid()) - continue; - auto control = controller.getChildWithProperty (tags::uuid, controlId); - if (! control.isValid()) - continue; - - MidiMapping mapping (String {}); - mapping.setProperty (tags::device, controller.getProperty (tags::inputDevice).toString()); - mapping.setProperty (tags::name, control.getProperty (tags::name).toString()); - mapping.setProperty (tags::eventType, control.getProperty ("eventType").toString()); - mapping.setProperty (tags::eventId, (int) control.getProperty ("eventId", 0)); - mapping.setProperty (tags::midiChannel, (int) control.getProperty (tags::midiChannel, 0)); - mapping.setProperty (tags::toggle, ! (bool) control.getProperty ("momentary", false)); - mapping.setProperty (tags::targetType, "parameter"); - mapping.setProperty (tags::node, map.getProperty (tags::node).toString()); - mapping.setProperty (tags::parameter, (int) map.getProperty (tags::parameter, -1)); - - mappings.addChild (mapping.data(), -1, nullptr); - } -} - void Session::setActiveGraph (int index) { if (! isPositiveAndBelow (index, getNumGraphs())) diff --git a/test/MidiMappingSessionTests.cpp b/test/MidiMappingSessionTests.cpp index c340cf162..6284792f3 100644 --- a/test/MidiMappingSessionTests.cpp +++ b/test/MidiMappingSessionTests.cpp @@ -11,6 +11,10 @@ using namespace element; using namespace juce; +namespace element::detail { +extern void migrateControllerMaps (ValueTree session); +} + BOOST_AUTO_TEST_SUITE (MidiMappingSessionTests) BOOST_AUTO_TEST_CASE (AddRemoveFind) @@ -79,8 +83,11 @@ BOOST_AUTO_TEST_CASE (MigrateLegacyControllerMaps) map.setProperty (tags::parameter, 5, nullptr); maps.addChild (map, -1, nullptr); - migrateControllerMaps (session); + element::detail::migrateControllerMaps (session); + BOOST_REQUIRE(! session.getChildWithName(tags::controllers).isValid()); + BOOST_REQUIRE(! session.getChildWithName(tags::maps).isValid()); + auto mappings = session.getChildWithName (tags::midiMappings); BOOST_REQUIRE (mappings.isValid()); BOOST_REQUIRE_EQUAL (mappings.getNumChildren(), 1);