Blender V5.3
asset_shelf_asset_view.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_library.hh"
13
14#include "BKE_screen.hh"
15
16#include "BLI_fnmatch.hh"
17#include "BLI_listbase.hh"
18#include "BLI_string.hh"
19
20#include "BLT_translation.hh"
21
22#include "BLT_translation.hh"
23
24#include "DNA_asset_types.h"
25#include "DNA_screen_types.h"
26
27#include "ED_asset.hh"
29#include "ED_asset_shelf.hh"
30
31#include "UI_grid_view.hh"
32#include "UI_interface.hh"
34
35#include "RNA_access.hh"
36#include "RNA_prototypes.hh"
37
38#include "WM_api.hh"
39
40#include "asset_shelf.hh"
41
43
45 const AssetLibraryReference library_ref_;
46 const AssetShelf &shelf_;
47 std::optional<AssetWeakReference> active_asset_;
48 std::optional<asset_system::AssetCatalogFilter> catalog_filter_ = std::nullopt;
49
50 friend class AssetViewItem;
51 friend class AssetDragController;
52
53 public:
54 AssetView(const AssetLibraryReference &library_ref, const AssetShelf &shelf);
55
56 void build_items() override;
57 bool begin_filtering(const bContext &C) const override;
58
59 void set_catalog_filter(const std::optional<asset_system::AssetCatalogFilter> &catalog_filter);
60};
61
64 bool allow_asset_drag_ = true;
65
66 public:
68
69 void disable_asset_drag();
70 void build_grid_tile(const bContext &C, ui::Layout &layout) const override;
71 void build_context_menu(bContext &C, ui::Layout &column) const override;
72 std::optional<bool> should_be_active() const override;
73 void on_activate(bContext &C) override;
74 bool should_be_filtered_visible(StringRefNull filter_string) const override;
75
76 std::unique_ptr<ui::AbstractViewItemDragController> create_drag_controller() const override;
77};
78
81
82 public:
84
85 std::optional<eWM_DragDataType> get_drag_type() const override;
86 void *create_drag_data() const override;
87 void on_drag_start(bContext &C, ui::AbstractViewItem &item) override;
88};
89
91 : library_ref_(library_ref), shelf_(shelf)
92{
93 if (shelf.type->get_active_asset) {
94 if (const AssetWeakReference *weak_ref = shelf.type->get_active_asset(shelf.type)) {
95 active_asset_ = *weak_ref;
96 }
97 else {
98 active_asset_.reset();
99 }
100 }
101}
102
104{
106 if (!library) {
107 return;
108 }
109
111 if (!shelf::type_asset_poll(*shelf_.type, asset)) {
112 /* Skip this asset. */
113 return true;
114 }
115
116 const AssetMetaData &asset_data = asset.get_metadata();
117 if (catalog_filter_ && !catalog_filter_->contains(asset_data.catalog_id)) {
118 /* Skip this asset. */
119 return true;
120 }
121
122 const bool show_names = (shelf_.settings.display_flag & ASSETSHELF_SHOW_NAMES);
123 const StringRef identifier = asset.library_relative_identifier();
124
125 AssetViewItem &item = this->add_item<AssetViewItem>(asset, identifier, asset.get_name());
126 if (!show_names) {
127 item.hide_label();
128 }
129 if (shelf_.type->flag & ASSET_SHELF_TYPE_FLAG_NO_ASSET_DRAG) {
130 item.disable_asset_drag();
131 }
132 if (!shelf_.type->drag_operator.empty()) {
133 /* For now always select/activate items on click instead of press when there's a drag
134 * operator set. Important for pose library blending. Maybe we want to make this an explicit
135 * option of the asset shelf instead. */
136 item.select_on_click_set();
137 }
138 /* Make sure every click calls the #bl_activate_operator. We might want to add a flag to
139 * enable/disable this. Or we only call #bl_activate_operator when an item becomes active, and
140 * add a #bl_click_operator for repeated execution on every click. So far it seems like every
141 * asset shelf use case works with activating on every click though. */
143 if (shelf_.type->flag & ASSET_SHELF_TYPE_FLAG_ACTIVATE_FOR_CONTEXT_MENU &&
144 !asset.is_online_only())
145 {
147 }
148
149 return true;
150 });
151}
152
154{
155 const ScrArea *area = CTX_wm_area(&C);
156 for (ARegion &region : area->regionbase) {
157 if (ui::textbutton_activate_rna(&C, &region, &shelf_, "search_filter")) {
158 return true;
159 }
160 }
161
162 return false;
163}
164
166 const std::optional<asset_system::AssetCatalogFilter> &catalog_filter)
167{
168 if (catalog_filter) {
169 catalog_filter_.emplace(*catalog_filter);
170 }
171 else {
172 catalog_filter_ = std::nullopt;
173 }
174}
175
176static std::optional<asset_system::AssetCatalogFilter> catalog_filter_from_shelf_settings(
177 const AssetShelfSettings &shelf_settings, const asset_system::AssetLibrary &library)
178{
179 if (!shelf_settings.active_catalog_path) {
180 return {};
181 }
182
183 asset_system::AssetCatalog *active_catalog = library.catalog_service().find_catalog_by_path(
184 shelf_settings.active_catalog_path);
185 if (!active_catalog) {
186 return {};
187 }
188
189 return library.catalog_service().create_catalog_filter(active_catalog->catalog_id);
190}
191
192/* ---------------------------------------------------------------------- */
193
195 StringRef identifier,
197 : ui::PreviewGridItem(identifier, label, ICON_NONE), asset_(asset)
198{
199}
200
202{
203 allow_asset_drag_ = false;
204}
205
210static std::optional<wmOperatorCallParams> create_asset_operator_params(
212{
213 if (op_name.is_empty()) {
214 return {};
215 }
216 wmOperatorType *ot = WM_operatortype_find(op_name.c_str(), true);
217 if (!ot) {
218 return {};
219 }
220
221 PointerRNA *op_props = MEM_new<PointerRNA>(__func__, WM_operator_properties_create_ptr(ot));
224}
225
227{
228 const AssetView &asset_view = reinterpret_cast<const AssetView &>(this->get_view());
229 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
230
231 PointerRNA asset_ptr = RNA_pointer_create_discrete(nullptr, RNA_AssetRepresentation, &asset_);
232 button_context_ptr_set(
233 layout.block(), reinterpret_cast<ui::Button *>(view_item_but_), "asset", &asset_ptr);
234
235 ui::Button *item_but = reinterpret_cast<ui::Button *>(this->view_item_button());
236 if (std::optional<wmOperatorCallParams> activate_op = create_asset_operator_params(
237 shelf_type.activate_operator, asset_))
238 {
239 /* Attach the operator, but don't call it through the button. We call it using
240 * #on_activate(). */
241 button_operator_set(item_but, activate_op->optype, activate_op->opcontext, activate_op->opptr);
242 button_operator_set_never_call(item_but);
243
244 MEM_delete(activate_op->opptr);
245 }
246 const ui::GridViewStyle &style = this->get_view().get_style();
247 /* Increase background draw size slightly, so highlights are well visible behind previews with an
248 * opaque background. */
249 button_view_item_draw_size_set(
250 item_but, style.tile_width + 2 * U.pixelsize, style.tile_height + 2 * U.pixelsize);
251
252 button_func_tooltip_custom_set(
253 item_but,
254 [](bContext & /*C*/, ui::TooltipData &tip, ui::Button * /*but*/, void *argN) {
256 static_cast<const asset_system::AssetRepresentation *>(argN);
257 asset_tooltip(*asset, tip);
258 },
259 (&asset_),
260 nullptr);
261
262 /* Request preview when drawing. Grid views have an optimization to only draw items that are
263 * actually visible, so only previews scrolled into view will be loaded this way. This reduces
264 * total loading time and memory footprint. */
265 asset_.ensure_previewable(C);
266
267 const int preview_id = [&]() -> int {
268 /* Show loading icon while list is loading still. Previews might get pushed out of view again
269 * while the list grows, which can cause a lot of flickering. Note that this also means the
270 * actual loading of previews is delayed, because that only happens when a preview icon-ID is
271 * attached to a button. */
272 if (!list::is_loaded(&asset_view.library_ref_)) {
273 return ICON_PREVIEW_LOADING;
274 }
275 return asset_preview_or_icon(asset_);
276 }();
277
278 ui::GridViewStyle grid_style = asset_view.get_style();
279 /* Add overlap layout so indicator icons can be displayed on top of the preview. */
280 ui::Layout &overlap = layout.overlap();
281 overlap.ui_units_x_set(grid_style.tile_width / UI_UNIT_X);
282 overlap.ui_units_y_set(grid_style.tile_height / UI_UNIT_Y);
283
284 ui::PreviewGridItem::build_grid_tile_button(overlap.column(true), preview_id);
285
286 ui::Layout &overlay_row = overlap.row(true);
288
289 if (asset_.is_online_only()) {
290 ui::Button *online_icon = uiItemL_ex(&overlay_row, "", ICON_INTERNET, false, false);
291 button_label_alpha_factor_set(online_icon, 0.6f);
292 button_label_draw_icon_border_set(online_icon, true);
293 }
294 else if (asset_.needs_download()) {
295 ui::Button *needs_download_icon = uiItemL_ex(
296 &overlay_row, "", ICON_STATUS_WARNING_FILLED, false, false);
297 button_label_alpha_factor_set(needs_download_icon, 0.6f);
298 button_label_draw_icon_border_set(needs_download_icon, true);
299 }
300
301 /* Download overlay button for online assets. */
302 if (is_hovered() && asset_.needs_download()) {
303 ui::Block *block = overlap.block();
304
305 ui::Layout &center_row = overlap.row(true);
307 center_row.ui_units_x_set(overlap.ui_units_x());
308
309 center_row.column(true);
310
311 const int overlay_width = ICON_DEFAULT_WIDTH_SCALE * 2.0f;
312 const int overlay_height = ICON_DEFAULT_HEIGHT_SCALE * 2.0f;
313 const int preview_height = tile_height(asset_view.shelf_.settings) -
314 ((asset_view.shelf_.settings.display_flag & ASSETSHELF_SHOW_NAMES) ?
315 UI_UNIT_Y :
316 0.0f);
317
318 /* Insert padding above the overlay to center it vertically. */
319 ui::uiDefBut(block,
321 "",
322 0,
323 0,
324 1,
325 std::max(0.0f, (preview_height - overlay_height + U.pixelsize) * 0.5f),
326 nullptr,
327 0,
328 0,
329 std::nullopt);
330
331 ui::Button *but = uiDefIconButO(block,
333 "ASSET_OT_asset_download",
335 ICON_DOWNLOAD,
336 0,
337 0,
338 overlay_width,
339 overlay_height,
340 std::nullopt);
343 ui::button_icon_scale_set(but, 1.5f);
345 }
346}
347
349{
350 const AssetView &asset_view = dynamic_cast<const AssetView &>(this->get_view());
351 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
352
353 if (shelf_type.draw_context_menu) {
354 shelf_type.draw_context_menu(&C, &shelf_type, &asset_, column);
355 }
356}
357
358std::optional<bool> AssetViewItem::should_be_active() const
359{
360 const AssetView &asset_view = dynamic_cast<const AssetView &>(this->get_view());
361 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
362 if (!shelf_type.get_active_asset) {
363 return {};
364 }
365 if (!asset_view.active_asset_) {
366 return false;
367 }
368 AssetWeakReference weak_ref = asset_.make_weak_reference();
369 const bool matches = *asset_view.active_asset_ == weak_ref;
370
371 return matches;
372}
373
375{
376 const AssetView &asset_view = dynamic_cast<const AssetView &>(this->get_view());
377 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
378
379 /* Don't allow activating the asset when it requires downloading. */
380 if (asset_.is_online_only()) {
381 return;
382 }
383
384 if (std::optional<wmOperatorCallParams> activate_op = create_asset_operator_params(
385 shelf_type.activate_operator, asset_))
386 {
388 &C, activate_op->optype, activate_op->opcontext, activate_op->opptr, nullptr);
389 WM_operator_properties_free(activate_op->opptr);
390 MEM_delete(activate_op->opptr);
391 }
392}
393
395{
396 const StringRefNull asset_name = asset_.get_name();
397 return fnmatch(filter_string.c_str(), asset_name.c_str(), FNM_CASEFOLD) == 0;
398}
399
400std::unique_ptr<ui::AbstractViewItemDragController> AssetViewItem::create_drag_controller() const
401{
402 const AssetView &asset_view = dynamic_cast<const AssetView &>(this->get_view());
403 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
404
405 if (!allow_asset_drag_ && shelf_type.drag_operator.empty()) {
406 return nullptr;
407 }
408 return std::make_unique<AssetDragController>(this->get_view(), asset_);
409}
410
411/* ---------------------------------------------------------------------- */
412
413static std::string filter_string_get(const AssetShelf &shelf)
414{
415 /* Copy of the filter string from #AssetShelfSettings, with extra '*' added to the beginning and
416 * end of the string, for `fnmatch()` to work. */
417 char search_string[sizeof(AssetShelfSettings::search_string) + 2];
418 BLI_strncpy_ensure_pad(search_string, shelf.settings.search_string, '*', sizeof(search_string));
419 return search_string;
420}
421
423 const AssetLibraryReference &library_ref,
424 const AssetShelf &shelf,
425 const bContext &C)
426{
427 list::storage_fetch(&library_ref, &C);
428
430 if (!library) {
431 return;
432 }
433
434 const float tile_width = shelf::tile_width(shelf.settings);
435 const float tile_height = shelf::tile_height(shelf.settings);
438
439 std::unique_ptr asset_view = std::make_unique<AssetView>(library_ref, shelf);
440 asset_view->set_catalog_filter(catalog_filter_from_shelf_settings(shelf.settings, *library));
441 asset_view->set_tile_size(tile_width, tile_height);
442
443 ui::Block *block = layout.block();
444 ui::AbstractGridView *grid_view = block_add_view(
445 *block, "asset shelf asset view", std::move(asset_view));
446 grid_view->set_context_menu_title("Asset Shelf");
447
448 ui::GridViewBuilder builder(*block);
449 builder.build_grid_view(C, *grid_view, layout, filter_string_get(shelf));
450
451 /* Show an informative label when there are no assets visible. */
452 {
453 std::optional<StringRef> empty_list_hint;
454
455 const bool has_active_catalog = shelf.settings.active_catalog_path &&
456 shelf.settings.active_catalog_path[0];
457 if (grid_view->get_item_count() == 0) {
458 empty_list_hint = has_active_catalog ? RPT_("No assets in this catalog.") :
459 RPT_("No assets found.");
460 }
461 else if (grid_view->get_item_count_filtered() == 0) {
462 empty_list_hint = has_active_catalog ? RPT_("No search results in this catalog.") :
463 RPT_("No results found.");
464 }
465
466 if (empty_list_hint) {
467 ui::Layout &row = layout.row(false);
468 /* Align with first button in asset shelf header. */
469 row.separator();
470
471 ui::Layout &column = row.column(false);
472
473 column.separator();
474 column.label(*empty_list_hint, ICON_STATUS_INFO);
475
476 std::optional<StringRef> access_filter_hint;
477 if (U.asset_access == AssetAccess::OnlyOnline) {
478 access_filter_hint = RPT_("\"Online Only\" filter is enabled.");
479 }
480 else if (U.asset_access == AssetAccess::OnlyOffline) {
481 access_filter_hint = RPT_("\"Offline Only\" filter is enabled.");
482 }
483
484 if (access_filter_hint) {
485 column.label(*access_filter_hint, ICON_BLANK1);
486 }
487 }
488 }
489}
490
491/* ---------------------------------------------------------------------- */
492/* Dragging. */
493
499
500std::optional<eWM_DragDataType> AssetDragController::get_drag_type() const
501{
502 const AssetView &asset_view = this->get_view<AssetView>();
503 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
504
505 /* Disable asset dragging, only call #AssetShelfType::drag_operator in #on_drag_start(). */
506 if (!shelf_type.drag_operator.empty()) {
507 return std::nullopt;
508 }
509 return asset_.is_local_id() ? WM_DRAG_ID : WM_DRAG_ASSET;
510}
511
513{
514 const AssetView &asset_view = this->get_view<AssetView>();
515 const AssetShelfType &shelf_type = *asset_view.shelf_.type;
516
517 if (std::optional<wmOperatorCallParams> drag_op = create_asset_operator_params(
518 shelf_type.drag_operator, asset_))
519 {
520 WM_operator_name_call_ptr(&C, drag_op->optype, drag_op->opcontext, drag_op->opptr, nullptr);
521 WM_operator_properties_free(drag_op->opptr);
522 MEM_delete(drag_op->opptr);
523
524 /* Display as active so it's clear which item is being operated on. #activate() would trigger
525 * the activation operator. We really don't want this for poses, since dragging shouldn't fully
526 * apply a pose, but trigger interactive pose blending instead.
527 *
528 * Messing with the active state could cause problems, in that case a separate highlighting
529 * feature might make sense (so e.g. dragged from assets get an outline). */
530 item.set_state_active();
531 }
532}
533
535{
536 ID *local_id = asset_.local_id();
537 if (local_id) {
538 return static_cast<void *>(local_id);
539 }
540
541 eAssetImportMethod import_method = asset_.get_import_method().value_or(ASSET_IMPORT_PACK);
542 if (U.experimental.no_data_block_packing && import_method == ASSET_IMPORT_PACK) {
543 import_method = ASSET_IMPORT_APPEND_REUSE;
544 }
545
546 AssetImportSettings import_settings{};
547 import_settings.method = import_method;
548 import_settings.use_instance_collections = false;
549
550 return WM_drag_create_asset_data(&asset_, import_settings);
551}
552
553} // namespace blender::ed::asset::shelf
Main runtime representation of an asset.
#define RPT_(msgid)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define ICON_DEFAULT_WIDTH_SCALE
#define ICON_DEFAULT_HEIGHT_SCALE
constexpr const char * c_str() const
constexpr bool is_empty() const
std::optional< eWM_DragDataType > get_drag_type() const override
AssetDragController(ui::AbstractGridView &view, asset_system::AssetRepresentation &asset)
void on_drag_start(bContext &C, ui::AbstractViewItem &item) override
void build_context_menu(bContext &C, ui::Layout &column) const override
bool should_be_filtered_visible(StringRefNull filter_string) const override
void build_grid_tile(const bContext &C, ui::Layout &layout) const override
AssetViewItem(asset_system::AssetRepresentation &asset_, StringRef identifier, StringRef label)
std::unique_ptr< ui::AbstractViewItemDragController > create_drag_controller() const override
std::optional< bool > should_be_active() const override
bool begin_filtering(const bContext &C) const override
AssetView(const AssetLibraryReference &library_ref, const AssetShelf &shelf)
void set_catalog_filter(const std::optional< asset_system::AssetCatalogFilter > &catalog_filter)
AbstractGridView & get_view() const
Definition grid_view.cc:388
bool matches(const AbstractViewItem &other) const override
Definition grid_view.cc:359
ItemT & add_item(Args &&...args)
const GridViewStyle & get_style() const
Definition grid_view.cc:97
ui::ButtonViewItem * view_item_button() const
ui::ButtonViewItem * view_item_but_
void set_context_menu_title(const std::string &title)
void build_grid_view(const bContext &C, AbstractGridView &grid_view, Layout &layout, std::optional< StringRef > search_string={})
Definition grid_view.cc:612
PreviewGridItem(StringRef identifier, StringRef label, int preview_icon_id)
Definition grid_view.cc:640
void build_grid_tile_button(Layout &layout, BIFIconID override_preview_icon_id=ICON_NONE) const
Definition grid_view.cc:645
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
void iterate(const AssetLibraryReference &library_reference, AssetListIterFn fn)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
bool is_loaded(const AssetLibraryReference *library_reference)
static std::optional< asset_system::AssetCatalogFilter > catalog_filter_from_shelf_settings(const AssetShelfSettings &shelf_settings, const asset_system::AssetLibrary &library)
int tile_height(const AssetShelfSettings &settings)
int tile_width(const AssetShelfSettings &settings)
void build_asset_view(ui::Layout &layout, const AssetLibraryReference &library_ref, const AssetShelf &shelf, const bContext &C)
static std::string filter_string_get(const AssetShelf &shelf)
static std::optional< wmOperatorCallParams > create_asset_operator_params(const StringRefNull op_name, const asset_system::AssetRepresentation &asset)
bool type_asset_poll(const AssetShelfType &shelf_type, const asset_system::AssetRepresentation &asset)
void asset_tooltip(const asset_system::AssetRepresentation &asset, ui::TooltipData &tip, const bool include_name)
void operator_asset_reference_props_set(const asset_system::AssetRepresentation &asset, PointerRNA &ptr)
BIFIconID asset_preview_or_icon(const asset_system::AssetRepresentation &asset)
bool textbutton_activate_rna(const bContext *C, ARegion *region, const void *rna_poin_data, const char *rna_prop_id)
PointerRNA * button_operator_ptr_ensure(Button *but)
void button_icon_scale_set(Button *but, float scale)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
void button_pushbutton_draw_as_overlay_set(Button *but, bool value)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
wmDragAsset * WM_drag_create_asset_data(const asset_system::AssetRepresentation *asset, const AssetImportSettings &import_settings)
void WM_operator_properties_free(PointerRNA *ptr)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
@ WM_DRAG_ASSET
Definition WM_types.hh:1294
char char * BLI_strncpy_ensure_pad(char *__restrict dst, const char *__restrict src, char pad, size_t dst_maxncpy) ATTR_NONNULL(1
@ ASSET_IMPORT_APPEND_REUSE
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
@ ASSET_SHELF_TYPE_FLAG_ACTIVATE_FOR_CONTEXT_MENU
@ ASSET_SHELF_TYPE_FLAG_NO_ASSET_DRAG
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
AssetShelfSettings_DisplayFlag display_flag
std::string activate_operator
const AssetWeakReference *(*) get_active_asset(const AssetShelfType *shelf_type)
void(*) draw_context_menu(const bContext *C, const AssetShelfType *shelf_type, const asset_system::AssetRepresentation *asset, ui::Layout &layout)
struct AssetShelfType * type
AssetShelfSettings settings
ListBaseT< ARegion > regionbase
wmOperatorType * optype
void alignment_set(LayoutAlign alignment)
void label(StringRef name, int icon)
void ui_units_x_set(float width)
void ui_units_y_set(float height)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)