Skip to content

Commit

Permalink
feat(WASM): 🎸 implement playback observer callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Dec 17, 2024
1 parent ba1c5e1 commit ad37a4f
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 128 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ cpp_link_args = [
'--no-entry',
'--strip-all',
'--emit-tsd=${WASM_MODULE}.d.ts',
'--closure=1']
'--js-library=$(PROJECT_DIR)/$(RUNTIME_FFI)/emscripten_library.js']

[host_machine]
system = '$(SYSTEM)'
Expand Down
67 changes: 2 additions & 65 deletions dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
using namespace emscripten;
using namespace dotlottie_player;

extern "C"
{
/*
This is a workaround as instant crate expects a _ prefix for the emscripten_get_now function
https://github.com/sebcrozet/instant/issues/35
*/
double _emscripten_get_now()
{
return emscripten_get_now();
}
}

val buffer(DotLottiePlayer &player)
{
auto buffer_ptr = player.buffer_ptr();
Expand All @@ -38,8 +26,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
register_vector<float>("VectorFloat");
register_vector<Marker>("VectorMarker");
register_vector<std::string>("VectorString");
// register_vector<ManifestTheme>("VectorManifestTheme");
// register_vector<ManifestAnimation>("VectorManifestAnimation");

enum_<Mode>("Mode")
.value("Forward", Mode::kForward)
Expand Down Expand Up @@ -81,52 +67,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
function("createDefaultConfig", &create_default_config);
function("transformThemeToLottieSlots", &transform_theme_to_lottie_slots);

// value_object<ManifestTheme>("ManifestTheme")
// .field("id", &ManifestTheme::id)
// .field("animations", &ManifestTheme::animations);

// value_object<ManifestAnimation>("ManifestAnimation")
// .field("autoplay", &ManifestAnimation::autoplay)
// .field("defaultTheme", &ManifestAnimation::default_theme)
// .field("direction", &ManifestAnimation::direction)
// .field("hover", &ManifestAnimation::hover)
// .field("id", &ManifestAnimation::id)
// .field("intermission", &ManifestAnimation::intermission)
// .field("loop", &ManifestAnimation::loop)
// .field("loop_count", &ManifestAnimation::loop_count)
// .field("playMode", &ManifestAnimation::play_mode)
// .field("speed", &ManifestAnimation::speed)
// .field("themeColor", &ManifestAnimation::theme_color);

// value_object<Manifest>("Manifest")
// .field("active_animation_id", &Manifest::active_animation_id)
// .field("animations", &Manifest::animations)
// .field("author", &Manifest::author)
// .field("description", &Manifest::description)
// .field("generator", &Manifest::generator)
// .field("keywords", &Manifest::keywords)
// .field("revision", &Manifest::revision)
// .field("themes", &Manifest::themes)
// .field("states", &Manifest::states)
// .field("version", &Manifest::version);

// class_<Observer>("Observer")
// .smart_ptr<std::shared_ptr<Observer>>("Observer")
// .function("onFrame", &Observer::on_frame)
// .function("onLoad", &Observer::on_load)
// .function("onLoop", &Observer::on_loop)
// .function("onPause", &Observer::on_pause)
// .function("onPlay", &Observer::on_play)
// .function("onRender", &Observer::on_render)
// .function("onComplete", &Observer::on_complete)
// .function("onStop", &Observer::on_stop);

// class_<StateMachineObserver>("StateMachineObserver")
// .smart_ptr<std::shared_ptr<StateMachineObserver>>("StateMachineObserver")
// .function("OnTransition", &StateMachineObserver::on_transition);
// .function("onStateEntered", &StateMachineObserver::on_state_entered);
// .function("onStateExit", &StateMachineObserver::on_state_exit);

class_<DotLottiePlayer>("DotLottiePlayer")
.smart_ptr<std::shared_ptr<DotLottiePlayer>>("DotLottiePlayer")
.constructor(&DotLottiePlayer::init, allow_raw_pointers())
Expand Down Expand Up @@ -156,8 +96,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.function("seek", &DotLottiePlayer::seek)
.function("stop", &DotLottiePlayer::stop)
.function("totalFrames", &DotLottiePlayer::total_frames)
// .function("subscribe", &DotLottiePlayer::subscribe)
// .function("unsubscribe", &DotLottiePlayer::unsubscribe)
.function("isComplete", &DotLottiePlayer::is_complete)
.function("setTheme", &DotLottiePlayer::set_theme)
.function("setThemeData", &DotLottiePlayer::set_theme_data)
Expand Down Expand Up @@ -187,7 +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("state_machine_subscribe", &DotLottiePlayer::state_machine_subscribe)
// .function("state_machine_unsubscribe", &DotLottiePlayer::state_machine_unsubscribe)
.function("postSetNumericContext", &DotLottiePlayer::post_set_numeric_context)
.function("instanceId", &DotLottiePlayer::instance_id);
}
33 changes: 33 additions & 0 deletions dotlottie-ffi/emscripten_library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// _DOTLOTTIE_BRIDGE_JS is a global object which will hold the implementation of the bridge functions
mergeInto(LibraryManager.library, {
_emscripten_get_now: function () {
return Date.now();
},
observer_on_load(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_load(dotlottie_instance_id);
},
observer_on_load_error(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_load_error(dotlottie_instance_id);
},
observer_on_play(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_play(dotlottie_instance_id);
},
observer_on_pause(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_pause(dotlottie_instance_id);
},
observer_on_stop(dotlottie_instance_id) {
_DOTLOTTIE_BRIDGE_JS.observer_on_stop(dotlottie_instance_id);
},
observer_on_frame(dotlottie_instance_id, frame_no) {
_DOTLOTTIE_BRIDGE_JS.observer_on_frame(dotlottie_instance_id, frame_no);
},
observer_on_render(dotlottie_instance_id, frame_no) {
_DOTLOTTIE_BRIDGE_JS.observer_on_render(dotlottie_instance_id, frame_no);
},
observer_on_loop(dotlottie_instance_id, loop_count) {
_DOTLOTTIE_BRIDGE_JS.observer_on_loop(dotlottie_instance_id, loop_count);
},
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();
};
Loading

0 comments on commit ad37a4f

Please sign in to comment.