vmc_rs/src/lib.rs

1815 lines
76 KiB
Rust
Raw Normal View History

2023-12-30 15:38:19 +01:00
use godot::{
builtin::{Basis, Quaternion},
2023-12-30 17:26:34 +01:00
prelude::{Color, Transform3D, Vector3},
2023-12-30 15:38:19 +01:00
};
use rosc::{OscMessage, OscType};
use std::fmt;
2023-12-30 15:38:19 +01:00
pub trait VmcMessage {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage;
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> where Self: Sized;
fn get_addr(&self) -> String;
2023-12-30 15:38:19 +01:00
}
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 FromIntRangeErr {
expected_lower: i32,
expected_upper: i32,
actual: i32,
addr: String,
arg_count: usize,
}
impl fmt::Display for FromIntRangeErr {
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 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 struct AddrErr {
addr: String
}
impl fmt::Display for AddrErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid address: {}", self.addr)
}
}
pub enum FromMessageErr {
ArgCount(ArgCountErr),
ArgType(ArgTypeErr),
IntRange(FromIntRangeErr),
StrVal(StrValErr),
Addr(AddrErr),
}
pub struct CreateIntRangeErr {
expected_lower: i32,
expected_upper: i32,
actual: i32,
calling_func: String,
arg_name: String,
}
impl fmt::Display for CreateIntRangeErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid value of integer argument \"{}\" for function \"{}\". Expected in range [{}, {}], received {}.", self.arg_name, self.calling_func, self.expected_lower, self.expected_upper, self.actual)
}
}
pub struct CreateOptionalArgsErr {
expected_sets: Vec<String>,
actual: String,
calling_func: String,
}
impl fmt::Display for CreateOptionalArgsErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Invalid set of optional arguments for function \"{}\". Expected set from {:?}, received [{}].", self.calling_func, self.expected_sets, self.actual)
}
}
pub enum MessageCreationErr {
IntRange(CreateIntRangeErr),
OptionalArgs(CreateOptionalArgsErr),
}
#[derive(Debug)]
2024-01-01 18:09:38 +01:00
pub struct VmcExtOk {
2023-12-30 15:38:19 +01:00
loaded: i32,
calibration_state: Option<i32>,
calibration_mode: Option<i32>,
tracking_status: Option<i32>,
2023-12-30 14:03:40 +01:00
}
impl VmcMessage for VmcExtOk {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 15:38:19 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2024-01-01 18:26:02 +01:00
let mut result = Self {
2023-12-30 15:38:19 +01:00
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}));
2023-12-30 15:38:19 +01:00
}
}
if result.loaded < 0 || result.loaded > 1 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: result.loaded, addr: msg.addr, arg_count: 0}));
}
2023-12-30 15:38:19 +01:00
} else {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1, 3, 4], actual: msg.args.len(), addr: msg.addr.to_owned()}));
2023-12-30 15:38:19 +01:00
}
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}));
2023-12-30 15:38:19 +01:00
}
}
if result.calibration_state.unwrap() < 0 || result.calibration_state.unwrap() > 3 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 3, actual: result.loaded, addr: msg.addr, arg_count: 1}));
}
2023-12-30 15:38:19 +01:00
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}));
2023-12-30 15:38:19 +01:00
}
}
if result.calibration_mode.unwrap() < 0 || result.calibration_mode.unwrap() > 2 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 2, actual: result.loaded, addr: msg.addr, arg_count: 2}));
}
2023-12-30 15:38:19 +01:00
}
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}));
2023-12-30 15:38:19 +01:00
}
}
if result.tracking_status.unwrap() < 0 || result.tracking_status.unwrap() > 1 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: result.loaded, addr: msg.addr, arg_count: 3}));
}
}
2024-01-01 18:26:02 +01:00
return Ok(Box::new(result));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/OK");
}
}
impl VmcExtOk {
2024-01-01 18:09:38 +01:00
pub fn new(loaded: i32, calibration_state: Option<i32>, calibration_mode: Option<i32>, tracking_status: Option<i32>) -> Result<Self, MessageCreationErr> {
if loaded < 0 || loaded > 1 {
return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 1, actual: loaded, calling_func: String::from("VmcExtOk::new"), arg_name: String::from("loaded")}));
}
if calibration_state.is_some() && calibration_mode.is_some() {
if calibration_state.unwrap() < 0 || calibration_state.unwrap() > 3 {
return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 3, actual: loaded, calling_func: String::from("VmcExtOk::new"), arg_name: String::from("calibration_state")}));
}
if calibration_mode.unwrap() < 0 || calibration_mode.unwrap() > 2 {
return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 2, actual: loaded, calling_func: String::from("VmcExtOk::new"), arg_name: String::from("calibration_mode")}));
}
if tracking_status.is_some() && (tracking_status.unwrap() < 0 || tracking_status.unwrap() > 1) {
return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 1, actual: loaded, calling_func: String::from("VmcExtOk::new"), arg_name: String::from("tracking_status")}));
}
2024-01-01 18:07:54 +01:00
return Ok(Self { loaded, calibration_state, calibration_mode, tracking_status})
}
if calibration_state.is_some() && tracking_status.is_none() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("calibration_state, calibration_mode"), String::from("calibration_state, calibration_mode, tracking_status")], actual: String::from("calibration_state"), calling_func: String::from("VmcExtOk::new")}))
}
if calibration_mode.is_some() && tracking_status.is_none() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("calibration_state, calibration_mode"), String::from("calibration_state, calibration_mode, tracking_status")], actual: String::from("calibration_mode"), calling_func: String::from("VmcExtOk::new")}))
}
if calibration_state.is_some() && tracking_status.is_some() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("calibration_state, calibration_mode"), String::from("calibration_state, calibration_mode, tracking_status")], actual: String::from("calibration_state, tracking_status"), calling_func: String::from("VmcExtOk::new")}))
}
if calibration_mode.is_some() && tracking_status.is_some() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("calibration_state, calibration_mode"), String::from("calibration_state, calibration_mode, tracking_status")], actual: String::from("calibration_mode, tracking_status"), calling_func: String::from("VmcExtOk::new")}))
}
if tracking_status.is_some() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("calibration_state, calibration_mode"), String::from("calibration_state, calibration_mode, tracking_status")], actual: String::from("tracking_status"), calling_func: String::from("VmcExtOk::new")}))
2023-12-30 15:38:19 +01:00
}
2024-01-01 18:07:54 +01:00
return Ok(Self { loaded, calibration_state: None, calibration_mode: None, tracking_status: None})
2023-12-30 15:38:19 +01:00
}
2024-01-01 18:09:38 +01:00
pub fn new_vmc_message(loaded: i32, calibration_state: Option<i32>, calibration_mode: Option<i32>, tracking_status: Option<i32>) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
let msg = Self::new(loaded, calibration_state, calibration_mode, tracking_status);
if msg.is_ok() {
2024-01-01 18:26:02 +01:00
return Ok(Box::new(msg.ok().unwrap()));
}
return Err(msg.err().unwrap());
}
2023-12-30 15:38:19 +01:00
}
2023-12-30 14:03:40 +01:00
#[derive(Debug)]
2024-01-01 18:09:38 +01:00
pub struct VmcExtT {
2023-12-30 15:38:19 +01:00
time: f32,
}
impl VmcMessage for VmcExtT {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 15:38:19 +01:00
return OscMessage {
addr: String::from("/VMC/Ext/T"),
args: vec![OscType::from(self.time)],
};
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 15:38:19 +01:00
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 15:38:19 +01:00
}
let time: f32;
2023-12-30 15:38:19 +01:00
match msg.args[0] {
OscType::Float(f) => 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})),
2023-12-30 15:38:19 +01:00
}
2024-01-01 18:26:02 +01:00
return Ok(Box::new(Self{time}));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/T");
2023-12-30 15:38:19 +01:00
}
}
impl VmcExtT {
2024-01-01 18:09:38 +01:00
pub fn new(time: f32) -> Result<Self, MessageCreationErr> {
2024-01-01 18:07:54 +01:00
return Ok(Self { time });
}
2024-01-01 18:09:38 +01:00
pub fn new_vmc_message(time: f32) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
let msg = Self::new(time);
if msg.is_ok() {
2024-01-01 18:26:02 +01:00
return Ok(Box::new(msg.ok().unwrap()));
}
return Err(msg.err().unwrap());
}
}
#[derive(Debug)]
2024-01-01 18:09:38 +01:00
pub struct VmcExtRootPos {
2023-12-30 15:38:19 +01:00
name: String,
transform: Transform3D,
mr_scale: Option<Vector3>,
mr_offset: Option<Vector3>,
}
impl VmcMessage for VmcExtRootPos {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 15:38:19 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2024-01-01 18:26:02 +01:00
let mut result: Self;
2023-12-30 15:38:19 +01:00
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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}));
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
}
2024-01-01 18:26:02 +01:00
return Ok(Box::new(result));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Root/Pos");
2023-12-30 15:38:19 +01:00
}
}
2024-01-01 18:00:13 +01:00
impl VmcExtRootPos {
2024-01-01 18:09:38 +01:00
pub fn new(name: String, transform: Transform3D, mr_scale: Option<Vector3>, mr_offset: Option<Vector3>) -> Result<Self, MessageCreationErr> {
2024-01-01 18:00:13 +01:00
if mr_scale.is_some() && mr_offset.is_none() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("mr_scale, mr_offset")], actual: String::from("mr_scale"), calling_func: String::from("VmcExtRootPos::new")}));
}
if mr_scale.is_none() && mr_offset.is_some() {
return Err(MessageCreationErr::OptionalArgs(CreateOptionalArgsErr { expected_sets: vec![String::from("None"), String::from("mr_scale, mr_offset")], actual: String::from("mr_offset"), calling_func: String::from("VmcExtRootPos::new")}))
}
2024-01-01 18:07:54 +01:00
return Ok(Self { name, transform, mr_scale, mr_offset});
2024-01-01 18:00:13 +01:00
}
2024-01-01 18:09:38 +01:00
pub fn new_vmc_message(name: String, transform: Transform3D, mr_scale: Option<Vector3>, mr_offset: Option<Vector3>) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
2024-01-01 18:00:13 +01:00
let message = Self::new(name, transform, mr_scale, mr_offset);
if message.is_ok() {
2024-01-01 18:26:02 +01:00
return Ok(Box::new(message.ok().unwrap()));
2024-01-01 18:00:13 +01:00
}
return Err(message.err().unwrap());
}
}
#[derive(Debug)]
2024-01-01 18:09:38 +01:00
pub struct VmcExtBonePos {
2023-12-30 15:38:19 +01:00
name: String,
transform: Transform3D,
}
impl VmcMessage for VmcExtBonePos {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 15:38:19 +01:00
let quat = self.transform.basis.to_quat();
return OscMessage {
2023-12-30 15:44:35 +01:00
addr: String::from("/VMC/Ext/Bone/Pos"),
2023-12-30 15:38:19 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 15:38:19 +01:00
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}));
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
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})),
2023-12-30 15:38:19 +01:00
}
2024-01-01 18:26:02 +01:00
return Ok(Box::new(Self {
2023-12-30 15:38:19 +01:00
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
2024-01-01 18:26:02 +01:00
}));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Bone/Pos");
2023-12-30 14:03:40 +01:00
}
}
2023-12-30 15:44:35 +01:00
2024-01-01 18:03:35 +01:00
impl VmcExtBonePos {
2024-01-01 18:09:38 +01:00
pub fn new(name: String, transform: Transform3D) -> Result<Self, MessageCreationErr> {
2024-01-01 18:07:54 +01:00
return Ok(Self{name, transform});
2024-01-01 18:03:35 +01:00
}
2024-01-01 18:09:38 +01:00
pub fn new_vmc_message(name: String, transform: Transform3D) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
2024-01-01 18:03:35 +01:00
let message = Self::new(name, transform);
if message.is_ok() {
2024-01-01 18:26:02 +01:00
return Ok(Box::new(message.ok().unwrap()));
2024-01-01 18:03:35 +01:00
}
return Err(message.err().unwrap());
}
}
#[derive(Debug)]
2024-01-01 18:09:38 +01:00
pub struct VmcExtBlendVal {
2023-12-30 15:44:35 +01:00
name: String,
value: f32,
}
impl VmcMessage for VmcExtBlendVal {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 15:44:35 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 15:44:35 +01:00
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}));
2023-12-30 15:44:35 +01:00
}
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})),
2023-12-30 15:44:35 +01:00
}
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})),
2023-12-30 15:44:35 +01:00
}
2024-01-01 18:26:02 +01:00
return Ok(Box::new(Self { name, value }));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Blend/Val");
2023-12-30 15:44:35 +01:00
}
}
2023-12-30 16:00:15 +01:00
2024-01-01 18:07:54 +01:00
impl VmcExtBlendVal {
2024-01-01 18:09:38 +01:00
pub fn new(name: String, value: f32) -> Result<Self, MessageCreationErr> {
2024-01-01 18:07:54 +01:00
return Ok(Self{name, value});
}
2024-01-01 18:09:38 +01:00
pub fn new_vmc_message(name: String, value: f32) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
2024-01-01 18:07:54 +01:00
let message = Self::new(name, value);
if message.is_ok() {
let boxed_result = Box::new(message.ok().unwrap());
2024-01-01 18:07:54 +01:00
return Ok(boxed_result);
}
return Err(message.err().unwrap());
}
}
#[derive(Debug)]
pub struct VmcExtCam {
2023-12-30 16:00:15 +01:00
name: String,
transform: Transform3D,
fov: f32,
}
impl VmcMessage for VmcExtCam {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 16:00:15 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:00:15 +01:00
if msg.args.len() != 9 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![9], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
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})),
2023-12-30 16:00:15 +01:00
}
let boxed_result = Box::new(VmcExtCam {
2023-12-30 16:00:15 +01:00
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
fov,
});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Cam");
2023-12-30 16:00:15 +01:00
}
}
2023-12-30 16:17:28 +01:00
impl VmcExtCam {
pub fn new(name: String, transform: Transform3D, fov: f32) ->Result<VmcExtCam, MessageCreationErr> {
return Ok(Self{name, transform, fov})
}
pub fn new_vmc_message(name: String, transform: Transform3D, fov: f32) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
let message = Self::new(name, transform, fov);
if message.is_ok() {
let boxed_result = Box::new(message.ok().unwrap());
return Ok(boxed_result);
}
return Err(message.err().unwrap());
}
}
#[derive(Debug)]
2024-01-01 18:22:30 +01:00
pub struct VmcExtCon {
2023-12-30 16:17:28 +01:00
active: i32,
name: String,
2024-01-01 18:22:30 +01:00
is_left: bool,
is_touch: bool,
2023-12-30 16:17:28 +01:00
axis: Vector3,
}
impl VmcMessage for VmcExtCon {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2024-01-01 18:22:30 +01:00
let mut args = vec![
OscType::from(self.active),
OscType::from(self.name.to_owned())
];
if self.is_left {
args.push(OscType::from(1));
} else {
args.push(OscType::from(0));
}
if self.is_touch {
args.push(OscType::from(1));
} else {
args.push(OscType::from(0));
}
args.append(&mut vec![OscType::from(self.axis.x),
OscType::from(self.axis.y),
OscType::from(self.axis.z),]);
2023-12-30 16:17:28 +01:00
return OscMessage {
addr: String::from("/VMC/Ext/Con"),
2024-01-01 18:22:30 +01:00
args,
2023-12-30 16:17:28 +01:00
};
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:17:28 +01:00
if msg.args.len() != 7 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![7], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 16:17:28 +01:00
}
let active: i32;
let name: String;
2024-01-01 18:22:30 +01:00
let is_left: bool;
let is_touch: bool;
2023-12-30 16:17:28 +01:00
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})),
2023-12-30 16:17:28 +01:00
}
if active > 2 || active < 0 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 2, actual: active, addr: msg.addr, arg_count: 0}));
2023-12-30 16:17:28 +01:00
}
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})),
2023-12-30 16:17:28 +01:00
}
match msg.args[2] {
2024-01-01 18:22:30 +01:00
OscType::Int(i) => {if i == 1 {
is_left = true;
} else if i == 0 {
is_left = false;
} else {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 2}))
}},
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[2].to_owned(), addr: msg.addr, arg_count: 2})),
2023-12-30 16:17:28 +01:00
}
match msg.args[3] {
2024-01-01 18:22:30 +01:00
OscType::Int(i) => {
if i == 1 {
is_touch = true;
} else if i == 0 {
is_touch = false;
} else {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 3}));
}
},
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
2023-12-30 16:17:28 +01:00
}
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})),
2023-12-30 16:17:28 +01:00
}
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})),
2023-12-30 16:17:28 +01:00
}
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})),
2023-12-30 16:17:28 +01:00
}
2024-01-01 18:26:02 +01:00
return Ok(Box::new(Self {
2023-12-30 16:17:28 +01:00
active,
name,
is_left,
is_touch,
axis,
2024-01-01 18:22:30 +01:00
}));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Con");
2023-12-30 16:17:28 +01:00
}
}
2023-12-30 16:29:52 +01:00
2024-01-01 18:22:30 +01:00
impl VmcExtCon {
pub fn new(active: i32, name: String, is_left: bool, is_touch: bool, axis: Vector3) -> Result<Self, MessageCreationErr> {
if active > 2 || active < 0 {
return Err(MessageCreationErr::IntRange(CreateIntRangeErr { expected_lower: 0, expected_upper: 2, actual: active, calling_func: String::from("VmcExtCon::new"), arg_name: String::from("active")}))
}
return Ok(Self{ active, name, is_left, is_touch, axis });
}
pub fn new_vmc_message(active: i32, name: String, is_left: bool, is_touch: bool, axis: Vector3) -> Result<Box<dyn VmcMessage>, MessageCreationErr> {
let message = Self::new(active, name, is_left, is_touch, axis);
if message.is_ok() {
return Ok(Box::new(message.ok().unwrap()));
}
return Err(message.err().unwrap());
}
}
#[derive(Debug)]
2023-12-30 16:29:52 +01:00
struct VmcExtKey {
active: bool,
name: String,
keycode: i32,
}
impl VmcMessage for VmcExtKey {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 16:29:52 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:29:52 +01:00
if msg.args.len() != 3 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![3], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 16:29:52 +01:00
}
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(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
2023-12-30 16:29:52 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
2023-12-30 16:29:52 +01:00
}
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})),
2023-12-30 16:29:52 +01:00
}
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})),
2023-12-30 16:29:52 +01:00
}
let boxed_result = Box::new(VmcExtKey {
2023-12-30 16:29:52 +01:00
active,
name,
keycode,
});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Key");
2023-12-30 16:29:52 +01:00
}
}
2023-12-30 16:35:01 +01:00
#[derive(Debug)]
2023-12-30 16:35:01 +01:00
struct VmcExtMidiNote {
active: bool,
channel: i32,
note: i32,
velocity: f32,
}
impl VmcMessage for VmcExtMidiNote {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 16:35:01 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:35:01 +01:00
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 16:35:01 +01:00
}
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(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
2023-12-30 16:35:01 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
2023-12-30 16:35:01 +01:00
}
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})),
2023-12-30 16:35:01 +01:00
}
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})),
2023-12-30 16:35:01 +01:00
}
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})),
2023-12-30 16:35:01 +01:00
}
let boxed_result = Box::new(VmcExtMidiNote {
2023-12-30 16:35:01 +01:00
active,
channel,
note,
velocity,
});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Midi/Note");
2023-12-30 16:35:01 +01:00
}
}
2023-12-30 16:38:53 +01:00
#[derive(Debug)]
2023-12-30 16:38:53 +01:00
struct VmcExtMidiCcVal {
knob: i32,
value: f32,
}
impl VmcMessage for VmcExtMidiCcVal {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 16:38:53 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:38:53 +01:00
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 16:38:53 +01:00
}
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})),
2023-12-30 16:38:53 +01:00
}
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})),
2023-12-30 16:38:53 +01:00
}
let boxed_result = Box::new(VmcExtMidiCcVal { knob, value });
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Midi/CC/Val");
2023-12-30 16:38:53 +01:00
}
}
2023-12-30 16:41:16 +01:00
#[derive(Debug)]
2023-12-30 16:41:16 +01:00
struct VmcExtMidiCcBit {
knob: i32,
active: bool,
}
impl VmcMessage for VmcExtMidiCcBit {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 16:41:16 +01:00
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/Bit"),
2023-12-30 16:41:16 +01:00
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:41:16 +01:00
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 16:41:16 +01:00
}
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})),
2023-12-30 16:41:16 +01:00
}
match msg.args[1] {
OscType::Int(i) => {
if i == 0 {
active = false;
} else if i == 1 {
active = true;
} else {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 1}));
2023-12-30 16:41:16 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
2023-12-30 16:41:16 +01:00
}
let boxed_result = Box::new(VmcExtMidiCcBit { knob, active });
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Midi/CC/Bit");
2023-12-30 16:41:16 +01:00
}
}
2023-12-30 16:49:48 +01:00
#[derive(Debug)]
2023-12-30 16:49:48 +01:00
struct DeviceTranform {
addr: String,
serial: String,
transform: Transform3D,
}
impl VmcMessage for DeviceTranform {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 16:49:48 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 16:49:48 +01:00
if msg.args.len() != 8 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![8], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 17:03:37 +01:00
}
2023-12-30 16:49:48 +01:00
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
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})),
2023-12-30 16:49:48 +01:00
}
let boxed_result = Box::new(DeviceTranform {
2023-12-30 16:49:48 +01:00
addr: msg.addr.to_owned(),
serial,
transform: Transform3D::new(Basis::from_quat(quat), origin),
});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return self.addr.to_owned();
2023-12-30 16:49:48 +01:00
}
}
2023-12-30 17:03:37 +01:00
#[derive(Debug)]
2023-12-30 17:03:37 +01:00
struct VmcExtRvc {
enable: bool,
port: u16,
ip_addr: Option<String>,
}
impl VmcMessage for VmcExtRvc {
2023-12-30 17:26:34 +01:00
fn to_osc_message(&self) -> OscMessage {
2023-12-30 17:03:37 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:03:37 +01:00
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}));
2023-12-30 17:03:37 +01:00
}
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(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
2023-12-30 17:03:37 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
2023-12-30 17:03:37 +01:00
}
match msg.args[1] {
OscType::Int(i) => {
if i >= 0 && i <= 65535 {
port = i as u16;
} else {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 65535, actual: i, addr: msg.addr, arg_count: 1}));
2023-12-30 17:03:37 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
2023-12-30 17:03:37 +01:00
}
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})),
2023-12-30 17:03:37 +01:00
}
}
let boxed_result = Box::new(VmcExtRvc {
2023-12-30 17:03:37 +01:00
enable,
port,
ip_addr,
});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Rvc");
2023-12-30 17:03:37 +01:00
}
}
2023-12-30 17:26:34 +01:00
#[derive(Debug)]
2023-12-30 17:26:34 +01:00
struct VmcExtLight {
name: String,
transform: Transform3D,
color: Color,
}
impl VmcMessage for VmcExtLight {
2023-12-30 17:26:34 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:26:34 +01:00
if msg.args.len() != 12 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![12], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
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})),
2023-12-30 17:26:34 +01:00
}
let boxed_result = Box::new(VmcExtLight {
2023-12-30 17:26:34 +01:00
name,
transform: Transform3D::new(Basis::from_quat(quat), origin),
color,
});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Light");
2023-12-30 17:26:34 +01:00
}
}
2023-12-30 17:32:33 +01:00
#[derive(Debug)]
2023-12-30 17:32:33 +01:00
struct VmcExtVrm {
path: String,
title: String,
hash: Option<String>,
}
impl VmcMessage for VmcExtVrm {
2023-12-30 17:32:33 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:32:33 +01:00
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}));
2023-12-30 17:32:33 +01:00
}
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})),
2023-12-30 17:32:33 +01:00
}
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})),
2023-12-30 17:32:33 +01:00
}
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})),
2023-12-30 17:32:33 +01:00
}
}
let boxed_result = Box::new(VmcExtVrm { path, title, hash });
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/Vmc/Ext/VRM");
2023-12-30 17:32:33 +01:00
}
}
2023-12-30 17:36:41 +01:00
#[derive(Debug)]
2023-12-30 17:36:41 +01:00
struct VmcExtRemote {
service: String,
json: String,
}
impl VmcMessage for VmcExtRemote {
2023-12-30 17:36:41 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:36:41 +01:00
if msg.args.len() != 2 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![2], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 17:36:41 +01:00
}
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})),
2023-12-30 17:36:41 +01:00
}
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})),
2023-12-30 17:36:41 +01:00
}
let boxed_result = Box::new(VmcExtRemote{ service, json });
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Remote");
2023-12-30 17:36:41 +01:00
}
}
2023-12-30 17:40:04 +01:00
#[derive(Debug)]
2023-12-30 17:40:04 +01:00
struct VmcExtOpt {
option: String,
}
impl VmcMessage for VmcExtOpt {
2023-12-30 17:40:04 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:40:04 +01:00
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 17:40:04 +01:00
}
let option: String;
2023-12-30 17:40:04 +01:00
match &msg.args[0] {
OscType::String(s) =>
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})),
2023-12-30 17:40:04 +01:00
}
let boxed_result = Box::new(VmcExtOpt{option});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Opt");
2023-12-30 17:40:04 +01:00
}
}
2023-12-30 17:44:28 +01:00
#[derive(Debug)]
2023-12-30 17:44:28 +01:00
struct VmcExtSettingColor {
color: Color,
}
impl VmcMessage for VmcExtSettingColor {
2023-12-30 17:44:28 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:44:28 +01:00
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 17:44:28 +01:00
}
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})),
2023-12-30 17:44:28 +01:00
}
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})),
2023-12-30 17:44:28 +01:00
}
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})),
2023-12-30 17:44:28 +01:00
}
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})),
2023-12-30 17:44:28 +01:00
}
let boxed_result = Box::new(VmcExtSettingColor{color});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Setting/Color");
2023-12-30 17:44:28 +01:00
}
}
2023-12-30 17:52:50 +01:00
#[derive(Debug)]
2023-12-30 17:52:50 +01:00
struct VmcExtSettingWin {
is_top_most: bool,
is_transparent: bool,
window_click_through: i32,
hide_border: i32,
}
impl VmcMessage for VmcExtSettingWin {
2023-12-30 17:52:50 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-30 17:52:50 +01:00
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
2023-12-30 17:52:50 +01:00
}
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(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 0}));
2023-12-30 17:52:50 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0})),
2023-12-30 17:52:50 +01:00
}
match msg.args[1] {
OscType::Int(i) => {
if i == 1 {
result.is_transparent = true;
} else if i != 0 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: i, addr: msg.addr, arg_count: 1}));
2023-12-30 17:52:50 +01:00
}
}
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[1].to_owned(), addr: msg.addr, arg_count: 1})),
2023-12-30 17:52:50 +01:00
}
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})),
2023-12-30 17:52:50 +01:00
}
match msg.args[3] {
2023-12-30 17:52:50 +01:00
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})),
2023-12-30 17:52:50 +01:00
}
let boxed_result = Box::new(result);
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Setting/Win");
2023-12-30 17:52:50 +01:00
}
}
2023-12-30 17:56:22 +01:00
#[derive(Debug)]
2023-12-30 17:56:22 +01:00
struct VmcExtConfig {
path: String,
}
impl VmcMessage for VmcExtConfig {
2023-12-30 17:56:22 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr }));
2023-12-30 17:56:22 +01:00
}
let path: String;
2023-12-30 17:56:22 +01:00
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})),
2023-12-30 17:56:22 +01:00
}
let boxed_result = Box::new(VmcExtConfig{path});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Config");
2023-12-30 17:56:22 +01:00
}
}
2023-12-31 16:16:57 +01:00
#[derive(Clone, Copy, Debug)]
2023-12-31 16:16:57 +01:00
enum ThruType {
Float(f32),
Int(i32),
}
#[derive(Debug)]
2023-12-31 16:16:57 +01:00
struct VmcThru {
addr: String,
arg1: String,
arg2: Option<ThruType>
}
impl VmcMessage for VmcThru {
2023-12-31 16:16:57 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-31 16:16:57 +01:00
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}));
2023-12-31 16:16:57 +01:00
}
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})),
2023-12-31 16:16:57 +01:00
}
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}))
2023-12-31 16:16:57 +01:00
}
}
let boxed_result = Box::new(VmcThru { addr: msg.addr, arg1 , arg2 });
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return self.addr.to_owned();
2023-12-31 16:16:57 +01:00
}
}
2023-12-31 16:24:07 +01:00
#[derive(Debug)]
2023-12-31 16:24:07 +01:00
struct VmcExtSetPeriod {
status: i32,
root: i32,
bone: i32,
blendshape: i32,
camera: i32,
devices: i32
}
impl VmcMessage for VmcExtSetPeriod {
2023-12-31 16:24:07 +01:00
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),
2023-12-31 16:31:48 +01:00
] };
2023-12-31 16:24:07 +01:00
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-31 16:24:07 +01:00
if msg.args.len() != 6 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![6], actual: msg.args.len(), addr: msg.addr}));
2023-12-31 16:24:07 +01:00
}
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}))
2023-12-31 16:24:07 +01:00
}
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}))
2023-12-31 16:24:07 +01:00
}
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}))
2023-12-31 16:24:07 +01:00
}
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}))
2023-12-31 16:24:07 +01:00
}
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}))
2023-12-31 16:24:07 +01:00
}
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}))
2023-12-31 16:24:07 +01:00
}
let boxed_result = Box::new(VmcExtSetPeriod { status, root, bone, blendshape, camera, devices});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Set/Period");
2023-12-31 16:24:07 +01:00
}
}
2023-12-31 16:31:48 +01:00
#[derive(Debug)]
2023-12-31 16:31:48 +01:00
struct VmcExtSetEye {
enable: i32,
position: Vector3,
}
impl VmcMessage for VmcExtSetEye {
2023-12-31 16:31:48 +01:00
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<Box<dyn VmcMessage>, FromMessageErr> {
2023-12-31 16:31:48 +01:00
if msg.args.len() != 4 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![4], actual: msg.args.len(), addr: msg.addr}));
2023-12-31 16:31:48 +01:00
}
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}))
2023-12-31 16:31:48 +01:00
}
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}))
2023-12-31 16:31:48 +01:00
}
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}))
2023-12-31 16:31:48 +01:00
}
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}))
2023-12-31 16:31:48 +01:00
}
let boxed_result = Box::new(VmcExtSetEye { enable, position: Vector3::new(x, y, z)});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Set/Eye");
2023-12-31 16:31:48 +01:00
}
}
#[derive(Debug)]
struct VmcExtSetRes {
response: String
}
impl VmcMessage 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<Box<dyn VmcMessage>, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
let response: String;
match &msg.args[0] {
OscType::String(s) => 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}))
}
let boxed_result = Box::new(VmcExtSetRes{response});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Set/Res");
}
}
#[derive(Debug)]
struct VmcExtSetCalibExec {
mode: i32
}
impl VmcMessage for VmcExtSetCalibExec {
fn to_osc_message(&self) -> OscMessage {
return OscMessage{addr: String::from("/VMC/Ext/Set/Calib/Exec"), args: vec![OscType::from(self.mode)]};
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
let mode: i32;
match msg.args[0] {
OscType::Int(i) => mode = i,
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[0].to_owned(), addr: msg.addr, arg_count: 0}))
}
let boxed_result = Box::new(VmcExtSetCalibExec{mode});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Set/Calib/Exec");
}
}
#[derive(Debug)]
struct VmcExtSetConfig {
path: String
}
impl VmcMessage for VmcExtSetConfig {
fn to_osc_message(&self) -> OscMessage {
return OscMessage{addr: String::from("/VMC/Ext/Set/Res"), args: vec![OscType::from(self.path.to_owned())]};
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
if msg.args.len() != 1 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![1], actual: msg.args.len(), addr: msg.addr}));
}
let path: String;
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}))
}
let boxed_result = Box::new(VmcExtSetConfig{path});
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Set/Res");
}
}
#[derive(Debug)]
struct VmcMessageNoArgs {
addr: String
}
impl VmcMessage for VmcMessageNoArgs {
fn to_osc_message(&self) -> OscMessage {
return OscMessage { addr: self.addr.to_owned(), args: Vec::new()}
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
if msg.args.len() != 0 {
return Err(FromMessageErr::ArgCount(ArgCountErr { expected_in: vec![0], actual: msg.args.len(), addr: msg.addr}))
}
let boxed_result = Box::new(VmcMessageNoArgs { addr: msg.addr });
return Ok(boxed_result);
}
fn get_addr(&self) -> String {
return self.addr.to_owned();
}
}
pub fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
match msg.addr.as_str() {
"/VMC/Ext/OK" => VmcExtOk::from_osc_message(msg),
"/VMC/Ext/T" => VmcExtT::from_osc_message(msg),
"/VMC/Ext/Root/Pos" => VmcExtRootPos::from_osc_message(msg),
"/VMC/Ext/Bone/Pos" => VmcExtBonePos::from_osc_message(msg),
"/VMC/Ext/Blend/Val" => VmcExtBlendVal::from_osc_message(msg),
"/VMC/Ext/Blend/Apply" | "/VMC/Ext/Set/Req" | "/VMC/Ext/Set/Calib/Ready" => VmcMessageNoArgs::from_osc_message(msg),
"/VMC/Ext/Cam" => VmcExtCam::from_osc_message(msg),
"/VMC/Ext/Con" => VmcExtCon::from_osc_message(msg),
"/VMC/Ext/Key" => VmcExtKey::from_osc_message(msg),
"/VMC/Ext/Midi/Note" => VmcExtMidiNote::from_osc_message(msg),
"/VMC/Ext/Midi/CC/Val" => VmcExtMidiCcVal::from_osc_message(msg),
"/VMC/Ext/Midi/CC/Bit" => VmcExtMidiCcBit::from_osc_message(msg),
"/VMC/Ext/Hmd/Pos" | "/VMC/Ext/Con/Pos" | "/VMC/Ext/Tra/Pos" | "/VMC/Ext/Hmd/Pos/Local" | "/VMC/Ext/Con/Pos/Local" | "/VMC/Ext/Tra/Pos/Local" => DeviceTranform::from_osc_message(msg),
"/VMC/Ext/Rcv" => VmcExtRvc::from_osc_message(msg),
"/VMC/Ext/Light" => VmcExtLight::from_osc_message(msg),
"/VMC/Ext/VRM" => VmcExtVrm::from_osc_message(msg),
"/VMC/Ext/Remote" => VmcExtRemote::from_osc_message(msg),
"/VMC/Ext/Opt" => VmcExtOpt::from_osc_message(msg),
"/VMC/Ext/Setting/Color" => VmcExtSettingColor::from_osc_message(msg),
"/VMC/Ext/Setting/Win" => VmcExtSettingWin::from_osc_message(msg),
"/VMC/Ext/Config" => VmcExtConfig::from_osc_message(msg),
"/VMC/Ext/Set/Period" => VmcExtSetPeriod::from_osc_message(msg),
"/VMC/Ext/Set/Eye" => VmcExtSetEye::from_osc_message(msg),
"/VMC/Ext/Set/Res" => VmcExtSetRes::from_osc_message(msg),
"/VMC/Ext/Set/Calib/Exec" => VmcExtSetCalibExec::from_osc_message(msg),
"/VMC/Ext/Set/Config" => VmcExtSetConfig::from_osc_message(msg),
_ => {
if &msg.addr[0..10] == "/VMC/Thru/" {
return VmcThru::from_osc_message(msg)
}
Err(FromMessageErr::Addr(AddrErr { addr: msg.addr }))
}
}
}