comparison libcore/core/theme.c @ 145:7f1af54bb35a

core: rework label alignment, closes #2494 @1h Now labels can be positioned on a bounding box with much more convenient possibilities.
author David Demelier <markand@malikania.fr>
date Wed, 14 Oct 2020 18:11:38 +0200
parents eadfed7674ac
children 7d7ea7a9cf50
comparison
equal deleted inserted replaced
144:28d9bb62fcb1 145:7f1af54bb35a
67 } 67 }
68 68
69 static void 69 static void
70 draw_label(struct theme *t, const struct label *label) 70 draw_label(struct theme *t, const struct label *label)
71 { 71 {
72 struct font *font;
72 struct texture tex; 73 struct texture tex;
73 int x = label->x, y = label->y; 74 int x, y, bx, by;
74 int *px = &x, *py = &y; 75 unsigned int tw, th, bw, bh;
75 76
76 if (label->flags & LABEL_NO_HCENTER) 77 /* Compute real box size according to padding. */
77 px = NULL; 78 bx = label->x + t->padding;
78 if (label->flags & LABEL_NO_VCENTER) 79 by = label->y + t->padding;
79 py = NULL; 80 bw = label->w - (t->padding * 2);
81 bh = label->h - (t->padding * 2);
82
83 /* Make a shallow copy of the interface font. */
84 font = t->fonts[THEME_FONT_INTERFACE];
85
86 /* Compute text size. */
87 if (!font_box(font, label->text, &tw, &th))
88 panic();
89
90 /* Compute position according to alignment and box. */
91 switch (label->align) {
92 case LABEL_ALIGN_CENTER:
93 maths_centerize(&x, &y, tw, th, bx, by, bw, bh);
94 break;
95 case LABEL_ALIGN_TOP_LEFT:
96 x = bx;
97 y = by;
98 break;
99 case LABEL_ALIGN_TOP:
100 maths_centerize(&x, NULL, tw, th, bx, by, bw, bh);
101 y = by;
102 break;
103 case LABEL_ALIGN_TOP_RIGHT:
104 x = bx + bw - tw;
105 y = by;
106 break;
107 case LABEL_ALIGN_RIGHT:
108 maths_centerize(NULL, &y, tw, th, bx, by, bw, bh);
109 x = bx + bw - tw;
110 break;
111 case LABEL_ALIGN_BOTTOM_RIGHT:
112 x = bx + bw - tw;
113 y = by + bh - th;
114 break;
115 case LABEL_ALIGN_BOTTOM:
116 maths_centerize(&x, NULL, tw, th, bx, by, bw, bh);
117 y = by + bh - th;
118 break;
119 case LABEL_ALIGN_BOTTOM_LEFT:
120 x = bx;
121 y = by + bh - th;
122 break;
123 case LABEL_ALIGN_LEFT:
124 maths_centerize(NULL, &y, tw, th, bx, by, bw, bh);
125 x = bx;
126 default:
127 break;
128 }
80 129
81 /* Shadow text, only if enabled. */ 130 /* Shadow text, only if enabled. */
82 if (!(label->flags & LABEL_NO_SHADOW)) { 131 if (label->flags & LABEL_FLAGS_SHADOW) {
83 t->fonts[THEME_FONT_INTERFACE]->color = t->colors[THEME_COLOR_SHADOW]; 132 font->color = t->colors[THEME_COLOR_SHADOW];
84 133
85 if (!font_render(t->fonts[THEME_FONT_INTERFACE], &tex, label->text)) 134 if (!font_render(font, &tex, label->text))
86 panic(); 135 panic();
87
88 maths_centerize(px, py, tex.w, tex.h,
89 label->x, label->y, label->w, label->h);
90 136
91 texture_draw(&tex, x + 1, y + 1); 137 texture_draw(&tex, x + 1, y + 1);
92 texture_finish(&tex); 138 texture_finish(&tex);
93 } 139 }
94 140
95 /* Normal text. */ 141 /* Normal text. */
96 t->fonts[THEME_FONT_INTERFACE]->color = label->color 142 font->color = t->colors[THEME_COLOR_NORMAL];
97 ? label->color 143
98 : t->colors[THEME_COLOR_NORMAL]; 144 if (!font_render(font, &tex, label->text))
99
100 if (!font_render(t->fonts[THEME_FONT_INTERFACE], &tex, label->text))
101 panic(); 145 panic();
102
103 maths_centerize(px, py, tex.w, tex.h,
104 label->x, label->y, label->w, label->h);
105 146
106 texture_draw(&tex, x, y); 147 texture_draw(&tex, x, y);
107 texture_finish(&tex); 148 texture_finish(&tex);
108 } 149 }
109 150
140 const unsigned int w = cb->w - (t->padding * 2) - CHECKBOX_W; 181 const unsigned int w = cb->w - (t->padding * 2) - CHECKBOX_W;
141 const int x = cb->x + (t->padding * 2) + CHECKBOX_W; 182 const int x = cb->x + (t->padding * 2) + CHECKBOX_W;
142 183
143 struct label label = { 184 struct label label = {
144 .text = cb->label, 185 .text = cb->label,
145 .flags = LABEL_NO_HCENTER, 186 .align = LABEL_ALIGN_LEFT,
146 .x = x, 187 .x = x,
147 .y = cb->y, 188 .y = cb->y,
148 .w = w, 189 .w = w,
149 .h = cb->h 190 .h = cb->h
150 }; 191 };