UI: Configurable Layout Separators #117310

Merged
Harley Acheson merged 6 commits from Harley/blender:VerticalSeparator into main 2024-02-09 17:38:44 +01:00
5 changed files with 83 additions and 16 deletions

View File

@ -1463,6 +1463,12 @@ enum eButProgressType {
UI_BUT_PROGRESS_TYPE_RING = 1,
};
enum class LayoutSeparatorType : int8_t {
Auto,
Space,
Line,
};
/***************************** ID Utilities *******************************/
int UI_icon_from_id(const ID *id);
@ -2961,7 +2967,9 @@ void uiItemV(uiLayout *layout, const char *name, int icon, int argval);
/** Separator item */
void uiItemS(uiLayout *layout);
/** Separator item */
void uiItemS_ex(uiLayout *layout, float factor);
void uiItemS_ex(uiLayout *layout,
float factor,
LayoutSeparatorType type = LayoutSeparatorType::Auto);
/** Flexible spacing. */
void uiItemSpacer(uiLayout *layout);

View File

@ -198,6 +198,7 @@ struct uiBut {
* - UI_BTYPE_LABEL: Use `(a1 == 1.0f)` to use a2 as a blending factor (imaginative!).
* - UI_BTYPE_SCROLL: Use as scroll size.
* - UI_BTYPE_SEARCH_MENU: Use as number or rows.
* - UI_BTYPE_SEPR_LINE: 1.0 = vertical
*/
float a1 = 0;

View File

@ -3440,29 +3440,46 @@ void uiItemV(uiLayout *layout, const char *name, int icon, int argval)
}
}
void uiItemS_ex(uiLayout *layout, float factor)
void uiItemS_ex(uiLayout *layout, float factor, const LayoutSeparatorType type)
{
uiBlock *block = layout->root->block;
const bool is_menu = ui_block_is_menu(block);
if (is_menu && !UI_block_can_add_separator(block)) {
return;
}
int space = (is_menu) ? int(0.35f * UI_UNIT_X) : int(0.3f * UI_UNIT_X);
space *= factor;
bool is_vertical = (layout->w > 0);

const LayoutSeparatorType

`const LayoutSeparatorType`
int width = is_vertical ? int(UI_UNIT_X) : int(0.3f * UI_UNIT_X * factor);
int height = is_vertical ? int(0.35f * UI_UNIT_X * factor) : int(UI_UNIT_Y);
eButType but_type;
switch (type) {
case LayoutSeparatorType::Line:
but_type = UI_BTYPE_SEPR_LINE;
Harley marked this conversation as resolved Outdated

Parentheses around is_vertical are unnecessary

Parentheses around `is_vertical` are unnecessary
break;
case LayoutSeparatorType::Auto:
but_type = is_menu ? UI_BTYPE_SEPR_LINE : UI_BTYPE_SEPR;
break;
Harley marked this conversation as resolved Outdated

Can this be a switch statement?

Can this be a switch statement?
default:
but_type = UI_BTYPE_SEPR;
}
bool is_vertical_bar = !is_vertical && but_type == UI_BTYPE_SEPR_LINE;
UI_block_layout_set_current(block, layout);
uiDefBut(block,
(is_menu) ? UI_BTYPE_SEPR_LINE : UI_BTYPE_SEPR,
but_type,
0,
"",
0,
0,
space,
space,
width,
height,
nullptr,
0.0,
0.0,
0,
is_vertical_bar ? 1.0f : 0.0f,
0,
Harley marked this conversation as resolved Outdated

Clang format

Clang format
"");
}

View File

@ -3357,10 +3357,11 @@ static void ui_draw_but_HSV_v(uiBut *but, const rcti *rect)
UI_draw_roundbox_4fv_ex(&rectf, col2, nullptr, 0.0f, inner1, U.pixelsize, 0.0f);
}
/** Separator for menus. */
static void ui_draw_separator(const rcti *rect, const uiWidgetColors *wcol)
/** Separator line. */
static void ui_draw_separator(const uiWidgetColors *wcol, uiBut *but, const rcti *rect)
{
const int y = rect->ymin + BLI_rcti_size_y(rect) / 2;
const bool vertical = but->a1 == 1.0f;
const int mid = vertical ? BLI_rcti_cent_x(rect) : BLI_rcti_cent_y(rect);
const uchar col[4] = {
wcol->text[0],
wcol->text[1],
@ -3377,8 +3378,16 @@ static void ui_draw_separator(const rcti *rect, const uiWidgetColors *wcol)
GPU_line_width(1.0f);
immBegin(GPU_PRIM_LINES, 2);
immVertex2f(pos, rect->xmin, y);
immVertex2f(pos, rect->xmax, y);
if (vertical) {
immVertex2f(pos, mid, rect->ymin);
immVertex2f(pos, mid, rect->ymax);
}
else {
immVertex2f(pos, rect->xmin, mid);
immVertex2f(pos, rect->xmax, mid);
}
immEnd();
GPU_blend(GPU_BLEND_NONE);
@ -4837,7 +4846,7 @@ void ui_draw_but(const bContext *C, ARegion *region, uiStyle *style, uiBut *but,
case UI_BTYPE_SEPR:
break;
case UI_BTYPE_SEPR_LINE:
ui_draw_separator(rect, &tui->wcol_menu_item);
ui_draw_separator(&tui->wcol_menu_item, but, rect);
break;
default: {
const bool use_unpadded = (but->flag & UI_BUT_ICON_PREVIEW) ||
@ -4889,9 +4898,11 @@ void ui_draw_but(const bContext *C, ARegion *region, uiStyle *style, uiBut *but,
break;
case UI_BTYPE_SEPR:
case UI_BTYPE_SEPR_LINE:
case UI_BTYPE_SEPR_SPACER:
break;
case UI_BTYPE_SEPR_LINE:
ui_draw_separator(&tui->wcol_menu_item, but, rect);
break;
case UI_BTYPE_BUT:
case UI_BTYPE_DECORATOR:

View File

@ -516,6 +516,11 @@ static void rna_uiItemProgress(uiLayout *layout,
uiItemProgressIndicator(layout, text, factor, eButProgressType(progress_type));
}
static void rna_uiItemSeparator(uiLayout *layout, float factor, int type)
{
uiItemS_ex(layout, factor, LayoutSeparatorType(type));
}
static void rna_uiTemplateID(uiLayout *layout,
bContext *C,
PointerRNA *ptr,
@ -1071,6 +1076,25 @@ void RNA_api_ui_layout(StructRNA *srna)
{0, nullptr, 0, nullptr, nullptr},
};
static const EnumPropertyItem rna_enum_separator_type_items[] = {
{int(LayoutSeparatorType::Auto),
"AUTO",
0,
"Auto",
"Best guess at what type of separator is needed."},
{int(LayoutSeparatorType::Space),
"SPACE",
0,
"Empty space",
"Horizontal or Vertical empty space, depending on layout direction."},
{int(LayoutSeparatorType::Line),
"LINE",
0,
"Line",
"Horizontal or Vertical line, depending on layout direction."},
{0, nullptr, 0, nullptr, nullptr},
};
static float node_socket_color_default[] = {0.0f, 0.0f, 0.0f, 1.0f};
/* simple layout specifiers */
@ -1480,7 +1504,7 @@ void RNA_api_ui_layout(StructRNA *srna)
parm = RNA_def_string(func, "category", nullptr, 0, "", "panel type category");
RNA_def_parameter_flags(parm, PropertyFlag(0), PARM_REQUIRED);
func = RNA_def_function(srna, "separator", "uiItemS_ex");
func = RNA_def_function(srna, "separator", "rna_uiItemSeparator");
RNA_def_function_ui_description(func, "Item. Inserts empty space into the layout between items");
RNA_def_float(func,
"factor",
@ -1491,6 +1515,12 @@ void RNA_api_ui_layout(StructRNA *srna)
"Percentage of width to space (leave unset for default space)",
0.0f,
FLT_MAX);
RNA_def_enum(func,
"type",
rna_enum_separator_type_items,
int(LayoutSeparatorType::Auto),
"Type",
"The type of the separator");
func = RNA_def_function(srna, "separator_spacer", "uiItemSpacer");
RNA_def_function_ui_description(