[Bf-blender-cvs] [01a3c956093] master: UI: tool-tip refactor, don't hard code field types

Campbell Barton noreply at git.blender.org
Sat Nov 4 11:10:52 CET 2017


Commit: 01a3c9560938c98793a2d2a0b61e574ed5c27e4a
Author: Campbell Barton
Date:   Sat Nov 4 19:24:43 2017 +1100
Branches: master
https://developer.blender.org/rB01a3c9560938c98793a2d2a0b61e574ed5c27e4a

UI: tool-tip refactor, don't hard code field types

Allow other kinds of tips be created w/o exceeding hard limits.

===================================================================

M	source/blender/editors/interface/interface_region_tooltip.c

===================================================================

diff --git a/source/blender/editors/interface/interface_region_tooltip.c b/source/blender/editors/interface/interface_region_tooltip.c
index 49c6fff7fc0..ace1afb79aa 100644
--- a/source/blender/editors/interface/interface_region_tooltip.c
+++ b/source/blender/editors/interface/interface_region_tooltip.c
@@ -68,44 +68,66 @@
 #define UI_TIP_PADDING      (int)(UI_TIP_PAD_FAC * UI_UNIT_Y)
 #define UI_TIP_MAXWIDTH     600
 
-#define MAX_TOOLTIP_LINES 8
-typedef struct uiTooltipData {
-	rcti bbox;
-	uiFontStyle fstyle;
-	char lines[MAX_TOOLTIP_LINES][2048];
-	char header[2048], active_info[2048];
-	struct {
-		enum {
-			UI_TIP_STYLE_NORMAL = 0,
-			UI_TIP_STYLE_HEADER,
-			UI_TIP_STYLE_MONO,
-		} style : 3;
-		enum {
-			UI_TIP_LC_MAIN = 0,     /* primary text */
-			UI_TIP_LC_VALUE,        /* the value of buttons (also shortcuts) */
-			UI_TIP_LC_ACTIVE,       /* titles of active enum values */
-			UI_TIP_LC_NORMAL,       /* regular text */
-			UI_TIP_LC_PYTHON,       /* Python snippet */
-			UI_TIP_LC_ALERT,        /* description of why operator can't run */
-		} color_id : 4;
-		int is_pad : 1;
-	} format[MAX_TOOLTIP_LINES];
 
+typedef struct uiTooltipFormat {
+	enum {
+		UI_TIP_STYLE_NORMAL = 0,
+		UI_TIP_STYLE_HEADER,
+		UI_TIP_STYLE_MONO,
+	} style : 3;
+	enum {
+		UI_TIP_LC_MAIN = 0,     /* primary text */
+		UI_TIP_LC_VALUE,        /* the value of buttons (also shortcuts) */
+		UI_TIP_LC_ACTIVE,       /* titles of active enum values */
+		UI_TIP_LC_NORMAL,       /* regular text */
+		UI_TIP_LC_PYTHON,       /* Python snippet */
+		UI_TIP_LC_ALERT,        /* description of why operator can't run */
+	} color_id : 4;
+	int is_pad : 1;
+} uiTooltipFormat;
+
+typedef struct uiTooltipField {
+	char *text;
+	char *text_suffix;
 	struct {
 		uint x_pos;     /* x cursor position at the end of the last line */
 		uint lines;     /* number of lines, 1 or more with word-wrap */
-	} line_geom[MAX_TOOLTIP_LINES];
+	} geom;
+	uiTooltipFormat format;
 
-	int wrap_width;
+} uiTooltipField;
 
-	int totline;
+#define MAX_TOOLTIP_LINES 8
+typedef struct uiTooltipData {
+	rcti bbox;
+	uiTooltipField *fields;
+	uint            fields_len;
+	uiFontStyle fstyle;
+	int wrap_width;
 	int toth, lineh;
 } uiTooltipData;
 
 #define UI_TIP_LC_MAX 6
 
 BLI_STATIC_ASSERT(UI_TIP_LC_MAX == UI_TIP_LC_ALERT + 1, "invalid lc-max");
-BLI_STATIC_ASSERT(sizeof(((uiTooltipData *)NULL)->format[0]) <= sizeof(int), "oversize");
+BLI_STATIC_ASSERT(sizeof(uiTooltipFormat) <= sizeof(int), "oversize");
+
+static uiTooltipField *text_field_add_only(
+        uiTooltipData *data)
+{
+	data->fields_len += 1;
+	data->fields = MEM_recallocN(data->fields, sizeof(*data->fields) * data->fields_len);
+	return &data->fields[data->fields_len - 1];
+}
+
+static uiTooltipField *text_field_add(
+        uiTooltipData *data,
+        const uiTooltipFormat *format)
+{
+	uiTooltipField *field = text_field_add_only(data);
+	field->format = *format;
+	return field;
+}
 
 /* -------------------------------------------------------------------- */
 /** \name ToolTip Callbacks (Draw & Free)
@@ -187,12 +209,14 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
 	bbox.xmin += 0.5f * pad_px;  /* add padding to the text */
 	bbox.ymax -= 0.25f * pad_px;
 
-	for (i = 0; i < data->totline; i++) {
-		bbox.ymin = bbox.ymax - (data->lineh * data->line_geom[i].lines);
-		if (data->format[i].style == UI_TIP_STYLE_HEADER) {
+	for (i = 0; i < data->fields_len; i++) {
+		const uiTooltipField *field = &data->fields[i];
+		const uiTooltipField *field_next = (i + 1) != data->fields_len ? &data->fields[i + 1] : NULL;
+
+		bbox.ymin = bbox.ymax - (data->lineh * field->geom.lines);
+		if (field->format.style == UI_TIP_STYLE_HEADER) {
 			/* draw header and active data (is done here to be able to change color) */
 			uiFontStyle fstyle_header = data->fstyle;
-			float xofs, yofs;
 
 			/* override text-style */
 			fstyle_header.shadow = 1;
@@ -203,23 +227,26 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
 
 			UI_fontstyle_set(&fstyle_header);
 			glColor3fv(tip_colors[UI_TIP_LC_MAIN]);
-			UI_fontstyle_draw(&fstyle_header, &bbox, data->header);
+			UI_fontstyle_draw(&fstyle_header, &bbox, field->text);
 
-			/* offset to the end of the last line */
-			xofs = data->line_geom[i].x_pos;
-			yofs = data->lineh * (data->line_geom[i].lines - 1);
-			bbox.xmin += xofs;
-			bbox.ymax -= yofs;
-
-			glColor3fv(tip_colors[UI_TIP_LC_ACTIVE]);
 			fstyle_header.shadow = 0;
-			UI_fontstyle_draw(&fstyle_header, &bbox, data->active_info);
 
-			/* undo offset */
-			bbox.xmin -= xofs;
-			bbox.ymax += yofs;
+			/* offset to the end of the last line */
+			if (field->text_suffix) {
+				float xofs = field->geom.x_pos;
+				float yofs = data->lineh * (field->geom.lines - 1);
+				bbox.xmin += xofs;
+				bbox.ymax -= yofs;
+
+				glColor3fv(tip_colors[UI_TIP_LC_ACTIVE]);
+				UI_fontstyle_draw(&fstyle_header, &bbox, field->text_suffix);
+
+				/* undo offset */
+				bbox.xmin -= xofs;
+				bbox.ymax += yofs;
+			}
 		}
-		else if (data->format[i].style == UI_TIP_STYLE_MONO) {
+		else if (field->format.style == UI_TIP_STYLE_MONO) {
 			uiFontStyle fstyle_mono = data->fstyle;
 			fstyle_mono.uifont_id = blf_mono_font;
 			fstyle_mono.word_wrap = true;
@@ -227,23 +254,23 @@ static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
 			UI_fontstyle_set(&fstyle_mono);
 			/* XXX, needed because we dont have mono in 'U.uifonts' */
 			BLF_size(fstyle_mono.uifont_id, fstyle_mono.points * U.pixelsize, U.dpi);
-			glColor3fv(tip_colors[data->format[i].color_id]);
-			UI_fontstyle_draw(&fstyle_mono, &bbox, data->lines[i]);
+			glColor3fv(tip_colors[field->format.color_id]);
+			UI_fontstyle_draw(&fstyle_mono, &bbox, field->text);
 		}
 		else {
 			uiFontStyle fstyle_normal = data->fstyle;
-			BLI_assert(data->format[i].style == UI_TIP_STYLE_NORMAL);
+			BLI_assert(field->format.style == UI_TIP_STYLE_NORMAL);
 			fstyle_normal.word_wrap = true;
 
 			/* draw remaining data */
 			UI_fontstyle_set(&fstyle_normal);
-			glColor3fv(tip_colors[data->format[i].color_id]);
-			UI_fontstyle_draw(&fstyle_normal, &bbox, data->lines[i]);
+			glColor3fv(tip_colors[field->format.color_id]);
+			UI_fontstyle_draw(&fstyle_normal, &bbox, field->text);
 		}
 
-		bbox.ymax -= data->lineh * data->line_geom[i].lines;
+		bbox.ymax -= data->lineh * field->geom.lines;
 
-		if ((i + 1 != data->totline) && data->format[i + 1].is_pad) {
+		if (field_next && field_next->format.is_pad) {
 			bbox.ymax -= data->lineh * (UI_TIP_PAD_FAC - 1);
 		}
 	}
@@ -260,6 +287,15 @@ static void ui_tooltip_region_free_cb(ARegion *ar)
 	uiTooltipData *data;
 
 	data = ar->regiondata;
+
+	for (int  i = 0; i < data->fields_len; i++) {
+		const uiTooltipField *field = &data->fields[i];
+		MEM_freeN(field->text);
+		if (field->text_suffix) {
+			MEM_freeN(field->text_suffix);
+		}
+	}
+	MEM_freeN(data->fields);
 	MEM_freeN(data);
 	ar->regiondata = NULL;
 }
@@ -289,48 +325,63 @@ static uiTooltipData *ui_tooltip_data_from_button(bContext *C, uiBut *but)
 
 	/* Tip */
 	if (but_tip.strinfo) {
-		if (enum_label.strinfo) {
-			BLI_snprintf(data->header, sizeof(data->header), "%s:  ", but_tip.strinfo);
-			BLI_strncpy(data->active_info, enum_label.strinfo, sizeof(data->lines[0]));
-		}
-		else {
-			BLI_snprintf(data->header, sizeof(data->header), "%s.", but_tip.strinfo);
+		{
+			uiTooltipField *field = text_field_add(
+			        data, &(uiTooltipFormat){
+			            .style = UI_TIP_STYLE_HEADER,
+			            .color_id = UI_TIP_LC_NORMAL,
+			        });
+			if (enum_label.strinfo) {
+				field->text = BLI_sprintfN("%s:  ", but_tip.strinfo);
+				field->text_suffix = BLI_strdup(enum_label.strinfo);
+			}
+			else {
+				field->text = BLI_sprintfN("%s.", but_tip.strinfo);
+			}
 		}
-		data->format[data->totline].style = UI_TIP_STYLE_HEADER;
-		data->totline++;
 
 		/* special case enum rna buttons */
 		if ((but->type & UI_BTYPE_ROW) && but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG) {
-			BLI_strncpy(data->lines[data->totline], IFACE_("(Shift-Click/Drag to select multiple)"),
-			            sizeof(data->lines[0]));
-
-			data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
-			data->totline++;
+			uiTooltipField *field = text_field_add(
+			        data, &(uiTooltipFormat){
+			            .style = UI_TIP_STYLE_NORMAL,
+			            .color_id = UI_TIP_LC_NORMAL,
+			        });
+			field->text = BLI_strdup(IFACE_("(Shift-Click/Drag to select multiple)"));
 		}
 
 	}
-	/* Enum item label & tip */
+	/* Enum field label & tip */
 	if (enum_tip.strinfo) {
-		BLI_strncpy(data->lines[data->totline], enum_tip.strinfo, sizeof(data->lines[0]));
-		data->format[data->totline].is_pad = true;
-		data->format[data->totline].color_id = UI_TIP_LC_VALUE;
-		data->totline++;
+		uiTooltipField *field = text_field_add(
+		        data, &(uiTooltipFormat){
+		            .style = UI_TIP_STYLE_NORMAL,
+		            .color_id = UI_TIP_LC_VALUE,
+		            .is_pad = true,
+		        });
+		field->text = BLI_strdup(enum_tip.strinfo);
 	}
 
 	/* Op shortcut */
 	if (op_keymap.strinfo) {
-		BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), op_keymap.strinfo);
-		data->format[data->totline].is_pad = true;
-		data->format[data->totline].color_id = UI_TIP_LC_VALUE;
-		data->totline++;
+		uiTooltipField *field = text_field_add(
+		        data, &(uiTooltipFormat){
+		            .style = UI_TIP_STYLE_NORMAL,
+		            .color_id = UI_TIP_LC_VALUE,
+		            .is_pad = true,
+		        });
+		field->text = BLI_sprintfN(TIP_("Shortcut: %s"), op_keymap.strinfo);
 	}
 
 	/* Property context-toggle shortcut */
 	if (prop_keymap.strinfo) {
-		BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), prop_keymap.strinfo);
-		data->format[data->totline].is_pad = true;
-		data->format[data->totline].color_id = UI_TIP_LC_VALUE;
-		data->totline++;
+		uiTooltipField *field = text_field_add(
+		        data, &(uiTooltipFormat){
+		            .style = UI_TIP_STYLE_NORMAL,
+		            .color_id = UI_TIP_LC_VALUE,
+		            .is_pad = true,
+		    

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list