# HG changeset patch # User David Demelier # Date 1602751540 -7200 # Node ID a43e79d489ea285c60edf81202f98801c00fdbd8 # Parent c577c15df07fc97bfc1bb50fdcb78917b78e1ef8 ui: allow custom position of labels diff -r c577c15df07f -r a43e79d489ea examples/example-label.c --- a/examples/example-label.c Thu Oct 15 10:32:18 2020 +0200 +++ b/examples/example-label.c Thu Oct 15 10:45:40 2020 +0200 @@ -122,9 +122,13 @@ .w = W, .h = H, .text = "The world is Malikania.", - .flags = LABEL_FLAGS_SHADOW + .flags = LABEL_FLAGS_SHADOW, + .align = LABEL_ALIGN_CENTER }, }; + struct label mlabel = { + .text = "This one follows your mouse and is not aligned." + }; clock_start(&clock); @@ -136,6 +140,10 @@ while (event_poll(&ev)) { switch (ev.type) { + case EVENT_MOUSE: + mlabel.x = ev.mouse.x; + mlabel.y = ev.mouse.y; + break; case EVENT_QUIT: return; default: @@ -149,6 +157,7 @@ for (size_t i = 0; i < NELEM(labels); ++i) label_draw(&labels[i]); + label_draw(&mlabel); painter_present(); if ((elapsed = clock_elapsed(&clock)) < 20) diff -r c577c15df07f -r a43e79d489ea examples/example-trace.c --- a/examples/example-trace.c Thu Oct 15 10:32:18 2020 +0200 +++ b/examples/example-trace.c Thu Oct 15 10:45:40 2020 +0200 @@ -78,7 +78,7 @@ } } - painter_set_color(0xffffffff); + painter_set_color(0x4f8fbaff); painter_clear(); trace_hud_update(elapsed); trace_hud_draw(); diff -r c577c15df07f -r a43e79d489ea libadventure/adventure/trace_hud.c --- a/libadventure/adventure/trace_hud.c Thu Oct 15 10:32:18 2020 +0200 +++ b/libadventure/adventure/trace_hud.c Thu Oct 15 10:45:40 2020 +0200 @@ -100,7 +100,7 @@ .y = y, .text = data.lines[i], .theme = th, - .align = LABEL_ALIGN_TOP_LEFT + .flags = LABEL_FLAGS_SHADOW }); y += font_height(th->fonts[THEME_FONT_INTERFACE]); diff -r c577c15df07f -r a43e79d489ea libui/ui/label.c --- a/libui/ui/label.c Thu Oct 15 10:32:18 2020 +0200 +++ b/libui/ui/label.c Thu Oct 15 10:45:40 2020 +0200 @@ -29,8 +29,8 @@ assert(label); assert(label->text); - if (label->w == 0 || label->h == 0) - trace("label %p has null dimensions", label); + if (label->align != LABEL_ALIGN_NONE && (label->w == 0 || label->h == 0)) + trace("label %p has alignment but null dimensions", label); theme_draw_label(label->theme, label); } diff -r c577c15df07f -r a43e79d489ea libui/ui/label.h --- a/libui/ui/label.h Thu Oct 15 10:32:18 2020 +0200 +++ b/libui/ui/label.h Thu Oct 15 10:45:40 2020 +0200 @@ -50,7 +50,8 @@ * ``` */ enum label_align { - LABEL_ALIGN_CENTER, /*!< Align to the center (default). */ + LABEL_ALIGN_NONE, /*!< No alignment. */ + LABEL_ALIGN_CENTER, /*!< Align to the center. */ LABEL_ALIGN_TOP_LEFT, /*!< Top left. */ LABEL_ALIGN_TOP, /*!< Top center (aligned horizontally). */ LABEL_ALIGN_TOP_RIGHT, /*!< Top right. */ @@ -63,12 +64,16 @@ /** * \brief GUI label. + * + * A label can be conveniently positioned using a bounding box and an alignment + * in that case the fields `w`, `h` and `align` must be specified, otherwise + * the label is drawn immediately at `x` and `y` field. */ struct label { int x; /*!< (+) Position in x. */ int y; /*!< (+) Position in y. */ - unsigned int w; /*!< (+) Width. */ - unsigned int h; /*!< (+) Height. */ + unsigned int w; /*!< (+?) Width. */ + unsigned int h; /*!< (+?) Height. */ const char *text; /*!< (+&) Text to show. */ enum label_flags flags; /*!< (+) Optional flags. */ enum label_align align; /*!< (+) How to positionate label. */ diff -r c577c15df07f -r a43e79d489ea libui/ui/theme.c --- a/libui/ui/theme.c Thu Oct 15 10:32:18 2020 +0200 +++ b/libui/ui/theme.c Thu Oct 15 10:45:40 2020 +0200 @@ -125,7 +125,10 @@ case LABEL_ALIGN_LEFT: maths_centerize(NULL, &y, tw, th, bx, by, bw, bh); x = bx; + break; default: + x = label->x; + y = label->y; break; }