Blender V5.3
asset_catalog_tree_view.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_space_types.h"
10
11#include "AS_asset_catalog.hh"
13#include "AS_asset_library.hh"
14
15#include "BKE_asset.hh"
16#include "BKE_asset_edit.hh"
17#include "BKE_context.hh"
18#include "BKE_report.hh"
19
20#include "BLI_assert.hh"
21#include "BLI_listbase.hh"
22#include "BLI_string_ref.hh"
23
24#include "BLT_translation.hh"
25
26#include "ED_asset.hh"
27#include "ED_asset_catalog.hh"
28#include "ED_asset_list.hh"
29#include "ED_fileselect.hh"
30#include "ED_undo.hh"
31
32#include "RNA_access.hh"
33
35#include "UI_resources.hh"
36#include "UI_tree_view.hh"
37
38#include "WM_api.hh"
39#include "WM_types.hh"
40
41#include "file_intern.hh"
42#include "filelist.hh"
43
44#include <fmt/format.h>
45
46namespace blender {
47
48using namespace blender::asset_system;
49
51
53
55 asset_system::AssetLibrary *asset_library_;
57 std::shared_ptr<const asset_system::AssetCatalogTree> catalog_tree_;
58 FileAssetSelectParams *params_;
59 SpaceFile &space_file_;
60
64
65 public:
68 SpaceFile &space_file);
69
70 void build_tree() override;
71
72 void activate_catalog_by_id(CatalogID catalog_id);
73
74 private:
75 ui::BasicTreeViewItem &build_catalog_items_recursive(ui::TreeViewOrItem &view_parent_item,
76 const AssetCatalogTreeItem &catalog);
77
78 AssetCatalogTreeViewAllItem &add_all_item();
79 void add_unassigned_item();
80 bool is_active_catalog(CatalogID catalog_id) const;
81};
82
83/* ---------------------------------------------------------------------- */
84
87 const AssetCatalogTreeItem &catalog_item_;
88
89 public:
91
92 void on_activate(bContext &C) override;
93
94 void build_row(ui::Layout &row) override;
95 void build_context_menu(bContext &C, ui::Layout &column) const override;
96
97 bool supports_renaming() const override;
98 bool rename(const bContext &C, StringRefNull new_name) override;
99 void delete_item(bContext *C) override;
100
102 std::unique_ptr<ui::AbstractViewItemDragController> create_drag_controller() const override;
104 std::unique_ptr<ui::TreeViewItemDropTarget> create_drop_target() override;
105};
106
108 const AssetCatalogTreeItem &catalog_item_;
109
110 public:
112 const AssetCatalogTreeItem &catalog_item);
113
114 std::optional<eWM_DragDataType> get_drag_type() const override;
115 void *create_drag_data() const override;
116 void on_drag_start(bContext &C, ui::AbstractViewItem &item) override;
117};
118
120 const AssetCatalogTreeItem &catalog_item_;
121
122 public:
124
125 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override;
126 std::string drop_tooltip(const ui::DragInfo &drag_info) const override;
127 bool on_drop(bContext *C, const ui::DragInfo &drag_info) const override;
128
130
131 static AssetCatalog *get_drag_catalog(const wmDrag &drag,
132 const asset_system::AssetLibrary &asset_library);
133 static bool has_droppable_asset(const wmDrag &drag, const char **r_disabled_hint);
134 static bool can_modify_catalogs(const asset_system::AssetLibrary &asset_library,
135 const char **r_disabled_hint);
137 const AssetCatalogTreeView &tree_view,
138 const wmDrag &drag,
139 CatalogID catalog_id,
140 StringRefNull simple_name = "");
145 const wmDrag &drag,
146 AssetCatalogTreeView &tree_view,
147 const std::optional<CatalogID> drop_catalog_id = std::nullopt);
148
149 private:
150 std::string drop_tooltip_asset_list(const wmDrag &drag) const;
151 std::string drop_tooltip_asset_catalog(const wmDrag &drag) const;
152};
153
159 using BasicTreeViewItem::BasicTreeViewItem;
160
161 void build_row(ui::Layout &row) override;
162
163 struct DropTarget : public ui::TreeViewItemDropTarget {
164 DropTarget(AssetCatalogTreeViewAllItem &item);
165
166 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override;
167 std::string drop_tooltip(const ui::DragInfo &drag_info) const override;
168 bool on_drop(bContext *C, const ui::DragInfo &drag_info) const override;
169 };
170
171 std::unique_ptr<ui::TreeViewItemDropTarget> create_drop_target() override;
172};
173
175 using BasicTreeViewItem::BasicTreeViewItem;
176
177 struct DropTarget : public ui::TreeViewItemDropTarget {
178 DropTarget(AssetCatalogTreeViewUnassignedItem &item);
179
180 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override;
181 std::string drop_tooltip(const ui::DragInfo &drag_info) const override;
182 bool on_drop(bContext *C, const ui::DragInfo &drag_info) const override;
183 };
184
185 std::unique_ptr<ui::TreeViewItemDropTarget> create_drop_target() override;
186};
187
188/* ---------------------------------------------------------------------- */
189
191 FileAssetSelectParams *params,
192 SpaceFile &space_file)
193 : asset_library_(library), params_(params), space_file_(space_file)
194{
195 if (library) {
196 /* Take shared ownership of the catalog service so it can't be freed by a concurrent catalog
197 * reload job (running on a separate thread) while we're reading the catalog tree from it. */
198 std::shared_ptr<asset_system::AssetCatalogService> catalog_service =
199 library->catalog_service_ptr();
200 catalog_tree_ = catalog_service ? catalog_service->catalog_tree() : nullptr;
201 }
202 else {
203 catalog_tree_ = nullptr;
204 }
205}
206
208{
209 AssetCatalogTreeViewAllItem &all_item = add_all_item();
210 all_item.uncollapse_by_default();
211
212 if (catalog_tree_) {
213 /* Pass the "All" item on as parent of the actual catalog items. */
214 catalog_tree_->foreach_root_item([this, &all_item](const AssetCatalogTreeItem &item) {
215 build_catalog_items_recursive(all_item, item);
216 });
217 }
218
219 /* Hide the unassigned catalog for the Essentials library, it should be empty in releases. Still
220 * show it when "Unassigned" _is_ active. */
221 const bool is_essentials = params_->asset_library_ref.type == ASSET_LIBRARY_ESSENTIALS;
222 const bool shows_unassigned_item = params_->asset_catalog_visibility ==
224 if (!is_essentials || shows_unassigned_item) {
225 add_unassigned_item();
226 }
227}
228
229ui::BasicTreeViewItem &AssetCatalogTreeView::build_catalog_items_recursive(
230 ui::TreeViewOrItem &view_parent_item, const AssetCatalogTreeItem &catalog)
231{
232 ui::BasicTreeViewItem &view_item = view_parent_item.add_tree_item<AssetCatalogTreeViewItem>(
233 catalog);
234 view_item.set_is_active_fn(
235 [this, &catalog]() { return is_active_catalog(catalog.get_catalog_id()); });
236
237 catalog.foreach_child([&view_item, this](const AssetCatalogTreeItem &child) {
238 build_catalog_items_recursive(view_item, child);
239 });
240 return view_item;
241}
242
243AssetCatalogTreeViewAllItem &AssetCatalogTreeView::add_all_item()
244{
245 FileAssetSelectParams *params = params_;
246
248 item.set_on_activate_fn([params](bContext & /*C*/, ui::BasicTreeViewItem & /*item*/) {
251 });
252 item.set_is_active_fn(
253 [params]() { return params->asset_catalog_visibility == FILE_SHOW_ASSETS_ALL_CATALOGS; });
254 return item;
255}
256
257void AssetCatalogTreeView::add_unassigned_item()
258{
259 FileAssetSelectParams *params = params_;
260
261 AssetCatalogTreeViewUnassignedItem &item = add_tree_item<AssetCatalogTreeViewUnassignedItem>(
262 IFACE_("Unassigned"), ICON_FILE_HIDDEN);
263
264 item.set_on_activate_fn([params](bContext & /*C*/, ui::BasicTreeViewItem & /*item*/) {
267 });
268 item.set_is_active_fn(
269 [params]() { return params->asset_catalog_visibility == FILE_SHOW_ASSETS_WITHOUT_CATALOG; });
270}
271
273{
274 params_->asset_catalog_visibility = FILE_SHOW_ASSETS_FROM_CATALOG;
275 params_->catalog_id = catalog_id;
277}
278
279bool AssetCatalogTreeView::is_active_catalog(CatalogID catalog_id) const
280{
282 (params_->catalog_id == catalog_id);
283}
284
285/* ---------------------------------------------------------------------- */
286
288 : BasicTreeViewItem(catalog_item.get_name()), catalog_item_(catalog_item)
289{
290}
291
293{
294 AssetCatalogTreeView &tree_view = static_cast<AssetCatalogTreeView &>(get_tree_view());
295 tree_view.activate_catalog_by_id(catalog_item_.get_catalog_id());
296}
297
299{
300 /* Show "*" to the left for consistency with unsaved files in the title bar. */
301 const std::string label_override = catalog_item_.has_unsaved_changes() ? ("* " + label_) :
302 label_;
303 this->add_label(row, label_override);
304
305 if (!is_hovered()) {
306 return;
307 }
308
309 ui::ButtonViewItem *view_item_but = view_item_button();
310 PointerRNA *props;
311
312 props = button_extra_operator_icon_add(reinterpret_cast<ui::Button *>(view_item_but),
313 "ASSET_OT_catalog_new",
315 ICON_ADD);
316 RNA_string_set(props, "parent_path", catalog_item_.catalog_path().c_str());
317}
318
320{
321 PointerRNA props;
322
323 props = column.op("ASSET_OT_catalog_new",
324 IFACE_("New Catalog"),
325 ICON_NONE,
328 RNA_string_set(&props, "parent_path", catalog_item_.catalog_path().c_str());
329
330 props = column.op("ASSET_OT_catalog_delete",
331 IFACE_("Delete Catalog"),
332 ICON_NONE,
335 RNA_string_set(&props, "catalog_id", catalog_item_.get_catalog_id().str().c_str());
336 column.op("UI_OT_view_item_rename", IFACE_("Rename"), ICON_NONE);
337
338 /* Doesn't actually exist right now, but could be defined in Python. Reason that this isn't done
339 * in Python yet is that catalogs are not exposed in BPY, and we'd somehow pass the clicked on
340 * catalog to the menu draw callback (via context probably). */
341 MenuType *mt = WM_menutype_find("ASSETBROWSER_MT_catalog_context_menu", true);
342 if (!mt) {
343 return;
344 }
345 ui::menutype_draw(&C, mt, &column);
346}
347
349{
350 const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
351 this->get_tree_view());
352 return !asset::catalogs_read_only(*tree_view.asset_library_);
353}
354
356{
357 /* Important to keep state. */
358 BasicTreeViewItem::rename(C, new_name);
359
360 const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
361 this->get_tree_view());
362 asset::catalog_rename(tree_view.asset_library_, catalog_item_.get_catalog_id(), new_name);
363 return true;
364}
365
367{
368 const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
369 this->get_tree_view());
370 ed::asset::catalog_remove(tree_view.asset_library_, catalog_item_.get_catalog_id());
371}
372
373std::unique_ptr<ui::TreeViewItemDropTarget> AssetCatalogTreeViewItem::create_drop_target()
374{
375 return std::make_unique<AssetCatalogDropTarget>(*this, catalog_item_);
376}
377
378std::unique_ptr<ui::AbstractViewItemDragController> AssetCatalogTreeViewItem::
380{
381 return std::make_unique<AssetCatalogDragController>(
382 static_cast<AssetCatalogTreeView &>(this->get_tree_view()), catalog_item_);
383}
384
385/* ---------------------------------------------------------------------- */
386
388 const AssetCatalogTreeItem &catalog_item)
389 : ui::TreeViewItemDropTarget(item), catalog_item_(catalog_item)
390{
391}
392
393bool AssetCatalogDropTarget::can_drop(const wmDrag &drag, const char **r_disabled_hint) const
394{
395 if (drag.type == WM_DRAG_ASSET_CATALOG) {
397 if (!can_modify_catalogs(library, r_disabled_hint)) {
398 return false;
399 }
400
401 const AssetCatalog *drag_catalog = get_drag_catalog(drag, library);
402 /* NOTE: Technically it's not an issue to allow this (the catalog will just receive a new
403 * path and the catalog system will generate missing parents from the path). But it does
404 * appear broken to users, so disabling entirely. */
405 if (catalog_item_.catalog_path().is_contained_in(drag_catalog->path)) {
406 *r_disabled_hint = RPT_("Catalog cannot be dropped into itself");
407 return false;
408 }
409 if (catalog_item_.catalog_path() == drag_catalog->path.parent()) {
410 *r_disabled_hint = RPT_("Catalog is already placed inside this catalog");
411 return false;
412 }
413 return true;
414 }
415
416 if (drag.type == WM_DRAG_ASSET_LIST && has_droppable_asset(drag, r_disabled_hint)) {
417 return true;
418 }
419 return false;
420}
421
422std::string AssetCatalogDropTarget::drop_tooltip(const ui::DragInfo &drag_info) const
423{
424 if (drag_info.drag_data.type == WM_DRAG_ASSET_CATALOG) {
425 return this->drop_tooltip_asset_catalog(drag_info.drag_data);
426 }
427 return this->drop_tooltip_asset_list(drag_info.drag_data);
428}
429
430std::string AssetCatalogDropTarget::drop_tooltip_asset_catalog(const wmDrag &drag) const
431{
433 const AssetCatalog *src_catalog = get_drag_catalog(drag, get_asset_library());
434
435 return fmt::format(fmt::runtime(TIP_("Move catalog {} into {}")),
436 src_catalog->path.name(),
437 catalog_item_.get_name());
438}
439
440std::string AssetCatalogDropTarget::drop_tooltip_asset_list(const wmDrag &drag) const
441{
443
444 const ListBaseT<wmDragAssetListItem> *asset_drags = WM_drag_asset_list_get(&drag);
445 const bool is_multiple_assets = !asset_drags->is_single();
446
447 /* Don't try to be smart by dynamically adding the 's' for the plural. Just makes translation
448 * harder, so use full literals. */
449 std::string basic_tip = is_multiple_assets ? TIP_("Move assets to catalog") :
450 TIP_("Move asset to catalog");
451
452 basic_tip += ": " + catalog_item_.get_name();
453
454 /* Display the full catalog path, but only if it's not exactly the same as the already shown name
455 * (i.e. not a root level catalog with no parent). */
456 if (catalog_item_.get_name() != catalog_item_.catalog_path().str()) {
457 basic_tip += " (" + catalog_item_.catalog_path().str() + ")";
458 }
459
460 return basic_tip;
461}
462
464{
465 if (drag_info.drag_data.type == WM_DRAG_ASSET_CATALOG) {
467 this->get_view<AssetCatalogTreeView>(),
468 catalog_item_.get_catalog_id());
469 }
472 drag_info.drag_data,
473 catalog_item_.get_catalog_id(),
474 catalog_item_.get_simple_name());
475}
476
478 const wmDrag &drag,
479 AssetCatalogTreeView &tree_view,
480 const std::optional<CatalogID> drop_catalog_id)
481{
484 asset::catalog_move(tree_view.asset_library_, catalog_drag->drag_catalog_id, drop_catalog_id);
485 tree_view.activate_catalog_by_id(catalog_drag->drag_catalog_id);
486
488 return true;
489}
490
492 const AssetCatalogTreeView &tree_view,
493 const wmDrag &drag,
494 CatalogID catalog_id,
495 StringRefNull simple_name)
496{
498 Main *bmain = CTX_data_main(C);
499 ReportList *reports = CTX_wm_reports(C);
501
502 const ListBaseT<wmDragAssetListItem> *asset_drags = WM_drag_asset_list_get(&drag);
503 if (!asset_drags) {
504 return false;
505 }
506
507 bool is_local_id_modified = false;
508 for (wmDragAssetListItem &asset_item : *asset_drags) {
509 ID *asset_id = nullptr;
510
511 if (asset_item.is_external) {
512 /* Load the external asset. Note that asset_edit_id_from_weak_reference() can still return a
513 * local datablock (if it was already used in the local file), which is why the remainder of
514 * this loop iteration just uses `ID_IS_LINKED()` and not `asset_item.is_external`. */
515 const asset_system::AssetRepresentation *asset = asset_item.asset_data.external_info->asset;
516 const AssetWeakReference asset_reference = asset->make_weak_reference();
518 *bmain, asset->get_id_type(), asset_reference);
519 if (asset_id == nullptr) {
520 continue;
521 }
522
523 if (ID_IS_LINKED(asset_id) && (!bke::asset_edit_id_is_editable(*asset_id) ||
525 {
526 continue;
527 }
528 }
529 else {
530 asset_id = asset_item.asset_data.local_id;
531 BLI_assert_msg(!ID_IS_LINKED(asset_id), "Local assets are not expected to be linked");
532 }
533
534 /* Set the catalog. */
535 BLI_assert(asset_id != nullptr);
536 BKE_asset_metadata_catalog_id_set(asset_id->asset_data, catalog_id, simple_name.c_str());
537
538 /* Ensure the asset system also sees the new metadata, without having to reload the planet.
539 * It's a little hacky, but when reloading the planet, the asset browser also reloads the
540 * catalogs, which in turn collapses the entire tree except for the active catalog, which means
541 * that after dragging into another catalog, that destination catalog may suddenly be hidden.
542 * And that's nasty. */
543 if (asset_item.is_external) {
544 const asset_system::AssetRepresentation *asset = asset_item.asset_data.external_info->asset;
545 AssetMetaData &cached_metadata = asset->get_metadata();
546 BKE_asset_metadata_catalog_id_set(&cached_metadata, catalog_id, simple_name.c_str());
547 }
548
549 if (ID_IS_LINKED(asset_id)) {
550 /* Saving is not needed for local assets. */
551 bke::asset_edit_id_save(*bmain, *asset_id, *reports);
552 }
553 else {
554 is_local_id_modified = true;
555 }
556 }
557
558 /* Trigger re-run of filtering to update visible assets. */
559 filelist_tag_needs_filtering(tree_view.space_file_.files);
563
564 if (is_local_id_modified) {
565 /* Only create undo-step for local actions. Undoing external files isn't supported. */
566 ED_undo_push(C, "Assign Asset Catalog");
567 }
568 return true;
569}
570
572 const wmDrag &drag, const asset_system::AssetLibrary &asset_library)
573{
574 if (drag.type != WM_DRAG_ASSET_CATALOG) {
575 return nullptr;
576 }
577 const AssetCatalogService &catalog_service = asset_library.catalog_service();
578 const wmDragAssetCatalog *catalog_drag = WM_drag_get_asset_catalog_data(&drag);
579
580 return catalog_service.find_catalog(catalog_drag->drag_catalog_id);
581}
582
583bool AssetCatalogDropTarget::has_droppable_asset(const wmDrag &drag, const char **r_disabled_hint)
584{
585 const ListBaseT<wmDragAssetListItem> *asset_drags = WM_drag_asset_list_get(&drag);
586
587 /* There needs to be at least one asset from the current file. */
588 for (const wmDragAssetListItem &asset_item : *asset_drags) {
589 if (!asset_item.is_external) {
590 return true;
591 }
592 BLI_assert(asset_item.asset_data.external_info);
593 BLI_assert(asset_item.asset_data.external_info->asset);
594 if (asset_item.asset_data.external_info->asset->is_potentially_editable_asset_blend()) {
595 return true;
596 }
597 }
598
599 *r_disabled_hint = RPT_(
600 "Only assets from the current file or asset system files (.asset.blend) can be moved "
601 "between catalogs");
602 return false;
603}
604
606 const char **r_disabled_hint)
607{
609 *r_disabled_hint = RPT_("Catalogs cannot be edited in this asset library");
610 return false;
611 }
612 return true;
613}
614
619
620/* ---------------------------------------------------------------------- */
621
623 const AssetCatalogTreeItem &catalog_item)
624 : ui::AbstractViewItemDragController(tree_view), catalog_item_(catalog_item)
625{
626}
627
628std::optional<eWM_DragDataType> AssetCatalogDragController::get_drag_type() const
629{
631}
632
634{
635 wmDragAssetCatalog *drag_catalog = MEM_new<wmDragAssetCatalog>(__func__);
636 drag_catalog->drag_catalog_id = catalog_item_.get_catalog_id();
637 return drag_catalog;
638}
639
641{
643 tree_view_.activate_catalog_by_id(catalog_item_.get_catalog_id());
644}
645
646/* ---------------------------------------------------------------------- */
647
648void AssetCatalogTreeViewAllItem::build_row(ui::Layout &row)
649{
651
652 PointerRNA *props;
653
654 button_extra_operator_icon_add(reinterpret_cast<ui::Button *>(this->view_item_button()),
655 "ASSET_OT_catalogs_save",
657 ICON_FILE_TICK);
658
659 props = button_extra_operator_icon_add(reinterpret_cast<ui::Button *>(this->view_item_button()),
660 "ASSET_OT_catalog_new",
662 ICON_ADD);
663 /* No parent path to use the root level. */
664 RNA_string_set(props, "parent_path", nullptr);
665}
666
667std::unique_ptr<ui::TreeViewItemDropTarget> AssetCatalogTreeViewAllItem::create_drop_target()
668{
669 return std::make_unique<AssetCatalogTreeViewAllItem::DropTarget>(*this);
670}
671
672AssetCatalogTreeViewAllItem::DropTarget::DropTarget(AssetCatalogTreeViewAllItem &item)
673 : ui::TreeViewItemDropTarget(item)
674{
675}
676
677bool AssetCatalogTreeViewAllItem::DropTarget::can_drop(const wmDrag &drag,
678 const char **r_disabled_hint) const
679{
680 if (drag.type != WM_DRAG_ASSET_CATALOG) {
681 return false;
682 }
683 asset_system::AssetLibrary &library = *this->get_view<AssetCatalogTreeView>().asset_library_;
684 if (!AssetCatalogDropTarget::can_modify_catalogs(library, r_disabled_hint)) {
685 return false;
686 }
687
688 const AssetCatalog *drag_catalog = AssetCatalogDropTarget::get_drag_catalog(drag, library);
689 if (drag_catalog->path.parent() == "") {
690 *r_disabled_hint = RPT_("Catalog is already placed at the highest level");
691 return false;
692 }
693
694 return true;
695}
696
697std::string AssetCatalogTreeViewAllItem::DropTarget::drop_tooltip(
698 const ui::DragInfo &drag_info) const
699{
701 const AssetCatalog *drag_catalog = AssetCatalogDropTarget::get_drag_catalog(
702 drag_info.drag_data, *this->get_view<AssetCatalogTreeView>().asset_library_);
703
704 return fmt::format(fmt::runtime(TIP_("Move catalog {} to the top level of the tree")),
705 drag_catalog->path.name());
706}
707
708bool AssetCatalogTreeViewAllItem::DropTarget::on_drop(bContext * /*C*/,
709 const ui::DragInfo &drag_info) const
710{
713 drag_info.drag_data,
714 this->get_view<AssetCatalogTreeView>(),
715 /* No value to drop into the root level. */
716 std::nullopt);
717}
718
719/* ---------------------------------------------------------------------- */
720
721std::unique_ptr<ui::TreeViewItemDropTarget> AssetCatalogTreeViewUnassignedItem::
723{
724 return std::make_unique<AssetCatalogTreeViewUnassignedItem::DropTarget>(*this);
725}
726
727AssetCatalogTreeViewUnassignedItem::DropTarget::DropTarget(
728 AssetCatalogTreeViewUnassignedItem &item)
729 : ui::TreeViewItemDropTarget(item)
730{
731}
732
733bool AssetCatalogTreeViewUnassignedItem::DropTarget::can_drop(const wmDrag &drag,
734 const char **r_disabled_hint) const
735{
736 if (drag.type != WM_DRAG_ASSET_LIST) {
737 return false;
738 }
739 return AssetCatalogDropTarget::has_droppable_asset(drag, r_disabled_hint);
740}
741
742std::string AssetCatalogTreeViewUnassignedItem::DropTarget::drop_tooltip(
743 const ui::DragInfo &drag_info) const
744{
745 const ListBaseT<wmDragAssetListItem> *asset_drags = WM_drag_asset_list_get(&drag_info.drag_data);
746 const bool is_multiple_assets = !asset_drags->is_single();
747
748 return is_multiple_assets ? TIP_("Move assets out of any catalog") :
749 TIP_("Move asset out of any catalog");
750}
751
752bool AssetCatalogTreeViewUnassignedItem::DropTarget::on_drop(bContext *C,
753 const ui::DragInfo &drag_info) const
754{
755 /* Assign to nil catalog ID. */
757 C, this->get_view<AssetCatalogTreeView>(), drag_info.drag_data, CatalogID{});
758}
759
760/* ---------------------------------------------------------------------- */
761
769
771{
772 return MEM_new<AssetCatalogFilterSettings>(__func__);
773}
774
776{
777 MEM_delete(*filter_settings);
778 *filter_settings = nullptr;
779}
780
782 AssetCatalogFilterSettings *filter_settings,
784 const bUUID &catalog_id)
785{
786 bool needs_update = false;
787
788 if (filter_settings->asset_catalog_visibility != catalog_visibility) {
789 filter_settings->asset_catalog_visibility = catalog_visibility;
790 needs_update = true;
791 }
792
794 !BLI_uuid_equal(filter_settings->asset_catalog_id, catalog_id))
795 {
796 filter_settings->asset_catalog_id = catalog_id;
797 needs_update = true;
798 }
799
800 return needs_update;
801}
802
804 const asset_system::AssetLibrary *asset_library)
805{
806 const AssetCatalogService &catalog_service = asset_library->catalog_service();
807
809 filter_settings->catalog_filter = std::make_unique<AssetCatalogFilter>(
810 catalog_service.create_catalog_filter(filter_settings->asset_catalog_id));
811 }
812}
813
815 const AssetCatalogFilterSettings *filter_settings, const AssetMetaData *asset_data)
816{
817 switch (filter_settings->asset_catalog_visibility) {
819 return !filter_settings->catalog_filter->is_known(asset_data->catalog_id);
821 return filter_settings->catalog_filter->contains(asset_data->catalog_id);
823 /* All asset files should be visible. */
824 return true;
825 }
826
828 return false;
829}
830
831/* ---------------------------------------------------------------------- */
832
834 asset_system::AssetLibrary *asset_library,
835 ui::Layout &layout,
836 SpaceFile *space_file,
837 FileAssetSelectParams *params)
838{
839 ui::Block *block = layout.block();
840
841 ui::block_layout_set_current(block, &layout);
842
843 ui::AbstractTreeView *tree_view = block_add_view(
844 *block,
845 "asset catalog tree view",
846 std::make_unique<ed::asset_browser::AssetCatalogTreeView>(
847 asset_library, params, *space_file));
848 tree_view->set_context_menu_title("Catalog");
849 ui::TreeViewBuilder::build_tree_view(*C, *tree_view, layout);
850}
851
852} // namespace ed::asset_browser
853
854} // namespace blender
#define RPT_(msgid)
#define TIP_(msgid)
#define IFACE_(msgid)
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ND_SPACE_ASSET_PARAMS
Definition WM_types.hh:568
#define NC_ASSET
Definition WM_types.hh:417
#define ND_ASSET_CATALOGS
Definition WM_types.hh:599
#define NC_SPACE
Definition WM_types.hh:405
#define ND_SPACE_FILE_LIST
Definition WM_types.hh:567
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define BLI_assert_msg(a, msg)
constexpr const char * c_str() const
AssetCatalogFilter create_catalog_filter(CatalogID active_catalog_id) const
AssetCatalog * find_catalog(CatalogID catalog_id) const
void foreach_child(ItemIterFn callback) const
AssetCatalogService & catalog_service() const
std::optional< eWM_DragDataType > get_drag_type() const override
void on_drag_start(bContext &C, ui::AbstractViewItem &item) override
AssetCatalogDragController(AssetCatalogTreeView &tree_view, const AssetCatalogTreeItem &catalog_item)
bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override
static bool drop_asset_catalog_into_catalog(const wmDrag &drag, AssetCatalogTreeView &tree_view, const std::optional< CatalogID > drop_catalog_id=std::nullopt)
static AssetCatalog * get_drag_catalog(const wmDrag &drag, const asset_system::AssetLibrary &asset_library)
static bool has_droppable_asset(const wmDrag &drag, const char **r_disabled_hint)
static bool drop_assets_into_catalog(bContext *C, const AssetCatalogTreeView &tree_view, const wmDrag &drag, CatalogID catalog_id, StringRefNull simple_name="")
std::string drop_tooltip(const ui::DragInfo &drag_info) const override
static bool can_modify_catalogs(const asset_system::AssetLibrary &asset_library, const char **r_disabled_hint)
bool on_drop(bContext *C, const ui::DragInfo &drag_info) const override
AssetCatalogDropTarget(AssetCatalogTreeViewItem &item, const AssetCatalogTreeItem &catalog_item)
eFileSel_Params_AssetCatalogVisibility asset_catalog_visibility
void build_context_menu(bContext &C, ui::Layout &column) const override
AssetCatalogTreeViewItem(const AssetCatalogTreeItem &catalog_item)
bool rename(const bContext &C, StringRefNull new_name) override
std::unique_ptr< ui::AbstractViewItemDragController > create_drag_controller() const override
std::unique_ptr< ui::TreeViewItemDropTarget > create_drop_target() override
AssetCatalogTreeView(asset_system::AssetLibrary *library, FileAssetSelectParams *params, SpaceFile &space_file)
AbstractTreeView & get_tree_view() const
Definition tree_view.cc:775
virtual std::unique_ptr< TreeViewItemDropTarget > create_drop_target()
Definition tree_view.cc:765
ui::ButtonViewItem * view_item_button() const
void set_context_menu_title(const std::string &title)
void add_label(Layout &layout, StringRefNull label_override="")
BasicTreeViewItem(StringRef label, BIFIconID icon=ICON_NONE)
void set_is_active_fn(IsActiveFn is_active_fn)
void build_row(Layout &row) override
static void build_tree_view(const bContext &C, AbstractTreeView &tree_view, Layout &layout, bool add_box=true)
ItemT & add_tree_item(Args &&...args)
TreeViewItemDropTarget(AbstractTreeViewItem &view_item, DropBehavior behavior=DropBehavior::Insert)
Definition tree_view.cc:557
ID * asset_edit_id_from_weak_reference(Main &global_main, ID_Type id_type, const AssetWeakReference &weak_ref)
bool asset_edit_id_save(Main &global_main, ID &id, ReportList &reports)
bool asset_edit_id_is_writable(const ID &id)
bool asset_edit_id_is_editable(const ID &id)
void file_ensure_updated_catalog_filter_data(AssetCatalogFilterSettings *filter_settings, const asset_system::AssetLibrary *asset_library)
bool file_set_asset_catalog_filter_settings(AssetCatalogFilterSettings *filter_settings, eFileSel_Params_AssetCatalogVisibility catalog_visibility, const bUUID &catalog_id)
void file_create_asset_catalog_tree_view_in_layout(const bContext *C, asset_system::AssetLibrary *asset_library, ui::Layout &layout, SpaceFile *space_file, FileAssetSelectParams *params)
AssetCatalogFilterSettings * file_create_asset_catalog_filter_settings()
bool file_is_asset_visible_in_catalog_filter_settings(const AssetCatalogFilterSettings *filter_settings, const AssetMetaData *asset_data)
void file_delete_asset_catalog_filter_settings(AssetCatalogFilterSettings **filter_settings)
void catalog_remove(asset_system::AssetLibrary *library, const asset_system::CatalogID &catalog_id)
bool catalogs_read_only(const asset_system::AssetLibrary &library)
void catalog_move(asset_system::AssetLibrary *library, asset_system::CatalogID src_catalog_id, std::optional< asset_system::CatalogID > dst_parent_catalog_id=std::nullopt)
void catalog_rename(asset_system::AssetLibrary *library, asset_system::CatalogID catalog_id, StringRefNull new_name)
TreeViewItemContainer TreeViewOrItem
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
void block_layout_set_current(Block *block, Layout *layout)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const ListBaseT< wmDragAssetListItem > * WM_drag_asset_list_get(const wmDrag *drag)
void filelist_tag_needs_filtering(FileList *filelist)
bool BLI_uuid_equal(bUUID uuid1, bUUID uuid2)
Definition uuid.cc:77
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
@ WM_DRAG_ASSET_CATALOG
Definition WM_types.hh:1314
@ WM_DRAG_ASSET_LIST
Definition WM_types.hh:1300
void BKE_asset_metadata_catalog_id_set(AssetMetaData *asset_data, bUUID catalog_id, const char *catalog_simple_name)
Definition asset.cc:164
void file_select_deselect_all(SpaceFile *sfile, eDirEntry_SelectFlag flag)
Definition filesel.cc:1232
@ ASSET_LIBRARY_ESSENTIALS
wmDragAssetCatalog * WM_drag_get_asset_catalog_data(const wmDrag *drag)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
eFileSel_Params_AssetCatalogVisibility
@ FILE_SHOW_ASSETS_FROM_CATALOG
@ FILE_SHOW_ASSETS_WITHOUT_CATALOG
@ FILE_SHOW_ASSETS_ALL_CATALOGS
Main * CTX_data_main(const bContext *C)
ReportList * CTX_wm_reports(const bContext *C)
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...
eFileSel_Params_AssetCatalogVisibility asset_catalog_visibility
struct AssetMetaData * asset_data
Definition DNA_ID.h:424
struct FileList * files
Universally Unique Identifier according to RFC4122.
const wmDrag & drag_data
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
eWM_DragDataType type
Definition WM_types.hh:1424