Mercurial > molko
comparison examples/example-gridmenu/example-gridmenu.c @ 507:d49a05e7a5b5
ui: separate delegate/style
Now UI elements do have different styling properties:
- _delegate: functions used to update, draw or perform specific actions on the
UI element.
- _style: basic properties that the delegate should support if possible.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 02 Mar 2023 21:36:43 +0100 |
parents | e205625015ba |
children | f45a023f6690 |
comparison
equal
deleted
inserted
replaced
506:e205625015ba | 507:d49a05e7a5b5 |
---|---|
36 | 36 |
37 #include <mlk/example/example.h> | 37 #include <mlk/example/example.h> |
38 #include <mlk/example/glower.h> | 38 #include <mlk/example/glower.h> |
39 #include <mlk/example/registry.h> | 39 #include <mlk/example/registry.h> |
40 | 40 |
41 static void menu_update(struct mlk_gridmenu_delegate *, struct mlk_gridmenu *, unsigned int); | |
42 | |
41 static struct mlk_state *states[8]; | 43 static struct mlk_state *states[8]; |
42 | 44 |
43 static const char * const items[] = { | 45 static const char * const items[] = { |
44 "Feu mineur", | 46 "Feu mineur", |
45 "Feu majeur", | 47 "Feu majeur", |
62 | 64 |
63 static struct mlk_frame frame = { | 65 static struct mlk_frame frame = { |
64 .w = 300, | 66 .w = 300, |
65 .h = 100 | 67 .h = 100 |
66 }; | 68 }; |
67 static struct mlk_gridmenu_style menu_style = { | 69 static struct mlk_gridmenu_style menu_style = {0}; |
68 .padding = 10, | 70 static struct mlk_gridmenu_delegate menu_delegate = { |
69 .text_color = 0x222323ff | 71 .update = menu_update |
70 }; | 72 }; |
71 static struct mlk_gridmenu menu = { | 73 static struct mlk_gridmenu menu = { |
72 .nrows = 3, | 74 .nrows = 3, |
73 .ncols = 2, | 75 .ncols = 2, |
74 .items = items, | 76 .items = items, |
75 .itemsz = MLK_UTIL_SIZE(items), | 77 .itemsz = MLK_UTIL_SIZE(items), |
76 .style = &menu_style | 78 .style = &menu_style, |
79 .delegate = &menu_delegate | |
77 }; | 80 }; |
78 static struct mlk_glower menu_glower = { | 81 static struct mlk_glower menu_glower = { |
79 .color = &menu_style.text_selected_color, | |
80 .start = 0x00bfa3ff, | 82 .start = 0x00bfa3ff, |
81 .end = 0x006b6dff, | 83 .end = 0x006b6dff, |
82 .delay = 20 | 84 .delay = 20 |
83 }; | 85 }; |
86 | |
87 static void | |
88 menu_update(struct mlk_gridmenu_delegate *delegate, struct mlk_gridmenu *menu, unsigned int ticks) | |
89 { | |
90 (void)delegate; | |
91 (void)menu; | |
92 | |
93 mlk_glower_update(&menu_glower, ticks); | |
94 menu_style.selected_color = menu_glower.color; | |
95 } | |
84 | 96 |
85 static void | 97 static void |
86 init(void) | 98 init(void) |
87 { | 99 { |
88 int err; | 100 int err; |
89 | 101 |
90 if ((err = mlk_example_init("example-gridmenu")) < 0) | 102 if ((err = mlk_example_init("example-gridmenu")) < 0) |
91 mlk_panicf("mlk_example_init: %s", mlk_err_string(err)); | 103 mlk_panicf("mlk_example_init: %s", mlk_err_string(err)); |
92 | 104 |
105 menu_style = mlk_gridmenu_style; | |
93 mlk_glower_init(&menu_glower); | 106 mlk_glower_init(&menu_glower); |
94 } | 107 } |
95 | 108 |
96 static void | 109 static void |
97 handle(struct mlk_state *st, const union mlk_event *ev) | 110 handle(struct mlk_state *st, const union mlk_event *ev) |
137 .handle = handle, | 150 .handle = handle, |
138 .update = update, | 151 .update = update, |
139 .draw = draw, | 152 .draw = draw, |
140 }; | 153 }; |
141 | 154 |
142 mlk_gridmenu_init(&menu); | |
143 mlk_gridmenu_resize(&menu, 0, 0, 300, 100); | 155 mlk_gridmenu_resize(&menu, 0, 0, 300, 100); |
144 | 156 |
145 mlk_align(MLK_ALIGN_CENTER, &menu.x, &menu.y, menu.w, menu.h, 0, 0, mlk_window.w, mlk_window.h); | 157 mlk_align(MLK_ALIGN_CENTER, &menu.x, &menu.y, menu.w, menu.h, 0, 0, mlk_window.w, mlk_window.h); |
146 frame.x = menu.x; | 158 frame.x = menu.x; |
147 frame.y = menu.y; | 159 frame.y = menu.y; |