# HG changeset patch # User David Demelier # Date 1678303304 -3600 # Node ID 97af110e9e4dead91103c9bc36631982b68164c7 # Parent e91f37da2992348e6109239959b80086d3156d81 core: introduce MLK_WINDOW_CURSOR_OFF diff -r e91f37da2992 -r 97af110e9e4d examples/example-cursor/example-cursor.c --- a/examples/example-cursor/example-cursor.c Wed Mar 08 20:09:21 2023 +0100 +++ b/examples/example-cursor/example-cursor.c Wed Mar 08 20:21:44 2023 +0100 @@ -35,21 +35,22 @@ #include -static char help_text[128]; +static char help_cursor[128]; +static char help_enable[128]; static enum mlk_window_cursor cursor = MLK_WINDOW_CURSOR_ARROW; +static int visible = 1; -static struct mlk_label help = { +static struct mlk_label label_help_cursor = { .x = 10, .y = 10, - .text = help_text + .text = help_cursor }; -static void -init(void) -{ - if (mlk_example_init("example-cursor") < 0) - mlk_panic(); -} +static struct mlk_label label_help_enable = { + .x = 10, + .y = 30, + .text = help_enable +}; static void change(enum mlk_window_cursor cursor) @@ -64,11 +65,34 @@ [MLK_WINDOW_CURSOR_HAND] = "MLK_WINDOW_CURSOR_HAND" }; - snprintf(help_text, sizeof (help_text), "Keys: / to change cursor. Current: %s", names[cursor]); + snprintf(help_cursor, sizeof (help_cursor), "Keys: / to change cursor. Current: %s", names[cursor]); mlk_window_set_cursor(cursor); } static void +toggle(int enable) +{ + visible = enable; + + snprintf(help_enable, sizeof (help_enable), "Keys: to toggle. Current: %s", + enable ? "visible" : "hidden"); + + if (visible) + mlk_window_set_cursor(cursor); + else + mlk_window_set_cursor(MLK_WINDOW_CURSOR_OFF); +} + +static void +init(void) +{ + if (mlk_example_init("example-cursor") < 0) + mlk_panic(); + + toggle(1); +} + +static void handle(struct mlk_state *st, const union mlk_event *ev) { (void)st; @@ -81,9 +105,12 @@ change(--cursor); break; case MLK_KEY_RIGHT: - if (cursor + 1 < MLK_WINDOW_CURSOR_LAST) + if (cursor + 1 < MLK_WINDOW_CURSOR_OFF) change(++cursor); break; + case MLK_KEY_SPACE: + toggle(!visible); + break; default: break; } @@ -104,7 +131,8 @@ mlk_painter_set_color(MLK_EXAMPLE_BG); mlk_painter_clear(); - mlk_label_draw(&help); + mlk_label_draw(&label_help_cursor); + mlk_label_draw(&label_help_enable); mlk_painter_present(); } diff -r e91f37da2992 -r 97af110e9e4d libmlk-core/mlk/core/window.c --- a/libmlk-core/mlk/core/window.c Wed Mar 08 20:09:21 2023 +0100 +++ b/libmlk-core/mlk/core/window.c Wed Mar 08 20:21:44 2023 +0100 @@ -102,7 +102,12 @@ { assert(cursor < MLK_WINDOW_CURSOR_LAST); - SDL_SetCursor(cursors[cursor]); + if (cursor == MLK_WINDOW_CURSOR_OFF) + SDL_ShowCursor(0); + else { + SDL_ShowCursor(1); + SDL_SetCursor(cursors[cursor]); + } } void diff -r e91f37da2992 -r 97af110e9e4d libmlk-core/mlk/core/window.h --- a/libmlk-core/mlk/core/window.h Wed Mar 08 20:09:21 2023 +0100 +++ b/libmlk-core/mlk/core/window.h Wed Mar 08 20:21:44 2023 +0100 @@ -102,6 +102,11 @@ MLK_WINDOW_CURSOR_HAND, /** + * Disable cursor entirely. + */ + MLK_WINDOW_CURSOR_OFF, + + /** * Unused sentinel value. */ MLK_WINDOW_CURSOR_LAST