]> git.sesse.net Git - nageru/blobdiff - shared/midi_mapper_util.h
Make MIDIMappingDialog reuse some of the code from midi_mapper_util.h.
[nageru] / shared / midi_mapper_util.h
index 07f2a53b4241dd7f69b71b62e12de93e83775659..072494e07606dee1f576e2f1e6ef8574189535ff 100644 (file)
@@ -7,31 +7,43 @@
 #include <google/protobuf/descriptor.h>
 
 template <class Proto>
-inline bool match_controller_helper(const Proto &msg, int field_number, int controller)
+inline int get_controller_mapping_helper(const Proto &msg, int field_number, int default_value)
 {
        using namespace google::protobuf;
        const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
        const Reflection *reflection = msg.GetReflection();
        if (!reflection->HasField(msg, descriptor)) {
-               return false;
+               return default_value;
        }
        const MIDIControllerProto &controller_proto =
                static_cast<const MIDIControllerProto &>(reflection->GetMessage(msg, descriptor));
-       return (controller_proto.controller_number() == controller);
+       return controller_proto.controller_number();
 }
 
 template <class Proto>
-inline bool match_button_helper(const Proto &msg, int field_number, int note)
+inline bool match_controller_helper(const Proto &msg, int field_number, int controller)
+{
+       return (get_controller_mapping_helper(msg, field_number, -1) == controller);
+}
+
+template <class Proto>
+inline int get_button_mapping_helper(const Proto &msg, int field_number, int default_value)
 {
        using namespace google::protobuf;
        const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
        const Reflection *reflection = msg.GetReflection();
        if (!reflection->HasField(msg, descriptor)) {
-               return false;
+               return default_value;
        }
        const MIDIButtonProto &button_proto =
                static_cast<const MIDIButtonProto &>(reflection->GetMessage(msg, descriptor));
-       return (button_proto.note_number() == note);
+       return button_proto.note_number();
+}
+
+template <class Proto>
+inline bool match_button_helper(const Proto &msg, int field_number, int note)
+{
+       return (get_button_mapping_helper(msg, field_number, -1) == note);
 }
 
 template <class Proto>
@@ -49,7 +61,7 @@ inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
 
 // Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
 template <class Proto>
-void activate_mapped_light(const Proto &msg, int field_number, std::map<unsigned, uint8_t> *active_lights)
+void activate_mapped_light(const Proto &msg, int field_number, std::map<MIDIDevice::LightKey, uint8_t> *active_lights)
 {
        using namespace google::protobuf;
        const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
@@ -59,7 +71,8 @@ void activate_mapped_light(const Proto &msg, int field_number, std::map<unsigned
        }
        const MIDILightProto &light_proto =
                static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
-       active_lights->emplace(light_proto.note_number(), light_proto.velocity());
+       active_lights->emplace(MIDIDevice::LightKey{MIDIDevice::LightKey::NOTE, unsigned(light_proto.note_number())},
+               light_proto.velocity());
 }
 
 inline double map_controller_to_float(int controller, int val)