add VmcExtKey

This commit is contained in:
Cassandra de la Cruz-Munoz 2023-12-30 10:29:52 -05:00
parent e8bb18c47e
commit cc84da6563

View File

@ -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<OscType>;
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<Self, String> {
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,
});
}
}