comparison examples/example-gridmenu.c @ 181:867b60e6258a

ui: add gridmenu object, closes #2511 @4h
author David Demelier <markand@malikania.fr>
date Tue, 27 Oct 2020 16:18:21 +0100
parents
children 4ad7420ab678
comparison
equal deleted inserted replaced
180:43aec6678cad 181:867b60e6258a
1 /*
2 * example-gridmenu.c -- show how to use grid menu
3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <core/clock.h>
20 #include <core/core.h>
21 #include <core/event.h>
22 #include <core/painter.h>
23 #include <core/panic.h>
24 #include <core/sys.h>
25 #include <core/trace.h>
26 #include <core/util.h>
27 #include <core/window.h>
28
29 #include <ui/align.h>
30 #include <ui/gridmenu.h>
31 #include <ui/label.h>
32 #include <ui/theme.h>
33 #include <ui/ui.h>
34
35 #define W (1280)
36 #define H (720)
37
38 static void
39 init(void)
40 {
41 if (!core_init() || !ui_init())
42 panic();
43 if (!window_open("Example - Grid menu", W, H))
44 panic();
45 }
46
47 static void
48 quit(void)
49 {
50 window_finish();
51 ui_finish();
52 core_finish();
53 }
54
55 static void
56 run(void)
57 {
58 struct clock clock = {0};
59 struct gridmenu menu = {
60 .menu = {
61 "Feu mineur",
62 "Feu majeur",
63 "Feu septième",
64 "Glace mineure",
65 "Glace majeure",
66 "Glace septième",
67 "Foudre mineure",
68 "Foudre majeure",
69 "Foudre septième",
70 "Choc mineur",
71 "Choc majeur",
72 "Choc septième",
73 },
74 .w = 300,
75 .h = 100,
76 .nrows = 3,
77 .ncols = 2
78 };
79
80 clock_start(&clock);
81 align(ALIGN_CENTER, &menu.x, &menu.y, menu.w, menu.h, 0, 0, W, H);
82
83 /* Need to repaint at least once. */
84 gridmenu_repaint(&menu);
85
86 for (;;) {
87 union event ev;
88 unsigned int elapsed = clock_elapsed(&clock);
89
90 clock_start(&clock);
91
92 while (event_poll(&ev)) {
93 switch (ev.type) {
94 case EVENT_QUIT:
95 return;
96 default:
97 gridmenu_handle(&menu, &ev);
98 break;
99 }
100 }
101
102 if (menu.state == GRIDMENU_STATE_SELECTED) {
103 tracef("selected index: %u", (unsigned int)menu.selected);
104 gridmenu_reset(&menu);
105 }
106
107 painter_set_color(0x4f8fbaff);
108 painter_clear();
109 gridmenu_draw(&menu);
110 painter_present();
111
112 if ((elapsed = clock_elapsed(&clock)) < 20)
113 delay(20 - elapsed);
114 }
115 }
116
117 int
118 main(int argc, char **argv)
119 {
120 (void)argc;
121 (void)argv;
122
123 init();
124 run();
125 quit();
126 }