vmc_rs/src/lib.rs

1421 lines
57 KiB
Rust

use godot::{
builtin::{Basis, Quaternion},
prelude::{Color, Transform3D, Vector3},
};
use rosc::{OscMessage, OscType};
use std::fmt;
pub trait MessageBehavior {
fn to_osc_message(&self) -> OscMessage;
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr>
where
Self: Sized;
}
pub struct ArgCountErr {
expected_in: Vec<usize>,
actual: usize,
addr: String,
}
impl fmt::Display for ArgCountErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid number of arguments for message with address {}. Expected {:?}, received {}.", self.addr, self.expected_in, self.actual)
}
}
pub struct ArgTypeErr {
expected: Vec<OscType>,
actual: OscType,
addr: String,
arg_count: usize,
}
impl fmt::Display for ArgTypeErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid type of argument {} for message with address {}. Expected {:?}, received {:?}.", self.arg_count, self.addr, self.expected, self.actual)
}
}
pub struct IntRangeErr {
expected_lower: i32,
expected_upper: i32,
actual: i32,
addr: String,
arg_count: usize,
}
impl fmt::Display for IntRangeErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid value of integer argument {} for message with address {}. Expected in range [{}, {}], received {}.", self.arg_count, self.addr, self.expected_lower, self.expected_upper, self.actual)
}
}
pub struct IntValErr {
expected_in: Vec<i32>,
actual: i32,
addr: String,
arg_count: usize,
}
impl fmt::Display for IntValErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid value of integer argument {} for message with address {}. Expected {:?}, received {:?}.", self.arg_count, self.addr, self.expected_in, self.actual)
}
}
pub struct StrValErr {
expected_in: Vec<String>,
actual: String,
addr: String,
arg_count: usize,
}
impl fmt::Display for StrValErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid value of string argument {} for message with address {}. Expected {:?}, received {:?}.", self.arg_count, self.addr, self.expected_in, self.actual)
}
}
pub enum FromMessageErr {
ArgCount(ArgCountErr),
ArgType(ArgTypeErr),
IntRange(IntRangeErr),
IntVal(IntValErr),
StrVal(StrValErr),
}
#[derive(Debug)]
struct VmcExtOk {
loaded: i32,
calibration_state: Option<i32>,
calibration_mode: Option<i32>,
tracking_status: Option<i32>,
}
impl MessageBehavior for VmcExtOk {
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() {
args.append(&mut vec![
OscType::from(self.calibration_state.unwrap()),
OscType::from(self.calibration_mode.unwrap()),
]);
}
if self.tracking_status.is_some() {
args.push(OscType::from(self.tracking_status.unwrap()));
}
return OscMessage { addr, args };
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
let mut result = VmcExtOk {
loaded: -1,
calibration_state: None,
calibration_mode: None,
tracking_status: None,
};
if msg.args.len() == 1 || msg.args.len() == 3 || msg.args.len() == 4 {
match msg.args[0] {
OscType::Int(i) => {
result.loaded = i;
}
_ => {
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(),addr: msg.addr.to_owned(), arg_count: 0}));
}
}
} else {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1, 3, 4], actual: msg.args.len(), addr: msg.addr.to_owned()}));
}
if msg.args.len() == 3 || msg.args.len() == 4 {
match msg.args[1] {
OscType::Int(i) => {
result.calibration_state = Some(i);
}
_ => {
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr.to_owned(), arg_count: 1}));
}
}
match msg.args[2] {
OscType::Int(i) => {
result.calibration_mode = Some(i);
}
_ => {
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr.to_owned(), arg_count: 2}));
}
}
}
if msg.args.len() == 4 {
match msg.args[3] {
OscType::Int(i) => {
result.tracking_status = Some(i);
}
_ => {
return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[3].to_owned(), addr: msg.addr.to_owned(), arg_count: 3}));
}
}
}
return Ok(result);
}
}
#[derive(Debug)]
struct VmcExtT {
time: f32,
}
impl MessageBehavior for VmcExtT {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/T"),
args: vec![OscType::from(self.time)],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
match msg.args[0] {
OscType::Float(f) => return Ok(VmcExtT { time: f }),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
}
}
#[derive(Debug)]
struct VmcExtRootPos {
name: String,
transform: Transform3D,
mr_scale: Option<Vector3>,
mr_offset: Option<Vector3>,
}
impl MessageBehavior for VmcExtRootPos {
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![
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),
];
if self.mr_offset.is_some() {
args.append(&mut vec![
OscType::from(self.mr_scale.unwrap().x),
OscType::from(self.mr_scale.unwrap().y),
OscType::from(self.mr_scale.unwrap().z),
OscType::from(self.mr_offset.unwrap().x),
OscType::from(self.mr_offset.unwrap().y),
OscType::from(self.mr_offset.unwrap().z),
]);
}
return OscMessage { addr, args };
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
let mut result: VmcExtRootPos;
if msg.args.len() == 8 || msg.args.len() == 14 {
let mut origin = Vector3::new(0.0, 0.0, 0.0);
let name: String;
match &msg.args[0] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
let mut quat = Quaternion {
x: 0.0,
y: 0.0,
z: 0.0,
w: 0.0,
};
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[6].to_owned(), addr: msg.addr, arg_count: 6})),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[7].to_owned(), addr: msg.addr, arg_count: 7})),
}
result = VmcExtRootPos {
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
mr_scale: None,
mr_offset: None,
};
} else {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![8, 14], actual: msg.args.len(), addr: msg.addr}));
}
if msg.args.len() == 14 {
result.mr_scale = Some(Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
});
result.mr_offset = Some(Vector3 {
x: 0.0,
y: 0.0,
z: 0.0,
});
match msg.args[8] {
OscType::Float(f) => result.mr_scale.unwrap().x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[8].to_owned(), addr: msg.addr, arg_count: 8})),
}
match msg.args[9] {
OscType::Float(f) => result.mr_scale.unwrap().y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[9].to_owned(), addr: msg.addr, arg_count: 9})),
}
match msg.args[10] {
OscType::Float(f) => result.mr_scale.unwrap().z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[10].to_owned(), addr: msg.addr, arg_count: 10})),
}
match msg.args[11] {
OscType::Float(f) => result.mr_offset.unwrap().x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[11].to_owned(), addr: msg.addr, arg_count: 11})),
}
match msg.args[12] {
OscType::Float(f) => result.mr_offset.unwrap().y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[12].to_owned(), addr: msg.addr, arg_count: 12})),
}
match msg.args[13] {
OscType::Float(f) => result.mr_offset.unwrap().z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[13].to_owned(), addr: msg.addr, arg_count: 13})),
}
}
return Ok(result);
}
}
#[derive(Debug)]
struct VmcExtBonePos {
name: String,
transform: Transform3D,
}
impl MessageBehavior for VmcExtBonePos {
fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat();
return OscMessage {
addr: String::from("/VMC/Ext/Bone/Pos"),
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),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
let name: String;
let mut origin = Vector3::new(0.0, 0.0, 0.0);
if msg.args.len() != 8 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![8], actual: msg.args.len(), addr: msg.addr}));
}
match &msg.args[0] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
let mut quat = Quaternion {
x: 0.0,
y: 0.0,
z: 0.0,
w: 0.0,
};
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[6].to_owned(), addr: msg.addr, arg_count: 6})),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[7].to_owned(), addr: msg.addr, arg_count: 7})),
}
return Ok(VmcExtBonePos {
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
});
}
}
#[derive(Debug)]
struct VmcExtBlendVal {
name: String,
value: f32,
}
impl MessageBehavior for VmcExtBlendVal {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Blend/Val"),
args: vec![
OscType::from(self.name.to_owned()),
OscType::from(self.value),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
let name: String;
let value: f32;
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
}
match &msg.args[0] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => value = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
return Ok(VmcExtBlendVal { name, value });
}
}
#[derive(Debug)]
struct VmcExtCam {
name: String,
transform: Transform3D,
fov: f32,
}
impl MessageBehavior for VmcExtCam {
fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat();
return OscMessage {
addr: String::from("/VMC/Ext/Cam"),
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.fov),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 9 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![9], actual: msg.args.len(), addr: msg.addr}));
}
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 fov: f32;
match &msg.args[0] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[6].to_owned(), addr: msg.addr, arg_count: 6})),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[7].to_owned(), addr: msg.addr, arg_count: 7})),
}
match msg.args[8] {
OscType::Float(f) => fov = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[8].to_owned(), addr: msg.addr, arg_count: 8})),
}
return Ok(VmcExtCam {
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
fov,
});
}
}
#[derive(Debug)]
struct VmcExtCon {
active: i32,
name: String,
is_left: i32,
is_touch: i32,
axis: Vector3,
}
impl MessageBehavior for VmcExtCon {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Cam"),
args: vec![
OscType::from(self.active),
OscType::from(self.name.to_owned()),
OscType::from(self.is_left),
OscType::from(self.is_touch),
OscType::from(self.axis.x),
OscType::from(self.axis.y),
OscType::from(self.axis.z),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 7 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![7], actual: msg.args.len(), addr: msg.addr}));
}
let active: i32;
let name: String;
let is_left: i32;
let is_touch: i32;
let mut axis = Vector3::new(0.0, 0.0, 0.0);
match msg.args[0] {
OscType::Int(i) => active = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
if active > 2 || active < 0 {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 2, actual: active, addr: msg.addr, arg_count: 0}));
}
match &msg.args[1] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Int(i) => is_left = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
if is_left < 0 || is_left > 1 {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: active, addr: msg.addr, arg_count: 2}));
}
match msg.args[3] {
OscType::Int(i) => is_touch = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
if is_touch < 0 || is_touch > 1 {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: active, addr: msg.addr, arg_count: 3}));
}
match msg.args[4] {
OscType::Float(f) => axis.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => axis.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => axis.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[6].to_owned(), addr: msg.addr, arg_count: 6})),
}
return Ok(VmcExtCon {
active,
name,
is_left,
is_touch,
axis,
});
}
}
#[derive(Debug)]
struct VmcExtKey {
active: bool,
name: String,
keycode: i32,
}
impl MessageBehavior for VmcExtKey {
fn to_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 from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 3 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![3], actual: msg.args.len(), addr: msg.addr}));
}
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(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match &msg.args[1] {
OscType::String(s) => name = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Int(i) => keycode = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
return Ok(VmcExtKey {
active,
name,
keycode,
});
}
}
#[derive(Debug)]
struct VmcExtMidiNote {
active: bool,
channel: i32,
note: i32,
velocity: f32,
}
impl MessageBehavior for VmcExtMidiNote {
fn to_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.channel),
OscType::from(self.note),
OscType::from(self.velocity),
]);
return OscMessage {
addr: String::from("/VMC/Ext/Midi/Note"),
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
}
let active: bool;
let channel: i32;
let note: i32;
let velocity: f32;
match msg.args[0] {
OscType::Int(i) => {
if i == 1 {
active = true;
} else if i == 0 {
active = false;
} else {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Int(i) => channel = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Int(i) => note = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => velocity = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
return Ok(VmcExtMidiNote {
active,
channel,
note,
velocity,
});
}
}
#[derive(Debug)]
struct VmcExtMidiCcVal {
knob: i32,
value: f32,
}
impl MessageBehavior for VmcExtMidiCcVal {
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 from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
}
let knob: i32;
let value: f32;
match msg.args[0] {
OscType::Int(i) => knob = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => value = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
return Ok(VmcExtMidiCcVal { knob, value });
}
}
#[derive(Debug)]
struct VmcExtMidiCcBit {
knob: i32,
active: bool,
}
impl MessageBehavior for VmcExtMidiCcBit {
fn to_osc_message(&self) -> OscMessage {
let mut args = vec![OscType::from(self.knob)];
if self.active {
args.push(OscType::from(1));
} else {
args.push(OscType::from(0));
}
return OscMessage {
addr: String::from("/VMC/Ext/Midi/CC/Val"),
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
}
let knob: i32;
let active: bool;
match msg.args[0] {
OscType::Int(i) => knob = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Int(i) => {
if i == 0 {
active = false;
} else if i == 1 {
active = true;
} else {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 1}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
return Ok(VmcExtMidiCcBit { knob, active });
}
}
#[derive(Debug)]
struct DeviceTranform {
addr: String,
serial: String,
transform: Transform3D,
}
impl MessageBehavior for DeviceTranform {
fn to_osc_message(&self) -> OscMessage {
let quat = self.transform.basis.to_quat();
return OscMessage {
addr: self.addr.to_owned(),
args: vec![
OscType::from(self.serial.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),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 8 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![8], actual: msg.args.len(), addr: msg.addr}));
}
let serial: 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);
match &msg.args[0] {
OscType::String(s) => serial = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[6].to_owned(), addr: msg.addr, arg_count: 6})),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[7].to_owned(), addr: msg.addr, arg_count: 7})),
}
return Ok(DeviceTranform {
addr: msg.addr.to_owned(),
serial,
transform: Transform3D::new(Basis::from_quat(quat), origin),
});
}
}
#[derive(Debug)]
struct VmcExtRvc {
enable: bool,
port: u16,
ip_addr: Option<String>,
}
impl MessageBehavior for VmcExtRvc {
fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>;
if self.enable {
args = vec![OscType::from(1)];
} else {
args = vec![OscType::from(0)];
}
args.push(OscType::from(self.port as i32));
if self.ip_addr.is_some() {
args.push(OscType::from(self.ip_addr.clone().unwrap()));
}
return OscMessage {
addr: String::from("/VMC/Ext/Rvc"),
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() < 2 || msg.args.len() > 3 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2, 3], actual: msg.args.len(), addr: msg.addr}));
}
let enable: bool;
let port: u16;
let mut ip_addr: Option<String> = None;
match msg.args[0] {
OscType::Int(i) => {
if i == 0 {
enable = false;
} else if i == 1 {
enable = true;
} else {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Int(i) => {
if i >= 0 && i <= 65535 {
port = i as u16;
} else {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 65535, actual: i, addr: msg.addr, arg_count: 1}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
if msg.args.len() == 3 {
match &msg.args[2] {
OscType::String(s) => ip_addr = Some(s.to_owned()),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
}
return Ok(VmcExtRvc {
enable,
port,
ip_addr,
});
}
}
#[derive(Debug)]
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, FromMessageErr> {
if msg.args.len() != 12 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![12], actual: msg.args.len(), addr: msg.addr}));
}
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(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => origin.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => origin.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => origin.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
match msg.args[4] {
OscType::Float(f) => quat.x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4})),
}
match msg.args[5] {
OscType::Float(f) => quat.y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5})),
}
match msg.args[6] {
OscType::Float(f) => quat.z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[6].to_owned(), addr: msg.addr, arg_count: 6})),
}
match msg.args[7] {
OscType::Float(f) => quat.w = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[7].to_owned(), addr: msg.addr, arg_count: 7})),
}
match msg.args[8] {
OscType::Float(f) => color.r = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[8].to_owned(), addr: msg.addr, arg_count: 8})),
}
match msg.args[9] {
OscType::Float(f) => color.g = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[9].to_owned(), addr: msg.addr, arg_count: 9})),
}
match msg.args[10] {
OscType::Float(f) => color.b = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[10].to_owned(), addr: msg.addr, arg_count: 10})),
}
match msg.args[11] {
OscType::Float(f) => color.a = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[11].to_owned(), addr: msg.addr, arg_count: 11})),
}
return Ok(VmcExtLight {
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
color,
});
}
}
#[derive(Debug)]
struct VmcExtVrm {
path: String,
title: String,
hash: Option<String>,
}
impl MessageBehavior for VmcExtVrm {
fn to_osc_message(&self) -> OscMessage {
let mut args = vec![
OscType::from(self.path.to_owned()),
OscType::from(self.title.to_owned()),
];
if self.hash.is_some() {
args.push(OscType::from(self.hash.clone().unwrap()));
}
return OscMessage {
addr: String::from("/Vmc/Ext/VRM"),
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() < 2 || msg.args.len() > 3 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2, 3], actual: msg.args.len(), addr: msg.addr}));
}
let path: String;
let title: String;
let mut hash: Option<String> = None;
match &msg.args[0] {
OscType::String(s) => path = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match &msg.args[1] {
OscType::String(s) => title = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
if msg.args.len() == 3 {
match &msg.args[2] {
OscType::String(s) => hash = Some(s.to_owned()),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
}
return Ok(VmcExtVrm { path, title, hash });
}
}
#[derive(Debug)]
struct VmcExtRemote {
service: String,
json: String,
}
impl MessageBehavior for VmcExtRemote {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Remote"),
args: vec![
OscType::from(self.service.to_owned()),
OscType::from(self.json.to_owned()),
],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
}
let service: String;
let json: String;
match &msg.args[0] {
OscType::String(s) => service = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match &msg.args[1] {
OscType::String(s) => json = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
return Ok(VmcExtRemote { service, json });
}
}
#[derive(Debug)]
struct VmcExtOpt {
option: String,
}
impl MessageBehavior for VmcExtOpt {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Opt"),
args: vec![OscType::from(self.option.to_owned())],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
match &msg.args[0] {
OscType::String(s) => {
return Ok(VmcExtOpt {
option: s.to_owned(),
})
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
}
}
#[derive(Debug)]
struct VmcExtSettingColor {
color: Color,
}
impl MessageBehavior for VmcExtSettingColor {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Setting/Color"),
args: vec![
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, FromMessageErr> {
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
}
let mut color = Color::from_rgba(0.0, 0.0, 0.0, 0.0);
match msg.args[0] {
OscType::Float(f) => color.r = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Float(f) => color.g = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Float(f) => color.b = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Float(f) => color.a = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
return Ok(VmcExtSettingColor { color });
}
}
#[derive(Debug)]
struct VmcExtSettingWin {
is_top_most: bool,
is_transparent: bool,
window_click_through: i32,
hide_border: i32,
}
impl MessageBehavior for VmcExtSettingWin {
fn to_osc_message(&self) -> OscMessage {
let mut args: Vec<OscType>;
if self.is_top_most {
args = vec![OscType::from(1)];
} else {
args = vec![OscType::from(0)];
}
if self.is_transparent {
args.push(OscType::from(1));
} else {
args.push(OscType::from(0));
}
args.append(&mut vec![
OscType::from(self.window_click_through),
OscType::from(self.hide_border),
]);
return OscMessage {
addr: String::from("/VMC/Ext/Setting/Win"),
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
}
let mut result = Self {
is_top_most: false,
is_transparent: false,
window_click_through: -1,
hide_border: -1,
};
match msg.args[0] {
OscType::Int(i) => {
if i == 1 {
result.is_top_most = true;
} else if i != 0 {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
match msg.args[1] {
OscType::Int(i) => {
if i == 1 {
result.is_transparent = true;
} else if i != 0 {
return Err(FromMessageErr::IntRange(IntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 1}));
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
}
match msg.args[2] {
OscType::Int(i) => result.window_click_through = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
}
match msg.args[3] {
OscType::Int(i) => result.hide_border = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
}
return Ok(result);
}
}
#[derive(Debug)]
struct VmcExtConfig {
path: String,
}
impl MessageBehavior for VmcExtConfig {
fn to_osc_message(&self) -> OscMessage {
return OscMessage {
addr: String::from("/VMC/Ext/Config"),
args: vec![OscType::from(self.path.to_owned())],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr }));
}
match &msg.args[0] {
OscType::String(s) => return Ok(VmcExtConfig { path: s.to_owned() }),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
}
}
#[derive(Clone, Copy, Debug)]
enum ThruType {
Float(f32),
Int(i32),
}
#[derive(Debug)]
struct VmcThru {
addr: String,
arg1: String,
arg2: Option<ThruType>
}
impl MessageBehavior for VmcThru {
fn to_osc_message(&self) -> OscMessage {
let mut args = vec![OscType::from(self.arg1.to_owned()
)];
if self.arg2.is_some() {
match self.arg2.unwrap() {
ThruType::Float(val) => args.push(OscType::from(val)),
ThruType::Int(val) => args.push(OscType::from(val)),
}
}
return OscMessage { addr: self.addr.to_owned(), args}
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() > 2 || msg.args.len() < 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1, 2], actual: msg.args.len(), addr: msg.addr}));
}
let arg1: String;
match &msg.args[0] {
OscType::String(s) => arg1 = s.to_owned(),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
}
let mut arg2: Option<ThruType> = None;
if msg.args.len() == 2 {
match msg.args[1] {
OscType::Int(i) => arg2 = Some(ThruType::Int(i)),
OscType::Float(f) => arg2 = Some(ThruType::Float(f)),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0), OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1}))
}
}
return Ok(VmcThru { addr: msg.addr, arg1 , arg2 })
}
}
#[derive(Debug)]
struct VmcExtSetPeriod {
status: i32,
root: i32,
bone: i32,
blendshape: i32,
camera: i32,
devices: i32
}
impl MessageBehavior for VmcExtSetPeriod {
fn to_osc_message(&self) -> OscMessage {
return OscMessage { addr: String::from("/VMC/Ext/Set/Period"), args: vec![
OscType::from(self.status),
OscType::from(self.root),
OscType::from(self.bone),
OscType::from(self.blendshape),
OscType::from(self.camera),
OscType::from(self.devices),
] };
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 6 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![6], actual: msg.args.len(), addr: msg.addr}));
}
let status: i32;
let root: i32;
let bone: i32;
let blendshape: i32;
let camera: i32;
let devices: i32;
match msg.args[0] {
OscType::Int(i) => status = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0}))
}
match msg.args[1] {
OscType::Int(i) => root = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1}))
}
match msg.args[2] {
OscType::Int(i) => bone = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2}))
}
match msg.args[3] {
OscType::Int(i) => blendshape = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3}))
}
match msg.args[4] {
OscType::Int(i) => camera = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[4].to_owned(), addr: msg.addr, arg_count: 4}))
}
match msg.args[5] {
OscType::Int(i) => devices = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[5].to_owned(), addr: msg.addr, arg_count: 5}))
}
return Ok(VmcExtSetPeriod { status, root, bone, blendshape, camera, devices})
}
}
#[derive(Debug)]
struct VmcExtSetEye {
enable: i32,
position: Vector3,
}
impl MessageBehavior for VmcExtSetEye {
fn to_osc_message(&self) -> OscMessage {
return OscMessage{ addr: String::from("/VMC/Ext/Set/Eye"), args: vec![
OscType::from(self.enable),
OscType::from(self.position.x),
OscType::from(self.position.y),
OscType::from(self.position.z),
]
};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
}
let enable: i32;
let x: f32;
let y: f32;
let z: f32;
match msg.args[0] {
OscType::Int(i) => enable = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0}))
}
match msg.args[1] {
OscType::Float(f) => x = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1}))
}
match msg.args[2] {
OscType::Float(f) => y = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2}))
}
match msg.args[3] {
OscType::Float(f) => z = f,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Float(0.0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3}))
}
return Ok(VmcExtSetEye { enable, position: Vector3::new(x, y, z)})
}
}
#[derive(Debug)]
struct VmcExtSetRes {
response: String
}
impl MessageBehavior for VmcExtSetRes {
fn to_osc_message(&self) -> OscMessage {
return OscMessage{addr: String::from("/VMC/Ext/Set/Res"), args: vec![OscType::from(self.response.to_owned())]};
}
fn from_osc_message(msg: OscMessage) -> Result<Self, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
match &msg.args[0] {
OscType::String(s) => return Ok(VmcExtSetRes{response: s.to_owned()}),
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::String(String::new())], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0}))
}
}
}