|
| 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_image.h" |
| 12 | + |
| 13 | +#define _apps_image_pixmap(image) ((image)->widget.window->pixmap) |
| 14 | +#define D(x) twin_double_to_fixed(x) |
| 15 | +#define ASSET_PATH "assets/" |
| 16 | +#define APP_WIDTH 400 |
| 17 | +#define APP_HEIGHT 400 |
| 18 | +typedef struct { |
| 19 | + twin_widget_t widget; |
| 20 | + twin_pixmap_t **pixes; |
| 21 | + int image_idx; |
| 22 | +} apps_image_t; |
| 23 | + |
| 24 | +static const char *tvg_files[] = { |
| 25 | + /* https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/ */ |
| 26 | + ASSET_PATH "tiger.tvg", |
| 27 | + /* https://tinyvg.tech/img/chart.svg */ |
| 28 | + ASSET_PATH "chart.tvg", |
| 29 | + /* https://freesvg.org/betrayed */ |
| 30 | + ASSET_PATH "comic.tvg", |
| 31 | + /* https://github.com/PapirusDevelopmentTeam/papirus-icon-theme */ |
| 32 | + ASSET_PATH "folder.tvg", |
| 33 | + /* https://materialdesignicons.com/ */ |
| 34 | + ASSET_PATH "shield.tvg", |
| 35 | + /* https://tinyvg.tech/img/flowchart.png */ |
| 36 | + ASSET_PATH "flowchart.tvg", |
| 37 | +}; |
| 38 | + |
| 39 | +static void _apps_image_paint(apps_image_t *img) |
| 40 | +{ |
| 41 | + twin_operand_t srcop = { |
| 42 | + .source_kind = TWIN_PIXMAP, |
| 43 | + .u.pixmap = img->pixes[img->image_idx], |
| 44 | + }; |
| 45 | + |
| 46 | + twin_composite(_apps_image_pixmap(img), 0, 0, &srcop, 0, 0, NULL, 0, 0, |
| 47 | + TWIN_SOURCE, APP_WIDTH, APP_HEIGHT); |
| 48 | +} |
| 49 | + |
| 50 | +static twin_dispatch_result_t _apps_image_dispatch(twin_widget_t *widget, |
| 51 | + twin_event_t *event) |
| 52 | +{ |
| 53 | + apps_image_t *img = (apps_image_t *) widget; |
| 54 | + if (_twin_widget_dispatch(widget, event) == TwinDispatchDone) |
| 55 | + return TwinDispatchDone; |
| 56 | + switch (event->kind) { |
| 57 | + case TwinEventPaint: |
| 58 | + _apps_image_paint(img); |
| 59 | + break; |
| 60 | + default: |
| 61 | + break; |
| 62 | + } |
| 63 | + return TwinDispatchContinue; |
| 64 | +} |
| 65 | + |
| 66 | +static void _apps_image_button_signal(maybe_unused twin_button_t *button, |
| 67 | + twin_button_signal_t signal, |
| 68 | + void *closure) |
| 69 | +{ |
| 70 | + if (signal != TwinButtonSignalDown) |
| 71 | + return; |
| 72 | + |
| 73 | + apps_image_t *img = closure; |
| 74 | + const int n = sizeof(tvg_files) / sizeof(tvg_files[0]); |
| 75 | + img->image_idx = img->image_idx == n - 1 ? 0 : img->image_idx + 1; |
| 76 | + if (!img->pixes[img->image_idx]) { |
| 77 | + twin_pixmap_t *pix = twin_tvg_to_pixmap_scale( |
| 78 | + tvg_files[img->image_idx], TWIN_ARGB32, APP_WIDTH, APP_HEIGHT); |
| 79 | + if (!pix) |
| 80 | + return; |
| 81 | + img->pixes[img->image_idx] = pix; |
| 82 | + } |
| 83 | + _twin_widget_queue_paint(&img->widget); |
| 84 | +} |
| 85 | + |
| 86 | +static void _apps_image_init(apps_image_t *img, |
| 87 | + twin_box_t *parent, |
| 88 | + twin_dispatch_proc_t dispatch) |
| 89 | +{ |
| 90 | + static twin_widget_layout_t preferred = {0, 0, 1, 1}; |
| 91 | + preferred.height = parent->widget.window->screen->height * 3.0 / 4.0; |
| 92 | + _twin_widget_init(&img->widget, parent, 0, preferred, dispatch); |
| 93 | + img->image_idx = 0; |
| 94 | + img->pixes = calloc(sizeof(tvg_files), sizeof(twin_pixmap_t *)); |
| 95 | + img->pixes[0] = twin_tvg_to_pixmap_scale(tvg_files[0], TWIN_ARGB32, |
| 96 | + APP_WIDTH, APP_HEIGHT); |
| 97 | + twin_button_t *button = |
| 98 | + twin_button_create(parent, "Next Image", 0xFF482722, D(10), |
| 99 | + TwinStyleBold | TwinStyleOblique); |
| 100 | + twin_widget_set(&button->label.widget, 0xFFFEE4CE); |
| 101 | + button->signal = _apps_image_button_signal; |
| 102 | + button->closure = img; |
| 103 | + button->label.widget.shape = TwinShapeRectangle; |
| 104 | +} |
| 105 | + |
| 106 | +static apps_image_t *apps_image_create(twin_box_t *parent) |
| 107 | +{ |
| 108 | + apps_image_t *img = malloc(sizeof(apps_image_t)); |
| 109 | + |
| 110 | + _apps_image_init(img, parent, _apps_image_dispatch); |
| 111 | + return img; |
| 112 | +} |
| 113 | + |
| 114 | +void apps_image_start(twin_screen_t *screen, const char *name, int x, int y) |
| 115 | +{ |
| 116 | + twin_toplevel_t *toplevel = |
| 117 | + twin_toplevel_create(screen, TWIN_ARGB32, TwinWindowApplication, x, y, |
| 118 | + APP_WIDTH, APP_HEIGHT, name); |
| 119 | + apps_image_t *img = apps_image_create(&toplevel->box); |
| 120 | + (void) img; |
| 121 | + twin_toplevel_show(toplevel); |
| 122 | +} |
0 commit comments