diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/simple_window.c | 149 |
1 files changed, 126 insertions, 23 deletions
diff --git a/examples/simple_window.c b/examples/simple_window.c index 41a5375..a6819f9 100644 --- a/examples/simple_window.c +++ b/examples/simple_window.c @@ -1,8 +1,27 @@ #include "yasl/yasl.h" #include <stdio.h> #include <stdlib.h> +#include <stdbool.h> static bool running = true; +static int mouse_x = -1; +static int mouse_y = -1; +static bool mouse_down = false; +static int color_theme = 0; +static uint16_t pen_pressure = 0; +static int8_t pen_tilt_x = 0; +static int8_t pen_tilt_y = 0; +static enum yasl_pointer_device pointer_device = YASL_PTR_DEV_MOUSE; + +static const char *device_name(enum yasl_pointer_device dev) +{ + switch (dev) { + case YASL_PTR_DEV_PEN: return "Pen/Stylus"; + case YASL_PTR_DEV_ERASER: return "Eraser"; + case YASL_PTR_DEV_TOUCH: return "Touch"; + default: return "Mouse"; + } +} static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud) { @@ -10,25 +29,75 @@ static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud) int w = frame->target.size.x; int h = frame->target.size.y; uint32_t *pixels = frame->target.pixels; - if (pixels) { - for (int y = 0; y < h; y++) { - for (int x = 0; x < w; x++) { - uint8_t r = (uint8_t)((x * 255) / (w > 0 ? w : 1)); - uint8_t g = (uint8_t)((y * 255) / (h > 0 ? h : 1)); - uint8_t b = 128; - pixels[y * w + x] = (255u << 24) | (r << 16) | (g << 8) | b; + if (!pixels || w <= 0 || h <= 0) + return; + + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + uint8_t r, g, b; + uint8_t u = (uint8_t)((x * 255) / (w > 0 ? w : 1)); + uint8_t v = (uint8_t)((y * 255) / (h > 0 ? h : 1)); + + switch (color_theme % 3) { + case 0: + r = u; + g = v; + b = 160; + break; + case 1: + r = 40; + g = u; + b = v; + break; + default: + r = v; + g = 60; + b = u; + break; } + + /* Draw interactive pointer / stylus indicator */ + if (mouse_x >= 0 && mouse_y >= 0) { + int dx = x - mouse_x; + int dy = y - mouse_y; + int dist_sq = dx * dx + dy * dy; + + /* Pressure-sensitive radius: scales dynamically from 8px to 64px */ + int radius; + if (pen_pressure > 0) { + radius = 8 + (int)(((uint32_t)pen_pressure * 56) / 65535); + } else { + radius = mouse_down ? 36 : 18; + } + + if (dist_sq < radius * radius) { + r = (uint8_t)(255 - r / 2); + g = (uint8_t)(255 - g / 2); + b = (uint8_t)(255 - b / 2); + } + } + + pixels[y * w + x] = (255u << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; } } + struct yasl_damage_list dmg = { .n = -1 }; yasl_surface_present(s, &frame->target, &dmg); } +static void on_configure(struct yasl_surface *s, struct yasl_geometry geo, void *ud) +{ + (void)ud; + printf("[YASL] Resized to %dx%d (scale: %.2fx)\n", + geo.size.x, geo.size.y, (double)geo.scale_120 / YASL_SCALE_UNIT); + yasl_surface_schedule_redraw(s); +} + static void on_close(struct yasl_surface *s, void *ud) { (void)s; (void)ud; - printf("Window close requested\n"); + printf("[YASL] Window close requested\n"); running = false; } @@ -40,58 +109,92 @@ static void on_quit(void *ud) int main(void) { - struct yasl_loop *loop = yasl_loop_create(); - if (!loop) { - fprintf(stderr, "Failed to create event loop\n"); - return 1; - } - struct yasl_display_cbs disp_cbs = { .quit = on_quit }; struct yasl_display_spec disp_spec = { .app_id = "org.yasl.example", .cbs = &disp_cbs, }; - struct yasl_display *d = yasl_display_open(loop, &disp_spec); + struct yasl_display *d = yasl_display_open(NULL, &disp_spec); if (!d) { fprintf(stderr, "Failed to open display\n"); - yasl_loop_destroy(loop); return 1; } struct yasl_surface_spec surf_spec = { - .title = "YASL Simple Window", + .title = "YASL Interactive Window (Pen & Touch Enabled)", .size = { .x = 640, .y = 480 }, }; struct yasl_surface *s = yasl_surface_create_toplevel(d, &surf_spec); if (!s) { fprintf(stderr, "Failed to create surface\n"); yasl_display_close(d); - yasl_loop_destroy(loop); return 1; } struct yasl_surface_cbs surf_cbs = { .draw = on_draw, + .configure = on_configure, .close = on_close, }; yasl_surface_set_callbacks(s, &surf_cbs, NULL); yasl_surface_schedule_redraw(s); + printf("=== YASL Demo Running ===\n"); + printf(" - Stylus/Pen: full pressure (0..65535) and tilt detection\n"); + printf(" - Mouse/Touch: interactive click/motion highlights\n"); + printf(" - Space: cycle background themes\n"); + printf(" - Escape: exit\n\n"); + while (running) { yasl_display_dispatch(d); + struct yasl_input_event ev; while (yasl_surface_input_poll(s, &ev)) { - if (ev.kind == YASL_INPUT_KEY && ev.key.action == YASL_KEY_PRESS) { - printf("Key pressed: keysym=%u\n", ev.key.key); - if (ev.key.key == 0xff1b) /* Escape */ - running = false; + if (ev.kind == YASL_INPUT_KEY) { + if (ev.key.action == YASL_KEY_PRESS) { + if (ev.key.key == 0xff1b) { /* Escape */ + running = false; + } else if (ev.key.key == ' ' || ev.key.key == 0xff0d) { /* Space or Return */ + color_theme++; + yasl_surface_schedule_redraw(s); + } + } + } else if (ev.kind == YASL_INPUT_MOUSE) { + mouse_x = ev.mouse.pos.x; + mouse_y = ev.mouse.pos.y; + pen_pressure = ev.mouse.pen.pressure; + pen_tilt_x = ev.mouse.pen.tilt_x; + pen_tilt_y = ev.mouse.pen.tilt_y; + pointer_device = ev.mouse.device; + + if (ev.mouse.kind == YASL_PTR_PRESS) { + mouse_down = true; + if (pen_pressure > 0 || pointer_device == YASL_PTR_DEV_PEN) { + printf("[Pen Down] Pressure: %5u/65535 (%.1f%%) | Tilt: (%+3d°, %+3d°) | Device: %s\n", + pen_pressure, (pen_pressure * 100.0) / 65535.0, + pen_tilt_x, pen_tilt_y, device_name(pointer_device)); + } + } else if (ev.mouse.kind == YASL_PTR_RELEASE) { + mouse_down = false; + pen_pressure = 0; + } else if (ev.mouse.kind == YASL_PTR_MOTION && pen_pressure > 0) { + printf("[Pen Move] Pressure: %5u/65535 (%.1f%%) | Tilt: (%+3d°, %+3d°) | Device: %s\n", + pen_pressure, (pen_pressure * 100.0) / 65535.0, + pen_tilt_x, pen_tilt_y, device_name(pointer_device)); + } else if (ev.mouse.kind == YASL_PTR_LEAVE) { + mouse_x = -1; + mouse_y = -1; + mouse_down = false; + pen_pressure = 0; + } + yasl_surface_schedule_redraw(s); } } + yasl_display_flush(d); } yasl_surface_destroy(s); yasl_display_close(d); - yasl_loop_destroy(loop); return 0; } |
