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
This commit is contained in:
Bastien Montagne 2018-06-09 16:50:05 +02:00
parent 5159ce640f
commit ec8a20fec0
3 changed files with 423 additions and 3 deletions

View File

@ -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);

View File

@ -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 != NULL) {
for (int row = 0; row < parameters->tot_rows; row++) {
if (parameters->even_rows) {
results->heights_array[row] = global_max_h;
}
else {
results->heights_array[row] = max_h[row];
}
results->cos_y_array[row] = row ? results->cos_y_array[row - 1] - parameters->space_y - results->heights_array[row] : parameters->litem_y - results->heights_array[row];
}
}
if (results->global_avg_w) {
*results->global_avg_w = global_avg_w;
}
if (results->global_max_h) {
*results->global_max_h = global_max_h;
}
if (results->tot_w) {
*results->tot_w = (int)tot_w + parameters->space_x * (parameters->tot_columns - 1);
}
if (results->tot_h) {
*results->tot_h = tot_h + parameters->space_y * (parameters->tot_rows - 1);
}
}
static void ui_litem_estimate_grid_flow(uiLayout *litem)
{
uiStyle *style = litem->root->style;
uiLayoutItemGridFlow *gflow = (uiLayoutItemGridFlow *)litem;
const int space_x = style->columnspace;
const int space_y = style->buttonspacey;
/* Estimate average needed width and height per item. */
{
float avg_w;
int max_h;
ui_litem_grid_flow_compute(
&litem->items,
&((UILayoutGridFlowInput) {
.row_major = gflow->row_major,
.even_columns = gflow->even_columns,
.even_rows = gflow->even_rows,
.litem_w = litem->w,
.litem_x = litem->x,
.litem_y = litem->y,
.space_x = space_x,
.space_y = space_y,
}),
&((UILayoutGridFlowOutput) {
.tot_items = &gflow->tot_items,
.global_avg_w = &avg_w,
.global_max_h = &max_h,
}));
if (gflow->tot_items == 0) {
litem->w = litem->h = 0;
gflow->tot_columns = gflow->tot_rows = 0;
return;
}
/* Even in varying column width case, we fix our columns number from weighted average width of items,
* a proper solving of required width would be too costly, and this should give reasonably good results
* in all resonable cases... */
if (gflow->num_columns > 0) {
gflow->tot_columns = gflow->num_columns;
}
else {
if (avg_w == 0.0f) {
gflow->tot_columns = 1;
}
else {
gflow->tot_columns = min_ii(max_ii((int)(litem->w / avg_w), 1), gflow->tot_items);
}
}
gflow->tot_rows = (int)ceilf((float)gflow->tot_items / gflow->tot_columns);
/* Try to tweak number of columns and rows to get better filling of last column or row,
* and apply 'modulo' value to number of columns or rows.
* Note that modulo does not prevent ending with fewer columns/rows than modulo, if mandatory
* to avoid empty column/row. */
{
const int modulo = (gflow->num_columns < -1) ? -gflow->num_columns : 0;
const int step = modulo ? modulo : 1;
if (gflow->row_major) {
/* Adjust number of columns to be mutiple of given modulo. */
if (modulo && gflow->tot_columns % modulo != 0 && gflow->tot_columns > modulo) {
gflow->tot_columns = gflow->tot_columns - (gflow->tot_columns % modulo);
}
/* Find smallest number of columns conserving computed optimal number of rows. */
for (gflow->tot_rows = (int)ceilf((float)gflow->tot_items / gflow->tot_columns);
(gflow->tot_columns - step) > 0 &&
(int)ceilf((float)gflow->tot_items / (gflow->tot_columns - step)) <= gflow->tot_rows;
gflow->tot_columns -= step);
}
else {
/* Adjust number of rows to be mutiple of given modulo. */
if (modulo && gflow->tot_rows % modulo != 0) {
gflow->tot_rows = min_ii(gflow->tot_rows + modulo - (gflow->tot_rows % modulo), gflow->tot_items);
}
/* Find smallest number of rows conserving computed optimal number of columns. */
for (gflow->tot_columns = (int)ceilf((float)gflow->tot_items / gflow->tot_rows);
(gflow->tot_rows - step) > 0 &&
(int)ceilf((float)gflow->tot_items / (gflow->tot_rows - step)) <= gflow->tot_columns;
gflow->tot_rows -= step);
}
}
/* Set evenly-spaced axes size (quick optimization in case we have even columns and rows). */
if (gflow->even_columns && gflow->even_rows) {
litem->w = (int)(gflow->tot_columns * avg_w) + space_x * (gflow->tot_columns - 1);
litem->h = (int)(gflow->tot_rows * max_h) + space_y * (gflow->tot_rows - 1);
return;
}
}
/* Now that we have our final number of columns and rows,
* we can compute actual needed space for non-evenly sized axes. */
{
int tot_w, tot_h;
ui_litem_grid_flow_compute(
&litem->items,
&((UILayoutGridFlowInput) {
.row_major = gflow->row_major,
.even_columns = gflow->even_columns,
.even_rows = gflow->even_rows,
.litem_w = litem->w,
.litem_x = litem->x,
.litem_y = litem->y,
.space_x = space_x,
.space_y = space_y,
.tot_columns = gflow->tot_columns,
.tot_rows = gflow->tot_rows,
}),
&((UILayoutGridFlowOutput) {
.tot_w = &tot_w,
.tot_h = &tot_h,
}));
litem->w = tot_w;
litem->h = tot_h;
}
}
static void ui_litem_layout_grid_flow(uiLayout *litem)
{
int i;
uiStyle *style = litem->root->style;
uiLayoutItemGridFlow *gflow = (uiLayoutItemGridFlow *)litem;
uiItem *item;
if (gflow->tot_items == 0) {
litem->w = litem->h = 0;
return;
}
BLI_assert(gflow->tot_columns > 0);
BLI_assert(gflow->tot_rows > 0);
const int space_x = style->columnspace;
const int space_y = style->buttonspacey;
int *widths = BLI_array_alloca(widths, gflow->tot_columns);
int *heights = BLI_array_alloca(heights, gflow->tot_rows);
int *cos_x = BLI_array_alloca(cos_x, gflow->tot_columns);
int *cos_y = BLI_array_alloca(cos_y, gflow->tot_rows);
/* This time we directly compute coordinates and sizes of all cells. */
ui_litem_grid_flow_compute(
&litem->items,
&((UILayoutGridFlowInput) {
.row_major = gflow->row_major,
.even_columns = gflow->even_columns,
.even_rows = gflow->even_rows,
.litem_w = litem->w,
.litem_x = litem->x,
.litem_y = litem->y,
.space_x = space_x,
.space_y = space_y,
.tot_columns = gflow->tot_columns,
.tot_rows = gflow->tot_rows,
}),
&((UILayoutGridFlowOutput) {
.cos_x_array = cos_x,
.cos_y_array = cos_y,
.widths_array = widths,
.heights_array = heights,
}));
for (item = litem->items.first, i = 0; item; item = item->next, i++) {
const int col = gflow->row_major ? i % gflow->tot_columns : i / gflow->tot_rows;
const int row = gflow->row_major ? i / gflow->tot_columns : i % gflow->tot_rows;
int item_w, item_h;
ui_item_size(item, &item_w, &item_h);
const int w = widths[col];
const int h = heights[row];
item_w = (litem->alignment == UI_LAYOUT_ALIGN_EXPAND) ? w : min_ii(w, item_w);
item_h = (litem->alignment == UI_LAYOUT_ALIGN_EXPAND) ? h : min_ii(h, item_h);
ui_item_position(item, cos_x[col], cos_y[row], item_w, item_h);
}
litem->h = litem->y - cos_y[gflow->tot_rows - 1];
litem->x = (cos_x[gflow->tot_columns - 1] - litem->x) + widths[gflow->tot_columns - 1];
litem->y = litem->y - litem->h;
}
/* free layout */
static void ui_litem_estimate_absolute(uiLayout *litem)
{
@ -3007,6 +3383,8 @@ static void ui_litem_init_from_parent(uiLayout *litem, uiLayout *layout, int ali
{
litem->root = layout->root;
litem->align = align;
/* Children of gridflow layout shall never have "ideal big size" returned as estimated size. */
litem->variable_size = layout->variable_size || layout->item.type == ITEM_LAYOUT_GRID_FLOW;
litem->active = true;
litem->enabled = true;
litem->context = layout->context;
@ -3064,6 +3442,26 @@ uiLayout *uiLayoutColumnFlow(uiLayout *layout, int number, int align)
return &flow->litem;
}
uiLayout *uiLayoutGridFlow(
uiLayout *layout, int row_major, int num_columns, int even_columns, int even_rows, int align)
{
uiLayoutItemGridFlow *flow;
flow = MEM_callocN(sizeof(uiLayoutItemGridFlow), __func__);
flow->litem.item.type = ITEM_LAYOUT_GRID_FLOW;
ui_litem_init_from_parent(&flow->litem, layout, align);
flow->litem.space = (flow->litem.align) ? 0 : layout->root->style->columnspace;
flow->row_major = row_major;
flow->num_columns = num_columns;
flow->even_columns = even_columns;
flow->even_rows = even_rows;
UI_block_layout_set_current(layout->root->block, &flow->litem);
return &flow->litem;
}
static uiLayoutItemBx *ui_layout_box(uiLayout *layout, int type)
{
uiLayoutItemBx *box;
@ -3359,6 +3757,9 @@ static void ui_item_estimate(uiItem *item)
case ITEM_LAYOUT_COLUMN_FLOW:
ui_litem_estimate_column_flow(litem);
break;
case ITEM_LAYOUT_GRID_FLOW:
ui_litem_estimate_grid_flow(litem);
break;
case ITEM_LAYOUT_ROW:
ui_litem_estimate_row(litem);
break;
@ -3458,6 +3859,9 @@ static void ui_item_layout(uiItem *item)
case ITEM_LAYOUT_COLUMN_FLOW:
ui_litem_layout_column_flow(litem);
break;
case ITEM_LAYOUT_GRID_FLOW:
ui_litem_layout_grid_flow(litem);
break;
case ITEM_LAYOUT_ROW:
ui_litem_layout_row(litem);
break;
@ -3737,6 +4141,7 @@ static void ui_intro_items(DynStr *ds, ListBase *lb)
case ITEM_LAYOUT_COLUMN: BLI_dynstr_append(ds, "'type':'COLUMN', "); break;
case ITEM_LAYOUT_COLUMN_FLOW: BLI_dynstr_append(ds, "'type':'COLUMN_FLOW', "); break;
case ITEM_LAYOUT_ROW_FLOW: BLI_dynstr_append(ds, "'type':'ROW_FLOW', "); break;
case ITEM_LAYOUT_GRID_FLOW: BLI_dynstr_append(ds, "'type':'GRID_FLOW', "); break;
case ITEM_LAYOUT_BOX: BLI_dynstr_append(ds, "'type':'BOX', "); break;
case ITEM_LAYOUT_ABSOLUTE: BLI_dynstr_append(ds, "'type':'ABSOLUTE', "); break;
case ITEM_LAYOUT_SPLIT: BLI_dynstr_append(ds, "'type':'SPLIT', "); break;

View File

@ -518,6 +518,19 @@ void RNA_api_ui_layout(StructRNA *srna)
RNA_def_function_return(func, parm);
RNA_def_boolean(func, "align", false, "", "Align buttons to each other");
func = RNA_def_function(srna, "grid_flow", "uiLayoutGridFlow");
RNA_def_boolean(func, "row_major", false, "", "Fill row by row, instead of column by column");
RNA_def_int(func, "num_columns", 0, INT_MIN, INT_MAX, "",
"Number of columns, positive are absolute fixed numbers, 0 is automatic, negative are "
"automatic multiple numbers along major axis (e.g. -2 will only produce 2, 4, 6 etc. "
"columns for row major layout, and 2, 4, 6 etc. rows for column major layout)",
INT_MIN, INT_MAX);
RNA_def_boolean(func, "even_columns", false, "", "All columns will have the same width");
RNA_def_boolean(func, "even_rows", false, "", "All rows will have the same height");
RNA_def_boolean(func, "align", false, "", "Align buttons to each other");
parm = RNA_def_pointer(func, "layout", "UILayout", "", "Sub-layout to put items in");
RNA_def_function_return(func, parm);
/* box layout */
func = RNA_def_function(srna, "box", "uiLayoutBox");
parm = RNA_def_pointer(func, "layout", "UILayout", "", "Sub-layout to put items in");