Mercurial > molko
comparison examples/example-label.c @ 153:aa6e70e330a1
ui: make label less smart
It should not contain alignment because it should be handled by the user
tself.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 15 Oct 2020 18:45:27 +0200 |
parents | 9733d379be89 |
children | c3a40062acc2 |
comparison
equal
deleted
inserted
replaced
152:1008a796a9e7 | 153:aa6e70e330a1 |
---|---|
22 #include <core/panic.h> | 22 #include <core/panic.h> |
23 #include <core/sys.h> | 23 #include <core/sys.h> |
24 #include <core/util.h> | 24 #include <core/util.h> |
25 #include <core/window.h> | 25 #include <core/window.h> |
26 | 26 |
27 #include <ui/align.h> | |
27 #include <ui/label.h> | 28 #include <ui/label.h> |
28 #include <ui/theme.h> | 29 #include <ui/theme.h> |
29 | 30 |
30 #define W (1280) | 31 #define W (1280) |
31 #define H (720) | 32 #define H (720) |
33 | |
34 struct { | |
35 enum align align; | |
36 struct label label; | |
37 } table[] = { | |
38 { | |
39 .align = ALIGN_TOP_LEFT, | |
40 .label = { | |
41 .text = "Top left" | |
42 } | |
43 }, | |
44 { | |
45 .align = ALIGN_TOP, | |
46 .label = { | |
47 .text = "Top", | |
48 } | |
49 }, | |
50 { | |
51 .align = ALIGN_TOP_RIGHT, | |
52 .label = { | |
53 .text = "Top right", | |
54 } | |
55 }, | |
56 { | |
57 .align = ALIGN_RIGHT, | |
58 .label = { | |
59 .text = "Right", | |
60 } | |
61 }, | |
62 { | |
63 .align = ALIGN_BOTTOM_RIGHT, | |
64 .label = { | |
65 .text = "Bottom right", | |
66 } | |
67 }, | |
68 { | |
69 .align = ALIGN_BOTTOM, | |
70 .label = { | |
71 .text = "Bottom", | |
72 } | |
73 }, | |
74 { | |
75 .align = ALIGN_BOTTOM_LEFT, | |
76 .label = { | |
77 .text = "Bottom left", | |
78 } | |
79 }, | |
80 { | |
81 .align = ALIGN_LEFT, | |
82 .label = { | |
83 .text = "Left", | |
84 } | |
85 }, | |
86 { | |
87 .align = ALIGN_CENTER, | |
88 .label = { | |
89 .text = "The world is Malikania.", | |
90 .flags = LABEL_FLAGS_SHADOW | |
91 } | |
92 } | |
93 }; | |
32 | 94 |
33 static void | 95 static void |
34 init(void) | 96 init(void) |
35 { | 97 { |
36 if (!sys_init() || | 98 if (!sys_init() || |
37 !window_init("Example - Label", W, H) || | 99 !window_init("Example - Label", W, H) || |
38 !theme_init()) | 100 !theme_init()) |
39 panic(); | 101 panic(); |
102 | |
103 for (size_t i = 0; i < NELEM(table); ++i) { | |
104 struct label *l = &table[i].label; | |
105 | |
106 label_query(l); | |
107 align(table[i].align, &l->x, &l->y, l->w, l->h, 0, 0, W, H); | |
108 } | |
40 } | 109 } |
41 | 110 |
42 static void | 111 static void |
43 quit(void) | 112 quit(void) |
44 { | 113 { |
49 | 118 |
50 static void | 119 static void |
51 run(void) | 120 run(void) |
52 { | 121 { |
53 struct clock clock = {0}; | 122 struct clock clock = {0}; |
54 struct label labels[] = { | |
55 { | |
56 .x = 0, | |
57 .y = 0, | |
58 .w = W, | |
59 .h = H, | |
60 .text = "Top left", | |
61 .align = ALIGN_TOP_LEFT | |
62 }, | |
63 { | |
64 .x = 0, | |
65 .y = 0, | |
66 .w = W, | |
67 .h = H, | |
68 .text = "Top", | |
69 .align = ALIGN_TOP | |
70 }, | |
71 { | |
72 .x = 0, | |
73 .y = 0, | |
74 .w = W, | |
75 .h = H, | |
76 .text = "Top right", | |
77 .align = ALIGN_TOP_RIGHT | |
78 }, | |
79 { | |
80 .x = 0, | |
81 .y = 0, | |
82 .w = W, | |
83 .h = H, | |
84 .text = "Right", | |
85 .align = ALIGN_RIGHT | |
86 }, | |
87 { | |
88 .x = 0, | |
89 .y = 0, | |
90 .w = W, | |
91 .h = H, | |
92 .text = "Bottom right", | |
93 .align = ALIGN_BOTTOM_RIGHT | |
94 }, | |
95 { | |
96 .x = 0, | |
97 .y = 0, | |
98 .w = W, | |
99 .h = H, | |
100 .text = "Bottom", | |
101 .align = ALIGN_BOTTOM | |
102 }, | |
103 { | |
104 .x = 0, | |
105 .y = 0, | |
106 .w = W, | |
107 .h = H, | |
108 .text = "Bottom left", | |
109 .align = ALIGN_BOTTOM_LEFT | |
110 }, | |
111 { | |
112 .x = 0, | |
113 .y = 0, | |
114 .w = W, | |
115 .h = H, | |
116 .text = "Left", | |
117 .align = ALIGN_LEFT | |
118 }, | |
119 { | |
120 .x = 0, | |
121 .y = 0, | |
122 .w = W, | |
123 .h = H, | |
124 .text = "The world is Malikania.", | |
125 .flags = LABEL_FLAGS_SHADOW, | |
126 .align = ALIGN_CENTER | |
127 }, | |
128 }; | |
129 struct label mlabel = { | 123 struct label mlabel = { |
130 .text = "This one follows your mouse and is not aligned." | 124 .text = "This one follows your mouse and is not aligned." |
131 }; | 125 }; |
132 | 126 |
133 clock_start(&clock); | 127 clock_start(&clock); |
152 } | 146 } |
153 | 147 |
154 painter_set_color(0x4f8fbaff); | 148 painter_set_color(0x4f8fbaff); |
155 painter_clear(); | 149 painter_clear(); |
156 | 150 |
157 for (size_t i = 0; i < NELEM(labels); ++i) | 151 for (size_t i = 0; i < NELEM(table); ++i) |
158 label_draw(&labels[i]); | 152 label_draw(&table[i].label); |
159 | 153 |
160 label_draw(&mlabel); | 154 label_draw(&mlabel); |
161 painter_present(); | 155 painter_present(); |
162 | 156 |
163 if ((elapsed = clock_elapsed(&clock)) < 20) | 157 if ((elapsed = clock_elapsed(&clock)) < 20) |