comparison examples/example-gridmenu/example-gridmenu.c @ 506:e205625015ba

ui: gridmenu is stylable
author David Demelier <markand@malikania.fr>
date Thu, 02 Mar 2023 08:54:00 +0100
parents 41267f6e344d
children d49a05e7a5b5
comparison
equal deleted inserted replaced
505:6100c643dba0 506:e205625015ba
27 #include <mlk/core/trace.h> 27 #include <mlk/core/trace.h>
28 #include <mlk/core/util.h> 28 #include <mlk/core/util.h>
29 #include <mlk/core/window.h> 29 #include <mlk/core/window.h>
30 30
31 #include <mlk/ui/align.h> 31 #include <mlk/ui/align.h>
32 #include <mlk/ui/frame.h>
32 #include <mlk/ui/gridmenu.h> 33 #include <mlk/ui/gridmenu.h>
33 #include <mlk/ui/label.h> 34 #include <mlk/ui/label.h>
34 #include <mlk/ui/theme.h>
35 #include <mlk/ui/ui.h> 35 #include <mlk/ui/ui.h>
36 36
37 #include <mlk/example/example.h> 37 #include <mlk/example/example.h>
38 #include <mlk/example/glower.h>
38 #include <mlk/example/registry.h> 39 #include <mlk/example/registry.h>
39 40
40 static struct mlk_state *states[1]; 41 static struct mlk_state *states[8];
42
43 static const char * const items[] = {
44 "Feu mineur",
45 "Feu majeur",
46 "Feu septième",
47 "Glace mineure",
48 "Glace majeure",
49 "Glace septième",
50 "Foudre mineure",
51 "Foudre majeure",
52 "Foudre septième",
53 "Choc mineur",
54 "Choc majeur",
55 "Choc septième",
56 "Portée",
57 "Escapade",
58 "Destruction",
59 "Résurrection",
60 "Double tour"
61 };
62
63 static struct mlk_frame frame = {
64 .w = 300,
65 .h = 100
66 };
67 static struct mlk_gridmenu_style menu_style = {
68 .padding = 10,
69 .text_color = 0x222323ff
70 };
71 static struct mlk_gridmenu menu = {
72 .nrows = 3,
73 .ncols = 2,
74 .items = items,
75 .itemsz = MLK_UTIL_SIZE(items),
76 .style = &menu_style
77 };
78 static struct mlk_glower menu_glower = {
79 .color = &menu_style.text_selected_color,
80 .start = 0x00bfa3ff,
81 .end = 0x006b6dff,
82 .delay = 20
83 };
41 84
42 static void 85 static void
43 init(void) 86 init(void)
44 { 87 {
45 int err; 88 int err;
46 89
47 if ((err = mlk_example_init("example-gridmenu")) < 0) 90 if ((err = mlk_example_init("example-gridmenu")) < 0)
48 mlk_panicf("mlk_example_init: %s", mlk_err_string(err)); 91 mlk_panicf("mlk_example_init: %s", mlk_err_string(err));
92
93 mlk_glower_init(&menu_glower);
49 } 94 }
50 95
51 static void 96 static void
52 handle(struct mlk_state *st, const union mlk_event *ev) 97 handle(struct mlk_state *st, const union mlk_event *ev)
53 { 98 {
54 struct mlk_gridmenu *menu = st->data; 99 (void)st;
55 100
56 switch (ev->type) { 101 switch (ev->type) {
57 case MLK_EVENT_QUIT: 102 case MLK_EVENT_QUIT:
58 mlk_game_quit(); 103 mlk_game_quit();
59 break; 104 break;
60 default: 105 default:
61 if (mlk_gridmenu_handle(st->data, ev)) 106 if (mlk_gridmenu_handle(&menu, ev))
62 mlk_tracef("selected index: %zu (%s)", menu->selected, menu->items[menu->selected]); 107 mlk_tracef("selected index: %zu (%s)", menu.selected, menu.items[menu.selected]);
63 break; 108 break;
64 } 109 }
65 } 110 }
66 111
67 static void 112 static void
113 update(struct mlk_state *st, unsigned int ticks)
114 {
115 (void)st;
116
117 mlk_glower_update(&menu_glower, ticks);
118 mlk_gridmenu_update(&menu, ticks);
119 }
120
121 static void
68 draw(struct mlk_state *st) 122 draw(struct mlk_state *st)
69 { 123 {
70 mlk_painter_set_color(0x4f8fbaff); 124 (void)st;
125
126 mlk_painter_set_color(MLK_EXAMPLE_BG);
71 mlk_painter_clear(); 127 mlk_painter_clear();
72 mlk_gridmenu_draw(st->data); 128 mlk_frame_draw(&frame);
129 mlk_gridmenu_draw(&menu);
73 mlk_painter_present(); 130 mlk_painter_present();
74 } 131 }
75 132
76 static void 133 static void
77 run(void) 134 run(void)
78 { 135 {
79 const char * const items[] = {
80 "Feu mineur",
81 "Feu majeur",
82 "Feu septième",
83 "Glace mineure",
84 "Glace majeure",
85 "Glace septième",
86 "Foudre mineure",
87 "Foudre majeure",
88 "Foudre septième",
89 "Choc mineur",
90 "Choc majeur",
91 "Choc septième",
92 "Portée",
93 "Escapade",
94 "Destruction",
95 "Résurrection",
96 "Double tour"
97 };
98
99 struct mlk_gridmenu menu = {0};
100 struct mlk_state state = { 136 struct mlk_state state = {
101 .data = &menu,
102 .handle = handle, 137 .handle = handle,
138 .update = update,
103 .draw = draw, 139 .draw = draw,
104 }; 140 };
105 141
106 mlk_gridmenu_init(&menu, 3, 2, items, MLK_UTIL_SIZE(items)); 142 mlk_gridmenu_init(&menu);
107 mlk_gridmenu_resize(&menu, 0, 0, 300, 100); 143 mlk_gridmenu_resize(&menu, 0, 0, 300, 100);
108 144
109 mlk_align(MLK_ALIGN_CENTER, &menu.x, &menu.y, menu.w, menu.h, 0, 0, mlk_window.w, mlk_window.h); 145 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;
147 frame.y = menu.y;
110 148
111 mlk_game_init(states, MLK_UTIL_SIZE(states)); 149 mlk_game_init(states, MLK_UTIL_SIZE(states));
112 mlk_game_push(&state); 150 mlk_game_push(&state);
113 mlk_game_loop(); 151 mlk_game_loop();
114 } 152 }