create VmcExtLight

This commit is contained in:
Cassandra de la Cruz-Munoz 2023-12-30 11:26:34 -05:00
parent 166feab2a2
commit 1abd398b67

View File

@ -1,12 +1,12 @@
use godot::{ use godot::{
builtin::{Basis, Quaternion}, builtin::{Basis, Quaternion},
prelude::{Transform3D, Vector3}, prelude::{Color, Transform3D, Vector3},
}; };
use rosc::{OscMessage, OscType}; use rosc::{OscMessage, OscType};
pub trait MessageBehavior { pub trait MessageBehavior {
fn to_sendable_osc_message(&self) -> OscMessage; fn to_osc_message(&self) -> OscMessage;
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> fn from_osc_message(msg: OscMessage) -> Result<Self, String>
where where
Self: Sized; Self: Sized;
} }
@ -19,7 +19,7 @@ struct VmcExtOk {
} }
impl MessageBehavior for VmcExtOk { impl MessageBehavior for VmcExtOk {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let addr = String::from("/VMC/Ext/OK"); let addr = String::from("/VMC/Ext/OK");
let mut args: Vec<OscType> = vec![OscType::from(self.loaded)]; let mut args: Vec<OscType> = vec![OscType::from(self.loaded)];
if self.calibration_state.is_some() { if self.calibration_state.is_some() {
@ -33,7 +33,7 @@ impl MessageBehavior for VmcExtOk {
} }
return OscMessage { addr, args }; return OscMessage { addr, args };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
let mut result = VmcExtOk { let mut result = VmcExtOk {
loaded: -1, loaded: -1,
calibration_state: None, calibration_state: None,
@ -89,13 +89,13 @@ struct VmcExtT {
} }
impl MessageBehavior for VmcExtT { impl MessageBehavior for VmcExtT {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
return OscMessage { return OscMessage {
addr: String::from("/VMC/Ext/T"), addr: String::from("/VMC/Ext/T"),
args: vec![OscType::from(self.time)], args: vec![OscType::from(self.time)],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 1 { if msg.args.len() != 1 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -114,7 +114,7 @@ struct VmcExtRootPos {
} }
impl MessageBehavior for VmcExtRootPos { impl MessageBehavior for VmcExtRootPos {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let addr = String::from("/VMC/Ext/Root/Pos"); let addr = String::from("/VMC/Ext/Root/Pos");
let quat = self.transform.basis.to_quat(); let quat = self.transform.basis.to_quat();
let mut args: Vec<OscType> = vec![ let mut args: Vec<OscType> = vec![
@ -139,7 +139,7 @@ impl MessageBehavior for VmcExtRootPos {
} }
return OscMessage { addr, args }; return OscMessage { addr, args };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
let mut result: VmcExtRootPos; let mut result: VmcExtRootPos;
if msg.args.len() == 8 || msg.args.len() == 14 { if msg.args.len() == 8 || msg.args.len() == 14 {
let mut origin = Vector3::new(0.0, 0.0, 0.0); let mut origin = Vector3::new(0.0, 0.0, 0.0);
@ -237,7 +237,7 @@ struct VmcExtBonePos {
} }
impl MessageBehavior for VmcExtBonePos { impl MessageBehavior for VmcExtBonePos {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat(); let quat = self.transform.basis.to_quat();
return OscMessage { return OscMessage {
addr: String::from("/VMC/Ext/Bone/Pos"), addr: String::from("/VMC/Ext/Bone/Pos"),
@ -253,7 +253,7 @@ impl MessageBehavior for VmcExtBonePos {
], ],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
let name: String; let name: String;
let mut origin = Vector3::new(0.0, 0.0, 0.0); let mut origin = Vector3::new(0.0, 0.0, 0.0);
if msg.args.len() != 8 { if msg.args.len() != 8 {
@ -310,7 +310,7 @@ struct VmcExtBlendVal {
} }
impl MessageBehavior for VmcExtBlendVal { impl MessageBehavior for VmcExtBlendVal {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
return OscMessage { return OscMessage {
addr: String::from("/VMC/Ext/Blend/Val"), addr: String::from("/VMC/Ext/Blend/Val"),
args: vec![ args: vec![
@ -319,7 +319,7 @@ impl MessageBehavior for VmcExtBlendVal {
], ],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
let name: String; let name: String;
let value: f32; let value: f32;
if msg.args.len() != 2 { if msg.args.len() != 2 {
@ -344,7 +344,7 @@ struct VmcExtCam {
} }
impl MessageBehavior for VmcExtCam { impl MessageBehavior for VmcExtCam {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat(); let quat = self.transform.basis.to_quat();
return OscMessage { return OscMessage {
addr: String::from("/VMC/Ext/Cam"), addr: String::from("/VMC/Ext/Cam"),
@ -361,7 +361,7 @@ impl MessageBehavior for VmcExtCam {
], ],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 9 { if msg.args.len() != 9 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -422,7 +422,7 @@ struct VmcExtCon {
} }
impl MessageBehavior for VmcExtCon { impl MessageBehavior for VmcExtCon {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
return OscMessage { return OscMessage {
addr: String::from("/VMC/Ext/Cam"), addr: String::from("/VMC/Ext/Cam"),
args: vec![ args: vec![
@ -436,7 +436,7 @@ impl MessageBehavior for VmcExtCon {
], ],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 7 { if msg.args.len() != 7 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -499,7 +499,7 @@ struct VmcExtKey {
} }
impl MessageBehavior for VmcExtKey { impl MessageBehavior for VmcExtKey {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>; let mut args: Vec<OscType>;
if self.active { if self.active {
args = vec![OscType::from(1)]; args = vec![OscType::from(1)];
@ -515,7 +515,7 @@ impl MessageBehavior for VmcExtKey {
args, args,
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 3 { if msg.args.len() != 3 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -558,7 +558,7 @@ struct VmcExtMidiNote {
} }
impl MessageBehavior for VmcExtMidiNote { impl MessageBehavior for VmcExtMidiNote {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>; let mut args: Vec<OscType>;
if self.active { if self.active {
args = vec![OscType::from(1)]; args = vec![OscType::from(1)];
@ -575,7 +575,7 @@ impl MessageBehavior for VmcExtMidiNote {
args, args,
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 4 { if msg.args.len() != 4 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -622,13 +622,13 @@ struct VmcExtMidiCcVal {
} }
impl MessageBehavior for VmcExtMidiCcVal { impl MessageBehavior for VmcExtMidiCcVal {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
return OscMessage { return OscMessage {
addr: String::from("/VMC/Ext/Midi/CC/Val"), addr: String::from("/VMC/Ext/Midi/CC/Val"),
args: vec![OscType::from(self.knob), OscType::from(self.value)], args: vec![OscType::from(self.knob), OscType::from(self.value)],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 2 { if msg.args.len() != 2 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -652,7 +652,7 @@ struct VmcExtMidiCcBit {
} }
impl MessageBehavior for VmcExtMidiCcBit { impl MessageBehavior for VmcExtMidiCcBit {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let mut args = vec![OscType::from(self.knob)]; let mut args = vec![OscType::from(self.knob)];
if self.active { if self.active {
args.push(OscType::from(1)); args.push(OscType::from(1));
@ -664,7 +664,7 @@ impl MessageBehavior for VmcExtMidiCcBit {
args, args,
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 2 { if msg.args.len() != 2 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -697,7 +697,7 @@ struct DeviceTranform {
} }
impl MessageBehavior for DeviceTranform { impl MessageBehavior for DeviceTranform {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat(); let quat = self.transform.basis.to_quat();
return OscMessage { return OscMessage {
addr: self.addr.to_owned(), addr: self.addr.to_owned(),
@ -713,7 +713,7 @@ impl MessageBehavior for DeviceTranform {
], ],
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 8 { if msg.args.len() != 8 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -776,7 +776,7 @@ struct VmcExtRvc {
} }
impl MessageBehavior for VmcExtRvc { impl MessageBehavior for VmcExtRvc {
fn to_sendable_osc_message(&self) -> OscMessage { fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>; let mut args: Vec<OscType>;
if self.enable { if self.enable {
args = vec![OscType::from(1)]; args = vec![OscType::from(1)];
@ -792,7 +792,7 @@ impl MessageBehavior for VmcExtRvc {
args, args,
}; };
} }
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String> { fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() < 2 || msg.args.len() > 3 { if msg.args.len() < 2 || msg.args.len() > 3 {
return Err(String::from("arg count invalid")); return Err(String::from("arg count invalid"));
} }
@ -834,3 +834,94 @@ impl MessageBehavior for VmcExtRvc {
}); });
} }
} }
struct VmcExtLight {
name: String,
transform: Transform3D,
color: Color,
}
impl MessageBehavior for VmcExtLight {
fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat();
return OscMessage {
addr: String::from("/VMC/Ext/Light"),
args: vec![
OscType::from(self.name.to_owned()),
OscType::from(self.transform.origin.x),
OscType::from(self.transform.origin.y),
OscType::from(self.transform.origin.z),
OscType::from(quat.x),
OscType::from(quat.y),
OscType::from(quat.z),
OscType::from(quat.w),
OscType::from(self.color.r),
OscType::from(self.color.g),
OscType::from(self.color.b),
OscType::from(self.color.a),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, String> {
if msg.args.len() != 12 {
return Err(String::from("arg count invalid"));
}
let name: String;
let mut origin = Vector3::new(0.0, 0.0, 0.0);
let mut quat = Quaternion::new(0.0, 0.0, 0.0, 0.0);
let mut color = Color::from_rgba(0.0, 0.0, 0.0, 0.0);
match &msg.args[0] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(String::from("arg type invalid")),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[8] {
OscType::Float(f) => color.r = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[9] {
OscType::Float(f) => color.g = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[10] {
OscType::Float(f) => color.b = f,
_ => return Err(String::from("arg type invalid")),
}
match msg.args[11] {
OscType::Float(f) => color.a = f,
_ => return Err(String::from("arg type invalid")),
}
return Ok(VmcExtLight {
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
color,
});
}
}