changeset 565:97af110e9e4d

core: introduce MLK_WINDOW_CURSOR_OFF
author David Demelier <markand@malikania.fr>
date Wed, 08 Mar 2023 20:21:44 +0100
parents e91f37da2992
children d307f93d79af
files examples/example-cursor/example-cursor.c libmlk-core/mlk/core/window.c libmlk-core/mlk/core/window.h
diffstat 3 files changed, 51 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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 <mlk/example/example.h>
 
-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: <Left>/<Right> to change cursor. Current: %s", names[cursor]);
+	snprintf(help_cursor, sizeof (help_cursor), "Keys: <Left>/<Right> 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: <Space> 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();
 }
 
--- 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
--- 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