[Bf-blender-cvs] [ec8a20fec0d] blender2.8: Add new GridFlow layout.

Bastien Montagne noreply at git.blender.org
Sat Jun 9 16:54:56 CEST 2018


Commit: ec8a20fec0d103a799b9d9c86b0b55fba3b02b84
Author: Bastien Montagne
Date:   Sat Jun 9 16:50:05 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBec8a20fec0d103a799b9d9c86b0b55fba3b02b84

Add new GridFlow layout.

This mimics the 'spreadsheet' behavior. Columns and/or rows can have
equal sizes, or adapt to their content - but always in a grid way (i.e.
all items in a same column will always have same available width, and
all items in a same row will always have same available height).
Also, you can fill (order) the grid in a row- or column-major way.

Not used anywhere for now.

Differential: https://developer.blender.org/D2395

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

M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_layout.c
M	source/blender/makesrna/intern/rna_ui_api.c

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

diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index a283068853c..dee3104e6f4 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -979,6 +979,8 @@ bool uiLayoutGetPropSep(uiLayout *layout);
 uiLayout *uiLayoutRow(uiLayout *layout, int align);
 uiLayout *uiLayoutColumn(uiLayout *layout, int align);
 uiLayout *uiLayoutColumnFlow(uiLayout *layout, int number, int align);
+uiLayout *uiLayoutGridFlow(
+        uiLayout *layout, int row_major, int num_columns, int even_columns, int even_rows, int align);
 uiLayout *uiLayoutBox(uiLayout *layout);
 uiLayout *uiLayoutListBox(uiLayout *layout, struct uiList *ui_list, struct PointerRNA *ptr, struct PropertyRNA *prop,
                           struct PointerRNA *actptr, struct PropertyRNA *actprop);
diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c
index b4677d95361..339b333a5fb 100644
--- a/source/blender/editors/interface/interface_layout.c
+++ b/source/blender/editors/interface/interface_layout.c
@@ -37,6 +37,7 @@
 #include "DNA_armature_types.h"
 #include "DNA_userdef_types.h"
 
+#include "BLI_alloca.h"
 #include "BLI_listbase.h"
 #include "BLI_string.h"
 #include "BLI_rect.h"
@@ -101,6 +102,7 @@ typedef enum uiItemType {
 	ITEM_LAYOUT_COLUMN,
 	ITEM_LAYOUT_COLUMN_FLOW,
 	ITEM_LAYOUT_ROW_FLOW,
+	ITEM_LAYOUT_GRID_FLOW,
 	ITEM_LAYOUT_BOX,
 	ITEM_LAYOUT_ABSOLUTE,
 	ITEM_LAYOUT_SPLIT,
@@ -152,6 +154,7 @@ struct uiLayout {
 	bool enabled;
 	bool redalert;
 	bool keepaspect;
+	bool variable_size;  /* For layouts inside gridflow, they and their items shall never have a fixed maximal size. */
 	char alignment;
 	char emboss;
 };
@@ -162,6 +165,22 @@ typedef struct uiLayoutItemFlow {
 	int totcol;
 } uiLayoutItemFlow;
 
+typedef struct uiLayoutItemGridFlow {
+	uiLayout litem;
+
+	/* Extra parameters */
+	bool row_major;     /* Fill first row first, instead of filling first column first. */
+	bool even_columns;  /* Same width for all columns. */
+	bool even_rows;     /* Same height for all rows. */
+	/* If positive, absolute fixed number of columns.
+	 * If 0, fully automatic (based on available width).
+	 * If negative, automatic but only generates number of columns/rows multiple of given (absolute) value. */
+	int num_columns;
+
+	/* Pure internal runtime storage. */
+	int tot_items, tot_columns, tot_rows;
+} uiLayoutItemGridFlow;
+
 typedef struct uiLayoutItemBx {
 	uiLayout litem;
 	uiBut *roundbox;
@@ -237,6 +256,13 @@ static int ui_layout_vary_direction(uiLayout *layout)
 	        UI_ITEM_VARY_X : UI_ITEM_VARY_Y);
 }
 
+static bool ui_layout_variable_size(uiLayout *layout)
+{
+	/* Note that this code is probably a bit flacky, we'd probably want to know whether it's variable in X and/or Y,
+	 * etc. But for now it mimics previous one, with addition of variable flag set for children of gridflow layouts. */
+	return ui_layout_vary_direction(layout) == UI_ITEM_VARY_X || layout->variable_size;
+}
+
 /* estimated size of text + icon */
 static int ui_text_icon_width(uiLayout *layout, const char *name, int icon, bool compact)
 {
@@ -246,7 +272,7 @@ static int ui_text_icon_width(uiLayout *layout, const char *name, int icon, bool
 	if (icon && !name[0])
 		return unit_x;  /* icon only */
 
-	variable = (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X);
+	variable = ui_layout_variable_size(layout);
 
 	if (variable) {
 		if (layout->alignment != UI_LAYOUT_ALIGN_EXPAND) {
@@ -347,6 +373,7 @@ static int ui_layout_local_dir(uiLayout *layout)
 			return UI_LAYOUT_HORIZONTAL;
 		case ITEM_LAYOUT_COLUMN:
 		case ITEM_LAYOUT_COLUMN_FLOW:
+		case ITEM_LAYOUT_GRID_FLOW:
 		case ITEM_LAYOUT_SPLIT:
 		case ITEM_LAYOUT_ABSOLUTE:
 		case ITEM_LAYOUT_BOX:
@@ -713,7 +740,7 @@ static uiBut *ui_item_with_label(
 			w_label = (int)((w_hint * 2) * UI_ITEM_PROP_SEP_DIVIDE);
 		}
 		else {
-			if (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) {
+			if (ui_layout_variable_size(layout)) {
 				/* w_hint is width for label in this case. Use a default width for property button(s) */
 				prop_but_width = UI_UNIT_X * 5;
 				w_label = w_hint;
@@ -1426,7 +1453,7 @@ static void ui_item_rna_size(
 		else
 			h += len * UI_UNIT_Y;
 	}
-	else if (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) {
+	else if (ui_layout_variable_size(layout)) {
 		if (type == PROP_BOOLEAN && name[0])
 			w += UI_UNIT_X / 5;
 		else if (type == PROP_ENUM && !icon_only)
@@ -2834,6 +2861,355 @@ static void ui_litem_layout_column_flow(uiLayout *litem)
 	litem->y = miny;
 }
 
+/* multi-column and multi-row layout. */
+typedef struct UILayoutGridFlowInput {
+	/* General layout controll settings. */
+	const bool row_major : 1;  /* Fill rows before columns */
+	const bool even_columns : 1;  /* All columns will have same width. */
+	const bool even_rows : 1;  /* All rows will have same height. */
+	const int space_x;  /* Space between columns. */
+	const int space_y;  /* Space between rows. */
+	/* Real data about current position and size of this layout item (either estimated, or final values). */
+	const int litem_w;  /* Layout item width. */
+	const int litem_x;  /* Layout item X position. */
+	const int litem_y;  /* Layout item Y position. */
+	/* Actual number of columns and rows to generate (computed from first pass usually). */
+	const int tot_columns;  /* Number of columns. */
+	const int tot_rows;  /* Number of rows. */
+} UILayoutGridFlowInput;
+
+typedef struct UILayoutGridFlowOutput {
+	int *tot_items;  /* Total number of items in this grid layout. */
+	/* Width / X pos data. */
+	float *global_avg_w;  /* Computed average width of the columns. */
+	int *cos_x_array;  /* Computed X coordinate of each column. */
+	int *widths_array;  /* Computed width of each column. */
+	int *tot_w;  /* Computed total width. */
+	/* Height / Y pos data. */
+	int *global_max_h;  /* Computed height of the tallest item in the grid. */
+	int *cos_y_array;  /* Computed Y coordinate of each column. */
+	int *heights_array;  /* Computed height of each column. */
+	int *tot_h;  /* Computed total height. */
+} UILayoutGridFlowOutput;
+
+static void ui_litem_grid_flow_compute(
+        ListBase *items, UILayoutGridFlowInput *parameters, UILayoutGridFlowOutput *results)
+{
+	uiItem *item;
+	int i;
+
+	float tot_w = 0.0f, tot_h = 0.0f;
+	float global_avg_w = 0.0f, global_totweight_w = 0.0f;
+	int global_max_h = 0;
+
+	float *avg_w = NULL, *totweight_w = NULL;
+	int *max_h = NULL;
+
+	BLI_assert(parameters->tot_columns != 0 || (results->cos_x_array == NULL && results->widths_array == NULL && results->tot_w == NULL));
+	BLI_assert(parameters->tot_rows != 0 || (results->cos_y_array == NULL && results->heights_array == NULL && results->tot_h == NULL));
+
+	if (results->tot_items) {
+		*results->tot_items = 0;
+	}
+
+	if (items->first == NULL) {
+		if (results->global_avg_w) {
+			*results->global_avg_w = 0.0f;
+		}
+		if (results->global_max_h) {
+			*results->global_max_h = 0;
+		}
+		return;
+	}
+
+	if (parameters->tot_columns != 0) {
+		avg_w = BLI_array_alloca(avg_w, parameters->tot_columns);
+		totweight_w = BLI_array_alloca(totweight_w, parameters->tot_columns);
+		memset(avg_w, 0, sizeof(*avg_w) * parameters->tot_columns);
+		memset(totweight_w, 0, sizeof(*totweight_w) * parameters->tot_columns);
+	}
+	if (parameters->tot_rows != 0) {
+		max_h = BLI_array_alloca(max_h, parameters->tot_rows);
+		memset(max_h, 0, sizeof(*max_h) * parameters->tot_rows);
+	}
+
+	for (i = 0, item = items->first; item; item = item->next, i++) {
+		int item_w, item_h;
+		ui_item_size(item, &item_w, &item_h);
+
+		global_avg_w += (float)(item_w * item_w);
+		global_totweight_w += (float)item_w;
+		global_max_h = max_ii(global_max_h, item_h);
+
+		if (parameters->tot_rows != 0 && parameters->tot_columns != 0) {
+			const int index_col = parameters->row_major ? i % parameters->tot_columns : i / parameters->tot_rows;
+			const int index_row = parameters->row_major ? i / parameters->tot_columns : i % parameters->tot_rows;
+
+			avg_w[index_col] += (float)(item_w * item_w);
+			totweight_w[index_col] += (float)item_w;
+
+			max_h[index_row] = max_ii(max_h[index_row], item_h);
+		}
+
+		if (results->tot_items) {
+			(*results->tot_items)++;
+		}
+	}
+
+	/* Finalize computing of column average sizes */
+	global_avg_w /= global_totweight_w;
+	if (parameters->tot_columns != 0) {
+		for (i = 0; i < parameters->tot_columns; i++) {
+			avg_w[i] /= totweight_w[i];
+			tot_w += avg_w[i];
+		}
+		if (parameters->even_columns) {
+			tot_w = ceilf(global_avg_w) * parameters->tot_columns;
+		}
+	}
+	/* Finalize computing of rows max sizes */
+	if (parameters->tot_rows != 0) {
+		for (i = 0; i < parameters->tot_rows; i++) {
+			tot_h += max_h[i];
+		}
+		if (parameters->even_rows) {
+			tot_h = global_max_h * parameters->tot_columns;
+		}
+	}
+
+	/* Compute positions and sizes of all cells. */
+	if (results->cos_x_array != NULL && results->widths_array != NULL) {
+		/* We enlarge/narrow columns evenly to match available width. */
+		const float wfac = (float)(parameters->litem_w - (parameters->tot_columns - 1) * parameters->space_x) / tot_w;
+
+		for (int col = 0; col < parameters->tot_columns; col++) {
+			results->cos_x_array[col] = col ? results->cos_x_array[col - 1] + results->widths_array[col - 1] + parameters->space_x : parameters->litem_x;
+			if (parameters->even_columns) {
+				/*                     (<                        remaining width                          > - <       space between remaining columns              >) / <     remaining columns    > */
+				results->widths_array[col] = ((parameters->litem_w - (results->cos_x_array[col] - parameters->litem_x)) - (parameters->tot_columns - col - 1) * parameters->space_x) / (parameters->tot_columns - col);
+			}
+			else if (col == parameters->tot_columns - 1) {
+				/* Last column copes width rounding errors... */
+				results->widths_array[col] = parameters->litem_w - (results->cos_x_array[col] - parameters->litem_x);
+			}
+			else {
+				results->widths_array[col] = (int)(avg_w[col] * wfac);
+			}
+		}
+	}
+	if (results->cos_y_array != NULL && results->heights_array 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list