changeset 149:a43e79d489ea

ui: allow custom position of labels
author David Demelier <markand@malikania.fr>
date Thu, 15 Oct 2020 10:45:40 +0200
parents c577c15df07f
children 9733d379be89
files examples/example-label.c examples/example-trace.c libadventure/adventure/trace_hud.c libui/ui/label.c libui/ui/label.h libui/ui/theme.c
diffstat 6 files changed, 25 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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();
--- 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]);
--- 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);
 }
--- 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. */
--- 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;
 	}