UI: Support Displaying Enums as Tabs
authorJulian Eisel <eiseljulian@gmail.com>
Mon, 29 Oct 2018 20:20:58 +0000 (21:20 +0100)
committerJulian Eisel <eiseljulian@gmail.com>
Mon, 29 Oct 2018 20:20:58 +0000 (21:20 +0100)
Adds `uiLayout.prop_tabs_enum(data, property, icon_only)` to BPY.

source/blender/editors/include/UI_interface.h
source/blender/editors/interface/interface.c
source/blender/editors/interface/interface_align.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_intern.h
source/blender/editors/interface/interface_layout.c
source/blender/editors/interface/interface_templates.c
source/blender/makesrna/intern/rna_ui_api.c

index ac463fe87a2e0f5b17d403a922636e3c5485ce0e..efdc9ae011891965f26066b0d37ab113c36b90cf 100644 (file)
@@ -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 {
index 1e1c9c1fa3fbe3a1aaa2ed22b8eac3144651f0d7..4ed996f3afa49bac6ef4755bf167ab89c00a24e4 100644 (file)
@@ -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:
index ee55482267263e790becca6dde64ba394877545f..d102c7c582dab1d149d180ac793f0945d165e697 100644 (file)
@@ -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.
index 3f55cd5d73160697d412312b353b569cd78aff80..43787abd3520254a37df6376a7a6b993e359dc5c 100644 (file)
@@ -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);
index 350f78609a0b43fed4bdf3563ed290d7320a4046..7e8653bfc5691f77ea4e3c3f997a6a22ad100f19 100644 (file)
@@ -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 */
index 89b08d83b44e774996bb28b0c488ec71bf84d8b9..a826c55afb547dd1e9d84d347333469acd425fa1 100644 (file)
@@ -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 */
index 3cbfd1eb05e5b131cbb8172c6eb835c252439ecd..8a1ebbb205c4fbf18e02163c393b42d6eadcc4f3 100644 (file)
@@ -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);
index e40db8842af2dac00c5d71958199798f11f71812..84673029eb9d4ba3740c63a68424acc07369caf8 100644 (file)
@@ -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");