Blender V5.3
asset_shelf_catalog_selector.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "AS_asset_catalog.hh"
13
14#include "BLI_string_utf8.hh"
15
16#include "DNA_screen_types.h"
17
18#include "BLI_listbase.hh"
19
20#include "BKE_context.hh"
21#include "BKE_screen.hh"
22
23#include "BLT_translation.hh"
24
25#include "ED_asset_filter.hh"
26#include "ED_asset_list.hh"
27#include "ED_asset_shelf.hh"
28
29#include "RNA_access.hh"
30#include "RNA_prototypes.hh"
31
32#include "UI_interface.hh"
34#include "UI_tree_view.hh"
35
36#include "WM_api.hh"
37
38#include "asset_shelf.hh"
39
41
43 AssetShelf &shelf_;
44 AssetShelfSettings &shelf_settings_;
46
47 public:
48 class Item;
49
51 : shelf_(shelf), shelf_settings_(shelf_.settings)
52 {
53 catalog_tree_ = build_filtered_catalog_tree(
54 library,
55 shelf_settings_.asset_library_reference,
57 return type_asset_poll(*shelf_.type, asset);
58 });
59 }
60
61 void build_tree() override
62 {
63 if (catalog_tree_.is_empty()) {
64 auto &item = add_tree_item<ui::BasicTreeViewItem>(RPT_("No asset catalogs"),
65 ICON_STATUS_INFO);
66 item.disable_interaction();
67 this->is_flat_ = true;
68 return;
69 }
70
71 catalog_tree_.foreach_root_item(
72 [this](const asset_system::AssetCatalogTreeItem &catalog_item) {
73 Item &item = build_catalog_items_recursive(*this, catalog_item);
75 });
76 }
77
79 const asset_system::AssetCatalogTreeItem &catalog_item) const
80 {
81 Item &view_item = parent_view_item.add_tree_item<Item>(catalog_item, shelf_);
82
83 const int parent_count = view_item.count_parents() + 1;
84 catalog_item.foreach_child([&, this](const asset_system::AssetCatalogTreeItem &child) {
85 Item &child_item = build_catalog_items_recursive(view_item, child);
86
87 /* Uncollapse to some level (gives quick access, but don't let the tree get too big). */
88 if (parent_count < 2) {
89 child_item.uncollapse_by_default();
90 }
91 });
92
93 return view_item;
94 }
95
97
98 class Item : public ui::BasicTreeViewItem {
99 const asset_system::AssetCatalogTreeItem &catalog_item_;
100 /* Is the catalog path enabled in this redraw? Set on construction, updated by the UI (which
101 * gets a pointer to it). The UI needs it as char. */
102 char catalog_path_enabled_ = false;
103
104 public:
106 : ui::BasicTreeViewItem(catalog_item.get_name()),
107 catalog_item_(catalog_item),
108 catalog_path_enabled_(
110 {
112 }
113
115 {
116 return catalog_path_enabled_ != 0;
117 }
118
120 {
121 bool has_enabled = false;
122
124 [&has_enabled](const ui::AbstractTreeViewItem &abstract_item) {
125 const Item &item = dynamic_cast<const Item &>(abstract_item);
126 if (item.is_catalog_path_enabled()) {
127 has_enabled = true;
128 }
129 },
131
132 return has_enabled;
133 }
134
136 {
137 return catalog_item_.catalog_path();
138 }
139
140 void build_row(ui::Layout &row) override
141 {
143 ui::Block *block = row.block();
144
146
147 ui::Layout &subrow = row.row(false);
148 subrow.active_set(catalog_path_enabled_);
149 subrow.label(catalog_item_.get_name(), ICON_NONE);
150 ui::block_layout_set_current(block, &row);
151
152 ui::Button *toggle_but = uiDefButV(block,
154 "",
155 0,
156 0,
157 UI_UNIT_X,
158 UI_UNIT_Y,
159 &catalog_path_enabled_,
160 0,
161 0,
162 TIP_("Toggle catalog visibility in the asset shelf"));
163 button_func_set(toggle_but, [&tree](bContext &C) {
166 });
168 button_drawflag_enable(toggle_but, ui::BUT_INDETERMINATE);
169 }
170 button_flag_disable(toggle_but, ui::BUT_UNDO);
171 }
172 };
173};
174
176{
178 foreach_item([this](ui::AbstractTreeViewItem &view_item) {
179 const auto &selector_tree_item = dynamic_cast<AssetCatalogSelectorTree::Item &>(view_item);
180 if (selector_tree_item.is_catalog_path_enabled()) {
181 settings_set_catalog_path_enabled(shelf_, selector_tree_item.catalog_path());
182 }
183 });
184}
185
187{
189
191 &CTX_wm_screen(C)->id, RNA_AssetShelf, &shelf);
192
193 ui::Layout &row = layout.row(true);
194 row.prop(&shelf_ptr, "asset_library_reference", UI_ITEM_NONE, "", ICON_NONE);
195 if (shelf.settings.asset_library_reference.type != ASSET_LIBRARY_LOCAL) {
196 PointerRNA ptr = row.op("ASSET_OT_library_refresh", "", ICON_FILE_REFRESH);
197 RNA_boolean_set(&ptr, "use_shift_for_remote_listing", true);
198 }
199}
200
201static void catalog_selector_panel_draw(const bContext *C, Panel *panel)
202{
204 if (!shelf) {
205 return;
206 }
207
209
210 ui::Layout &layout = *panel->layout;
211
212 library_selector_draw(C, layout, *shelf);
213
215 shelf->settings.asset_library_reference);
216 if (!library) {
217 return;
218 }
219
220 ui::Block *block = layout.block();
221 ui::AbstractTreeView *tree_view = block_add_view(
222 *block,
223 "asset catalog tree view",
224 std::make_unique<AssetCatalogSelectorTree>(*library, *shelf));
225 tree_view->set_context_menu_title("Catalog");
226 ui::TreeViewBuilder::build_tree_view(*C, *tree_view, layout);
227}
228
230{
231 /* Uses global paneltype registry to allow usage as popover. So only register this once (may be
232 * called from multiple spaces). */
233 if (WM_paneltype_find("ASSETSHELF_PT_catalog_selector", true)) {
234 return;
235 }
236
237 PanelType *pt = MEM_new_zeroed<PanelType>(__func__);
238 STRNCPY_UTF8(pt->idname, "ASSETSHELF_PT_catalog_selector");
239 STRNCPY_UTF8(pt->label, N_("Catalog Selector"));
241 pt->description = N_(
242 "Select the asset library and the contained catalogs to display in the asset shelf");
245 BLI_addtail(&region_type->paneltypes, pt);
247}
248
249} // namespace blender::ed::asset::shelf
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define TIP_(msgid)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
void foreach_child(ItemIterFn callback) const
Item(const asset_system::AssetCatalogTreeItem &catalog_item, AssetShelf &shelf)
Item & build_catalog_items_recursive(ui::TreeViewOrItem &parent_view_item, const asset_system::AssetCatalogTreeItem &catalog_item) const
AssetCatalogSelectorTree(asset_system::AssetLibrary &library, AssetShelf &shelf)
Abstract base class for defining a customizable tree-view item.
AbstractTreeView & get_tree_view() const
Definition tree_view.cc:775
void foreach_item(ItemIterFn iter_fn, IterOptions options=IterOptions::None) const
Definition tree_view.cc:127
void set_context_menu_title(const std::string &title)
BasicTreeViewItem(StringRef label, BIFIconID icon=ICON_NONE)
static void build_tree_view(const bContext &C, AbstractTreeView &tree_view, Layout &layout, bool add_box=true)
ItemT & add_tree_item(Args &&...args)
void foreach_item_recursive(ItemIterFn iter_fn, IterOptions options=IterOptions::None) const
Definition tree_view.cc:67
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
void asset_reading_region_listen_fn(const wmRegionListenerParams *params)
static void catalog_selector_panel_draw(const bContext *C, Panel *panel)
void settings_clear_enabled_catalogs(AssetShelf &shelf)
AssetLibraryReference & settings_ensure_valid_library_ref(AssetShelfSettings &settings)
void catalog_selector_panel_register(ARegionType *region_type)
AssetShelf * active_shelf_from_context(const bContext *C)
bool settings_is_catalog_path_enabled(const AssetShelf &shelf, const asset_system::AssetCatalogPath &path)
void send_redraw_notifier(const bContext &C)
void settings_set_catalog_path_enabled(AssetShelf &shelf, const asset_system::AssetCatalogPath &path)
void library_selector_draw(const bContext *C, ui::Layout &layout, AssetShelf &shelf)
asset_system::AssetCatalogTree build_filtered_catalog_tree(const asset_system::AssetLibrary &library, const AssetLibraryReference &library_ref, FunctionRef< bool(const asset_system::AssetRepresentation &)> is_asset_visible_fn)
TreeViewItemContainer TreeViewOrItem
void block_layout_set_current(Block *block, Layout *layout)
bool WM_paneltype_add(PanelType *pt)
@ ASSET_LIBRARY_LOCAL
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
PanelType * WM_paneltype_find(const StringRef idname, bool quiet)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bScreen * CTX_wm_screen(const bContext *C)
ListBaseT< PanelType > paneltypes
char translation_context[BKE_ST_MAXNAME]
void(*) listener(const wmRegionListenerParams *params)
void(*) draw(const bContext *C, Panel *panel)
const char * description
char idname[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
ui::Layout * layout
void operator_context_set(wm::OpCallContext opcontext)
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & row(bool align)
void emboss_set(EmbossType emboss)
#define N_(msgid)