UI: Support Displaying Enums as Tabs
Adds `uiLayout.prop_tabs_enum(data, property, icon_only)` to BPY.
This commit is contained in:
parent
a0dfa320cd
commit
ce148716c8
Notes:
blender-bot
2023-02-14 19:45:25 +01:00
Referenced by issue #78575, Assert when using prop_tabs_enum
|
@ -1206,6 +1206,7 @@ void uiItemMenuEnumO_ptr(uiLayout *layout, struct bContext *C, struct wmOperator
|
|||
void uiItemMenuEnumO(uiLayout *layout, struct bContext *C, const char *opname, const char *propname, const char *name, int icon);
|
||||
void uiItemMenuEnumR_prop(uiLayout *layout, struct PointerRNA *ptr, PropertyRNA *prop, const char *name, int icon);
|
||||
void uiItemMenuEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *name, int icon);
|
||||
void uiItemTabsEnumR_prop(uiLayout *layout, struct bContext *C, struct PointerRNA *ptr, PropertyRNA *prop, bool icon_only);
|
||||
|
||||
/* UI Operators */
|
||||
typedef struct uiDragColorHandle {
|
||||
|
|
|
@ -1582,17 +1582,8 @@ int ui_but_is_pushed_ex(uiBut *but, double *value)
|
|||
break;
|
||||
case UI_BTYPE_ROW:
|
||||
case UI_BTYPE_LISTROW:
|
||||
UI_GET_BUT_VALUE_INIT(but, *value);
|
||||
/* support for rna enum buts */
|
||||
if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
|
||||
if ((int)*value & (int)but->hardmax) is_push = true;
|
||||
}
|
||||
else {
|
||||
if (*value == (double)but->hardmax) is_push = true;
|
||||
}
|
||||
break;
|
||||
case UI_BTYPE_TAB:
|
||||
if (but->rnaprop && but->custom_data) {
|
||||
if ((but->type == UI_BTYPE_TAB) && but->rnaprop && but->custom_data) {
|
||||
/* uiBut.custom_data points to data this tab represents (e.g. workspace).
|
||||
* uiBut.rnapoin/prop store an active value (e.g. active workspace). */
|
||||
if (RNA_property_type(but->rnaprop) == PROP_POINTER) {
|
||||
|
@ -1601,6 +1592,19 @@ int ui_but_is_pushed_ex(uiBut *but, double *value)
|
|||
is_push = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (but->optype) {
|
||||
break;
|
||||
}
|
||||
|
||||
UI_GET_BUT_VALUE_INIT(but, *value);
|
||||
/* support for rna enum buts */
|
||||
if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
|
||||
if ((int)*value & (int)but->hardmax) is_push = true;
|
||||
}
|
||||
else {
|
||||
if (*value == (double)but->hardmax) is_push = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -115,6 +115,22 @@ bool ui_but_can_align(const uiBut *but)
|
|||
return (btype_can_align && (BLI_rctf_size_x(&but->rect) > 0.0f) && (BLI_rctf_size_y(&but->rect) > 0.0f));
|
||||
}
|
||||
|
||||
int ui_but_align_opposite_to_area_align_get(const ARegion *ar)
|
||||
{
|
||||
switch (ar->alignment) {
|
||||
case RGN_ALIGN_TOP:
|
||||
return UI_BUT_ALIGN_DOWN;
|
||||
case RGN_ALIGN_BOTTOM:
|
||||
return UI_BUT_ALIGN_TOP;
|
||||
case RGN_ALIGN_LEFT:
|
||||
return UI_BUT_ALIGN_RIGHT;
|
||||
case RGN_ALIGN_RIGHT:
|
||||
return UI_BUT_ALIGN_LEFT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function checks a pair of buttons (assumed in a same align group), and if they are neighbors,
|
||||
* set needed data accordingly.
|
||||
|
|
|
@ -916,6 +916,7 @@ static void ui_apply_but_TAB(bContext *C, uiBut *but, uiHandleButtonData *data)
|
|||
ui_but_update_edited(but);
|
||||
}
|
||||
else {
|
||||
ui_but_value_set(but, but->hardmax);
|
||||
ui_apply_but_func(C, but);
|
||||
}
|
||||
|
||||
|
@ -3882,7 +3883,11 @@ static bool ui_but_is_mouse_over_icon_extra(const ARegion *region, uiBut *but, c
|
|||
static int ui_do_but_TAB(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event)
|
||||
{
|
||||
if (data->state == BUTTON_STATE_HIGHLIGHT) {
|
||||
if ((event->type == LEFTMOUSE) &&
|
||||
const int rna_type = RNA_property_type(but->rnaprop);
|
||||
|
||||
if (ELEM(rna_type, PROP_POINTER, PROP_STRING) &&
|
||||
(but->custom_data != NULL) &&
|
||||
(event->type == LEFTMOUSE) &&
|
||||
((event->val == KM_DBL_CLICK) || event->ctrl))
|
||||
{
|
||||
button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
|
||||
|
|
|
@ -813,6 +813,7 @@ void ui_item_paneltype_func(struct bContext *C, struct uiLayout *layout, void *a
|
|||
|
||||
/* interface_align.c */
|
||||
bool ui_but_can_align(const uiBut *but) ATTR_WARN_UNUSED_RESULT;
|
||||
int ui_but_align_opposite_to_area_align_get(const ARegion *ar) ATTR_WARN_UNUSED_RESULT;
|
||||
void ui_block_align_calc(uiBlock *block, const ARegion *region);
|
||||
|
||||
/* interface_anim.c */
|
||||
|
|
|
@ -639,9 +639,9 @@ static void ui_item_enum_expand_handle(bContext *C, void *arg1, void *arg2)
|
|||
RNA_property_enum_set(&but->rnapoin, but->rnaprop, current_value);
|
||||
}
|
||||
}
|
||||
static void ui_item_enum_expand(
|
||||
static void ui_item_enum_expand_exec(
|
||||
uiLayout *layout, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop,
|
||||
const char *uiname, int h, bool icon_only)
|
||||
const char *uiname, int h, int but_type, bool icon_only)
|
||||
{
|
||||
/* XXX The way this function currently handles uiname parameter is insane and inconsistent with general UI API:
|
||||
* * uiname is the *enum property* label.
|
||||
|
@ -659,6 +659,8 @@ static void ui_item_enum_expand(
|
|||
bool free;
|
||||
bool radial = (layout->root->type == UI_LAYOUT_PIEMENU);
|
||||
|
||||
BLI_assert(RNA_property_type(prop) == PROP_ENUM);
|
||||
|
||||
if (radial)
|
||||
RNA_property_enum_items_gettexted_all(block->evil_C, ptr, prop, &item_array, NULL, &free);
|
||||
else
|
||||
|
@ -704,11 +706,11 @@ static void ui_item_enum_expand(
|
|||
itemw = ui_text_icon_width(block->curlayout, icon_only ? "" : name, icon, 0);
|
||||
|
||||
if (icon && name[0] && !icon_only)
|
||||
but = uiDefIconTextButR_prop(block, UI_BTYPE_ROW, 0, icon, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
|
||||
but = uiDefIconTextButR_prop(block, but_type, 0, icon, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
|
||||
else if (icon)
|
||||
but = uiDefIconButR_prop(block, UI_BTYPE_ROW, 0, icon, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
|
||||
but = uiDefIconButR_prop(block, but_type, 0, icon, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
|
||||
else
|
||||
but = uiDefButR_prop(block, UI_BTYPE_ROW, 0, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
|
||||
but = uiDefButR_prop(block, but_type, 0, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, -1, -1, NULL);
|
||||
|
||||
if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
|
||||
UI_but_func_set(but, ui_item_enum_expand_handle, but, POINTER_FROM_INT(value));
|
||||
|
@ -723,6 +725,24 @@ static void ui_item_enum_expand(
|
|||
MEM_freeN((void *)item_array);
|
||||
}
|
||||
}
|
||||
static void ui_item_enum_expand(
|
||||
uiLayout *layout, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop,
|
||||
const char *uiname, int h, bool icon_only)
|
||||
{
|
||||
ui_item_enum_expand_exec(layout, block, ptr, prop, uiname, h, UI_BTYPE_ROW, icon_only);
|
||||
}
|
||||
static void ui_item_enum_expand_tabs(
|
||||
uiLayout *layout, bContext *C, uiBlock *block, PointerRNA *ptr, PropertyRNA *prop,
|
||||
const char *uiname, int h, bool icon_only)
|
||||
{
|
||||
uiBut *last = block->buttons.last;
|
||||
|
||||
ui_item_enum_expand_exec(layout, block, ptr, prop, uiname, h, UI_BTYPE_TAB, icon_only);
|
||||
BLI_assert(last != block->buttons.last);
|
||||
for (uiBut *tab = last ? last->next : block->buttons.first; tab; tab = tab->next) {
|
||||
UI_but_drawflag_enable(tab, ui_but_align_opposite_to_area_align_get(CTX_wm_region(C)));
|
||||
}
|
||||
}
|
||||
|
||||
/* callback for keymap item change button */
|
||||
static void ui_keymap_but_cb(bContext *UNUSED(C), void *but_v, void *UNUSED(key_v))
|
||||
|
@ -2550,6 +2570,14 @@ void uiItemMenuEnumR(uiLayout *layout, struct PointerRNA *ptr, const char *propn
|
|||
uiItemMenuEnumR_prop(layout, ptr, prop, name, icon);
|
||||
}
|
||||
|
||||
void uiItemTabsEnumR_prop(uiLayout *layout, bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool icon_only)
|
||||
{
|
||||
uiBlock *block = layout->root->block;
|
||||
|
||||
UI_block_layout_set_current(block, layout);
|
||||
ui_item_enum_expand_tabs(layout, C, block, ptr, prop, NULL, UI_UNIT_Y, icon_only);
|
||||
}
|
||||
|
||||
/**************************** Layout Items ***************************/
|
||||
|
||||
/* single-row layout */
|
||||
|
|
|
@ -847,7 +847,7 @@ static void template_ID_tabs(
|
|||
const PointerRNA active_ptr = RNA_property_pointer_get(&template->ptr, template->prop);
|
||||
MenuType *mt = WM_menutype_find(menu, false);
|
||||
|
||||
const int but_align = (region->alignment == RGN_ALIGN_TOP) ? UI_BUT_ALIGN_DOWN : UI_BUT_ALIGN_TOP;
|
||||
const int but_align = ui_but_align_opposite_to_area_align_get(region);
|
||||
const int but_height = UI_UNIT_Y * 1.1;
|
||||
|
||||
uiBlock *block = uiLayoutGetBlock(layout);
|
||||
|
|
|
@ -140,6 +140,25 @@ static void rna_uiItemMenuEnumR(
|
|||
uiItemMenuEnumR_prop(layout, ptr, prop, name, icon);
|
||||
}
|
||||
|
||||
static void rna_uiItemTabsEnumR(
|
||||
uiLayout *layout, bContext *C,
|
||||
struct PointerRNA *ptr, const char *propname,
|
||||
bool icon_only)
|
||||
{
|
||||
PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
|
||||
|
||||
if (!prop) {
|
||||
RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname);
|
||||
return;
|
||||
}
|
||||
if (RNA_property_type(prop) != PROP_ENUM) {
|
||||
RNA_warning("property is not an enum: %s.%s", RNA_struct_identifier(ptr->type), propname);
|
||||
return;
|
||||
}
|
||||
|
||||
uiItemTabsEnumR_prop(layout, C, ptr, prop, icon_only);
|
||||
}
|
||||
|
||||
static void rna_uiItemEnumR_string(
|
||||
uiLayout *layout, struct PointerRNA *ptr, const char *propname, const char *value,
|
||||
const char *name, const char *text_ctxt, bool translate, int icon)
|
||||
|
@ -617,6 +636,11 @@ void RNA_api_ui_layout(StructRNA *srna)
|
|||
api_ui_item_rna_common(func);
|
||||
api_ui_item_common(func);
|
||||
|
||||
func = RNA_def_function(srna, "prop_tabs_enum", "rna_uiItemTabsEnumR");
|
||||
RNA_def_function_flag(func, FUNC_USE_CONTEXT);
|
||||
api_ui_item_rna_common(func);
|
||||
RNA_def_boolean(func, "icon_only", false, "", "Draw only icons in tabs, no text");
|
||||
|
||||
func = RNA_def_function(srna, "prop_enum", "rna_uiItemEnumR_string");
|
||||
api_ui_item_rna_common(func);
|
||||
parm = RNA_def_string(func, "value", NULL, 0, "", "Enum property value");
|
||||
|
|
Loading…
Reference in New Issue