create constructors for VmcExtCon

This commit is contained in:
Cassandra de la Cruz-Munoz 2024-01-01 12:22:30 -05:00
parent d78afe2234
commit ce00dc2f5b
Signed by: cassdlcm
GPG Key ID: BFEBACEA812DDA70

View File

@ -679,27 +679,36 @@ impl VmcExtCam {
}
#[derive(Debug)]
struct VmcExtCon {
pub struct VmcExtCon {
active: i32,
name: String,
is_left: i32,
is_touch: i32,
is_left: bool,
is_touch: bool,
axis: Vector3,
}
impl VmcMessage for VmcExtCon {
fn to_osc_message(&self) -> OscMessage {
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),]);
return OscMessage {
addr: String::from("/VMC/Ext/Con"),
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),
],
args,
};
}
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
@ -708,8 +717,8 @@ impl VmcMessage for VmcExtCon {
}
let active: i32;
let name: String;
let is_left: i32;
let is_touch: i32;
let is_left: bool;
let is_touch: bool;
let mut axis = Vector3::new(0.0, 0.0, 0.0);
match msg.args[0] {
OscType::Int(i) => active = i,
@ -723,18 +732,26 @@ impl VmcMessage for VmcExtCon {
_ => 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,
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})),
}
if is_left < 0 || is_left > 1 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { 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})),
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}));
}
if is_touch < 0 || is_touch > 1 {
return Err(FromMessageErr::IntRange(FromIntRangeErr { expected_lower: 0, expected_upper: 1, actual: active, 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})),
}
match msg.args[4] {
OscType::Float(f) => axis.x = f,
@ -748,20 +765,35 @@ impl VmcMessage for VmcExtCon {
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})),
}
let boxed_result = Box::new(VmcExtCon {
return Ok(Box::new(VmcExtCon {
active,
name,
is_left,
is_touch,
axis,
});
return Ok(boxed_result);
}));
}
fn get_addr(&self) -> String {
return String::from("/VMC/Ext/Con");
}
}
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)]
struct VmcExtKey {
active: bool,