Skip to content

Commit

Permalink
fix: events tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Dec 17, 2024
1 parent f9793b6 commit 066b77e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 67 deletions.
3 changes: 2 additions & 1 deletion dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.function("postPointerMoveEvent", &DotLottiePlayer::post_pointer_move_event)
.function("postPointerEnterEvent", &DotLottiePlayer::post_pointer_enter_event)
.function("postPointerExitEvent", &DotLottiePlayer::post_pointer_exit_event)
.function("postSetNumericContext", &DotLottiePlayer::post_set_numeric_context);
.function("postSetNumericContext", &DotLottiePlayer::post_set_numeric_context)
.function("instanceId", &DotLottiePlayer::instance_id);
}
36 changes: 18 additions & 18 deletions dotlottie-ffi/emscripten_library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ mergeInto(LibraryManager.library, {
_emscripten_get_now: function () {
return Date.now();
},
observer_on_load() {
_DOTLOTTIE_BRIDGE_JS.observer_on_load();
observer_on_load(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_load(dotlottie_instance_id);
},
observer_on_load_error: function () {
_DOTLOTTIE_BRIDGE_JS.observer_on_load_error();
observer_on_load_error(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_load_error(dotlottie_instance_id);
},
observer_on_play: function () {
_DOTLOTTIE_BRIDGE_JS.observer_on_play();
observer_on_play(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_play(dotlottie_instance_id);
},
observer_on_pause: function () {
_DOTLOTTIE_BRIDGE_JS.observer_on_pause();
observer_on_pause(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_pause(dotlottie_instance_id);
},
observer_on_stop: function () {
_DOTLOTTIE_BRIDGE_JS.observer_on_stop();
observer_on_stop(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_stop(dotlottie_instance_id);
},
observer_on_frame: function (frame_no) {
_DOTLOTTIE_BRIDGE_JS.observer_on_frame(frame_no);
observer_on_frame(dotlottie_instance_id, frame_no) {
_DOTLOTTIE_BRIDGE_JS.observer_on_frame(dotlottie_instance_id, frame_no);
},
observer_on_render: function (frame_no) {
_DOTLOTTIE_BRIDGE_JS.observer_on_render(frame_no);
observer_on_render(dotlottie_instance_id, frame_no) {
_DOTLOTTIE_BRIDGE_JS.observer_on_render(dotlottie_instance_id, frame_no);
},
observer_on_loop: function (loop_count) {
_DOTLOTTIE_BRIDGE_JS.observer_on_loop(loop_count);
observer_on_loop(dotlottie_instance_id, loop_count) {
_DOTLOTTIE_BRIDGE_JS.observer_on_loop(dotlottie_instance_id, loop_count);
},
observer_on_complete: function () {
_DOTLOTTIE_BRIDGE_JS.observer_on_complete();
observer_on_complete(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_complete(dotlottie_instance_id);
},
});
2 changes: 2 additions & 0 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ interface DotLottiePlayer {
i32 post_pointer_enter_event(f32 x, f32 y);
i32 post_pointer_exit_event(f32 x, f32 y);
i32 post_set_numeric_context([ByRef] string key, f32 value);

u32 instance_id();
};
55 changes: 37 additions & 18 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ pub trait Observer: Send + Sync {
#[cfg(target_arch = "wasm32")]
mod wasm_observer_callbacks_ffi {
extern "C" {
pub fn observer_on_load();
pub fn observer_on_load_error();
pub fn observer_on_play();
pub fn observer_on_pause();
pub fn observer_on_stop();
pub fn observer_on_frame(frame_no: f32);
pub fn observer_on_render(frame_no: f32);
pub fn observer_on_loop(loop_count: u32);
pub fn observer_on_complete();
pub fn observer_on_load(dotlottie_instance_id: u32);
pub fn observer_on_load_error(dotlottie_instance_id: u32);
pub fn observer_on_play(dotlottie_instance_id: u32);
pub fn observer_on_pause(dotlottie_instance_id: u32);
pub fn observer_on_stop(dotlottie_instance_id: u32);
pub fn observer_on_frame(dotlottie_instance_id: u32, frame_no: f32);
pub fn observer_on_render(dotlottie_instance_id: u32, frame_no: f32);
pub fn observer_on_loop(dotlottie_instance_id: u32, loop_count: u32);
pub fn observer_on_complete(dotlottie_instance_id: u32);
}
}

Expand Down Expand Up @@ -900,15 +900,22 @@ pub struct DotLottiePlayerContainer {
runtime: RwLock<DotLottieRuntime>,
observers: RwLock<Vec<Arc<dyn Observer>>>,
state_machine: Rc<RwLock<Option<StateMachine>>>,
#[cfg(target_arch = "wasm32")]
instance_id: u32,
}

impl DotLottiePlayerContainer {
#[cfg(feature = "thorvg")]
pub fn new(config: Config) -> Self {
static NEXT_INSTANCE_ID: std::sync::atomic::AtomicU32 =
std::sync::atomic::AtomicU32::new(1);

DotLottiePlayerContainer {
runtime: RwLock::new(DotLottieRuntime::new(config)),
observers: RwLock::new(Vec::new()),
state_machine: Rc::new(RwLock::new(None)),
#[cfg(target_arch = "wasm32")]
instance_id: NEXT_INSTANCE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
}
}

Expand All @@ -917,6 +924,8 @@ impl DotLottiePlayerContainer {
runtime: RwLock::new(DotLottieRuntime::with_renderer(config, renderer)),
observers: RwLock::new(Vec::new()),
state_machine: Rc::new(RwLock::new(None)),
#[cfg(target_arch = "wasm32")]
instance_id: 0,
}
}

Expand All @@ -931,7 +940,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_load();
wasm_observer_callbacks_ffi::observer_on_load(self.instance_id);
}
}
}
Expand All @@ -947,7 +956,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_load_error();
wasm_observer_callbacks_ffi::observer_on_load_error(self.instance_id);
}
}
}
Expand All @@ -963,7 +972,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_play();
wasm_observer_callbacks_ffi::observer_on_play(self.instance_id);
}
}
}
Expand All @@ -979,7 +988,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_pause();
wasm_observer_callbacks_ffi::observer_on_pause(self.instance_id);
}
}
}
Expand All @@ -995,7 +1004,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_stop();
wasm_observer_callbacks_ffi::observer_on_stop(self.instance_id);
}
}
}
Expand All @@ -1011,7 +1020,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_frame(frame_no);
wasm_observer_callbacks_ffi::observer_on_frame(self.instance_id, frame_no);
}
}
}
Expand All @@ -1027,7 +1036,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_render(frame_no);
wasm_observer_callbacks_ffi::observer_on_render(self.instance_id, frame_no);
}
}
}
Expand All @@ -1043,7 +1052,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_loop(loop_count);
wasm_observer_callbacks_ffi::observer_on_loop(self.instance_id, loop_count);
}
}
}
Expand All @@ -1059,7 +1068,7 @@ impl DotLottiePlayerContainer {
#[cfg(target_arch = "wasm32")]
{
unsafe {
wasm_observer_callbacks_ffi::observer_on_complete();
wasm_observer_callbacks_ffi::observer_on_complete(self.instance_id);
}
}
}
Expand Down Expand Up @@ -1395,6 +1404,11 @@ impl DotLottiePlayerContainer {
Err(_) => None,
}
}

#[cfg(target_arch = "wasm32")]
pub fn instance_id(&self) -> u32 {
self.instance_id
}
}

pub struct DotLottiePlayer {
Expand Down Expand Up @@ -1944,6 +1958,11 @@ impl DotLottiePlayer {
pub fn animation_size(&self) -> Vec<f32> {
self.player.read().unwrap().animation_size()
}

#[cfg(target_arch = "wasm32")]
pub fn instance_id(&self) -> u32 {
self.player.read().unwrap().instance_id()
}
}

unsafe impl Send for DotLottiePlayer {}
Expand Down
64 changes: 34 additions & 30 deletions web-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,36 +98,6 @@ <h1>Test</h1>
},
});

globalThis._DOTLOTTIE_BRIDGE_JS = {
observer_on_load: () => {
console.log("Loaded");
},
observer_on_load_error: () => {
console.log("Error");
},
observer_on_play: () => {
console.log("Play");
},
observer_on_pause: () => {
console.log("Pause");
},
observer_on_stop: () => {
console.log("Stop");
},
observer_on_frame: (frame_no) => {
console.log("Frame", frame_no);
},
observer_on_render: (frame_no) => {
console.log("Render", frame_no);
},
observer_on_loop: (loop_count) => {
console.log("Loop", loop_count);
},
observer_on_complete: () => {
console.log("Complete");
},
};

function createSegment(startFrame, endFrame) {
const vector = new Module.VectorFloat();

Expand Down Expand Up @@ -159,6 +129,40 @@ <h1>Test</h1>
marker: "feather",
});

const instances = {};

instances[dotLottiePlayer.instanceId()] = dotLottiePlayer;

globalThis._DOTLOTTIE_BRIDGE_JS = {
observer_on_load: (dotlottie_instance_id) => {
console.log("Loaded", instances[dotlottie_instance_id]);
},
observer_on_load_error: (dotlottie_instance_id) => {
console.log("Error", instances[dotlottie_instance_id]);
},
observer_on_play: (dotlottie_instance_id) => {
console.log("Play", instances[dotlottie_instance_id]);
},
observer_on_pause: (dotlottie_instance_id) => {
console.log("Pause", instances[dotlottie_instance_id]);
},
observer_on_stop: (dotlottie_instance_id) => {
console.log("Stop", instances[dotlottie_instance_id]);
},
observer_on_frame: (dotlottie_instance_id, frame_no) => {
console.log("Frame", instances[dotlottie_instance_id], frame_no);
},
observer_on_render: (dotlottie_instance_id, frame_no) => {
console.log("Render", instances[dotlottie_instance_id], frame_no);
},
observer_on_loop: (dotlottie_instance_id, loop_count) => {
console.log("Loop", instances[dotlottie_instance_id], loop_count);
},
observer_on_complete: (dotlottie_instance_id) => {
console.log("Complete", instances[instance_id]);
},
};

const data = await fetch(
// "https://lottie.host/5c89381e-0d1a-4422-8247-f5b7e4b3c4e2/mqs5juC4PW.lottie"
"./examples/demo-player/src/v2/gradient.json"
Expand Down

0 comments on commit 066b77e

Please sign in to comment.