Skip to content

Commit

Permalink
feat: implement use_calculator for numincrment
Browse files Browse the repository at this point in the history
  • Loading branch information
Sequal32 committed Mar 18, 2023
1 parent 0fb85e2 commit 9425c29
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 52 deletions.
33 changes: 20 additions & 13 deletions src/yourcontrols/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ struct NumIncrementEntry<T> {
#[serde(default)]
// Whether to transmit the client event as a user/aircraft event
is_user_event: bool,
// Whether to use execute_calculator_code to transmit the event
#[serde(default)]
use_calculator: bool,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -860,18 +863,10 @@ impl Definitions {
where
T: Default,
{
let up_event_id = self.events.get_or_map_event_id(&var.up_event_name, false);
let down_event_id = self.events.get_or_map_event_id(&var.down_event_name, false);

let (var_string, _) =
self.add_var_string(category, &var.var_name, var.var_units.as_deref(), data_type)?;

let mut mapping = NumIncrement::new(
up_event_id,
down_event_id,
var.is_user_event,
var.increment_by,
);
let mut mapping = NumIncrement::new(var.is_user_event, var.increment_by);
mapping.set_pass_difference(var.pass_difference);

if let Some(up_event_param) = var.up_event_param {
Expand All @@ -882,6 +877,17 @@ impl Definitions {
mapping.set_down_event_param(down_event_param);
}

if var.use_calculator {
mapping.set_up_event_name(var.up_event_name);
mapping.set_down_event_name(var.down_event_name);
} else {
let up_event_id = self.events.get_or_map_event_id(&var.up_event_name, false);
let down_event_id = self.events.get_or_map_event_id(&var.down_event_name, false);

mapping.set_up_event_id(up_event_id);
mapping.set_down_event_id(down_event_id);
}

Ok((Box::new(mapping), var_string))
}

Expand Down Expand Up @@ -1269,10 +1275,11 @@ impl Definitions {
}

self.current_sync.events.push(Event::JSInput {
instrument: message.instrument_name,
value,
id,
})},
instrument: message.instrument_name,
value,
id,
})
}
JSPayloads::Time {
hour,
minute,
Expand Down
133 changes: 94 additions & 39 deletions src/yourcontrols/src/syncdefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,11 @@ where
}

pub struct NumIncrement<T> {
pub up_event_id: u32,
pub up_event_id: Option<u32>,
pub up_event_name: Option<String>,
pub up_event_param: Option<T>,
pub down_event_id: u32,
pub down_event_id: Option<u32>,
pub down_event_name: Option<String>,
pub down_event_param: Option<T>,
pub is_user_event: bool,
pub increment_amount: T,
Expand All @@ -295,15 +297,12 @@ impl<T> NumIncrement<T>
where
T: Default + ToString,
{
pub fn new(
up_event_id: u32,
down_event_id: u32,
is_user_event: bool,
increment_amount: T,
) -> Self {
pub fn new(is_user_event: bool, increment_amount: T) -> Self {
Self {
up_event_id,
down_event_id,
up_event_id: None,
down_event_id: None,
up_event_name: None,
down_event_name: None,
increment_amount,
is_user_event,
current: Default::default(),
Expand All @@ -324,6 +323,22 @@ where
pub fn set_down_event_param(&mut self, param: T) {
self.down_event_param = Some(param);
}

pub fn set_down_event_name(&mut self, name: String) {
self.down_event_name = Some(format!("K:{}", name));
}

pub fn set_up_event_name(&mut self, name: String) {
self.up_event_name = Some(format!("K:{}", name));
}

pub fn set_down_event_id(&mut self, id: u32) {
self.down_event_id = Some(id);
}

pub fn set_up_event_id(&mut self, id: u32) {
self.up_event_id = Some(id);
}
}

impl<T> Syncable<T> for NumIncrement<T>
Expand All @@ -334,49 +349,89 @@ where
self.current = current
}

fn set_new(&mut self, new: T, conn: &simconnect::SimConnector, _: &mut LVarSyncer) {
fn set_new(&mut self, new: T, conn: &simconnect::SimConnector, lvar_transfer: &mut LVarSyncer) {
let mut working = self.current;
let object_id = if self.is_user_event { 0 } else { 1 };

if self.pass_difference {
if new > self.current {
conn.transmit_client_event(
object_id,
self.up_event_id,
(new - self.current).to_i32().unwrap() as u32,
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
let difference = (new - self.current).to_i32().unwrap() as u32;

if let Some(event_id) = self.up_event_id {
conn.transmit_client_event(
object_id,
event_id,
difference,
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
} else if let Some(event_name) = &self.up_event_name {
lvar_transfer.set_unchecked(conn, event_name, None, &difference.to_string())
}
} else if new < self.current {
conn.transmit_client_event(
object_id,
self.down_event_id,
(self.current - new).to_i32().unwrap() as u32,
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
let difference = (self.current - new).to_i32().unwrap() as u32;

if let Some(event_id) = self.down_event_id {
conn.transmit_client_event(
object_id,
event_id,
difference,
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
} else if let Some(event_name) = &self.up_event_name {
lvar_transfer.set_unchecked(conn, event_name, None, &difference.to_string())
}
}
} else {
while working > new {
working -= self.increment_amount;
conn.transmit_client_event(
object_id,
self.down_event_id,
self.down_event_param.and_then(|x| x.to_u32()).unwrap_or(0),
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);

if let Some(event_id) = self.down_event_id {
conn.transmit_client_event(
object_id,
event_id,
self.down_event_param.and_then(|x| x.to_u32()).unwrap_or(0),
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
} else if let Some(event_name) = &self.down_event_name {
lvar_transfer.set_unchecked(
conn,
event_name,
None,
&self
.down_event_param
.and_then(|x| x.to_u32())
.unwrap_or(0)
.to_string(),
)
}
}

while working < new {
working += self.increment_amount;
conn.transmit_client_event(
object_id,
self.up_event_id,
self.up_event_param.and_then(|x| x.to_u32()).unwrap_or(0),
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);

if let Some(event_id) = self.up_event_id {
conn.transmit_client_event(
object_id,
event_id,
self.up_event_param.and_then(|x| x.to_u32()).unwrap_or(0),
GROUP_ID,
simconnect::SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY,
);
} else if let Some(event_name) = &self.up_event_name {
lvar_transfer.set_unchecked(
conn,
event_name,
None,
&self
.up_event_param
.and_then(|x| x.to_u32())
.unwrap_or(0)
.to_string(),
)
}
}
}
}
Expand Down

0 comments on commit 9425c29

Please sign in to comment.