-
Notifications
You must be signed in to change notification settings - Fork 23
Add Lottie animation playback support #152
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
Open
chungyichi
wants to merge
2
commits into
sysprog21:main
Choose a base branch
from
chungyichi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Twin - A Tiny Window System | ||
| * Copyright (c) 2025 National Cheng Kung University, Taiwan | ||
| * All rights reserved. | ||
| */ | ||
|
|
||
| #ifndef _APPS_LOTTIE_H_ | ||
| #define _APPS_LOTTIE_H_ | ||
|
|
||
| #include <twin.h> | ||
|
|
||
| /** | ||
| * Start Lottie animation player | ||
| * | ||
| * @param screen Screen to create window on | ||
| * @param name Window title | ||
| * @param path Path to Lottie file (.json or .lottie) | ||
| * @param x Window X position | ||
| * @param y Window Y position | ||
| */ | ||
| void apps_lottie_start(twin_screen_t *screen, | ||
| const char *name, | ||
| const char *path, | ||
| int x, | ||
| int y); | ||
|
|
||
| #endif /* _APPS_LOTTIE_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /* | ||
| * Twin - A Tiny Window System | ||
| * Copyright (c) 2024 National Cheng Kung University, Taiwan | ||
| * All rights reserved. | ||
| * | ||
| * Lottie player with Play/Pause and Loop controls | ||
| */ | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| #include <twin.h> | ||
|
|
||
| #include "apps_lottie.h" | ||
|
|
||
| #define BUTTON_SIZE twin_int_to_fixed(8) | ||
|
|
||
| typedef struct { | ||
| mado_lottie_image_t *lottie; | ||
| twin_pixmap_t *pix; | ||
| twin_timeout_t *timeout; | ||
| twin_label_t *status; | ||
| twin_custom_widget_t *display; | ||
| } lottie_app_t; | ||
|
|
||
| /* | ||
| * Animation display | ||
| */ | ||
|
|
||
| static void _lottie_paint(twin_custom_widget_t *widget) | ||
| { | ||
| lottie_app_t *app = (lottie_app_t *)twin_custom_widget_data(widget); | ||
| if (!app || !app->pix || !app->pix->animation) | ||
| return; | ||
|
|
||
| twin_pixmap_t *dst = twin_custom_widget_pixmap(widget); | ||
| if (!dst) | ||
| return; | ||
|
|
||
| twin_pixmap_t *frame = twin_animation_get_current_frame(app->pix->animation); | ||
| if (!frame) | ||
| return; | ||
|
|
||
| twin_fill(dst, 0xffe0e0e0, TWIN_SOURCE, 0, 0, dst->width, dst->height); | ||
|
|
||
| twin_coord_t ox = (dst->width - frame->width) / 2; | ||
| twin_coord_t oy = (dst->height - frame->height) / 2; | ||
| if (ox < 0) ox = 0; | ||
| if (oy < 0) oy = 0; | ||
|
|
||
| twin_operand_t srcop = { | ||
| .source_kind = TWIN_PIXMAP, | ||
| .u.pixmap = frame, | ||
| }; | ||
| twin_composite(dst, ox, oy, &srcop, 0, 0, NULL, 0, 0, | ||
| TWIN_OVER, frame->width, frame->height); | ||
| } | ||
|
|
||
| static twin_dispatch_result_t _lottie_dispatch(twin_widget_t *widget, | ||
| twin_event_t *event, | ||
| void *closure) | ||
| { | ||
| (void)closure; | ||
| twin_custom_widget_t *cw = twin_widget_get_custom(widget); | ||
| if (cw && event->kind == TwinEventPaint) | ||
| _lottie_paint(cw); | ||
| return TwinDispatchContinue; | ||
| } | ||
|
|
||
| /* | ||
| * Timer | ||
| */ | ||
|
|
||
| static twin_time_t _lottie_timeout(twin_time_t now, void *closure) | ||
| { | ||
| (void)now; | ||
| lottie_app_t *app = closure; | ||
|
|
||
| if (!app || !app->lottie || !app->pix) | ||
| return 100; | ||
|
|
||
| if (mado_lottie_is_playing(app->lottie)) { | ||
| twin_animation_advance_frame(app->pix->animation); | ||
| if (app->display) | ||
| twin_custom_widget_queue_paint(app->display); | ||
| } | ||
|
|
||
| return mado_lottie_get_frame_delay(app->lottie); | ||
| } | ||
|
|
||
| /* | ||
| * Status update | ||
| */ | ||
|
|
||
| static void update_status(lottie_app_t *app) | ||
| { | ||
| if (!app->status || !app->lottie) | ||
| return; | ||
|
|
||
| bool playing = mado_lottie_is_playing(app->lottie); | ||
| bool loop = mado_lottie_is_looping(app->lottie); | ||
|
|
||
| const char *text; | ||
| if (playing) | ||
| text = loop ? "Playing | Loop" : "Playing"; | ||
| else | ||
| text = loop ? "Paused | Loop" : "Paused"; | ||
|
|
||
| twin_label_set(app->status, text, 0xff000000, BUTTON_SIZE, TwinStyleBold); | ||
| } | ||
|
|
||
| /* | ||
| * Button callbacks | ||
| */ | ||
|
|
||
| static twin_dispatch_result_t _play_cb(twin_widget_t *widget, | ||
| twin_event_t *event, | ||
| void *closure) | ||
| { | ||
| (void)widget; | ||
| if (event->kind != TwinEventButtonSignalUp) | ||
| return TwinDispatchContinue; | ||
|
|
||
| lottie_app_t *app = closure; | ||
| mado_lottie_set_playback(app->lottie, !mado_lottie_is_playing(app->lottie)); | ||
| update_status(app); | ||
| return TwinDispatchDone; | ||
| } | ||
|
|
||
| static twin_dispatch_result_t _loop_cb(twin_widget_t *widget, | ||
| twin_event_t *event, | ||
| void *closure) | ||
| { | ||
| (void)widget; | ||
| if (event->kind != TwinEventButtonSignalUp) | ||
| return TwinDispatchContinue; | ||
|
|
||
| lottie_app_t *app = closure; | ||
| mado_lottie_set_loop(app->lottie, !mado_lottie_is_looping(app->lottie)); | ||
| update_status(app); | ||
| return TwinDispatchDone; | ||
| } | ||
|
|
||
| /* | ||
| * Public API | ||
| */ | ||
|
|
||
| void apps_lottie_start(twin_screen_t *screen, | ||
| const char *name, | ||
| const char *path, | ||
| int x, | ||
| int y) | ||
| { | ||
| twin_pixmap_t *pix = twin_pixmap_from_file(path, TWIN_ARGB32); | ||
| if (!pix) { | ||
| return; | ||
| } | ||
|
|
||
| if (!pix->animation || !twin_animation_is_lottie(pix->animation)) { | ||
| twin_pixmap_destroy(pix); | ||
| return; | ||
| } | ||
|
|
||
| mado_lottie_image_t *lottie = twin_animation_get_lottie(pix->animation); | ||
| twin_animation_t *anim = pix->animation; | ||
|
|
||
| int win_w = anim->width + 10; | ||
| int win_h = anim->height + 30; | ||
|
|
||
| twin_toplevel_t *top = twin_toplevel_create( | ||
| screen, TWIN_ARGB32, TwinWindowApplication, x, y, win_w, win_h, name); | ||
| if (!top) { | ||
| twin_pixmap_destroy(pix); | ||
| return; | ||
| } | ||
|
|
||
| twin_custom_widget_t *display = twin_custom_widget_create( | ||
| &top->box, 0xffe0e0e0, anim->width, anim->height, 1, 10, | ||
| _lottie_dispatch, sizeof(lottie_app_t)); | ||
|
|
||
| if (!display) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Resource leak: when Prompt for AI agents |
||
| twin_pixmap_destroy(pix); | ||
| return; | ||
| } | ||
|
|
||
| lottie_app_t *app = (lottie_app_t *)twin_custom_widget_data(display); | ||
| app->lottie = lottie; | ||
| app->pix = pix; | ||
| app->display = display; | ||
|
|
||
| /* Controls: Play | Loop | Status */ | ||
| twin_box_t *bar = twin_box_create(&top->box, TwinBoxHorz); | ||
|
|
||
| twin_button_t *play = twin_button_create(bar, "Play", 0xff000000, | ||
| BUTTON_SIZE, TwinStyleBold); | ||
| twin_widget_set_callback(&play->label.widget, _play_cb, app); | ||
|
|
||
| twin_button_t *loop = twin_button_create(bar, "Loop", 0xff000000, | ||
| BUTTON_SIZE, TwinStyleBold); | ||
| twin_widget_set_callback(&loop->label.widget, _loop_cb, app); | ||
|
|
||
| app->status = twin_label_create(bar, "Playing | Loop", 0xff000000, | ||
| BUTTON_SIZE, TwinStyleBold); | ||
|
|
||
| app->timeout = twin_set_timeout(_lottie_timeout, | ||
| mado_lottie_get_frame_delay(lottie), app); | ||
|
|
||
| twin_toplevel_show(top); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: The macOS installation instructions (line 71) should also be updated to include
rlottieandlibzipfor consistency with the Ubuntu/Debian changes. Without these packages, macOS users won't have the dependencies for Lottie animation support.Prompt for AI agents