Skip to content

Commit

Permalink
feat: handle v2 dotLottie specs
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Oct 29, 2024
1 parent ed899d3 commit 6a2c563
Show file tree
Hide file tree
Showing 27 changed files with 248 additions and 890 deletions.
60 changes: 9 additions & 51 deletions dotlottie-ffi/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,62 +82,25 @@ typedef struct DotLottieOption_DotLottieString {
bool defined;
} DotLottieOption_DotLottieString;

typedef struct DotLottieOption_u32 {
uint32_t value;
bool defined;
} DotLottieOption_u32;

typedef struct DotLottieManifest {
struct DotLottieOption_DotLottieString active_animation_id;
struct DotLottieOption_DotLottieString author;
struct DotLottieOption_DotLottieString description;
struct DotLottieOption_DotLottieString generator;
struct DotLottieOption_DotLottieString keywords;
struct DotLottieOption_u32 revision;
struct DotLottieOption_DotLottieString version;
} DotLottieManifest;

typedef struct DotLottieOption_bool {
bool value;
bool defined;
} DotLottieOption_bool;

typedef struct DotLottieOption_i8 {
int8_t value;
bool defined;
} DotLottieOption_i8;

typedef struct DotLottieOption_f32 {
float value;
bool defined;
} DotLottieOption_f32;

typedef struct DotLottieManifestAnimation {
struct DotLottieOption_bool autoplay;
struct DotLottieOption_DotLottieString default_theme;
struct DotLottieOption_i8 direction;
struct DotLottieOption_bool hover;
struct DotLottieOption_DotLottieString id;
struct DotLottieOption_u32 intermission;
struct DotLottieOption_bool loop;
struct DotLottieOption_u32 loop_count;
struct DotLottieOption_DotLottieString play_mode;
struct DotLottieOption_f32 speed;
struct DotLottieOption_DotLottieString theme_color;
struct DotLottieOption_DotLottieString initial_theme;
struct DotLottieOption_DotLottieString background;
} DotLottieManifestAnimation;

typedef struct DotLottieManifestState {
struct DotLottieString state;
} DotLottieManifestState;
typedef struct DotLottieManifestStateMachine {
struct DotLottieString state_machine;
} DotLottieManifestStateMachine;

typedef struct DotLottieManifestTheme {
struct DotLottieString id;
struct DotLottieString theme;
} DotLottieManifestTheme;

typedef struct DotLottieManifestThemeAnimation {
struct DotLottieString id;
} DotLottieManifestThemeAnimation;

typedef struct DotLottieMarker {
struct DotLottieString name;
float duration;
Expand Down Expand Up @@ -320,14 +283,9 @@ int32_t dotlottie_manifest_animations(struct DotLottiePlayer *ptr,
struct DotLottieManifestAnimation *result,
size_t *size);

int32_t dotlottie_manifest_states(struct DotLottiePlayer *ptr,
struct DotLottieManifestState *result,
size_t *size);

int32_t dotlottie_manifest_theme_animations(struct DotLottiePlayer *ptr,
const struct DotLottieManifestTheme *theme,
struct DotLottieManifestThemeAnimation *result,
size_t *size);
int32_t dotlottie_manifest_state_machines(struct DotLottiePlayer *ptr,
struct DotLottieManifestStateMachine *result,
size_t *size);

int32_t dotlottie_manifest_themes(struct DotLottiePlayer *ptr,
struct DotLottieManifestTheme *result,
Expand Down
33 changes: 11 additions & 22 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,25 @@ dictionary Config {
string marker;
};

dictionary ManifestTheme {
string id;
sequence<string> animations;
dictionary ManifestInitial {
string? animation;
string? state_machine;
};

dictionary ManifestAnimation {
boolean? autoplay;
string? defaultTheme;
i8? direction;
boolean? hover;
string id;
u32? intermission;
boolean? loop;
u32? loop_count;
string? playMode;
f32? speed;
string? themeColor;
string? initial_theme;
sequence<string>? themes;
string? background;
};

dictionary Manifest {
string? active_animation_id;
sequence<ManifestAnimation> animations;
string? author;
string? description;
string? generator;
string? keywords;
u32? revision;
sequence<ManifestTheme>? themes;
sequence<string>? states;
string? version;
string? generator;
ManifestInitial? initial;
sequence<ManifestAnimation> animations;
sequence<string>? themes;
sequence<string>? state_machines;
};

dictionary Marker {
Expand Down
49 changes: 10 additions & 39 deletions dotlottie-ffi/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use types::*;

pub mod types;

// TODO: dotlottie_manifest_initial
// TODO: dotlottie_manifest_animation_themes

// Allows to wrap every C API call with some additional logic. This is currently used to
// check if the dotlottie player pointer is valid or not
unsafe fn exec_dotlottie_player_op<Op>(ptr: *mut DotLottiePlayer, op: Op) -> i32
Expand Down Expand Up @@ -158,10 +161,10 @@ pub unsafe extern "C" fn dotlottie_manifest_themes(
exec_dotlottie_player_op(ptr, |dotlottie_player| {
let manifest = match dotlottie_player.manifest() {
Some(v) => v,
None => return DOTLOTTIE_MANIFEST_NOT_AVAILABLE,
None => return DOTLOTTIE_MANIFEST_THEMES_NOT_AVAILABLE,
};
if let Some(themes) = &manifest.themes {
DotLottieManifestTheme::transfer_all(themes, result, size)
if let Some(themes) = manifest.themes {
DotLottieManifestTheme::transfer_all(&themes, result, size)
} else {
*size = 0;
DOTLOTTIE_SUCCESS
Expand All @@ -170,50 +173,18 @@ pub unsafe extern "C" fn dotlottie_manifest_themes(
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_manifest_theme_animations(
ptr: *mut DotLottiePlayer,
theme: *const types::DotLottieManifestTheme,
result: *mut types::DotLottieManifestThemeAnimation,
size: *mut usize,
) -> i32 {
exec_dotlottie_player_op(ptr, |dotlottie_player| {
if theme.is_null() {
return DOTLOTTIE_INVALID_PARAMETER;
}
let theme = match theme.as_ref() {
Some(v) => v,
None => return DOTLOTTIE_INVALID_PARAMETER,
};
let theme_id = theme.id.to_string();
let manifest = match dotlottie_player.manifest() {
Some(v) => v,
None => return DOTLOTTIE_MANIFEST_NOT_AVAILABLE,
};
let themes = match manifest.themes {
Some(v) => v,
None => return DOTLOTTIE_MANIFEST_THEMES_NOT_AVAILABLE,
};
if let Some(theme) = themes.iter().find(|&v| v.id == theme_id) {
DotLottieManifestThemeAnimation::transfer_all(&theme.animations, result, size)
} else {
DOTLOTTIE_INVALID_PARAMETER
}
})
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_manifest_states(
pub unsafe extern "C" fn dotlottie_manifest_state_machines(
ptr: *mut DotLottiePlayer,
result: *mut types::DotLottieManifestState,
result: *mut types::DotLottieManifestStateMachine,
size: *mut usize,
) -> i32 {
exec_dotlottie_player_op(ptr, |dotlottie_player| {
let manifest = match dotlottie_player.manifest() {
Some(v) => v,
None => return DOTLOTTIE_MANIFEST_NOT_AVAILABLE,
};
if let Some(states) = manifest.states {
DotLottieManifestState::transfer_all(&states, result, size)
if let Some(state_machines) = manifest.state_machines {
DotLottieManifestStateMachine::transfer_all(&state_machines, result, size)
} else {
*size = 0;
DOTLOTTIE_SUCCESS
Expand Down
76 changes: 17 additions & 59 deletions dotlottie-ffi/src/ffi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use std::ffi::{c_char, CStr, CString};
use std::io;
use std::sync::Arc;

use dotlottie_rs::{
Config, Event, Fit, Layout, Manifest, ManifestAnimation, ManifestTheme, Marker, Mode,
};
use dotlottie_rs::{Config, Event, Fit, Layout, Manifest, ManifestAnimation, Marker, Mode};

// Function return codes
pub const DOTLOTTIE_SUCCESS: i32 = 0;
Expand Down Expand Up @@ -319,100 +317,60 @@ impl Transferable<Marker> for DotLottieMarker {
#[derive(Clone, PartialEq)]
#[repr(C)]
pub struct DotLottieManifestAnimation {
pub autoplay: DotLottieOption<bool>,
pub default_theme: DotLottieOption<DotLottieString>,
pub direction: DotLottieOption<i8>,
pub hover: DotLottieOption<bool>,
pub id: DotLottieOption<DotLottieString>,
pub intermission: DotLottieOption<u32>,
pub r#loop: DotLottieOption<bool>,
pub loop_count: DotLottieOption<u32>,
pub play_mode: DotLottieOption<DotLottieString>,
pub speed: DotLottieOption<f32>,
pub theme_color: DotLottieOption<DotLottieString>,
pub initial_theme: DotLottieOption<DotLottieString>,
pub background: DotLottieOption<DotLottieString>,
}

impl Transferable<ManifestAnimation> for DotLottieManifestAnimation {
unsafe fn new(animation: &ManifestAnimation) -> Result<DotLottieManifestAnimation, io::Error> {
Ok(DotLottieManifestAnimation {
autoplay: DotLottieOption::new(&animation.autoplay)?,
default_theme: DotLottieOption::new(&animation.defaultTheme)?,
direction: DotLottieOption::new(&animation.direction)?,
hover: DotLottieOption::new(&animation.hover)?,
id: DotLottieOption::new(&animation.id)?,
intermission: DotLottieOption::new(&animation.intermission)?,
r#loop: DotLottieOption::new(&animation.r#loop)?,
loop_count: DotLottieOption::new(&animation.loop_count)?,
play_mode: DotLottieOption::new(&animation.playMode)?,
speed: DotLottieOption::new(&animation.speed)?,
theme_color: DotLottieOption::new(&animation.themeColor)?,
initial_theme: DotLottieOption::new(&animation.initial_theme)?,
background: DotLottieOption::new(&animation.background)?,
})
}
}

#[derive(Clone, PartialEq)]
#[repr(C)]
pub struct DotLottieManifestTheme {
pub id: DotLottieString,
}

impl Transferable<ManifestTheme> for DotLottieManifestTheme {
unsafe fn new(theme: &ManifestTheme) -> Result<DotLottieManifestTheme, io::Error> {
Ok(DotLottieManifestTheme {
id: DotLottieString::new(&theme.id)?,
})
}
pub struct DotLottieManifestStateMachine {
pub state_machine: DotLottieString,
}

#[derive(Clone, PartialEq)]
#[repr(C)]
pub struct DotLottieManifestState {
pub state: DotLottieString,
}

impl Transferable<String> for DotLottieManifestState {
unsafe fn new(state: &String) -> Result<DotLottieManifestState, io::Error> {
Ok(DotLottieManifestState {
state: DotLottieString::new(state)?,
impl Transferable<String> for DotLottieManifestStateMachine {
unsafe fn new(state_machine: &String) -> Result<DotLottieManifestStateMachine, io::Error> {
Ok(DotLottieManifestStateMachine {
state_machine: DotLottieString::new(state_machine)?,
})
}
}

#[derive(Clone, PartialEq)]
#[repr(C)]
pub struct DotLottieManifestThemeAnimation {
pub id: DotLottieString,
pub struct DotLottieManifestTheme {
pub theme: DotLottieString,
}

impl Transferable<String> for DotLottieManifestThemeAnimation {
unsafe fn new(id: &String) -> Result<DotLottieManifestThemeAnimation, io::Error> {
Ok(DotLottieManifestThemeAnimation {
id: DotLottieString::new(id)?,
impl Transferable<String> for DotLottieManifestTheme {
unsafe fn new(theme: &String) -> Result<DotLottieManifestTheme, io::Error> {
Ok(DotLottieManifestTheme {
theme: DotLottieString::new(theme)?,
})
}
}

#[derive(Clone, PartialEq)]
#[repr(C)]
pub struct DotLottieManifest {
pub active_animation_id: DotLottieOption<DotLottieString>,
pub author: DotLottieOption<DotLottieString>,
pub description: DotLottieOption<DotLottieString>,
pub generator: DotLottieOption<DotLottieString>,
pub keywords: DotLottieOption<DotLottieString>,
pub revision: DotLottieOption<u32>,
pub version: DotLottieOption<DotLottieString>,
}

impl Transferable<Manifest> for DotLottieManifest {
unsafe fn new(manifest: &Manifest) -> Result<DotLottieManifest, io::Error> {
Ok(DotLottieManifest {
active_animation_id: DotLottieOption::new(&manifest.active_animation_id)?,
author: DotLottieOption::new(&manifest.author)?,
description: DotLottieOption::new(&manifest.description)?,
generator: DotLottieOption::new(&manifest.generator)?,
keywords: DotLottieOption::new(&manifest.keywords)?,
revision: DotLottieOption::new(&manifest.revision)?,
version: DotLottieOption::new(&manifest.version)?,
})
}
Expand Down
Loading

0 comments on commit 6a2c563

Please sign in to comment.