|
| 1 | +/* |
| 2 | + * Twin - A Tiny Window System |
| 3 | + * Copyright (c) 2024 National Cheng Kung University |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | + |
| 9 | +#include "twin_private.h" |
| 10 | + |
| 11 | +#include "apps_animation.h" |
| 12 | + |
| 13 | +#define _apps_animation_pixmap(animation) ((animation)->widget.window->pixmap) |
| 14 | + |
| 15 | +typedef struct { |
| 16 | + twin_widget_t widget; |
| 17 | + twin_pixmap_t *pix; |
| 18 | + twin_timeout_t *timeout; |
| 19 | +} apps_animation_t; |
| 20 | + |
| 21 | +static void _apps_animation_paint(apps_animation_t *anim) |
| 22 | +{ |
| 23 | + twin_pixmap_t *current_frame = NULL; |
| 24 | + |
| 25 | + if (twin_pixmap_is_animated(anim->pix)) { |
| 26 | + twin_animation_t *a = anim->pix->animation; |
| 27 | + current_frame = twin_animation_get_current_frame(a); |
| 28 | + twin_animation_advance_frame(a); |
| 29 | + } else { |
| 30 | + current_frame = anim->pix; |
| 31 | + } |
| 32 | + |
| 33 | + twin_operand_t srcop = { |
| 34 | + .source_kind = TWIN_PIXMAP, |
| 35 | + .u.pixmap = current_frame, |
| 36 | + }; |
| 37 | + twin_composite(_apps_animation_pixmap(anim), 0, 0, &srcop, 0, 0, NULL, 0, 0, |
| 38 | + TWIN_SOURCE, current_frame->width, current_frame->height); |
| 39 | +} |
| 40 | + |
| 41 | +static twin_time_t _apps_animation_timeout(twin_time_t maybe_unused now, |
| 42 | + void *closure) |
| 43 | +{ |
| 44 | + apps_animation_t *anim = closure; |
| 45 | + _twin_widget_queue_paint(&anim->widget); |
| 46 | + twin_animation_t *a = anim->pix->animation; |
| 47 | + twin_time_t delay = twin_animation_get_current_delay(a); |
| 48 | + return delay; |
| 49 | +} |
| 50 | + |
| 51 | +static twin_dispatch_result_t _apps_animation_dispatch(twin_widget_t *widget, |
| 52 | + twin_event_t *event) |
| 53 | +{ |
| 54 | + apps_animation_t *anim = (apps_animation_t *) widget; |
| 55 | + if (_twin_widget_dispatch(widget, event) == TwinDispatchDone) |
| 56 | + return TwinDispatchDone; |
| 57 | + switch (event->kind) { |
| 58 | + case TwinEventPaint: |
| 59 | + _apps_animation_paint(anim); |
| 60 | + break; |
| 61 | + default: |
| 62 | + break; |
| 63 | + } |
| 64 | + return TwinDispatchContinue; |
| 65 | +} |
| 66 | + |
| 67 | +static void _apps_animation_init(apps_animation_t *anim, |
| 68 | + twin_box_t *parent, |
| 69 | + twin_dispatch_proc_t dispatch) |
| 70 | +{ |
| 71 | + static const twin_widget_layout_t preferred = {0, 0, 1, 1}; |
| 72 | + _twin_widget_init(&anim->widget, parent, 0, preferred, dispatch); |
| 73 | + |
| 74 | + if (twin_pixmap_is_animated(anim->pix)) { |
| 75 | + twin_animation_t *a = anim->pix->animation; |
| 76 | + twin_time_t delay = twin_animation_get_current_delay(a); |
| 77 | + anim->timeout = twin_set_timeout(_apps_animation_timeout, delay, anim); |
| 78 | + } else { |
| 79 | + anim->timeout = NULL; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +static apps_animation_t *apps_animation_create(twin_box_t *parent, |
| 84 | + twin_pixmap_t *pix) |
| 85 | +{ |
| 86 | + apps_animation_t *anim = malloc(sizeof(apps_animation_t)); |
| 87 | + anim->pix = pix; |
| 88 | + _apps_animation_init(anim, parent, _apps_animation_dispatch); |
| 89 | + return anim; |
| 90 | +} |
| 91 | + |
| 92 | +void apps_animation_start(twin_screen_t *screen, |
| 93 | + const char *name, |
| 94 | + const char *path, |
| 95 | + int x, |
| 96 | + int y) |
| 97 | +{ |
| 98 | + twin_pixmap_t *pix = twin_pixmap_from_file(path, TWIN_ARGB32); |
| 99 | + twin_toplevel_t *toplevel = |
| 100 | + twin_toplevel_create(screen, TWIN_ARGB32, TwinWindowApplication, x, y, |
| 101 | + pix->width, pix->height, name); |
| 102 | + apps_animation_t *anim = apps_animation_create(&toplevel->box, pix); |
| 103 | + (void) anim; |
| 104 | + twin_toplevel_show(toplevel); |
| 105 | +} |
0 commit comments