diff --git a/src/lib.rs b/src/lib.rs index 89c5cbd..654ff28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -491,3 +491,61 @@ impl MessageBehavior for VmcExtCon { }); } } + +struct VmcExtKey { + active: bool, + name: String, + keycode: i32, +} + +impl MessageBehavior for VmcExtKey { + fn to_sendable_osc_message(&self) -> OscMessage { + let mut args: Vec; + if self.active { + args = vec![OscType::from(1)]; + } else { + args = vec![OscType::from(0)]; + } + args.append(&mut vec![ + OscType::from(self.name.to_owned()), + OscType::from(self.keycode), + ]); + return OscMessage { + addr: String::from("/VMC/Ext/Key"), + args, + }; + } + fn parse_from_osc_message(msg: OscMessage) -> Result { + if msg.args.len() != 3 { + return Err(String::from("arg count invalid")); + } + let active: bool; + let name: String; + let keycode: i32; + match msg.args[0] { + OscType::Int(i) => { + if i == 1 { + active = true; + } else if i == 0 { + active = false; + } else { + return Err(String::from("arg value invalid")); + } + } + _ => return Err(String::from("arg type invalid")), + } + match &msg.args[1] { + OscType::String(s) => name = s.to_owned(), + _ => return Err(String::from("arg type invalid")), + } + match msg.args[2] { + OscType::Int(i) => keycode = i, + _ => return Err(String::from("arg type invalid")), + } + return Ok(VmcExtKey { + active, + name, + keycode, + }); + } +}