Skip to content

Commit

Permalink
fix: pointer enter / exit on pointer move working (#272)
Browse files Browse the repository at this point in the history
* fix: pointer enter / exit on pointer move working
  • Loading branch information
samuelOsborne authored Dec 18, 2024
1 parent b6befd8 commit 4e3e4e6
Show file tree
Hide file tree
Showing 16 changed files with 799 additions and 150 deletions.
2 changes: 1 addition & 1 deletion deps/modules/thorvg
Submodule thorvg updated 63 files
+4 −1 AUTHORS
+15 −18 examples/Example.h
+1 −1 examples/LottieExtension.cpp
+1 −1 examples/resources/lottie/extensions/slotsample.json
+2 −1 inc/thorvg.h
+7 −1 meson.build
+5 −0 meson_options.txt
+4 −0 src/loaders/external_jpg/tvgJpgLoader.cpp
+4 −0 src/loaders/external_png/tvgPngLoader.cpp
+4 −0 src/loaders/external_webp/tvgWebpLoader.cpp
+4 −0 src/loaders/jpg/tvgJpgLoader.cpp
+145 −31 src/loaders/lottie/tvgLottieBuilder.cpp
+2 −1 src/loaders/lottie/tvgLottieCommon.h
+1 −1 src/loaders/lottie/tvgLottieExpressions.cpp
+1 −1 src/loaders/lottie/tvgLottieInterpolator.cpp
+23 −13 src/loaders/lottie/tvgLottieLoader.cpp
+1 −1 src/loaders/lottie/tvgLottieLoader.h
+121 −64 src/loaders/lottie/tvgLottieModel.cpp
+91 −80 src/loaders/lottie/tvgLottieModel.h
+207 −104 src/loaders/lottie/tvgLottieParser.cpp
+16 −10 src/loaders/lottie/tvgLottieParser.h
+6 −0 src/loaders/lottie/tvgLottieParserHandler.cpp
+1 −0 src/loaders/lottie/tvgLottieParserHandler.h
+117 −28 src/loaders/lottie/tvgLottieProperty.h
+4 −0 src/loaders/png/tvgPngLoader.cpp
+30 −2 src/loaders/svg/tvgSvgLoader.cpp
+4 −3 src/loaders/svg/tvgXmlParser.cpp
+13 −1 src/loaders/ttf/tvgTtfLoader.cpp
+4 −0 src/loaders/webp/tvgWebpLoader.cpp
+8 −3 src/renderer/gl_engine/tvgGlCommon.h
+2 −2 src/renderer/gl_engine/tvgGlGeometry.cpp
+39 −13 src/renderer/gl_engine/tvgGlGpuBuffer.cpp
+6 −4 src/renderer/gl_engine/tvgGlGpuBuffer.h
+32 −16 src/renderer/gl_engine/tvgGlRenderer.cpp
+2 −2 src/renderer/gl_engine/tvgGlRenderer.h
+114 −22 src/renderer/gl_engine/tvgGlShaderSrc.cpp
+1 −1 src/renderer/gl_engine/tvgGlTessellator.cpp
+6 −2 src/renderer/sw_engine/tvgSwCommon.h
+258 −58 src/renderer/sw_engine/tvgSwPostEffect.cpp
+15 −24 src/renderer/sw_engine/tvgSwRaster.cpp
+32 −0 src/renderer/sw_engine/tvgSwRasterC.h
+47 −21 src/renderer/sw_engine/tvgSwRenderer.cpp
+3 −3 src/renderer/sw_engine/tvgSwRenderer.h
+51 −35 src/renderer/sw_engine/tvgSwRle.cpp
+2 −0 src/renderer/sw_engine/tvgSwShape.cpp
+6 −2 src/renderer/tvgLoader.cpp
+2 −2 src/renderer/tvgPaint.cpp
+6 −6 src/renderer/tvgPaint.h
+14 −7 src/renderer/tvgPicture.cpp
+3 −3 src/renderer/tvgPicture.h
+31 −9 src/renderer/tvgRender.h
+1 −0 src/renderer/tvgSaver.cpp
+10 −2 src/renderer/tvgScene.cpp
+18 −17 src/renderer/tvgScene.h
+11 −11 src/renderer/tvgShape.cpp
+28 −23 src/renderer/tvgShape.h
+10 −1 src/renderer/tvgText.cpp
+1 −1 src/renderer/tvgText.h
+2 −2 src/renderer/wg_engine/tvgWgRenderer.cpp
+2 −2 src/renderer/wg_engine/tvgWgRenderer.h
+1 −1 test/capi/capiLottie.cpp
+1 −1 test/resources/lottieslot.json
+1 −1 test/testLottie.cpp
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
4 changes: 4 additions & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ interface DotLottiePlayer {
boolean state_machine_start();
boolean state_machine_stop();
sequence<string> state_machine_framework_setup();
i32 state_machine_post_event([ByRef] Event event);
i32 state_machine_post_pointer_down_event(f32 x, f32 y);
i32 state_machine_post_pointer_up_event(f32 x, f32 y);
i32 state_machine_post_pointer_move_event(f32 x, f32 y);
Expand All @@ -163,6 +164,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
43 changes: 13 additions & 30 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 @@ -69,15 +69,15 @@ pub enum Action {
value: f32,
},
SetTheme {
theme_id: String,
value: String,
},
SetFrame {
value: StringNumber,
},
SetProgress {
value: StringNumber,
},
SetSlot {
SetThemeData {
value: String,
},
FireCustomEvent {
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 All @@ -239,15 +239,15 @@ impl ActionTrait for Action {
);
// Ok(())
}
Action::SetTheme { theme_id } => {
Action::SetTheme { value } => {
let read_lock = player.try_read();

match read_lock {
Ok(player) => {
if !player.set_theme(theme_id) {
if !player.set_theme(value) {
return Err(StateMachineActionError::ExecuteError(format!(
"Error loading theme: {}",
theme_id
value
)));
}
}
Expand All @@ -259,7 +259,7 @@ impl ActionTrait for Action {
}
Ok(())
}
Action::SetSlot { value } => {
Action::SetThemeData { value } => {
let read_lock = player.read();

match read_lock {
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 4e3e4e6

Please sign in to comment.