Skip to content
163 changes: 74 additions & 89 deletions dotlottie-rs/src/state_machine_engine/inputs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the rustc_hash instead


use serde::Deserialize;

Expand All @@ -15,128 +15,113 @@ pub enum Input {
#[derive(Clone, Debug)]
pub enum InputValue {
Numeric(f32),
String(String),
Boolean(bool),
String(String),
Event(String),
}

pub trait InputTrait {
fn set_initial_boolean(&mut self, key: &str, value: bool);
fn set_initial_string(&mut self, key: &str, value: String);
fn set_initial_numeric(&mut self, key: &str, value: f32);
fn set_initial_event(&mut self, key: &str, value: &str);
fn new() -> Self;
fn set_boolean(&mut self, key: &str, value: bool) -> Option<InputValue>;
fn set_string(&mut self, key: &str, value: String) -> Option<InputValue>;
fn set_numeric(&mut self, key: &str, value: f32) -> Option<InputValue>;
fn get_numeric(&self, key: &str) -> Option<f32>;
fn get_string(&self, key: &str) -> Option<String>;
fn get_boolean(&self, key: &str) -> Option<bool>;
fn get_event(&self, key: &str) -> Option<String>;
fn reset(&mut self, key: &str) -> Option<(InputValue, InputValue)>;
}

pub struct InputManager {
pub inputs: HashMap<String, InputValue>,
default_values: HashMap<String, InputValue>,
pub(super) numeric: HashMap<String, (f32, f32)>,
pub(super) boolean: HashMap<String, (bool, bool)>,
pub(super) string: HashMap<String, (String, String)>,
pub(super) event: HashSet<String>,
}

impl InputTrait for InputManager {
fn new() -> Self {
let inputs = HashMap::new();

// Store defaults
let default_values = inputs.clone();

InputManager {
inputs,
default_values,
}
impl Default for InputManager {
fn default() -> Self {
Self::new()
}
}

fn reset(&mut self, key: &str) -> Option<(InputValue, InputValue)> {
if let Some(default_value) = self.default_values.get(key) {
return Some((
self.inputs
.insert(key.to_string(), default_value.clone())
.unwrap_or(default_value.clone()),
default_value.clone(),
));
impl InputManager {
pub fn new() -> Self {
Self {
numeric: HashMap::new(),
boolean: HashMap::new(),
string: HashMap::new(),
event: HashSet::new(),
}

None
}

fn set_numeric(&mut self, key: &str, value: f32) -> Option<InputValue> {
self.inputs
.insert(key.to_string(), InputValue::Numeric(value))
pub fn len(&self) -> usize {
self.numeric.len() + self.boolean.len() + self.string.len() + self.event.len()
}

// Get methods for each type
fn get_numeric(&self, key: &str) -> Option<f32> {
match self.inputs.get(key) {
Some(InputValue::Numeric(value)) => Some(*value),
_ => None,
}
pub fn is_empty(&self) -> bool {
self.numeric.is_empty()
&& self.boolean.is_empty()
&& self.string.is_empty()
&& self.event.is_empty()
}

fn set_string(&mut self, key: &str, value: String) -> Option<InputValue> {
self.inputs
.insert(key.to_string(), InputValue::String(value))
pub fn set_initial_numeric(&mut self, key: &str, value: f32) {
self.numeric.insert(key.to_string(), (value, value));
}

fn get_string(&self, key: &str) -> Option<String> {
match self.inputs.get(key) {
Some(InputValue::String(value)) => Some(value.clone()),
_ => None,
}
pub fn set_initial_string(&mut self, key: &str, value: &str) {
self.string
.insert(key.to_string(), (value.to_string(), value.to_string()));
}

fn set_boolean(&mut self, key: &str, value: bool) -> Option<InputValue> {
self.inputs
.insert(key.to_string(), InputValue::Boolean(value))
pub fn set_initial_boolean(&mut self, key: &str, value: bool) {
self.boolean.insert(key.to_string(), (value, value));
}

fn get_boolean(&self, key: &str) -> Option<bool> {
match self.inputs.get(key) {
Some(InputValue::Boolean(value)) => Some(*value),
_ => None,
}
pub fn set_initial_event(&mut self, key: &str) {
self.event.insert(key.to_string());
}

fn get_event(&self, key: &str) -> Option<String> {
match self.inputs.get(key) {
Some(InputValue::Event(value)) => Some(value.clone()),
_ => None,
}
pub fn set_numeric(&mut self, key: &str, value: f32) -> Option<f32> {
let (current, _) = self.numeric.get_mut(key)?;
let old = *current;
*current = value;
Some(old)
}

fn set_initial_numeric(&mut self, key: &str, value: f32) {
self.inputs
.insert(key.to_string(), InputValue::Numeric(value));
pub fn set_boolean(&mut self, key: &str, value: bool) -> Option<bool> {
let (current, _) = self.boolean.get_mut(key)?;
let old = *current;
*current = value;
Some(old)
}

self.default_values
.insert(key.to_string(), InputValue::Numeric(value));
pub fn set_string(&mut self, key: &str, value: &str) -> Option<String> {
let (current, _) = self.string.get_mut(key)?;
Some(std::mem::replace(current, value.to_string()))
}

fn set_initial_string(&mut self, key: &str, value: String) {
self.inputs
.insert(key.to_string(), InputValue::String(value.clone()));
pub fn get_numeric(&self, key: &str) -> Option<f32> {
self.numeric.get(key).map(|(v, _)| *v)
}

self.default_values
.insert(key.to_string(), InputValue::String(value.clone()));
pub fn get_boolean(&self, key: &str) -> Option<bool> {
self.boolean.get(key).map(|(v, _)| *v)
}

fn set_initial_boolean(&mut self, key: &str, value: bool) {
self.inputs
.insert(key.to_string(), InputValue::Boolean(value));
pub fn get_string(&self, key: &str) -> Option<&str> {
self.string.get(key).map(|(v, _)| v.as_str())
}

self.default_values
.insert(key.to_string(), InputValue::Boolean(value));
pub fn get_event(&self, key: &str) -> Option<&str> {
self.event.get(key).map(|s| s.as_str())
}

fn set_initial_event(&mut self, key: &str, value: &str) {
self.inputs
.insert(key.to_string(), InputValue::Event(value.to_string()));
pub fn reset(&mut self, key: &str) -> Option<(InputValue, InputValue)> {
if let Some((current, default)) = self.numeric.get_mut(key) {
let old = InputValue::Numeric(*current);
*current = *default;
return Some((old, InputValue::Numeric(*default)));
}
if let Some((current, default)) = self.boolean.get_mut(key) {
let old = InputValue::Boolean(*current);
*current = *default;
return Some((old, InputValue::Boolean(*default)));
}
if let Some((current, default)) = self.string.get_mut(key) {
let new_val = default.clone();
let old_val = std::mem::replace(current, new_val.clone());
return Some((InputValue::String(old_val), InputValue::String(new_val)));
}
None
}
}
Loading