Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 add unsubscribe method #64

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 70 additions & 27 deletions demo-player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,85 @@ use minifb::{Key, Window, WindowOptions};
pub const WIDTH: usize = 500;
pub const HEIGHT: usize = 500;

struct Timer {
last_update: Instant,
}
struct DummyObserver2;

impl Timer {
fn new() -> Self {
Self {
last_update: Instant::now(),
}
impl Observer for DummyObserver2 {
fn on_play(&self) {
println!("on_play2");
}

fn tick(&mut self, animation: &mut DotLottiePlayer) {
let next_frame = animation.request_frame();

// println!("next_frame: {}", next_frame);
animation.set_frame(next_frame);
animation.render();

self.last_update = Instant::now(); // Reset the timer
fn on_pause(&self) {
println!("on_pause2");
}
fn on_stop(&self) {
println!("on_stop2");
}
fn on_frame(&self, frame_no: f32) {
println!("on_frame2: {}", frame_no);
}
fn on_render(&self, frame_no: f32) {
println!("on_render2: {}", frame_no);
}
fn on_load(&self) {
println!("on_load2");
}
fn on_loop(&self, loop_count: u32) {
println!("on_loop2: {}", loop_count);
}
fn on_complete(&self) {
println!("on_complete2");
}
}

struct DummyObserver;
struct DummyObserver {
id: u32,
}

impl Observer for DummyObserver {
fn on_play(&self) {
println!("on_play");
println!("on_play {} ", self.id);
}
fn on_pause(&self) {
println!("on_pause");
println!("on_pause {} ", self.id);
}
fn on_stop(&self) {
println!("on_stop");
println!("on_stop {} ", self.id);
}
fn on_frame(&self, frame_no: f32) {
println!("on_frame: {}", frame_no);
println!("on_frame {}: {}", self.id, frame_no);
}
fn on_render(&self, frame_no: f32) {
println!("on_render: {}", frame_no);
println!("on_render {}: {}", self.id, frame_no);
}
fn on_load(&self) {
println!("on_load");
println!("on_load {} ", self.id);
}
fn on_loop(&self, loop_count: u32) {
println!("on_loop: {}", loop_count);
println!("on_loop {}: {}", self.id, loop_count);
}
fn on_complete(&self) {
println!("on_complete");
println!("on_complete {} ", self.id);
}
}

struct Timer {
last_update: Instant,
}

impl Timer {
fn new() -> Self {
Self {
last_update: Instant::now(),
}
}

fn tick(&mut self, animation: &mut DotLottiePlayer) {
let next_frame = animation.request_frame();

// println!("next_frame: {}", next_frame);
animation.set_frame(next_frame);
animation.render();

self.last_update = Instant::now(); // Reset the timer
}
}

Expand Down Expand Up @@ -100,7 +131,11 @@ fn main() {
// lottie_player.load_dotlottie_data(&buffer, WIDTH as u32, HEIGHT as u32);
// lottie_player.load_animation("confused", WIDTH as u32, HEIGHT as u32);

lottie_player.subscribe(Arc::new(DummyObserver));
let observer1: Arc<dyn Observer + 'static> = Arc::new(DummyObserver { id: 1 });
let observer2: Arc<dyn Observer + 'static> = Arc::new(DummyObserver { id: 2 });

lottie_player.subscribe(observer1.clone());
lottie_player.subscribe(observer2.clone());

let mut timer = Timer::new();

Expand All @@ -111,6 +146,14 @@ fn main() {
// lottie_player.next_animation(WIDTH as u32, HEIGHT as u32);
// }

if window.is_key_down(Key::Up) {
lottie_player.unsubscribe(&observer1);
}

if window.is_key_down(Key::Down) {
lottie_player.unsubscribe(&observer2);
}

let (buffer_ptr, buffer_len) = (lottie_player.buffer_ptr(), lottie_player.buffer_len());

let buffer =
Expand Down
2 changes: 2 additions & 0 deletions dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.function("setFrame", &DotLottiePlayer::set_frame)
.function("stop", &DotLottiePlayer::stop)
.function("totalFrames", &DotLottiePlayer::total_frames)
.function("subscribe", &DotLottiePlayer::subscribe)
.function("unsubscribe", &DotLottiePlayer::unsubscribe)
.function("isComplete", &DotLottiePlayer::is_complete);
}
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ interface DotLottiePlayer {
boolean resize(u32 width, u32 height);
void clear();
void subscribe(Observer observer);
void unsubscribe(Observer observer);
boolean is_complete();
};
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ interface DotLottiePlayer {
boolean resize(u32 width, u32 height);
void clear();
void subscribe(Observer observer);
void unsubscribe(Observer observer);
boolean is_complete();
};
10 changes: 10 additions & 0 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ impl DotLottieRuntime {
observer.on_loop(self.loop_count);
});
} else {
self.playback_state = PlaybackState::Stopped;

// notify the observers that the animation is complete
self.observers.iter().for_each(|observer| {
observer.on_complete();
Expand Down Expand Up @@ -629,6 +631,10 @@ impl DotLottieRuntime {
self.observers.push(observer);
}

pub fn unsubscribe(&mut self, observer: &Arc<dyn Observer>) {
self.observers.retain(|x| !Arc::ptr_eq(x, observer));
}

pub fn is_complete(&self) -> bool {
match self.config.mode {
Mode::Forward | Mode::ReverseBounce => self.current_frame() >= self.end_frame(),
Expand Down Expand Up @@ -778,6 +784,10 @@ impl DotLottiePlayer {
pub fn is_complete(&self) -> bool {
self.runtime.read().unwrap().is_complete()
}

pub fn unsubscribe(&self, observer: &Arc<dyn Observer>) {
self.runtime.write().unwrap().unsubscribe(&observer);
}
}

unsafe impl Send for DotLottiePlayer {}
Expand Down
Loading