Skip to content

Commit

Permalink
chore: 🤖 add Bounce and ReverseBounce modes
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Jan 12, 2024
1 parent 163616b commit 8946539
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions demo-player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ fn main() {
let mut lottie_player: DotLottiePlayer = DotLottiePlayer::new(Config {
mode: Mode::Reverse,
loop_animation: true,
speed: 10.0,
speed: 2.5,
use_frame_interpolation: true,
autoplay: false,
autoplay: true,
});
lottie_player.load_animation_data(animation_data.as_str(), WIDTH as u32, HEIGHT as u32);

Expand Down
2 changes: 2 additions & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace dotlottie_player {
enum Mode {
"Forward",
"Reverse",
"Bounce",
"ReverseBounce"
};

dictionary Config {
Expand Down
63 changes: 34 additions & 29 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::Mutex, time::SystemTime};
use std::{sync::RwLock, time::SystemTime};

use crate::LottieRenderer;

Expand All @@ -12,6 +12,8 @@ pub enum PlaybackState {
pub enum Mode {
Forward,
Reverse,
Bounce,
ReverseBounce,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -99,6 +101,7 @@ impl DotLottieRuntime {
Mode::Reverse => {
self.set_frame(self.total_frames());
}
_ => {}
}

true
Expand Down Expand Up @@ -133,6 +136,7 @@ impl DotLottieRuntime {
let next_frame = match self.config.mode {
Mode::Forward => next_frame,
Mode::Reverse => total_frames - next_frame,
_ => next_frame,
};

let next_frame = match self.config.mode {
Expand Down Expand Up @@ -162,6 +166,7 @@ impl DotLottieRuntime {
next_frame
}
}
_ => next_frame,
};

next_frame
Expand Down Expand Up @@ -228,6 +233,7 @@ impl DotLottieRuntime {
Mode::Reverse => {
self.set_frame(total_frames);
}
_ => {}
}

if self.config.autoplay && loaded {
Expand All @@ -254,6 +260,7 @@ impl DotLottieRuntime {
Mode::Reverse => {
self.set_frame(total_frames);
}
_ => {}
}

if self.config.autoplay && loaded {
Expand All @@ -273,118 +280,116 @@ impl DotLottieRuntime {
}

pub struct DotLottiePlayer {
runtime: Mutex<DotLottieRuntime>,
runtime: RwLock<DotLottieRuntime>,
}

impl DotLottiePlayer {
pub fn new(config: Config) -> Self {
DotLottiePlayer {
runtime: Mutex::new(DotLottieRuntime::new(config)),
runtime: RwLock::new(DotLottieRuntime::new(config)),
}
}

pub fn load_animation_data(&self, animation_data: &str, width: u32, height: u32) -> bool {
self.runtime
.lock()
.write()
.unwrap()
.load_animation_data(animation_data, width, height)
}

pub fn load_animation_path(&self, animation_path: &str, width: u32, height: u32) -> bool {
self.runtime
.lock()
.write()
.unwrap()
.load_animation_path(animation_path, width, height)
}

pub fn buffer_ptr(&self) -> u64 {
let ptr = self.runtime.lock().unwrap().buffer().as_ptr();

ptr as u64
self.runtime.read().unwrap().buffer().as_ptr() as u64
}

pub fn buffer_len(&self) -> u64 {
self.runtime.lock().unwrap().buffer().len() as u64
self.runtime.read().unwrap().buffer().len() as u64
}

pub fn clear(&self) {
self.runtime.lock().unwrap().clear()
self.runtime.write().unwrap().clear();
}

pub fn set_config(&self, config: Config) {
self.runtime.lock().unwrap().set_config(config);
self.runtime.write().unwrap().set_config(config);
}

pub fn set_speed(&self, speed: f32) {
self.runtime.lock().unwrap().set_speed(speed);
self.runtime.write().unwrap().set_speed(speed);
}

pub fn speed(&self) -> f32 {
self.runtime.lock().unwrap().speed()
self.runtime.read().unwrap().speed()
}

pub fn total_frames(&self) -> f32 {
self.runtime.lock().unwrap().total_frames()
self.runtime.read().unwrap().total_frames()
}

pub fn duration(&self) -> f32 {
self.runtime.lock().unwrap().duration()
self.runtime.read().unwrap().duration()
}

pub fn current_frame(&self) -> f32 {
self.runtime.lock().unwrap().current_frame()
self.runtime.read().unwrap().current_frame()
}

pub fn loop_count(&self) -> u32 {
self.runtime.lock().unwrap().loop_count()
self.runtime.read().unwrap().loop_count()
}

pub fn is_loaded(&self) -> bool {
self.runtime.lock().unwrap().is_loaded()
self.runtime.read().unwrap().is_loaded()
}

pub fn is_playing(&self) -> bool {
self.runtime.lock().unwrap().is_playing()
self.runtime.read().unwrap().is_playing()
}

pub fn is_paused(&self) -> bool {
self.runtime.lock().unwrap().is_paused()
self.runtime.read().unwrap().is_paused()
}

pub fn is_stopped(&self) -> bool {
self.runtime.lock().unwrap().is_stopped()
self.runtime.read().unwrap().is_stopped()
}

pub fn play(&self) -> bool {
self.runtime.lock().unwrap().play()
self.runtime.write().unwrap().play()
}

pub fn pause(&self) -> bool {
self.runtime.lock().unwrap().pause()
self.runtime.write().unwrap().pause()
}

pub fn stop(&self) -> bool {
self.runtime.lock().unwrap().stop()
self.runtime.write().unwrap().stop()
}

pub fn request_frame(&self) -> f32 {
self.runtime.lock().unwrap().request_frame()
self.runtime.write().unwrap().request_frame()
}

pub fn set_frame(&self, no: f32) -> bool {
self.runtime.lock().unwrap().set_frame(no)
self.runtime.write().unwrap().set_frame(no)
}

pub fn render(&self) -> bool {
self.runtime.lock().unwrap().render()
self.runtime.write().unwrap().render()
}

pub fn resize(&self, width: u32, height: u32) -> bool {
self.runtime.lock().unwrap().resize(width, height)
self.runtime.write().unwrap().resize(width, height)
}

pub fn config(&self) -> Config {
self.runtime.lock().unwrap().config()
self.runtime.read().unwrap().config()
}
}

Expand Down

0 comments on commit 8946539

Please sign in to comment.