comparison examples/example-cursor/example-cursor.c @ 565:97af110e9e4d

core: introduce MLK_WINDOW_CURSOR_OFF
author David Demelier <markand@malikania.fr>
date Wed, 08 Mar 2023 20:21:44 +0100
parents c7664b679a95
children
comparison
equal deleted inserted replaced
564:e91f37da2992 565:97af110e9e4d
33 #include <mlk/ui/label.h> 33 #include <mlk/ui/label.h>
34 #include <mlk/ui/ui.h> 34 #include <mlk/ui/ui.h>
35 35
36 #include <mlk/example/example.h> 36 #include <mlk/example/example.h>
37 37
38 static char help_text[128]; 38 static char help_cursor[128];
39 static char help_enable[128];
39 static enum mlk_window_cursor cursor = MLK_WINDOW_CURSOR_ARROW; 40 static enum mlk_window_cursor cursor = MLK_WINDOW_CURSOR_ARROW;
41 static int visible = 1;
40 42
41 static struct mlk_label help = { 43 static struct mlk_label label_help_cursor = {
42 .x = 10, 44 .x = 10,
43 .y = 10, 45 .y = 10,
44 .text = help_text 46 .text = help_cursor
45 }; 47 };
46 48
47 static void 49 static struct mlk_label label_help_enable = {
48 init(void) 50 .x = 10,
49 { 51 .y = 30,
50 if (mlk_example_init("example-cursor") < 0) 52 .text = help_enable
51 mlk_panic(); 53 };
52 }
53 54
54 static void 55 static void
55 change(enum mlk_window_cursor cursor) 56 change(enum mlk_window_cursor cursor)
56 { 57 {
57 static const char *names[] = { 58 static const char *names[] = {
62 [MLK_WINDOW_CURSOR_SIZE] = "MLK_WINDOW_CURSOR_SIZE", 63 [MLK_WINDOW_CURSOR_SIZE] = "MLK_WINDOW_CURSOR_SIZE",
63 [MLK_WINDOW_CURSOR_NO] = "MLK_WINDOW_CURSOR_NO", 64 [MLK_WINDOW_CURSOR_NO] = "MLK_WINDOW_CURSOR_NO",
64 [MLK_WINDOW_CURSOR_HAND] = "MLK_WINDOW_CURSOR_HAND" 65 [MLK_WINDOW_CURSOR_HAND] = "MLK_WINDOW_CURSOR_HAND"
65 }; 66 };
66 67
67 snprintf(help_text, sizeof (help_text), "Keys: <Left>/<Right> to change cursor. Current: %s", names[cursor]); 68 snprintf(help_cursor, sizeof (help_cursor), "Keys: <Left>/<Right> to change cursor. Current: %s", names[cursor]);
68 mlk_window_set_cursor(cursor); 69 mlk_window_set_cursor(cursor);
70 }
71
72 static void
73 toggle(int enable)
74 {
75 visible = enable;
76
77 snprintf(help_enable, sizeof (help_enable), "Keys: <Space> to toggle. Current: %s",
78 enable ? "visible" : "hidden");
79
80 if (visible)
81 mlk_window_set_cursor(cursor);
82 else
83 mlk_window_set_cursor(MLK_WINDOW_CURSOR_OFF);
84 }
85
86 static void
87 init(void)
88 {
89 if (mlk_example_init("example-cursor") < 0)
90 mlk_panic();
91
92 toggle(1);
69 } 93 }
70 94
71 static void 95 static void
72 handle(struct mlk_state *st, const union mlk_event *ev) 96 handle(struct mlk_state *st, const union mlk_event *ev)
73 { 97 {
79 case MLK_KEY_LEFT: 103 case MLK_KEY_LEFT:
80 if (cursor > 0) 104 if (cursor > 0)
81 change(--cursor); 105 change(--cursor);
82 break; 106 break;
83 case MLK_KEY_RIGHT: 107 case MLK_KEY_RIGHT:
84 if (cursor + 1 < MLK_WINDOW_CURSOR_LAST) 108 if (cursor + 1 < MLK_WINDOW_CURSOR_OFF)
85 change(++cursor); 109 change(++cursor);
110 break;
111 case MLK_KEY_SPACE:
112 toggle(!visible);
86 break; 113 break;
87 default: 114 default:
88 break; 115 break;
89 } 116 }
90 117
102 { 129 {
103 (void)st; 130 (void)st;
104 131
105 mlk_painter_set_color(MLK_EXAMPLE_BG); 132 mlk_painter_set_color(MLK_EXAMPLE_BG);
106 mlk_painter_clear(); 133 mlk_painter_clear();
107 mlk_label_draw(&help); 134 mlk_label_draw(&label_help_cursor);
135 mlk_label_draw(&label_help_enable);
108 mlk_painter_present(); 136 mlk_painter_present();
109 } 137 }
110 138
111 static void 139 static void
112 run(void) 140 run(void)