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::{
builtin::{Basis, Quaternion},
prelude::{Transform3D, Vector3},
prelude::{Color, Transform3D, Vector3},
};
use rosc::{OscMessage, OscType};
pub trait MessageBehavior {
fn to_sendable_osc_message(&self) -> OscMessage;
fn parse_from_osc_message(msg: OscMessage) -> Result<Self, String>
fn to_osc_message(&self) -> OscMessage;
fn from_osc_message(msg: OscMessage) -> Result<Self, String>
where
Self: Sized;
}
@ -19,7 +19,7 @@ struct 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 mut args: Vec<OscType> = vec![OscType::from(self.loaded)];
if self.calibration_state.is_some() {
@ -33,7 +33,7 @@ impl MessageBehavior for VmcExtOk {
}
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 {
loaded: -1,
calibration_state: None,
@ -89,13 +89,13 @@ struct VmcExtT {
}
impl MessageBehavior for VmcExtT {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/T"),
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 {
return Err(String::from("arg count invalid"));
}
@ -114,7 +114,7 @@ struct 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 quat = self.transform.basis.to_quat();
let mut args: Vec<OscType> = vec![
@ -139,7 +139,7 @@ impl MessageBehavior for VmcExtRootPos {
}
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;
if msg.args.len() == 8 || msg.args.len() == 14 {
let mut origin = Vector3::new(0.0, 0.0, 0.0);
@ -237,7 +237,7 @@ struct 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();
return OscMessage {
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 mut origin = Vector3::new(0.0, 0.0, 0.0);
if msg.args.len() != 8 {
@ -310,7 +310,7 @@ struct VmcExtBlendVal {
}
impl MessageBehavior for VmcExtBlendVal {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Blend/Val"),
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 value: f32;
if msg.args.len() != 2 {
@ -344,7 +344,7 @@ struct 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();
return OscMessage {
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 {
return Err(String::from("arg count invalid"));
}
@ -422,7 +422,7 @@ struct VmcExtCon {
}
impl MessageBehavior for VmcExtCon {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Cam"),
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 {
return Err(String::from("arg count invalid"));
}
@ -499,7 +499,7 @@ struct VmcExtKey {
}
impl MessageBehavior for VmcExtKey {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>;
if self.active {
args = vec![OscType::from(1)];
@ -515,7 +515,7 @@ impl MessageBehavior for VmcExtKey {
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 {
return Err(String::from("arg count invalid"));
}
@ -558,7 +558,7 @@ struct VmcExtMidiNote {
}
impl MessageBehavior for VmcExtMidiNote {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>;
if self.active {
args = vec![OscType::from(1)];
@ -575,7 +575,7 @@ impl MessageBehavior for VmcExtMidiNote {
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 {
return Err(String::from("arg count invalid"));
}
@ -622,13 +622,13 @@ struct VmcExtMidiCcVal {
}
impl MessageBehavior for VmcExtMidiCcVal {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Midi/CC/Val"),
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 {
return Err(String::from("arg count invalid"));
}
@ -652,7 +652,7 @@ struct 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)];
if self.active {
args.push(OscType::from(1));
@ -664,7 +664,7 @@ impl MessageBehavior for VmcExtMidiCcBit {
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 {
return Err(String::from("arg count invalid"));
}
@ -697,7 +697,7 @@ struct 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();
return OscMessage {
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 {
return Err(String::from("arg count invalid"));
}
@ -776,7 +776,7 @@ struct VmcExtRvc {
}
impl MessageBehavior for VmcExtRvc {
fn to_sendable_osc_message(&self) -> OscMessage {
fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>;
if self.enable {
args = vec![OscType::from(1)];
@ -792,7 +792,7 @@ impl MessageBehavior for VmcExtRvc {
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 {
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,
});
}
}