Skip to content

Commit

Permalink
fix: pointer enter / exit on pointer move working
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelOsborne committed Dec 12, 2024
1 parent b6befd8 commit fb0d462
Show file tree
Hide file tree
Showing 15 changed files with 600 additions and 89 deletions.
3 changes: 3 additions & 0 deletions dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.function("stateMachineSetNumericTrigger", &DotLottiePlayer::state_machine_set_numeric_trigger)
.function("stateMachineSetStringTrigger", &DotLottiePlayer::state_machine_set_string_trigger)
.function("stateMachineSetBooleanTrigger", &DotLottiePlayer::state_machine_set_boolean_trigger)
.function("stateMachineGetNumericTrigger", &DotLottiePlayer::state_machine_get_numeric_trigger)
.function("stateMachineGetStringTrigger", &DotLottiePlayer::state_machine_get_string_trigger)
.function("stateMachineGetBooleanTrigger", &DotLottiePlayer::state_machine_get_boolean_trigger)
.function("getLayerBounds", &DotLottiePlayer::get_layer_bounds)
.function("stateMachineCurrentState", &DotLottiePlayer::state_machine_current_state)
.function("stateMachinePostPointerDownEvent", &DotLottiePlayer::state_machine_post_pointer_down_event)
Expand Down
3 changes: 3 additions & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ interface DotLottiePlayer {
boolean state_machine_set_boolean_trigger([ByRef] string key, boolean value);
boolean state_machine_set_string_trigger([ByRef] string key, [ByRef] string value);
boolean state_machine_set_numeric_trigger([ByRef] string key, f32 value);
f32 state_machine_get_numeric_trigger([ByRef] string key);
string state_machine_get_string_trigger([ByRef] string key);
boolean state_machine_get_boolean_trigger([ByRef] string key);
string state_machine_current_state();
boolean state_machine_subscribe(StateMachineObserver observer);
boolean state_machine_unsubscribe([ByRef] StateMachineObserver observer);
Expand Down
3 changes: 3 additions & 0 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,7 @@ interface DotLottiePlayer {
boolean state_machine_set_boolean_trigger([ByRef] string key, boolean value);
boolean state_machine_set_string_trigger([ByRef] string key, [ByRef] string value);
boolean state_machine_set_numeric_trigger([ByRef] string key, f32 value);
f32 state_machine_get_numeric_trigger([ByRef] string key);
string state_machine_get_string_trigger([ByRef] string key);
boolean state_machine_get_boolean_trigger([ByRef] string key);
};
45 changes: 45 additions & 0 deletions dotlottie-ffi/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,51 @@ pub unsafe extern "C" fn dotlottie_state_machine_set_boolean_trigger(
})
}

// #[no_mangle]
// pub unsafe extern "C" fn dotlottie_state_machine_get_boolean_trigger(
// ptr: *mut DotLottiePlayer,
// key: *const c_char,
// ) -> bool {
// exec_dotlottie_player_op(ptr, |dotlottie_player| {
// if let Ok(key) = DotLottieString::read(key) {
// dotlottie_player.state_machine_get_boolean_trigger(&key)
// } else {
// false
// }
// })
// }

// #[no_mangle]
// pub unsafe extern "C" fn dotlottie_state_machine_get_string_trigger(
// ptr: *mut DotLottiePlayer,
// key: *const c_char,
// result: *mut types::DotLottieString,
// ) -> i32 {
// exec_dotlottie_player_op(ptr, |dotlottie_player| {
// if let Ok(key) = DotLottieString::read(key) {
// dotlottie_player
// .state_machine_get_string_trigger(&key)
// .copy(result);
// } else {
// DOTLOTTIE_INVALID_PARAMETER
// }
// })
// }

// #[no_mangle]
// pub unsafe extern "C" fn dotlottie_state_machine_get_numeric_trigger(
// ptr: *mut DotLottiePlayer,
// key: *const c_char,
// ) -> f32 {
// exec_dotlottie_player_op(ptr, |dotlottie_player| {
// if let Ok(key) = DotLottieString::read(key) {
// dotlottie_player.state_machine_get_numeric_trigger(&key)
// } else {
// f32::MIN
// }
// })
// }

#[no_mangle]
pub unsafe extern "C" fn dotlottie_state_machine_framework_setup(
ptr: *mut DotLottiePlayer,
Expand Down
63 changes: 59 additions & 4 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,21 +1392,28 @@ impl DotLottiePlayer {
listener_types.push("PointerDown".to_string())
}
crate::listeners::Listener::PointerEnter { .. } => {
// Push PointerMove so that can determine if the pointer entered the layer
listener_types.push("PointerMove".to_string())
// In case framework self detects pointer entering layers, push pointerExit
listener_types.push("PointerEnter".to_string());
// We push PointerMove too so that we can do hit detection instead of the framework
listener_types.push("PointerMove".to_string());
}
crate::listeners::Listener::PointerMove { .. } => {
listener_types.push("PointerMove".to_string())
}
crate::listeners::Listener::PointerExit { .. } => {
// Push PointerMove so that can determine if the pointer exited the layer
listener_types.push("PointerMove".to_string())
// In case framework self detects pointer exiting layers, push pointerExit
listener_types.push("PointerExit".to_string());
// We push PointerMove too so that we can do hit detection instead of the framework
listener_types.push("PointerMove".to_string());
}
crate::listeners::Listener::OnComplete { .. } => {
listener_types.push("OnComplete".to_string())
}
}
}

listener_types.sort();
listener_types.dedup();
listener_types
} else {
vec![]
Expand Down Expand Up @@ -1518,6 +1525,53 @@ impl DotLottiePlayer {
}
}

pub fn state_machine_get_numeric_trigger(&self, key: &str) -> f32 {
match self.state_machine.try_read() {
Ok(state_machine) => {
if let Some(sm) = &*state_machine {
if let Some(value) = sm.get_numeric_trigger(key) {
return value;
}
}
}
Err(_) => {
return f32::MIN;
}
}

f32::MIN
}

pub fn state_machine_get_string_trigger(&self, key: &str) -> String {
match self.state_machine.try_write() {
Ok(mut state_machine) => {
if let Some(sm) = state_machine.as_mut() {
if let Some(value) = sm.get_string_trigger(key) {
return value;
}
}
}
Err(_) => return "".to_string(),
}

"".to_string()
}

pub fn state_machine_get_boolean_trigger(&self, key: &str) -> bool {
match self.state_machine.try_write() {
Ok(mut state_machine) => {
if let Some(sm) = state_machine.as_mut() {
if let Some(value) = sm.get_boolean_trigger(key) {
return value;
}
}
}
Err(_) => return false,
}

false
}

pub fn state_machine_fire_event(&self, event: &str) {
if let Ok(mut state_machine) = self.state_machine.try_write() {
if let Some(sm) = state_machine.as_mut() {
Expand Down Expand Up @@ -1810,6 +1864,7 @@ impl DotLottiePlayer {
return "".to_string();
}
}

"".to_string()
}
}
Expand Down
31 changes: 7 additions & 24 deletions dotlottie-rs/src/state_machine_engine/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ pub trait ActionTrait {
) -> Result<(), StateMachineActionError>;
}

// Todo:
// - FireCustomEvent
// - Reset
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all_fields = "camelCase")]
#[serde(tag = "type")]
pub enum Action {
OpenUrl {
url: String,
},
Theme {
theme_id: String,
},
Increment {
trigger_name: String,
value: Option<StringNumber>,
Expand Down Expand Up @@ -215,14 +215,14 @@ impl ActionTrait for Action {

Ok(())
}
// Todo: Add support for setting a trigger to a trigger value
Action::Fire { trigger_name } => {
let _ = engine.fire(trigger_name, run_pipeline);
Ok(())
}
Action::Reset { trigger_name } => {
todo!("Reset trigger {}", trigger_name);
// Ok(())
Action::Reset { trigger_name: _ } => {
// todo!("Reset trigger {}", trigger_name);

Ok(())
}
Action::SetExpression {
layer_name,
Expand Down Expand Up @@ -360,23 +360,6 @@ impl ActionTrait for Action {

Ok(())
}
Action::Theme { theme_id } => {
let read_lock = player.read();

match read_lock {
Ok(player) => {
if !player.set_theme(theme_id) {
return Err(StateMachineActionError::ExecuteError(
"Error loading theme".to_string(),
));
}
Ok(())
}
Err(_) => Err(StateMachineActionError::ExecuteError(
"Error getting read lock on player".to_string(),
)),
}
}
}
}
}
22 changes: 22 additions & 0 deletions dotlottie-rs/src/state_machine_engine/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ impl EventName for Event {
}
}
}

#[macro_export]
macro_rules! event_type_name {
(PointerDown) => {
"PointerDown"
};
(PointerUp) => {
"PointerUp"
};
(PointerMove) => {
"PointerMove"
};
(PointerEnter) => {
"PointerEnter"
};
(PointerExit) => {
"PointerExit"
};
(OnComplete) => {
"OnComplete"
};
}
9 changes: 2 additions & 7 deletions dotlottie-rs/src/state_machine_engine/listeners/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub enum Listener {
actions: Vec<Action>,
},
PointerMove {
layer_name: Option<String>,
actions: Vec<Action>,
},
PointerExit {
Expand Down Expand Up @@ -75,12 +74,8 @@ impl Display for Listener {
.field("layer_name", layer_name)
.field("action", actions)
.finish(),
Self::PointerMove {
layer_name,
actions,
} => f
Self::PointerMove { actions } => f
.debug_struct("PointerUp")
.field("layer_name", layer_name)
.field("action", actions)
.finish(),
Self::PointerExit {
Expand Down Expand Up @@ -109,7 +104,7 @@ impl ListenerTrait for Listener {
Listener::PointerUp { layer_name, .. } => layer_name.clone(),
Listener::PointerDown { layer_name, .. } => layer_name.clone(),
Listener::PointerEnter { layer_name, .. } => layer_name.clone(),
Listener::PointerMove { layer_name, .. } => layer_name.clone(),
Listener::PointerMove { .. } => None,
Listener::PointerExit { layer_name, .. } => layer_name.clone(),
Listener::OnComplete { .. } => None,
}
Expand Down
Loading

0 comments on commit fb0d462

Please sign in to comment.