changeset 161:31d7f23c0588

ui: label no longer has w, h fields
author David Demelier <markand@malikania.fr>
date Sat, 17 Oct 2020 10:12:41 +0200
parents 22141b6b2e43
children 629f55f3961e
files examples/example-action.c examples/example-label.c examples/example-ui.c libui/ui/button.c libui/ui/label.c libui/ui/label.h
diffstat 6 files changed, 25 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-action.c	Sat Oct 17 10:01:46 2020 +0200
+++ b/examples/example-action.c	Sat Oct 17 10:12:41 2020 +0200
@@ -46,7 +46,6 @@
 #define H       (720)
 
 #define MW      (W * 0.75)
-#define MH      (H * 0.125)
 #define MX      ((W / 2) - (MW / 2))
 #define MY      (100)
 
@@ -73,7 +72,6 @@
 		.x = MX,
 		.y = MY,
 		.w = MW,
-		.h = MH,
 		.text = {
 			"100000 pièces.",
 			"Te voilà riche sale file de crevette."
@@ -123,7 +121,6 @@
 				.x = MX,
 				.y = MY,
 				.w = MW,
-				.h = MH,
 				.delay = MESSAGE_DELAY_DEFAULT,
 				.flags = MESSAGE_FLAGS_FADEIN,
 				.text = {
@@ -136,7 +133,6 @@
 				.x = MX,
 				.y = MY,
 				.w = MW,
-				.h = MH,
 				.text = {
 					"Penses tu vraiment pouvoir me battre ?"
 				}
@@ -147,7 +143,6 @@
 				.x = MX,
 				.y = MY,
 				.w = MW,
-				.h = MH,
 				.flags = MESSAGE_FLAGS_QUESTION,
 				.text = {
 					"Non j'ai la trouille.",
@@ -162,7 +157,6 @@
 				.x = MX,
 				.y = MY,
 				.w = MW,
-				.h = MH,
 				.text = {
 					"Poule mouillée."
 				}
@@ -175,7 +169,6 @@
 				.x = MX,
 				.y = MY,
 				.w = MW,
-				.h = MH,
 				.text = {
 					"Prépare toi à souffrir."
 				}
@@ -203,6 +196,7 @@
 	const int index = guide.msgs[2].msg.index + 3;
 
 	message_action(&guide.msgs[index].msg, &guide.msgs[index].act);
+	message_query(&guide.msgs[index].msg, NULL, &guide.msgs[index].msg.h);
 	action_stack_add(&modal, &guide.msgs[index].act);
 }
 
@@ -212,6 +206,7 @@
 	/* Prepare the script with first 3 messages. */
 	for (size_t i = 0; i < 3; ++i) {
 		message_action(&guide.msgs[i].msg, &guide.msgs[i].act);
+		message_query(&guide.msgs[i].msg, NULL, &guide.msgs[i].msg.h);
 		script_append(&guide.script, &guide.msgs[i].act);
 	}
 
@@ -284,6 +279,7 @@
 		    ev->click.x, ev->click.y)) {
 			chest.opened = true;
 			message_action(&chest.msg, &chest.msg_act);
+			message_query(&chest.msg, NULL, &chest.msg.h);
 			action_stack_add(&modal, &chest.msg_act);
 		}
 	default:
--- a/examples/example-label.c	Sat Oct 17 10:01:46 2020 +0200
+++ b/examples/example-label.c	Sat Oct 17 10:12:41 2020 +0200
@@ -104,9 +104,10 @@
 
 	for (size_t i = 0; i < NELEM(table); ++i) {
 		struct label *l = &table[i].label;
+		unsigned int w, h;
 
-		label_query(l);
-		align(table[i].align, &l->x, &l->y, l->w, l->h, 0, 0, W, H);
+		label_query(l, &w, &h);
+		align(table[i].align, &l->x, &l->y, w, h, 0, 0, W, H);
 	}
 }
 
--- a/examples/example-ui.c	Sat Oct 17 10:01:46 2020 +0200
+++ b/examples/example-ui.c	Sat Oct 17 10:12:41 2020 +0200
@@ -137,12 +137,13 @@
 static void
 resize_header(void)
 {
-	struct frame *h = &ui.panel.frame;
+	struct frame *f = &ui.panel.frame;
 	struct label *l = &ui.header.label;
+	unsigned int w, h;
 
 	/* Header. */
-	label_query(l);
-	align(ALIGN_LEFT, &l->x, &l->y, l->w, l->h, h->x, h->y, h->w, HEADER_HEIGHT);
+	label_query(l, &w, &h);
+	align(ALIGN_LEFT, &l->x, &l->y, w, h, f->x, f->y, f->w, HEADER_HEIGHT);
 
 	l->x += theme_default()->padding;
 }
@@ -158,7 +159,6 @@
 	c->x = f->x + padding;
 	c->y = f->y + HEADER_HEIGHT + padding;
 
-	l->w = f->w - c->w - padding;
 	l->x = c->x + c->w + padding;
 	l->y = c->y;
 }
--- a/libui/ui/button.c	Sat Oct 17 10:01:46 2020 +0200
+++ b/libui/ui/button.c	Sat Oct 17 10:12:41 2020 +0200
@@ -23,6 +23,7 @@
 #include <core/event.h>
 #include <core/maths.h>
 #include <core/painter.h>
+#include <core/trace.h>
 
 #include "align.h"
 #include "button.h"
@@ -63,9 +64,16 @@
 	struct label label = {
 		.text = button->text,
 	};
+	unsigned int lw, lh;
 
-	label_query(&label);
-	align(ALIGN_CENTER, &label.x, &label.y, label.w, label.h,
+	label_query(&label, &lw, &lh);
+
+	if (lw > button->w)
+		trace("button is too small for text: %u < %u", button->w, lw);
+	if (lh > button->h)
+		trace("button is too small for text: %u < %u", button->h, lh);
+
+	align(ALIGN_CENTER, &label.x, &label.y, lw, lh,
 	    button->x, button->y, button->w, button->h);
 
 	painter_set_color(0x577277ff);
--- a/libui/ui/label.c	Sat Oct 17 10:01:46 2020 +0200
+++ b/libui/ui/label.c	Sat Oct 17 10:12:41 2020 +0200
@@ -66,14 +66,14 @@
 }
 
 void
-label_query(struct label *label)
+label_query(struct label *label, unsigned int *w, unsigned int *h)
 {
 	assert(label);
 	assert(label->text);
 
 	struct theme *t = label->theme ? label->theme : theme_default();
 
-	if (!font_query(t->fonts[THEME_FONT_INTERFACE], label->text, &label->w, &label->h))
+	if (!font_query(t->fonts[THEME_FONT_INTERFACE], label->text, w, h))
 		panic();
 }
 
--- a/libui/ui/label.h	Sat Oct 17 10:01:46 2020 +0200
+++ b/libui/ui/label.h	Sat Oct 17 10:12:41 2020 +0200
@@ -38,16 +38,10 @@
 
 /**
  * \brief GUI label.
- *
- * A label has a position and a size. The size is only provided to the user as
- * information and is not used during rendering. It should be computed using the
- * \ref label_query command each time you change the theme or text.
  */
 struct label {
 	int x;                          /*!< (+) Position in x. */
 	int y;                          /*!< (+) Position in y. */
-	unsigned int w;                 /*!< (+?) Width. */
-	unsigned int h;                 /*!< (+?) Height. */
 	const char *text;               /*!< (+&) Text to show. */
 	enum label_flags flags;         /*!< (+) Optional flags. */
 	struct theme *theme;            /*!< (+&?) Theme to use. */
@@ -70,9 +64,11 @@
  *
  * \pre label != NULL
  * \param label the label
+ * \param w the pointer to width (may be NULL)
+ * \param h the pointer to height (may be NULL)
  */
 void
-label_query(struct label *label);
+label_query(struct label *label, unsigned int *w, unsigned int *h);
 
 /**
  * Draw the label.