[Bf-blender-cvs] [aae5f15238f] master: Asset Browser: Support dragging catalogs to move them in the hierarchy

Julian Eisel noreply at git.blender.org
Wed Oct 27 14:57:48 CEST 2021


Commit: aae5f15238f73fcac762a1690e052b86fad23be1
Author: Julian Eisel
Date:   Wed Oct 27 14:50:48 2021 +0200
Branches: master
https://developer.blender.org/rBaae5f15238f73fcac762a1690e052b86fad23be1

Asset Browser: Support dragging catalogs to move them in the hierarchy

Uses the additions to the UI tree-view API from the previous commit to
enable drag & drop of asset catalogs. The catalogs will be moved in the
tree including children.
A remaining issue is that a catalog with children will always be
collapsed when dropping. I need to find a way to fix that in the
tree-view API.

There are a few improvements I can think of for the tree-item drag &
drop support, but time for these is too short. These can be done as
normal cleanups at some point.

===================================================================

M	source/blender/editors/asset/ED_asset_catalog.hh
M	source/blender/editors/asset/intern/asset_catalog.cc
M	source/blender/editors/space_file/asset_catalog_tree_view.cc
M	source/blender/windowmanager/WM_api.h
M	source/blender/windowmanager/WM_types.h
M	source/blender/windowmanager/intern/wm_dragdrop.c

===================================================================

diff --git a/source/blender/editors/asset/ED_asset_catalog.hh b/source/blender/editors/asset/ED_asset_catalog.hh
index 8b8fc4d3574..8da8fc0d6c9 100644
--- a/source/blender/editors/asset/ED_asset_catalog.hh
+++ b/source/blender/editors/asset/ED_asset_catalog.hh
@@ -37,3 +37,6 @@ void ED_asset_catalog_remove(AssetLibrary *library, const blender::bke::CatalogI
 void ED_asset_catalog_rename(AssetLibrary *library,
                              blender::bke::CatalogID catalog_id,
                              blender::StringRefNull new_name);
+void ED_asset_catalog_move(AssetLibrary *library,
+                           blender::bke::CatalogID src_catalog_id,
+                           blender::bke::CatalogID dst_parent_catalog_id);
diff --git a/source/blender/editors/asset/intern/asset_catalog.cc b/source/blender/editors/asset/intern/asset_catalog.cc
index f3ba12a6324..8e1e5be2e47 100644
--- a/source/blender/editors/asset/intern/asset_catalog.cc
+++ b/source/blender/editors/asset/intern/asset_catalog.cc
@@ -121,6 +121,33 @@ void ED_asset_catalog_rename(::AssetLibrary *library,
   WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
 }
 
+void ED_asset_catalog_move(::AssetLibrary *library,
+                           const CatalogID src_catalog_id,
+                           const CatalogID dst_parent_catalog_id)
+{
+  bke::AssetCatalogService *catalog_service = BKE_asset_library_get_catalog_service(library);
+  if (!catalog_service) {
+    BLI_assert_unreachable();
+    return;
+  }
+
+  AssetCatalog *src_catalog = catalog_service->find_catalog(src_catalog_id);
+  AssetCatalog *dst_catalog = catalog_service->find_catalog(dst_parent_catalog_id);
+
+  const AssetCatalogPath new_path = dst_catalog->path / StringRef(src_catalog->path.name());
+  const AssetCatalogPath clean_new_path = new_path.cleanup();
+
+  if (new_path == src_catalog->path || clean_new_path == src_catalog->path) {
+    /* Nothing changed, so don't bother renaming for nothing. */
+    return;
+  }
+
+  catalog_service->undo_push();
+  catalog_service->tag_has_unsaved_changes(src_catalog);
+  catalog_service->update_catalog_path(src_catalog_id, clean_new_path);
+  WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, nullptr);
+}
+
 void ED_asset_catalogs_save_from_main_path(::AssetLibrary *library, const Main *bmain)
 {
   bke::AssetCatalogService *catalog_service = BKE_asset_library_get_catalog_service(library);
diff --git a/source/blender/editors/space_file/asset_catalog_tree_view.cc b/source/blender/editors/space_file/asset_catalog_tree_view.cc
index 53981ca244d..e6b76e05e16 100644
--- a/source/blender/editors/space_file/asset_catalog_tree_view.cc
+++ b/source/blender/editors/space_file/asset_catalog_tree_view.cc
@@ -95,26 +95,44 @@ class AssetCatalogTreeViewItem : public ui::BasicTreeViewItem {
   bool can_rename() const override;
   bool rename(StringRefNull new_name) override;
 
+  /** Add drag support for catalog items. */
+  std::unique_ptr<ui::AbstractTreeViewItemDragController> create_drag_controller() const override;
   /** Add dropping support for catalog items. */
   std::unique_ptr<ui::AbstractTreeViewItemDropController> create_drop_controller() const override;
 };
 
+class AssetCatalogDragController : public ui::AbstractTreeViewItemDragController {
+  AssetCatalogTreeItem &catalog_item_;
+
+ public:
+  explicit AssetCatalogDragController(AssetCatalogTreeItem &catalog_item);
+
+  int get_drag_type() const override;
+  void *create_drag_data() const override;
+};
+
 class AssetCatalogDropController : public ui::AbstractTreeViewItemDropController {
   AssetCatalogTreeItem &catalog_item_;
 
  public:
-  explicit AssetCatalogDropController(AssetCatalogTreeView &tree_view,
-                                      AssetCatalogTreeItem &catalog_item);
+  AssetCatalogDropController(AssetCatalogTreeView &tree_view, AssetCatalogTreeItem &catalog_item);
 
   bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override;
   std::string drop_tooltip(const wmDrag &drag) const override;
   bool on_drop(const wmDrag &drag) override;
 
-  static bool has_droppable_item(const wmDrag &drag, const char **r_disabled_hint);
-  static bool drop_into_catalog(const AssetCatalogTreeView &tree_view,
-                                const wmDrag &drag,
-                                CatalogID catalog_id,
-                                StringRefNull simple_name = "");
+  ::AssetLibrary &get_asset_library() const;
+
+  static bool has_droppable_asset(const wmDrag &drag, const char **r_disabled_hint);
+  static bool drop_assets_into_catalog(const AssetCatalogTreeView &tree_view,
+                                       const wmDrag &drag,
+                                       CatalogID catalog_id,
+                                       StringRefNull simple_name = "");
+
+ private:
+  bool drop_asset_catalog_into_catalog(const wmDrag &drag);
+  std::string drop_tooltip_asset_list(const wmDrag &drag) const;
+  std::string drop_tooltip_asset_catalog(const wmDrag &drag) const;
 };
 
 /** Only reason this isn't just `BasicTreeViewItem` is to add a '+' icon for adding a root level
@@ -308,6 +326,12 @@ std::unique_ptr<ui::AbstractTreeViewItemDropController> AssetCatalogTreeViewItem
       static_cast<AssetCatalogTreeView &>(get_tree_view()), catalog_item_);
 }
 
+std::unique_ptr<ui::AbstractTreeViewItemDragController> AssetCatalogTreeViewItem::
+    create_drag_controller() const
+{
+  return std::make_unique<AssetCatalogDragController>(catalog_item_);
+}
+
 /* ---------------------------------------------------------------------- */
 
 AssetCatalogDropController::AssetCatalogDropController(AssetCatalogTreeView &tree_view,
@@ -318,14 +342,41 @@ AssetCatalogDropController::AssetCatalogDropController(AssetCatalogTreeView &tre
 
 bool AssetCatalogDropController::can_drop(const wmDrag &drag, const char **r_disabled_hint) const
 {
-  if (drag.type != WM_DRAG_ASSET_LIST) {
-    return false;
+  if (drag.type == WM_DRAG_ASSET_CATALOG) {
+    /* Always supported. */
+    return true;
+  }
+  if (drag.type == WM_DRAG_ASSET_LIST) {
+    return has_droppable_asset(drag, r_disabled_hint);
   }
-  return has_droppable_item(drag, r_disabled_hint);
+  return false;
 }
 
 std::string AssetCatalogDropController::drop_tooltip(const wmDrag &drag) const
 {
+  if (drag.type == WM_DRAG_ASSET_CATALOG) {
+    return drop_tooltip_asset_catalog(drag);
+  }
+  return drop_tooltip_asset_list(drag);
+}
+
+std::string AssetCatalogDropController::drop_tooltip_asset_catalog(const wmDrag &drag) const
+{
+  BLI_assert(drag.type == WM_DRAG_ASSET_CATALOG);
+
+  const ::AssetLibrary *asset_library = tree_view<AssetCatalogTreeView>().asset_library_;
+  bke::AssetCatalogService *catalog_service = BKE_asset_library_get_catalog_service(asset_library);
+  wmDragAssetCatalog *catalog_drag = WM_drag_get_asset_catalog_data(&drag);
+  AssetCatalog *src_catalog = catalog_service->find_catalog(catalog_drag->drag_catalog_id);
+
+  return std::string(TIP_("Move Catalog")) + " '" + src_catalog->path.name() + "' " +
+         IFACE_("into") + " '" + catalog_item_.get_name() + "'";
+}
+
+std::string AssetCatalogDropController::drop_tooltip_asset_list(const wmDrag &drag) const
+{
+  BLI_assert(drag.type == WM_DRAG_ASSET_LIST);
+
   const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
   const bool is_multiple_assets = !BLI_listbase_is_single(asset_drags);
 
@@ -340,17 +391,32 @@ std::string AssetCatalogDropController::drop_tooltip(const wmDrag &drag) const
 
 bool AssetCatalogDropController::on_drop(const wmDrag &drag)
 {
-  return drop_into_catalog(tree_view<AssetCatalogTreeView>(),
-                           drag,
-                           catalog_item_.get_catalog_id(),
-                           catalog_item_.get_simple_name());
+  if (drag.type == WM_DRAG_ASSET_CATALOG) {
+    return drop_asset_catalog_into_catalog(drag);
+  }
+  return drop_assets_into_catalog(tree_view<AssetCatalogTreeView>(),
+                                  drag,
+                                  catalog_item_.get_catalog_id(),
+                                  catalog_item_.get_simple_name());
+}
+
+bool AssetCatalogDropController::drop_asset_catalog_into_catalog(const wmDrag &drag)
+{
+  BLI_assert(drag.type == WM_DRAG_ASSET_CATALOG);
+  wmDragAssetCatalog *catalog_drag = WM_drag_get_asset_catalog_data(&drag);
+  ED_asset_catalog_move(
+      &get_asset_library(), catalog_drag->drag_catalog_id, catalog_item_.get_catalog_id());
+
+  WM_main_add_notifier(NC_ASSET | ND_ASSET_CATALOGS, nullptr);
+  return true;
 }
 
-bool AssetCatalogDropController::drop_into_catalog(const AssetCatalogTreeView &tree_view,
-                                                   const wmDrag &drag,
-                                                   CatalogID catalog_id,
-                                                   StringRefNull simple_name)
+bool AssetCatalogDropController::drop_assets_into_catalog(const AssetCatalogTreeView &tree_view,
+                                                          const wmDrag &drag,
+                                                          CatalogID catalog_id,
+                                                          StringRefNull simple_name)
 {
+  BLI_assert(drag.type == WM_DRAG_ASSET_LIST);
   const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
   if (!asset_drags) {
     return false;
@@ -373,8 +439,8 @@ bool AssetCatalogDropController::drop_into_catalog(const AssetCatalogTreeView &t
   return true;
 }
 
-bool AssetCatalogDropController::has_droppable_item(const wmDrag &drag,
-                                                    const char **r_disabled_hint)
+bool AssetCatalogDropController::has_droppable_asset(const wmDrag &drag,
+                                                     const char **r_disabled_hint)
 {
   const ListBase *asset_drags = WM_drag_asset_list_get(&drag);
 
@@ -390,6 +456,31 @@ bool AssetCatalogDropController::has_droppable_item(const wmDrag &drag,
   return false;
 }
 
+::AssetLibrary &AssetCatalogDropController::get_asset_library() const
+{
+  return *tree_view<AssetCatalogTreeView>().asset_library_;
+}
+
+/* ---------------------------------------------------------------------- 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list