From 55eb0815dbe1e0303034e9e1d49294ae881736c0 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 1 Feb 2024 11:43:35 -0500 Subject: [PATCH 1/8] Shortcut attempt --- .../keyconfig/keymap_data/blender_default.py | 4 -- .../asset/intern/asset_shelf_asset_view.cc | 60 ++++++++++++++++--- .../blender/editors/sculpt_paint/paint_ops.cc | 23 +++---- .../windowmanager/intern/wm_keymap_utils.cc | 7 ++- 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/scripts/presets/keyconfig/keymap_data/blender_default.py b/scripts/presets/keyconfig/keymap_data/blender_default.py index d540b00b373..1ba25fab2c1 100644 --- a/scripts/presets/keyconfig/keymap_data/blender_default.py +++ b/scripts/presets/keyconfig/keymap_data/blender_default.py @@ -6801,10 +6801,6 @@ def km_asset_shelf_brushes(_params): {"items": items}, ) - items.extend([ - ("brush.asset_select", {"type": 'LEFTMOUSE', "value": 'CLICK'}, None), - ]) - return keymap diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index 9d958d62896..903ae006e9b 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -11,6 +11,7 @@ #include "AS_asset_library.hh" #include "AS_asset_representation.hh" +#include "BKE_icons.h" #include "BKE_screen.hh" #include "BLI_fnmatch.h" @@ -22,6 +23,7 @@ #include "ED_asset_handle.hh" #include "ED_asset_list.hh" +#include "ED_asset_menu_utils.hh" #include "ED_asset_shelf.hh" #include "UI_grid_view.hh" @@ -33,6 +35,7 @@ #include "WM_api.hh" +#include "../interface/interface_intern.hh" #include "asset_shelf.hh" namespace blender::ed::asset::shelf { @@ -194,16 +197,55 @@ void AssetViewItem::disable_asset_drag() void AssetViewItem::build_grid_tile(uiLayout &layout) const { - PointerRNA file_ptr = RNA_pointer_create( - nullptr, - &RNA_FileSelectEntry, - /* XXX passing file pointer here, should be asset handle or asset representation. */ - const_cast(asset_.file_data)); + // const ui::GridViewStyle &style = this->get_view().get_style(); + uiLayoutSetEmboss(&layout, UI_EMBOSS_NONE); + uiLayoutSetFixedSize(&layout, true); + PointerRNA op_ptr; + uiItemFullO(&layout, + "BRUSH_OT_asset_select", + hide_label_ ? "" : label.c_str(), + this->preview_icon_id, + nullptr, + WM_OP_EXEC_DEFAULT, + this->is_active() ? UI_ITEM_O_DEPRESS : UI_ITEM_NONE, + &op_ptr); + operator_asset_reference_props_set(*handle_get_representation(&asset_), op_ptr); - uiBlock *block = uiLayoutGetBlock(&layout); - UI_but_context_ptr_set( - block, reinterpret_cast(view_item_but_), "active_file", &file_ptr); - ui::PreviewGridItem::build_grid_tile(layout); + // uiBlock *block = uiLayoutGetBlock(&layout); + + // uiBut *but = uiDefButO(block, + // UI_BTYPE_BUT, + // "BRUSH_OT_asset_select", + // WM_OP_EXEC_DEFAULT, + // hide_label_ ? "" : label.c_str(), + // 0, + // 0, + // style.tile_width, + // style.tile_height, + // ""); + // PointerRNA *ptr = UI_but_operator_ptr_ensure(but); + // operator_asset_reference_props_set(*handle_get_representation(&asset_), *ptr); + + // if (this->is_active()) { + // UI_but_flag_enable(but, UI_SELECT_DRAW); + // } + + /* Draw icons that are not previews or images as normal icons with a fixed icon size. + * Otherwise they will be upscaled to the button size. Should probably be done by the widget + * code. */ + // const int is_preview_flag = (BKE_icon_is_preview(preview_icon_id) || + // BKE_icon_is_image(preview_icon_id)) ? + // int(UI_BUT_ICON_PREVIEW) : + // 0; + // ui_def_but_icon(but, + // preview_icon_id, + // /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */ + // UI_HAS_ICON | is_preview_flag); + // UI_but_func_set(but, [](bContext &C) { + + // }); + // UI_but_func_tooltip_label_set(but, [this](const uiBut * /*but*/) { return label; }); + // but->emboss = UI_EMBOSS_NONE; } void AssetViewItem::build_context_menu(bContext &C, uiLayout &column) const diff --git a/source/blender/editors/sculpt_paint/paint_ops.cc b/source/blender/editors/sculpt_paint/paint_ops.cc index c1340c19810..972018e6d01 100644 --- a/source/blender/editors/sculpt_paint/paint_ops.cc +++ b/source/blender/editors/sculpt_paint/paint_ops.cc @@ -45,6 +45,7 @@ #include "ED_asset_handle.hh" #include "ED_asset_list.hh" #include "ED_asset_mark_clear.hh" +#include "ED_asset_menu_utils.hh" #include "ED_image.hh" #include "ED_paint.hh" #include "ED_screen.hh" @@ -987,21 +988,18 @@ static void PAINT_OT_brush_select(wmOperatorType *ot) /**************************** Brush Assets **********************************/ -static bool brush_asset_select_poll(bContext *C) -{ - if (BKE_paint_get_active_from_context(C) == nullptr) { - return false; - } - - return CTX_wm_asset(C) != nullptr; -} - static int brush_asset_select_exec(bContext *C, wmOperator *op) { + using namespace blender; + using namespace blender::ed; /* This operator currently covers both cases: the file/asset browser file list and the asset list * used for the asset-view template. Once the asset list design is used by the Asset Browser, * this can be simplified to just that case. */ - blender::asset_system::AssetRepresentation *asset = CTX_wm_asset(C); + const asset_system::AssetRepresentation *asset = + asset::operator_asset_reference_props_get_asset_from_all_library(*C, *op->ptr, op->reports); + if (!asset) { + return OPERATOR_CANCELLED; + } AssetWeakReference *brush_asset_reference = asset->make_weak_reference(); Brush *brush = BKE_brush_asset_runtime_ensure(CTX_data_main(C), brush_asset_reference); @@ -1022,12 +1020,15 @@ static int brush_asset_select_exec(bContext *C, wmOperator *op) static void BRUSH_OT_asset_select(wmOperatorType *ot) { + using namespace blender; + using namespace blender::ed; ot->name = "Select Brush Asset"; ot->description = "Select a brush asset as current sculpt and paint tool"; ot->idname = "BRUSH_OT_asset_select"; ot->exec = brush_asset_select_exec; - ot->poll = brush_asset_select_poll; + + asset::operator_asset_reference_props_register(*ot->srna); } /* FIXME Quick dirty hack to generate a weak ref from 'raw' paths. diff --git a/source/blender/windowmanager/intern/wm_keymap_utils.cc b/source/blender/windowmanager/intern/wm_keymap_utils.cc index d02a2eb21b1..7c440f22dbf 100644 --- a/source/blender/windowmanager/intern/wm_keymap_utils.cc +++ b/source/blender/windowmanager/intern/wm_keymap_utils.cc @@ -204,11 +204,9 @@ wmKeyMap *WM_keymap_guess_from_context(const bContext *C) wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname) { /* Op types purposely skipped for now: - * BRUSH_OT * BOID_OT * BUTTONS_OT * CONSTRAINT_OT - * PAINT_OT * ED_OT * FLUID_OT * TEXTURE_OT @@ -331,7 +329,7 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname) km = WM_keymap_find_all( wm, "Paint Face Mask (Weight, Vertex, Texture)", SPACE_EMPTY, RGN_TYPE_WINDOW); } - else if (STRPREFIX(opname, "PAINT_OT")) { + else if (STRPREFIX(opname, "PAINT_OT") || STRPREFIX(opname, "BRUSH_OT")) { /* check for relevant mode */ switch (CTX_data_mode_enum(C)) { case CTX_MODE_PAINT_WEIGHT: @@ -346,6 +344,9 @@ wmKeyMap *WM_keymap_guess_opname(const bContext *C, const char *opname) case CTX_MODE_SCULPT: km = WM_keymap_find_all(wm, "Sculpt", SPACE_EMPTY, RGN_TYPE_WINDOW); break; + case CTX_MODE_SCULPT_CURVES: + km = WM_keymap_find_all(wm, "Sculpt Curves", SPACE_EMPTY, RGN_TYPE_WINDOW); + break; default: break; } -- 2.30.2 From ce575c4f6b06e0bb258870723a2930c40e444fb5 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 1 Feb 2024 14:05:47 -0500 Subject: [PATCH 2/8] Cleanup --- .../asset/intern/asset_shelf_asset_view.cc | 69 ++++++++----------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index 903ae006e9b..e48c6dbf91b 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -197,55 +197,40 @@ void AssetViewItem::disable_asset_drag() void AssetViewItem::build_grid_tile(uiLayout &layout) const { - // const ui::GridViewStyle &style = this->get_view().get_style(); - uiLayoutSetEmboss(&layout, UI_EMBOSS_NONE); - uiLayoutSetFixedSize(&layout, true); - PointerRNA op_ptr; - uiItemFullO(&layout, - "BRUSH_OT_asset_select", - hide_label_ ? "" : label.c_str(), - this->preview_icon_id, - nullptr, - WM_OP_EXEC_DEFAULT, - this->is_active() ? UI_ITEM_O_DEPRESS : UI_ITEM_NONE, - &op_ptr); - operator_asset_reference_props_set(*handle_get_representation(&asset_), op_ptr); + const ui::GridViewStyle &style = this->get_view().get_style(); - // uiBlock *block = uiLayoutGetBlock(&layout); + uiBlock *block = uiLayoutGetBlock(&layout); - // uiBut *but = uiDefButO(block, - // UI_BTYPE_BUT, - // "BRUSH_OT_asset_select", - // WM_OP_EXEC_DEFAULT, - // hide_label_ ? "" : label.c_str(), - // 0, - // 0, - // style.tile_width, - // style.tile_height, - // ""); - // PointerRNA *ptr = UI_but_operator_ptr_ensure(but); - // operator_asset_reference_props_set(*handle_get_representation(&asset_), *ptr); + uiBut *but = uiDefButO(block, + UI_BTYPE_BUT, + "BRUSH_OT_asset_select", + WM_OP_EXEC_DEFAULT, + hide_label_ ? "" : label.c_str(), + 0, + 0, + style.tile_width, + style.tile_height, + ""); + PointerRNA *ptr = UI_but_operator_ptr_ensure(but); + operator_asset_reference_props_set(*handle_get_representation(&asset_), *ptr); - // if (this->is_active()) { - // UI_but_flag_enable(but, UI_SELECT_DRAW); - // } + if (this->is_active()) { + UI_but_flag_enable(but, UI_SELECT_DRAW); + } /* Draw icons that are not previews or images as normal icons with a fixed icon size. * Otherwise they will be upscaled to the button size. Should probably be done by the widget * code. */ - // const int is_preview_flag = (BKE_icon_is_preview(preview_icon_id) || - // BKE_icon_is_image(preview_icon_id)) ? - // int(UI_BUT_ICON_PREVIEW) : - // 0; - // ui_def_but_icon(but, - // preview_icon_id, - // /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */ - // UI_HAS_ICON | is_preview_flag); - // UI_but_func_set(but, [](bContext &C) { - - // }); - // UI_but_func_tooltip_label_set(but, [this](const uiBut * /*but*/) { return label; }); - // but->emboss = UI_EMBOSS_NONE; + const int is_preview_flag = (BKE_icon_is_preview(preview_icon_id) || + BKE_icon_is_image(preview_icon_id)) ? + int(UI_BUT_ICON_PREVIEW) : + 0; + ui_def_but_icon(but, + preview_icon_id, + /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */ + UI_HAS_ICON | is_preview_flag); + UI_but_func_tooltip_label_set(but, [this](const uiBut * /*but*/) { return label; }); + but->emboss = UI_EMBOSS_NONE; } void AssetViewItem::build_context_menu(bContext &C, uiLayout &column) const -- 2.30.2 From 0aeb823a05b7ca44669f0b72514f1f823b613e21 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 1 Feb 2024 15:55:11 -0500 Subject: [PATCH 3/8] Working except for active status --- scripts/startup/bl_ui/space_view3d.py | 1 + source/blender/blenkernel/BKE_screen.hh | 2 + source/blender/blenkernel/intern/screen.cc | 4 +- .../asset/intern/asset_shelf_asset_view.cc | 24 ++++--- .../editors/interface/views/grid_view.cc | 8 +-- .../blender/editors/sculpt_paint/paint_ops.cc | 1 - source/blender/makesrna/intern/rna_ui.cc | 64 +++++++++++++------ 7 files changed, 70 insertions(+), 34 deletions(-) diff --git a/scripts/startup/bl_ui/space_view3d.py b/scripts/startup/bl_ui/space_view3d.py index 196755cd868..0baa55a00c1 100644 --- a/scripts/startup/bl_ui/space_view3d.py +++ b/scripts/startup/bl_ui/space_view3d.py @@ -8744,6 +8744,7 @@ class VIEW3D_PT_viewport_debug(Panel): class BrushAssetShelf: bl_space_type = "VIEW_3D" bl_options = {'NO_ASSET_DRAG'} + bl_activate_operator = "BRUSH_OT_asset_select" @classmethod def poll(cls, context): diff --git a/source/blender/blenkernel/BKE_screen.hh b/source/blender/blenkernel/BKE_screen.hh index 4887a331555..153ac7699d5 100644 --- a/source/blender/blenkernel/BKE_screen.hh +++ b/source/blender/blenkernel/BKE_screen.hh @@ -523,6 +523,8 @@ struct AssetShelfType { int space_type; + std::string activate_operator; + AssetShelfTypeFlag flag; /** Determine if asset shelves of this type should be available in current context or not. */ diff --git a/source/blender/blenkernel/intern/screen.cc b/source/blender/blenkernel/intern/screen.cc index 1a9d4e01a36..c49929e530d 100644 --- a/source/blender/blenkernel/intern/screen.cc +++ b/source/blender/blenkernel/intern/screen.cc @@ -231,7 +231,9 @@ static void spacetype_free(SpaceType *st) } BLI_freelistN(&st->regiontypes); - BLI_freelistN(&st->asset_shelf_types); + LISTBASE_FOREACH_MUTABLE (AssetShelfType *, shelf_type, &st->asset_shelf_types) { + delete shelf_type; + } } void BKE_spacetypes_free() diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index e48c6dbf91b..c146b6858e0 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -200,21 +200,25 @@ void AssetViewItem::build_grid_tile(uiLayout &layout) const const ui::GridViewStyle &style = this->get_view().get_style(); uiBlock *block = uiLayoutGetBlock(&layout); + const AssetView &asset_view = dynamic_cast(get_view()); + const AssetShelfType &shelf_type = *asset_view.shelf_.type; - uiBut *but = uiDefButO(block, - UI_BTYPE_BUT, - "BRUSH_OT_asset_select", - WM_OP_EXEC_DEFAULT, - hide_label_ ? "" : label.c_str(), - 0, - 0, - style.tile_width, - style.tile_height, - ""); + uiBut *but = uiDefIconTextButO(block, + UI_BTYPE_BUT, + shelf_type.activate_operator.c_str(), + WM_OP_EXEC_DEFAULT, + this->preview_icon_id, + hide_label_ ? "" : label.c_str(), + 0, + 0, + style.tile_width, + style.tile_height, + ""); PointerRNA *ptr = UI_but_operator_ptr_ensure(but); operator_asset_reference_props_set(*handle_get_representation(&asset_), *ptr); if (this->is_active()) { + printf("IS ACTIVE!\n"); UI_but_flag_enable(but, UI_SELECT_DRAW); } diff --git a/source/blender/editors/interface/views/grid_view.cc b/source/blender/editors/interface/views/grid_view.cc index 113ea8c2a60..bcc5f63ce51 100644 --- a/source/blender/editors/interface/views/grid_view.cc +++ b/source/blender/editors/interface/views/grid_view.cc @@ -342,9 +342,9 @@ void GridViewLayoutBuilder::build_grid_tile(uiLayout &grid_layout, void GridViewLayoutBuilder::build_from_view(const AbstractGridView &grid_view, const View2D &v2d) const { - uiLayout *parent_layout = current_layout(); + uiLayout *parent_layout = this->current_layout(); - uiLayout &layout = *uiLayoutColumn(current_layout(), true); + uiLayout &layout = *uiLayoutColumn(parent_layout, true); const GridViewStyle &style = grid_view.get_style(); const int cols_per_row = std::max(uiLayoutGetWidth(&layout) / style.tile_width, 1); @@ -367,7 +367,7 @@ void GridViewLayoutBuilder::build_from_view(const AbstractGridView &grid_view, row = uiLayoutRow(&layout, true); } - build_grid_tile(*row, item); + this->build_grid_tile(*row, item); item_idx++; }); @@ -411,7 +411,7 @@ PreviewGridItem::PreviewGridItem(StringRef identifier, StringRef label, int prev void PreviewGridItem::build_grid_tile(uiLayout &layout) const { - const GridViewStyle &style = get_view().get_style(); + const GridViewStyle &style = this->get_view().get_style(); uiBlock *block = uiLayoutGetBlock(&layout); uiBut *but = uiDefBut(block, diff --git a/source/blender/editors/sculpt_paint/paint_ops.cc b/source/blender/editors/sculpt_paint/paint_ops.cc index 972018e6d01..e5b1079501b 100644 --- a/source/blender/editors/sculpt_paint/paint_ops.cc +++ b/source/blender/editors/sculpt_paint/paint_ops.cc @@ -1020,7 +1020,6 @@ static int brush_asset_select_exec(bContext *C, wmOperator *op) static void BRUSH_OT_asset_select(wmOperatorType *ot) { - using namespace blender; using namespace blender::ed; ot->name = "Select Brush Asset"; ot->description = "Select a brush asset as current sculpt and paint tool"; diff --git a/source/blender/makesrna/intern/rna_ui.cc b/source/blender/makesrna/intern/rna_ui.cc index 0ae5605a8b4..b9316603b6e 100644 --- a/source/blender/makesrna/intern/rna_ui.cc +++ b/source/blender/makesrna/intern/rna_ui.cc @@ -1185,7 +1185,8 @@ static bool rna_AssetShelf_unregister(Main *bmain, StructRNA *type) RNA_struct_free_extension(type, &shelf_type->rna_ext); RNA_struct_free(&BLENDER_RNA, type); - BLI_freelinkN(&space_type->asset_shelf_types, shelf_type); + BLI_remlink(&space_type->asset_shelf_types, shelf_type); + delete shelf_type; /* update while blender is running */ WM_main_add_notifier(NC_WINDOW, nullptr); @@ -1201,11 +1202,11 @@ static StructRNA *rna_AssetShelf_register(Main *bmain, StructCallbackFunc call, StructFreeFunc free) { - AssetShelfType dummy_shelf_type = {}; - AssetShelf dummy_shelf = {}; + std::unique_ptr shelf_type = std::make_unique(); /* setup dummy shelf & shelf type to store static properties in */ - dummy_shelf.type = &dummy_shelf_type; + AssetShelf dummy_shelf = {}; + dummy_shelf.type = shelf_type.get(); PointerRNA dummy_shelf_ptr = RNA_pointer_create(nullptr, &RNA_AssetShelf, &dummy_shelf); bool have_function[3]; @@ -1215,16 +1216,16 @@ static StructRNA *rna_AssetShelf_register(Main *bmain, return nullptr; } - if (strlen(identifier) >= sizeof(dummy_shelf_type.idname)) { + if (strlen(identifier) >= sizeof(shelf_type->idname)) { BKE_reportf(reports, RPT_ERROR, "Registering asset shelf class: '%s' is too long, maximum length is %d", identifier, - (int)sizeof(dummy_shelf_type.idname)); + (int)sizeof(shelf_type->idname)); return nullptr; } - SpaceType *space_type = BKE_spacetype_from_id(dummy_shelf_type.space_type); + SpaceType *space_type = BKE_spacetype_from_id(shelf_type->space_type); if (!space_type) { BLI_assert_unreachable(); return nullptr; @@ -1232,47 +1233,63 @@ static StructRNA *rna_AssetShelf_register(Main *bmain, /* Check if we have registered this asset shelf type before, and remove it. */ LISTBASE_FOREACH (AssetShelfType *, iter_shelf_type, &space_type->asset_shelf_types) { - if (STREQ(iter_shelf_type->idname, dummy_shelf_type.idname)) { + if (STREQ(iter_shelf_type->idname, shelf_type->idname)) { if (iter_shelf_type->rna_ext.srna) { BKE_reportf(reports, RPT_INFO, "Registering asset shelf class: '%s' has been registered before, " "unregistering previous", - dummy_shelf_type.idname); + shelf_type->idname); rna_AssetShelf_unregister(bmain, iter_shelf_type->rna_ext.srna); } break; } } - if (!RNA_struct_available_or_report(reports, dummy_shelf_type.idname)) { + if (!RNA_struct_available_or_report(reports, shelf_type->idname)) { return nullptr; } - if (!RNA_struct_bl_idname_ok_or_report(reports, dummy_shelf_type.idname, "_AST_")) { + if (!RNA_struct_bl_idname_ok_or_report(reports, shelf_type->idname, "_AST_")) { return nullptr; } /* Create the new shelf type. */ - AssetShelfType *shelf_type = static_cast( - MEM_mallocN(sizeof(*shelf_type), __func__)); - memcpy(shelf_type, &dummy_shelf_type, sizeof(*shelf_type)); - shelf_type->rna_ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, shelf_type->idname, &RNA_AssetShelf); shelf_type->rna_ext.data = data; shelf_type->rna_ext.call = call; shelf_type->rna_ext.free = free; - RNA_struct_blender_type_set(shelf_type->rna_ext.srna, shelf_type); + RNA_struct_blender_type_set(shelf_type->rna_ext.srna, shelf_type.get()); shelf_type->poll = have_function[0] ? asset_shelf_poll : nullptr; shelf_type->asset_poll = have_function[1] ? asset_shelf_asset_poll : nullptr; shelf_type->draw_context_menu = have_function[2] ? asset_shelf_draw_context_menu : nullptr; - BLI_addtail(&space_type->asset_shelf_types, shelf_type); + StructRNA *srna = shelf_type->rna_ext.srna; + + BLI_addtail(&space_type->asset_shelf_types, shelf_type.release()); /* update while blender is running */ WM_main_add_notifier(NC_WINDOW, nullptr); - return shelf_type->rna_ext.srna; + return srna; +} + +static void rna_AssetShelf_activate_operator_get(PointerRNA *ptr, char *value) +{ + AssetShelf *shelf = static_cast(ptr->data); + strcpy(value, shelf->type->activate_operator.c_str()); +} + +static int rna_AssetShelf_activate_operator_length(PointerRNA *ptr) +{ + AssetShelf *shelf = static_cast(ptr->data); + return shelf->type->activate_operator.size(); +} + +static void rna_AssetShelf_activate_operator_set(PointerRNA *ptr, const char *value) +{ + AssetShelf *shelf = static_cast(ptr->data); + shelf->type->activate_operator = value; } static StructRNA *rna_AssetShelf_refine(PointerRNA *shelf_ptr) @@ -2293,6 +2310,17 @@ static void rna_def_asset_shelf(BlenderRNA *brna) RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL | PROP_ENUM_FLAG); RNA_def_property_ui_text(prop, "Options", "Options for this asset shelf type"); + prop = RNA_def_property(srna, "bl_activate_operator", PROP_STRING, PROP_NONE); + RNA_def_property_string_funcs(prop, + "rna_AssetShelf_activate_operator_get", + "rna_AssetShelf_activate_operator_length", + "rna_AssetShelf_activate_operator_set"); + RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL); + RNA_def_property_ui_text( + prop, + "Activate Operator", + "Operator to call when activating an item with asset reference properties"); + PropertyRNA *parm; FunctionRNA *func; -- 2.30.2 From 0da63eca67ba09bc9ba937464c1c85644a229bda Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Fri, 2 Feb 2024 10:56:16 -0500 Subject: [PATCH 4/8] Working --- .../asset/intern/asset_shelf_asset_view.cc | 42 +++------------ .../blender/editors/include/UI_grid_view.hh | 6 ++- .../editors/interface/interface_handlers.cc | 1 + .../interface/interface_region_tooltip.cc | 54 +++++++++---------- .../editors/interface/views/grid_view.cc | 42 +++++++++++---- 5 files changed, 71 insertions(+), 74 deletions(-) diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index c146b6858e0..3c54dc92d85 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -197,44 +197,14 @@ void AssetViewItem::disable_asset_drag() void AssetViewItem::build_grid_tile(uiLayout &layout) const { - const ui::GridViewStyle &style = this->get_view().get_style(); - - uiBlock *block = uiLayoutGetBlock(&layout); - const AssetView &asset_view = dynamic_cast(get_view()); + const AssetView &asset_view = reinterpret_cast(this->get_view()); const AssetShelfType &shelf_type = *asset_view.shelf_.type; + wmOperatorType *ot = WM_operatortype_find(shelf_type.activate_operator.c_str(), false); + PointerRNA op_props; + WM_operator_properties_create_ptr(&op_props, ot); + asset::operator_asset_reference_props_set(*handle_get_representation(&asset_), op_props); - uiBut *but = uiDefIconTextButO(block, - UI_BTYPE_BUT, - shelf_type.activate_operator.c_str(), - WM_OP_EXEC_DEFAULT, - this->preview_icon_id, - hide_label_ ? "" : label.c_str(), - 0, - 0, - style.tile_width, - style.tile_height, - ""); - PointerRNA *ptr = UI_but_operator_ptr_ensure(but); - operator_asset_reference_props_set(*handle_get_representation(&asset_), *ptr); - - if (this->is_active()) { - printf("IS ACTIVE!\n"); - UI_but_flag_enable(but, UI_SELECT_DRAW); - } - - /* Draw icons that are not previews or images as normal icons with a fixed icon size. - * Otherwise they will be upscaled to the button size. Should probably be done by the widget - * code. */ - const int is_preview_flag = (BKE_icon_is_preview(preview_icon_id) || - BKE_icon_is_image(preview_icon_id)) ? - int(UI_BUT_ICON_PREVIEW) : - 0; - ui_def_but_icon(but, - preview_icon_id, - /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */ - UI_HAS_ICON | is_preview_flag); - UI_but_func_tooltip_label_set(but, [this](const uiBut * /*but*/) { return label; }); - but->emboss = UI_EMBOSS_NONE; + ui::PreviewGridItem::build_grid_tile_button(layout, ot, op_props); } void AssetViewItem::build_context_menu(bContext &C, uiLayout &column) const diff --git a/source/blender/editors/include/UI_grid_view.hh b/source/blender/editors/include/UI_grid_view.hh index 69214c1955d..34baeecba49 100644 --- a/source/blender/editors/include/UI_grid_view.hh +++ b/source/blender/editors/include/UI_grid_view.hh @@ -19,6 +19,7 @@ #include "UI_resources.hh" struct bContext; +struct PointerRNA; struct uiBlock; struct uiButViewItem; struct uiLayout; @@ -197,12 +198,15 @@ class PreviewGridItem : public AbstractGridViewItem { bool hide_label_ = false; public: - std::string label{}; + std::string label; int preview_icon_id = ICON_NONE; PreviewGridItem(StringRef identifier, StringRef label, int preview_icon_id); void build_grid_tile(uiLayout &layout) const override; + void build_grid_tile_button(uiLayout &layout, + const wmOperatorType *ot, + const PointerRNA &op_props) const; /** * Set a custom callback to execute when activating this view item. This way users don't have to diff --git a/source/blender/editors/interface/interface_handlers.cc b/source/blender/editors/interface/interface_handlers.cc index 493c73479c1..73c62b47ee7 100644 --- a/source/blender/editors/interface/interface_handlers.cc +++ b/source/blender/editors/interface/interface_handlers.cc @@ -2335,6 +2335,7 @@ static void ui_apply_but( switch (but_type) { case UI_BTYPE_BUT: case UI_BTYPE_DECORATOR: + case UI_BTYPE_PREVIEW_TILE: ui_apply_but_BUT(C, but, data); break; case UI_BTYPE_TEXT: diff --git a/source/blender/editors/interface/interface_region_tooltip.cc b/source/blender/editors/interface/interface_region_tooltip.cc index 113ed830be4..5f86b0c2582 100644 --- a/source/blender/editors/interface/interface_region_tooltip.cc +++ b/source/blender/editors/interface/interface_region_tooltip.cc @@ -372,35 +372,33 @@ static bool ui_tooltip_data_append_from_keymap(bContext *C, uiTooltipData *data, LISTBASE_FOREACH (wmKeyMapItem *, kmi, &keymap->items) { wmOperatorType *ot = WM_operatortype_find(kmi->idname, true); - if (ot != nullptr) { - /* Tip. */ - { - UI_tooltip_text_field_add(data, - ot->description ? ot->description : ot->name, - {}, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_MAIN, - true); - } - /* Shortcut. */ - { - bool found = false; - if (WM_keymap_item_to_string(kmi, false, buf, sizeof(buf))) { - found = true; - } - UI_tooltip_text_field_add(data, - fmt::format(TIP_("Shortcut: {}"), found ? buf : "None"), - {}, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL); - } + if (!ot) { + continue; + } + /* Tip. */ + UI_tooltip_text_field_add(data, + ot->description ? ot->description : ot->name, + {}, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_MAIN, + true); - /* Python. */ - if (U.flag & USER_TOOLTIPS_PYTHON) { - std::string str = ui_tooltip_text_python_from_op(C, ot, kmi->ptr); - UI_tooltip_text_field_add( - data, fmt::format(TIP_("Python: {}"), str), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_PYTHON); - } + /* Shortcut. */ + bool found = false; + if (WM_keymap_item_to_string(kmi, false, buf, sizeof(buf))) { + found = true; + } + UI_tooltip_text_field_add(data, + fmt::format(TIP_("Shortcut: {}"), found ? buf : "None"), + {}, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_NORMAL); + + /* Python. */ + if (U.flag & USER_TOOLTIPS_PYTHON) { + std::string str = ui_tooltip_text_python_from_op(C, ot, kmi->ptr); + UI_tooltip_text_field_add( + data, fmt::format(TIP_("Python: {}"), str), {}, UI_TIP_STYLE_NORMAL, UI_TIP_LC_PYTHON); } } diff --git a/source/blender/editors/interface/views/grid_view.cc b/source/blender/editors/interface/views/grid_view.cc index bcc5f63ce51..4cfc103f7cd 100644 --- a/source/blender/editors/interface/views/grid_view.cc +++ b/source/blender/editors/interface/views/grid_view.cc @@ -17,6 +17,8 @@ #include "WM_types.hh" +#include "RNA_access.hh" + #include "UI_interface.hh" #include "interface_intern.hh" @@ -409,25 +411,42 @@ PreviewGridItem::PreviewGridItem(StringRef identifier, StringRef label, int prev { } -void PreviewGridItem::build_grid_tile(uiLayout &layout) const +void PreviewGridItem::build_grid_tile_button(uiLayout &layout, + const wmOperatorType *ot, + const PointerRNA &op_props) const { - const GridViewStyle &style = this->get_view().get_style(); + const GridViewStyle &style = get_view().get_style(); uiBlock *block = uiLayoutGetBlock(&layout); - uiBut *but = uiDefBut(block, + uiBut *but; + if (ot) { + but = uiDefButO_ptr(block, UI_BTYPE_PREVIEW_TILE, - 0, + const_cast(ot), + WM_OP_INVOKE_REGION_WIN, hide_label_ ? "" : label.c_str(), 0, 0, style.tile_width, style.tile_height, - nullptr, - 0, - 0, - 0, - 0, ""); + } + else { + but = uiDefBut(block, + UI_BTYPE_PREVIEW_TILE, + 0, + hide_label_ ? "" : label.c_str(), + 0, + 0, + style.tile_width, + style.tile_height, + nullptr, + 0, + 0, + 0, + 0, + ""); + } /* Draw icons that are not previews or images as normal icons with a fixed icon size. Otherwise * they will be upscaled to the button size. Should probably be done by the widget code. */ const int is_preview_flag = (BKE_icon_is_preview(preview_icon_id) || @@ -442,6 +461,11 @@ void PreviewGridItem::build_grid_tile(uiLayout &layout) const but->emboss = UI_EMBOSS_NONE; } +void PreviewGridItem::build_grid_tile(uiLayout &layout) const +{ + this->build_grid_tile_button(layout, nullptr, {}); +} + void PreviewGridItem::set_on_activate_fn(ActivateFn fn) { activate_fn_ = fn; -- 2.30.2 From 3d771509e92a4f171140c5bb401f4665aae8de36 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 5 Feb 2024 11:35:09 -0500 Subject: [PATCH 5/8] Cleanup, fix --- .../keyconfig/keymap_data/blender_default.py | 14 -------------- source/blender/blenkernel/BKE_screen.hh | 1 + .../editors/asset/intern/asset_shelf_asset_view.cc | 2 -- source/blender/editors/include/UI_grid_view.hh | 4 ++++ .../blender/editors/interface/views/grid_view.cc | 1 + 5 files changed, 6 insertions(+), 16 deletions(-) diff --git a/scripts/presets/keyconfig/keymap_data/blender_default.py b/scripts/presets/keyconfig/keymap_data/blender_default.py index 24ddb656e34..8c9f2fee40f 100644 --- a/scripts/presets/keyconfig/keymap_data/blender_default.py +++ b/scripts/presets/keyconfig/keymap_data/blender_default.py @@ -6796,17 +6796,6 @@ def km_node_link_modal_map(_params): return keymap -def km_asset_shelf_brushes(_params): - items = [] - keymap = ( - "Asset Shelf", - {"space_type": 'EMPTY', "region_type": 'WINDOW'}, - {"items": items}, - ) - - return keymap - - # Fallback for gizmos that don't have custom a custom key-map. def km_generic_gizmo(_params): keymap = ( @@ -8642,9 +8631,6 @@ def generate_keymaps(params=None): km_curve_pen_modal_map(params), km_node_link_modal_map(params), - # Asset Shelf Keymaps. - km_asset_shelf_brushes(params), - # Gizmos. km_generic_gizmo(params), km_generic_gizmo_drag(params), diff --git a/source/blender/blenkernel/BKE_screen.hh b/source/blender/blenkernel/BKE_screen.hh index 49c715110a6..82570b0a26e 100644 --- a/source/blender/blenkernel/BKE_screen.hh +++ b/source/blender/blenkernel/BKE_screen.hh @@ -523,6 +523,7 @@ struct AssetShelfType { int space_type; + /** Operator to call when activating a grid view item. */ std::string activate_operator; AssetShelfTypeFlag flag; diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index 39acfb54c34..c11c4a5ac35 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -12,7 +12,6 @@ #include "AS_asset_representation.hh" #include "BKE_asset.hh" -#include "BKE_icons.h" #include "BKE_screen.hh" #include "BLI_fnmatch.h" @@ -36,7 +35,6 @@ #include "WM_api.hh" -#include "../interface/interface_intern.hh" #include "asset_shelf.hh" namespace blender::ed::asset::shelf { diff --git a/source/blender/editors/include/UI_grid_view.hh b/source/blender/editors/include/UI_grid_view.hh index 34baeecba49..e27b44e8765 100644 --- a/source/blender/editors/include/UI_grid_view.hh +++ b/source/blender/editors/include/UI_grid_view.hh @@ -204,6 +204,10 @@ class PreviewGridItem : public AbstractGridViewItem { PreviewGridItem(StringRef identifier, StringRef label, int preview_icon_id); void build_grid_tile(uiLayout &layout) const override; + + /** + * \note: Takes ownership of the operator properies defined in \a op_props. + */ void build_grid_tile_button(uiLayout &layout, const wmOperatorType *ot, const PointerRNA &op_props) const; diff --git a/source/blender/editors/interface/views/grid_view.cc b/source/blender/editors/interface/views/grid_view.cc index 4cfc103f7cd..b46dbf95a29 100644 --- a/source/blender/editors/interface/views/grid_view.cc +++ b/source/blender/editors/interface/views/grid_view.cc @@ -430,6 +430,7 @@ void PreviewGridItem::build_grid_tile_button(uiLayout &layout, style.tile_width, style.tile_height, ""); + but->opptr = MEM_new(__func__, op_props); } else { but = uiDefBut(block, -- 2.30.2 From 67d3cbc25da0f66294023b819c4103732597d5e4 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 5 Feb 2024 11:44:30 -0500 Subject: [PATCH 6/8] Add newline --- source/blender/editors/interface/views/grid_view.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/source/blender/editors/interface/views/grid_view.cc b/source/blender/editors/interface/views/grid_view.cc index 6baa5ab903a..66abfc887d1 100644 --- a/source/blender/editors/interface/views/grid_view.cc +++ b/source/blender/editors/interface/views/grid_view.cc @@ -448,6 +448,7 @@ void PreviewGridItem::build_grid_tile_button(uiLayout &layout, 0, ""); } + /* Draw icons that are not previews or images as normal icons with a fixed icon size. Otherwise * they will be upscaled to the button size. Should probably be done by the widget code. */ const int is_preview_flag = (BKE_icon_is_preview(preview_icon_id) || -- 2.30.2 From a2305229cbf756d7d805e74e11dfae6bb33405d2 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 7 Feb 2024 12:40:55 -0500 Subject: [PATCH 7/8] Handle null/unused operator --- .../editors/asset/intern/asset_shelf_asset_view.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index c11c4a5ac35..c97c7af5560 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -209,10 +209,13 @@ void AssetViewItem::build_grid_tile(uiLayout &layout) const { const AssetView &asset_view = reinterpret_cast(this->get_view()); const AssetShelfType &shelf_type = *asset_view.shelf_.type; - wmOperatorType *ot = WM_operatortype_find(shelf_type.activate_operator.c_str(), false); - PointerRNA op_props; - WM_operator_properties_create_ptr(&op_props, ot); - asset::operator_asset_reference_props_set(*handle_get_representation(&asset_), op_props); + + wmOperatorType *ot = WM_operatortype_find(shelf_type.activate_operator.c_str(), true); + PointerRNA op_props = PointerRNA_NULL; + if (ot) { + WM_operator_properties_create_ptr(&op_props, ot); + asset::operator_asset_reference_props_set(*handle_get_representation(&asset_), op_props); + } ui::PreviewGridItem::build_grid_tile_button(layout, ot, op_props); } -- 2.30.2 From 3ef7652fdd44f858c8a0ec3ce314f76dd0ca71d9 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 7 Feb 2024 12:43:18 -0500 Subject: [PATCH 8/8] Add default arguments --- .../blender/editors/asset/intern/asset_shelf_asset_view.cc | 2 +- source/blender/editors/include/UI_grid_view.hh | 4 ++-- source/blender/editors/interface/views/grid_view.cc | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc index c97c7af5560..ab4340e4096 100644 --- a/source/blender/editors/asset/intern/asset_shelf_asset_view.cc +++ b/source/blender/editors/asset/intern/asset_shelf_asset_view.cc @@ -217,7 +217,7 @@ void AssetViewItem::build_grid_tile(uiLayout &layout) const asset::operator_asset_reference_props_set(*handle_get_representation(&asset_), op_props); } - ui::PreviewGridItem::build_grid_tile_button(layout, ot, op_props); + ui::PreviewGridItem::build_grid_tile_button(layout, ot, &op_props); } void AssetViewItem::build_context_menu(bContext &C, uiLayout &column) const diff --git a/source/blender/editors/include/UI_grid_view.hh b/source/blender/editors/include/UI_grid_view.hh index e27b44e8765..05ea6c06d4b 100644 --- a/source/blender/editors/include/UI_grid_view.hh +++ b/source/blender/editors/include/UI_grid_view.hh @@ -209,8 +209,8 @@ class PreviewGridItem : public AbstractGridViewItem { * \note: Takes ownership of the operator properies defined in \a op_props. */ void build_grid_tile_button(uiLayout &layout, - const wmOperatorType *ot, - const PointerRNA &op_props) const; + const wmOperatorType *ot = nullptr, + const PointerRNA *op_props = nullptr) const; /** * Set a custom callback to execute when activating this view item. This way users don't have to diff --git a/source/blender/editors/interface/views/grid_view.cc b/source/blender/editors/interface/views/grid_view.cc index ab2e7706096..d4ae95485db 100644 --- a/source/blender/editors/interface/views/grid_view.cc +++ b/source/blender/editors/interface/views/grid_view.cc @@ -413,7 +413,7 @@ PreviewGridItem::PreviewGridItem(StringRef identifier, StringRef label, int prev void PreviewGridItem::build_grid_tile_button(uiLayout &layout, const wmOperatorType *ot, - const PointerRNA &op_props) const + const PointerRNA *op_props) const { const GridViewStyle &style = this->get_view().get_style(); uiBlock *block = uiLayoutGetBlock(&layout); @@ -430,7 +430,7 @@ void PreviewGridItem::build_grid_tile_button(uiLayout &layout, style.tile_width, style.tile_height, ""); - but->opptr = MEM_new(__func__, op_props); + but->opptr = MEM_new(__func__, *op_props); } else { but = uiDefBut(block, @@ -465,7 +465,7 @@ void PreviewGridItem::build_grid_tile_button(uiLayout &layout, void PreviewGridItem::build_grid_tile(uiLayout &layout) const { - this->build_grid_tile_button(layout, nullptr, {}); + this->build_grid_tile_button(layout); } void PreviewGridItem::set_on_activate_fn(ActivateFn fn) -- 2.30.2