create constructors for VmcExtCon
This commit is contained in:
parent
d78afe2234
commit
ce00dc2f5b
82
src/lib.rs
82
src/lib.rs
|
@ -679,27 +679,36 @@ impl VmcExtCam {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct VmcExtCon {
|
pub struct VmcExtCon {
|
||||||
active: i32,
|
active: i32,
|
||||||
name: String,
|
name: String,
|
||||||
is_left: i32,
|
is_left: bool,
|
||||||
is_touch: i32,
|
is_touch: bool,
|
||||||
axis: Vector3,
|
axis: Vector3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VmcMessage for VmcExtCon {
|
impl VmcMessage for VmcExtCon {
|
||||||
fn to_osc_message(&self) -> OscMessage {
|
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 {
|
return OscMessage {
|
||||||
addr: String::from("/VMC/Ext/Con"),
|
addr: String::from("/VMC/Ext/Con"),
|
||||||
args: vec![
|
args,
|
||||||
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<Box<dyn VmcMessage>, FromMessageErr> {
|
fn from_osc_message(msg: OscMessage) -> Result<Box<dyn VmcMessage>, FromMessageErr> {
|
||||||
|
@ -708,8 +717,8 @@ impl VmcMessage for VmcExtCon {
|
||||||
}
|
}
|
||||||
let active: i32;
|
let active: i32;
|
||||||
let name: String;
|
let name: String;
|
||||||
let is_left: i32;
|
let is_left: bool;
|
||||||
let is_touch: i32;
|
let is_touch: bool;
|
||||||
let mut axis = Vector3::new(0.0, 0.0, 0.0);
|
let mut axis = Vector3::new(0.0, 0.0, 0.0);
|
||||||
match msg.args[0] {
|
match msg.args[0] {
|
||||||
OscType::Int(i) => active = i,
|
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})),
|
_ => 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] {
|
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})),
|
_ => 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] {
|
match msg.args[3] {
|
||||||
OscType::Int(i) => is_touch = i,
|
OscType::Int(i) => {
|
||||||
_ => return Err(FromMessageErr::ArgType(ArgTypeErr { expected: vec![OscType::Int(0)], actual: msg.args[3].to_owned(), addr: msg.addr, arg_count: 3})),
|
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] {
|
match msg.args[4] {
|
||||||
OscType::Float(f) => axis.x = f,
|
OscType::Float(f) => axis.x = f,
|
||||||
|
@ -748,20 +765,35 @@ impl VmcMessage for VmcExtCon {
|
||||||
OscType::Float(f) => axis.z = f,
|
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 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,
|
active,
|
||||||
name,
|
name,
|
||||||
is_left,
|
is_left,
|
||||||
is_touch,
|
is_touch,
|
||||||
axis,
|
axis,
|
||||||
});
|
}));
|
||||||
return Ok(boxed_result);
|
|
||||||
}
|
}
|
||||||
fn get_addr(&self) -> String {
|
fn get_addr(&self) -> String {
|
||||||
return String::from("/VMC/Ext/Con");
|
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)]
|
#[derive(Debug)]
|
||||||
struct VmcExtKey {
|
struct VmcExtKey {
|
||||||
active: bool,
|
active: bool,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user