Skip to content

Commit 5310088

Browse files
authored
mapping: purge old maps once migrated.
Merge pull request #1142 from kushview/duplicate-maps
2 parents 746fc66 + b081feb commit 5310088

3 files changed

Lines changed: 57 additions & 49 deletions

File tree

include/element/session.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,4 @@ class Session : public Model,
139139
typedef juce::ReferenceCountedObjectPtr<Session> SessionPtr;
140140
typedef SessionPtr SessionRef;
141141

142-
/** Convert legacy <controllers> + <maps> data into flat <midiMappings>.
143-
Operates directly on a session value tree so it can be unit tested.
144-
Appends one MidiMapping per resolvable ControllerMap; legacy trees are
145-
left untouched. Safe to call repeatedly only on freshly loaded data. */
146-
void migrateControllerMaps (juce::ValueTree session);
147-
148142
} // namespace element

src/session.cpp

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ using namespace juce;
1313

1414
namespace element {
1515

16+
namespace detail {
17+
18+
void migrateControllerMaps (ValueTree session)
19+
{
20+
if (! session.isValid())
21+
return;
22+
23+
auto controllers = session.getChildWithName (tags::controllers);
24+
auto maps = session.getChildWithName (tags::maps);
25+
auto mappings = session.getOrCreateChildWithName (tags::midiMappings, nullptr);
26+
27+
for (int i = 0; i < maps.getNumChildren(); ++i)
28+
{
29+
const auto map = maps.getChild (i);
30+
if (! map.hasType (types::ControllerMap))
31+
continue;
32+
33+
const auto controllerId = map.getProperty (tags::controller).toString();
34+
const auto controlId = map.getProperty (tags::control).toString();
35+
36+
auto controller = controllers.getChildWithProperty (tags::uuid, controllerId);
37+
if (! controller.isValid())
38+
continue;
39+
auto control = controller.getChildWithProperty (tags::uuid, controlId);
40+
if (! control.isValid())
41+
continue;
42+
43+
MidiMapping mapping (String {});
44+
mapping.setProperty (tags::device, controller.getProperty (tags::inputDevice).toString());
45+
mapping.setProperty (tags::name, control.getProperty (tags::name).toString());
46+
mapping.setProperty (tags::eventType, control.getProperty ("eventType").toString());
47+
mapping.setProperty (tags::eventId, (int) control.getProperty ("eventId", 0));
48+
mapping.setProperty (tags::midiChannel, (int) control.getProperty (tags::midiChannel, 0));
49+
mapping.setProperty (tags::toggle, ! (bool) control.getProperty ("momentary", false));
50+
mapping.setProperty (tags::targetType, "parameter");
51+
mapping.setProperty (tags::node, map.getProperty (tags::node).toString());
52+
mapping.setProperty (tags::parameter, (int) map.getProperty (tags::parameter, -1));
53+
54+
mappings.addChild (mapping.data(), -1, nullptr);
55+
}
56+
57+
session.removeChild (controllers, nullptr);
58+
session.removeChild (maps, nullptr);
59+
}
60+
1661
static Node findNodeRecursive (const Node& start, const Uuid& uuid)
1762
{
1863
if (! uuid.isNull() && uuid == start.getUuid())
@@ -28,6 +73,8 @@ static Node findNodeRecursive (const Node& start, const Uuid& uuid)
2873
return Node();
2974
}
3075

76+
} // namespace detail
77+
3178
class Session::Impl
3279
{
3380
public:
@@ -98,7 +145,7 @@ bool Session::loadData (const ValueTree& data)
98145
objectData.removeListener (this);
99146
objectData = data;
100147
setMissingProperties();
101-
migrateControllerMaps (objectData);
148+
detail::migrateControllerMaps (objectData);
102149
cleanOrphanMidiMappings();
103150
objectData.addListener (this);
104151
return true;
@@ -183,7 +230,7 @@ Node Session::findNodeById (const Uuid& uuid)
183230

184231
for (int i = getNumGraphs(); --i >= 0;)
185232
{
186-
node = findNodeRecursive (getGraph (i), uuid);
233+
node = detail::findNodeRecursive (getGraph (i), uuid);
187234
if (node.isValid())
188235
break;
189236
}
@@ -260,46 +307,6 @@ void Session::forEach (const ValueTree tree, ValueTreeFunction handler) const
260307
forEach (tree.getChild (i), handler);
261308
}
262309

263-
void migrateControllerMaps (ValueTree session)
264-
{
265-
if (! session.isValid())
266-
return;
267-
268-
auto controllers = session.getChildWithName (tags::controllers);
269-
auto maps = session.getChildWithName (tags::maps);
270-
auto mappings = session.getOrCreateChildWithName (tags::midiMappings, nullptr);
271-
272-
for (int i = 0; i < maps.getNumChildren(); ++i)
273-
{
274-
const auto map = maps.getChild (i);
275-
if (! map.hasType (types::ControllerMap))
276-
continue;
277-
278-
const auto controllerId = map.getProperty (tags::controller).toString();
279-
const auto controlId = map.getProperty (tags::control).toString();
280-
281-
auto controller = controllers.getChildWithProperty (tags::uuid, controllerId);
282-
if (! controller.isValid())
283-
continue;
284-
auto control = controller.getChildWithProperty (tags::uuid, controlId);
285-
if (! control.isValid())
286-
continue;
287-
288-
MidiMapping mapping (String {});
289-
mapping.setProperty (tags::device, controller.getProperty (tags::inputDevice).toString());
290-
mapping.setProperty (tags::name, control.getProperty (tags::name).toString());
291-
mapping.setProperty (tags::eventType, control.getProperty ("eventType").toString());
292-
mapping.setProperty (tags::eventId, (int) control.getProperty ("eventId", 0));
293-
mapping.setProperty (tags::midiChannel, (int) control.getProperty (tags::midiChannel, 0));
294-
mapping.setProperty (tags::toggle, ! (bool) control.getProperty ("momentary", false));
295-
mapping.setProperty (tags::targetType, "parameter");
296-
mapping.setProperty (tags::node, map.getProperty (tags::node).toString());
297-
mapping.setProperty (tags::parameter, (int) map.getProperty (tags::parameter, -1));
298-
299-
mappings.addChild (mapping.data(), -1, nullptr);
300-
}
301-
}
302-
303310
void Session::setActiveGraph (int index)
304311
{
305312
if (! isPositiveAndBelow (index, getNumGraphs()))

test/MidiMappingSessionTests.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
using namespace element;
1212
using namespace juce;
1313

14+
namespace element::detail {
15+
extern void migrateControllerMaps (ValueTree session);
16+
}
17+
1418
BOOST_AUTO_TEST_SUITE (MidiMappingSessionTests)
1519

1620
BOOST_AUTO_TEST_CASE (AddRemoveFind)
@@ -79,8 +83,11 @@ BOOST_AUTO_TEST_CASE (MigrateLegacyControllerMaps)
7983
map.setProperty (tags::parameter, 5, nullptr);
8084
maps.addChild (map, -1, nullptr);
8185

82-
migrateControllerMaps (session);
86+
element::detail::migrateControllerMaps (session);
8387

88+
BOOST_REQUIRE(! session.getChildWithName(tags::controllers).isValid());
89+
BOOST_REQUIRE(! session.getChildWithName(tags::maps).isValid());
90+
8491
auto mappings = session.getChildWithName (tags::midiMappings);
8592
BOOST_REQUIRE (mappings.isValid());
8693
BOOST_REQUIRE_EQUAL (mappings.getNumChildren(), 1);

0 commit comments

Comments
 (0)