Skip to content

Commit

Permalink
feat: handling of dotLottie v2 specs (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf authored Nov 20, 2024
1 parent ca32331 commit 09d0514
Show file tree
Hide file tree
Showing 41 changed files with 1,331 additions and 941 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ WASM_BUILD := $(BUILD)/$(WASM)

EMSDK := emsdk
EMSDK_DIR := $(PROJECT_DIR)/$(DEPS_MODULES_DIR)/$(EMSDK)
EMSDK_VERSION := 3.1.68
EMSDK_VERSION := 3.1.71
EMSDK_ENV := emsdk_env.sh

UNIFFI_BINDGEN_CPP := uniffi-bindgen-cpp
Expand Down
74 changes: 18 additions & 56 deletions dotlottie-ffi/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#define DOTLOTTIE_MANIFEST_NOT_AVAILABLE 3

#define DOTLOTTIE_MANIFEST_THEMES_NOT_AVAILABLE 4

#define DOTLOTTIE_MAX_STR_LENGTH 512

#define DOTLOTTIE_SUCCESS 0
Expand Down Expand Up @@ -68,6 +66,7 @@ typedef struct DotLottieConfig {
uint32_t background_color;
struct DotLottieLayout layout;
struct DotLottieString marker;
struct DotLottieString theme_id;
} DotLottieConfig;

typedef struct LayerBoundingBox {
Expand All @@ -82,62 +81,28 @@ 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 name;
struct DotLottieOption_DotLottieString initial_theme;
struct DotLottieOption_DotLottieString background;
} DotLottieManifestAnimation;

typedef struct DotLottieManifestState {
struct DotLottieString state;
} DotLottieManifestState;
typedef struct DotLottieManifestStateMachine {
struct DotLottieString id;
struct DotLottieOption_DotLottieString name;
} DotLottieManifestStateMachine;

typedef struct DotLottieManifestTheme {
struct DotLottieString id;
struct DotLottieOption_DotLottieString name;
} DotLottieManifestTheme;

typedef struct DotLottieManifestThemeAnimation {
struct DotLottieString id;
} DotLottieManifestThemeAnimation;

typedef struct DotLottieMarker {
struct DotLottieString name;
float duration;
Expand Down Expand Up @@ -308,10 +273,6 @@ int32_t dotlottie_load_state_machine(struct DotLottiePlayer *ptr, const char *st
int32_t dotlottie_load_state_machine_data(struct DotLottiePlayer *ptr,
const char *state_machine_definition);

int32_t dotlottie_load_theme(struct DotLottiePlayer *ptr, const char *theme_id);

int32_t dotlottie_load_theme_data(struct DotLottiePlayer *ptr, const char *theme_data);

int32_t dotlottie_loop_count(struct DotLottiePlayer *ptr, uint32_t *result);

int32_t dotlottie_manifest(struct DotLottiePlayer *ptr, struct DotLottieManifest *result);
Expand All @@ -320,14 +281,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 All @@ -349,6 +305,8 @@ int32_t dotlottie_render(struct DotLottiePlayer *ptr);

int32_t dotlottie_request_frame(struct DotLottiePlayer *ptr, float *result);

int32_t dotlottie_reset_theme(struct DotLottiePlayer *ptr);

int32_t dotlottie_resize(struct DotLottiePlayer *ptr, uint32_t width, uint32_t height);

int32_t dotlottie_seek(struct DotLottiePlayer *ptr, float no);
Expand All @@ -369,6 +327,10 @@ int32_t dotlottie_set_state_machine_string_context(struct DotLottiePlayer *ptr,
const char *key,
const char *value);

int32_t dotlottie_set_theme(struct DotLottiePlayer *ptr, const char *theme_id);

int32_t dotlottie_set_theme_data(struct DotLottiePlayer *ptr, const char *theme_data);

int32_t dotlottie_set_viewport(struct DotLottiePlayer *ptr,
int32_t x,
int32_t y,
Expand Down
10 changes: 7 additions & 3 deletions dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.field("segment", &Config::segment)
.field("backgroundColor", &Config::background_color)
.field("layout", &Config::layout)
.field("marker", &Config::marker);
.field("marker", &Config::marker)
.field("themeId", &Config::theme_id);

function("createDefaultConfig", &create_default_config);
function("transformThemeToLottieSlots", &transform_theme_to_lottie_slots);

// value_object<ManifestTheme>("ManifestTheme")
// .field("id", &ManifestTheme::id)
Expand Down Expand Up @@ -157,8 +159,10 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
// .function("subscribe", &DotLottiePlayer::subscribe)
// .function("unsubscribe", &DotLottiePlayer::unsubscribe)
.function("isComplete", &DotLottiePlayer::is_complete)
.function("loadTheme", &DotLottiePlayer::load_theme)
.function("loadThemeData", &DotLottiePlayer::load_theme_data)
.function("setTheme", &DotLottiePlayer::set_theme)
.function("setThemeData", &DotLottiePlayer::set_theme_data)
.function("resetTheme", &DotLottiePlayer::reset_theme)
.function("setSlots", &DotLottiePlayer::set_slots)
.function("markers", &DotLottiePlayer::markers)
.function("activeAnimationId", &DotLottiePlayer::active_animation_id)
.function("activeThemeId", &DotLottiePlayer::active_theme_id)
Expand Down
47 changes: 25 additions & 22 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,39 @@ dictionary Config {
u32 background_color;
Layout layout;
string marker;
string theme_id;
};

dictionary ManifestInitial {
string? animation;
string? state_machine;
};

dictionary ManifestAnimation {
string id;
string? name;
string? initial_theme;
sequence<string>? themes;
string? background;
};

dictionary ManifestTheme {
string id;
sequence<string> animations;
string? name;
};

dictionary ManifestAnimation {
boolean? autoplay;
string? defaultTheme;
i8? direction;
boolean? hover;
dictionary ManifestStateMachine {
string id;
u32? intermission;
boolean? loop;
u32? loop_count;
string? playMode;
f32? speed;
string? themeColor;
string? name;
};

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

dictionary Marker {
Expand Down Expand Up @@ -140,8 +141,10 @@ interface DotLottiePlayer {
void subscribe(Observer observer);
void unsubscribe([ByRef] Observer observer);
boolean is_complete();
boolean load_theme([ByRef] string theme_id);
boolean load_theme_data([ByRef] string theme_data);
boolean set_theme([ByRef] string theme_id);
boolean set_theme_data([ByRef] string theme_data);
boolean reset_theme();
boolean set_slots([ByRef] string slots);
sequence<Marker> markers();
string active_animation_id();
string active_theme_id();
Expand Down
8 changes: 6 additions & 2 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace dotlottie_player {
Layout create_default_layout();
Config create_default_config();
string transform_theme_to_lottie_slots([ByRef] string theme_data, [ByRef] string animation_id);
};

enum Mode {
Expand Down Expand Up @@ -34,6 +35,7 @@ dictionary Config {
u32 background_color;
Layout layout;
string marker;
string theme_id;
};

dictionary Marker {
Expand Down Expand Up @@ -71,8 +73,10 @@ interface DotLottiePlayer {
boolean resize(u32 width, u32 height);
void clear();
boolean is_complete();
boolean load_theme([ByRef] string theme_id);
boolean load_theme_data([ByRef] string theme_data);
boolean set_theme([ByRef] string theme_id);
boolean set_theme_data([ByRef] string theme_data);
boolean reset_theme();
boolean set_slots([ByRef] string slots);
sequence<Marker> markers();
string active_animation_id();
string active_theme_id();
Expand Down
62 changes: 20 additions & 42 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 @@ -160,8 +163,8 @@ pub unsafe extern "C" fn dotlottie_manifest_themes(
Some(v) => v,
None => return DOTLOTTIE_MANIFEST_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 Expand Up @@ -446,27 +417,34 @@ pub unsafe extern "C" fn dotlottie_is_complete(
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_load_theme(
pub unsafe extern "C" fn dotlottie_set_theme(
ptr: *mut DotLottiePlayer,
theme_id: *const c_char,
) -> i32 {
exec_dotlottie_player_op(ptr, |dotlottie_player| {
if let Ok(theme_id) = DotLottieString::read(theme_id) {
to_exit_status(dotlottie_player.load_theme(&theme_id))
to_exit_status(dotlottie_player.set_theme(&theme_id))
} else {
DOTLOTTIE_INVALID_PARAMETER
}
})
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_load_theme_data(
pub unsafe extern "C" fn dotlottie_reset_theme(ptr: *mut DotLottiePlayer) -> i32 {
exec_dotlottie_player_op(ptr, |dotlottie_player| {
to_exit_status(dotlottie_player.reset_theme())
})
}

#[no_mangle]
pub unsafe extern "C" fn dotlottie_set_theme_data(
ptr: *mut DotLottiePlayer,
theme_data: *const c_char,
) -> i32 {
exec_dotlottie_player_op(ptr, |dotlottie_player| {
if let Ok(theme_data) = DotLottieString::read(theme_data) {
to_exit_status(dotlottie_player.load_theme_data(&theme_data))
to_exit_status(dotlottie_player.set_theme_data(&theme_data))
} else {
DOTLOTTIE_INVALID_PARAMETER
}
Expand Down
Loading

0 comments on commit 09d0514

Please sign in to comment.