[Bf-blender-cvs] [1315f164d7c] temp-asset-browser-catalogs-ui: Get basic catalog activation and catalog based filtering to work

Julian Eisel noreply at git.blender.org
Wed Sep 15 22:33:24 CEST 2021


Commit: 1315f164d7c2f1ead1cf02a0c664ef0de27d69ba
Author: Julian Eisel
Date:   Wed Sep 15 22:28:21 2021 +0200
Branches: temp-asset-browser-catalogs-ui
https://developer.blender.org/rB1315f164d7c2f1ead1cf02a0c664ef0de27d69ba

Get basic catalog activation and catalog based filtering to work

It's now possible to activate a catalog in the catalog tree to only make
matching catalogs visible. This doesn't yet show assets from all
sub-catalogs when selecting a parent catalog.

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

M	source/blender/blenkernel/BKE_asset_catalog.hh
M	source/blender/blenkernel/intern/asset_catalog.cc
M	source/blender/editors/include/UI_tree_view.hh
M	source/blender/editors/interface/tree_view.cc
M	source/blender/editors/space_file/asset_catalog_tree_view.cc
M	source/blender/editors/space_file/file_intern.h
M	source/blender/editors/space_file/file_panels.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
M	source/blender/editors/space_file/space_file.c
M	source/blender/makesdna/DNA_space_types.h

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

diff --git a/source/blender/blenkernel/BKE_asset_catalog.hh b/source/blender/blenkernel/BKE_asset_catalog.hh
index 5986f4cae2e..77195b010d8 100644
--- a/source/blender/blenkernel/BKE_asset_catalog.hh
+++ b/source/blender/blenkernel/BKE_asset_catalog.hh
@@ -114,9 +114,12 @@ class AssetCatalogTreeItem {
   using ChildMap = std::map<std::string, AssetCatalogTreeItem>;
   using ItemIterFn = FunctionRef<void(const AssetCatalogTreeItem &)>;
 
-  AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent = nullptr);
+  AssetCatalogTreeItem(StringRef name,
+                       StringRef catalog_id,
+                       const AssetCatalogTreeItem *parent = nullptr);
 
   AssetCatalogTreeItemIterator children();
+  StringRef get_catalog_id() const;
   StringRef get_name() const;
   /** Return the full catalog path, defined as the name of this catalog prefixed by the full
    * catalog path of its parent and a separator. */
@@ -131,6 +134,7 @@ class AssetCatalogTreeItem {
   ChildMap children_;
   /** The user visible name of this component. */
   CatalogPathComponent name_;
+  CatalogID catalog_id_;
 
   /** Pointer back to the parent item. Used to reconstruct the hierarchy from an item (e.g. to
    * build a path). */
@@ -148,7 +152,7 @@ class AssetCatalogTree {
 
  public:
   /** Ensure an item representing \a path is in the tree, adding it if necessary. */
-  void insert_item(StringRef catalog_path_str);
+  void insert_item(AssetCatalog &catalog);
 
   AssetCatalogTreeItemIterator children();
   void foreach_item(const AssetCatalogTreeItem::ItemIterFn callback) const;
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index 9142e4fc3bd..22da9b25235 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -74,7 +74,7 @@ AssetCatalog *AssetCatalogService::create_catalog(const CatalogPath &catalog_pat
   /* Null when the service only writes, but didn't load anything
    * (#AssetCatalogService::load_from_disk() not called). */
   if (catalog_tree_) {
-    catalog_tree_->insert_item(catalog_ptr->path);
+    catalog_tree_->insert_item(*catalog_ptr);
   }
 
   return catalog_ptr;
@@ -244,7 +244,7 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
 
   /* Go through the catalogs, insert each path component into the tree where needed. */
   for (auto &catalog : catalogs_.values()) {
-    tree->insert_item(catalog->path);
+    tree->insert_item(*catalog);
   }
 
   return tree;
@@ -252,8 +252,10 @@ std::unique_ptr<AssetCatalogTree> AssetCatalogService::read_into_tree()
 
 /* ---------------------------------------------------------------------- */
 
-AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name, const AssetCatalogTreeItem *parent)
-    : name_(name), parent_(parent)
+AssetCatalogTreeItem::AssetCatalogTreeItem(StringRef name,
+                                           StringRef catalog_id,
+                                           const AssetCatalogTreeItem *parent)
+    : name_(name), catalog_id_(catalog_id), parent_(parent)
 {
 }
 
@@ -262,6 +264,11 @@ AssetCatalogTreeItemIterator AssetCatalogTreeItem::children()
   return AssetCatalogTreeItemIterator(children_.begin(), children_.end());
 }
 
+StringRef AssetCatalogTreeItem::get_catalog_id() const
+{
+  return catalog_id_;
+}
+
 StringRef AssetCatalogTreeItem::get_name() const
 {
   return name_;
@@ -340,11 +347,11 @@ bool operator!=(AssetCatalogTreeItemIterator a, AssetCatalogTreeItemIterator b)
 
 /* ---------------------------------------------------------------------- */
 
-void AssetCatalogTree::insert_item(StringRef catalog_path_str)
+void AssetCatalogTree::insert_item(AssetCatalog &catalog)
 {
   /* #fs::path adds useful behavior to the path. Remember that on Windows it uses "\" as
    * separator! For catalogs it should always be "/". Use #fs::path::generic_string if needed. */
-  fs::path catalog_path = std::string_view(catalog_path_str);
+  fs::path catalog_path = catalog.path;
 
   const AssetCatalogTreeItem *parent = nullptr;
   AssetCatalogTreeItem::ChildMap *insert_to_map = &children_;
@@ -357,7 +364,13 @@ void AssetCatalogTree::insert_item(StringRef catalog_path_str)
 
     /* Insert new tree element - if no matching one is there yet! */
     auto [item, was_inserted] = insert_to_map->emplace(
-        component_name, AssetCatalogTreeItem(component_name, parent));
+        component_name,
+        AssetCatalogTreeItem(
+            component_name,
+            /* TODO There may be components that don't relate to an actual
+               catalog, i.e. there's no catalog ID to set. This should be changed. */
+            catalog_path.filename() == component_name ? catalog.catalog_id : "",
+            parent));
 
     /* Walk further into the path (no matter if a new item was created or not). */
     parent = &item->second;
diff --git a/source/blender/editors/include/UI_tree_view.hh b/source/blender/editors/include/UI_tree_view.hh
index 703d8d1ab49..73c1245c18f 100644
--- a/source/blender/editors/include/UI_tree_view.hh
+++ b/source/blender/editors/include/UI_tree_view.hh
@@ -180,6 +180,8 @@ class uiAbstractTreeViewItem : public uiTreeViewItemContainer {
 
   virtual void build_row(uiLayout &row) = 0;
 
+  virtual void onActivate();
+
   /** Copy persistent state (e.g. is-collapsed flag, selection, etc.) from a matching item of the
    * last redraw to this item. If sub-classes introduce more advanced state they should override
    * this and make update their state accordingly. */
@@ -208,15 +210,21 @@ class uiAbstractTreeViewItem : public uiTreeViewItemContainer {
  */
 class uiBasicTreeViewItem : public uiAbstractTreeViewItem {
  public:
+  using ActivateFn = std::function<void(uiBasicTreeViewItem &new_active)>;
   BIFIconID icon;
 
-  uiBasicTreeViewItem(StringRef label, BIFIconID icon = ICON_NONE);
+  uiBasicTreeViewItem(StringRef label,
+                      BIFIconID icon = ICON_NONE,
+                      ActivateFn activate_fn = nullptr);
 
   void build_row(uiLayout &row) override;
+  void onActivate() override;
 
  protected:
   /** Created in the #build() function. */
   uiButTreeRow *tree_row_but_ = nullptr;
+  /** Called when activating this tree view item. */
+  ActivateFn activate_fn_;
 
   uiBut *button();
   BIFIconID get_draw_icon() const;
diff --git a/source/blender/editors/interface/tree_view.cc b/source/blender/editors/interface/tree_view.cc
index e25c3293be8..13601abb991 100644
--- a/source/blender/editors/interface/tree_view.cc
+++ b/source/blender/editors/interface/tree_view.cc
@@ -134,6 +134,11 @@ uiAbstractTreeViewItem *uiAbstractTreeView::find_matching_child(
 
 /* ---------------------------------------------------------------------- */
 
+void uiAbstractTreeViewItem::onActivate()
+{
+  /* Do nothing by default. */
+}
+
 void uiAbstractTreeViewItem::update_from_old(uiAbstractTreeViewItem &old)
 {
   is_open_ = old.is_open_;
@@ -156,9 +161,10 @@ int uiAbstractTreeViewItem::count_parents() const
 
 void uiAbstractTreeViewItem::set_active(bool value)
 {
-  if (value) {
+  if (value && !is_active()) {
     /* Deactivate other items in the tree. */
     get_tree_view().foreach_item([](auto &item) { item.set_active(false); });
+    onActivate();
   }
   is_active_ = value;
 }
@@ -229,7 +235,8 @@ uiLayout *uiTreeViewLayoutBuilder::current_layout() const
 
 /* ---------------------------------------------------------------------- */
 
-uiBasicTreeViewItem::uiBasicTreeViewItem(StringRef label, BIFIconID icon_) : icon(icon_)
+uiBasicTreeViewItem::uiBasicTreeViewItem(StringRef label, BIFIconID icon_, ActivateFn activate_fn)
+    : icon(icon_), activate_fn_(activate_fn)
 {
   label_ = label;
 }
@@ -273,6 +280,13 @@ void uiBasicTreeViewItem::build_row(uiLayout &row)
   UI_but_treerow_indentation_set(&tree_row_but_->but, count_parents());
 }
 
+void uiBasicTreeViewItem::onActivate()
+{
+  if (activate_fn_) {
+    activate_fn_(*this);
+  }
+}
+
 BIFIconID uiBasicTreeViewItem::get_draw_icon() const
 {
   if (icon) {
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 1995972b53f..065e744cb19 100644
--- a/source/blender/editors/space_file/asset_catalog_tree_view.cc
+++ b/source/blender/editors/space_file/asset_catalog_tree_view.cc
@@ -39,6 +39,7 @@
 #include "UI_resources.h"
 #include "UI_tree_view.hh"
 
+#include "WM_api.h"
 #include "WM_types.h"
 
 #include "file_intern.h"
@@ -47,6 +48,23 @@ using namespace blender;
 using namespace blender::ui;
 using namespace blender::bke;
 
+class AssetCatalogTreeView : public uiAbstractTreeView {
+  bke::AssetLibrary *library_;
+  FileAssetSelectParams *params_;
+
+  friend class AssetCatalogTreeViewItem;
+
+ public:
+  AssetCatalogTreeView(bke::AssetLibrary *library, FileAssetSelectParams *params);
+
+  void build_tree() override;
+
+ private:
+  uiBasicTreeViewItem &build_recursive(uiTreeViewItemContainer &view_parent_item,
+                                       AssetCatalogTreeItem &catalog);
+};
+/* ---------------------------------------------------------------------- */
+
 class AssetCatalogTreeViewItem : public uiBasicTreeViewItem {
   AssetCatalogTreeItem &catalog_;
 
@@ -56,6 +74,15 @@ class AssetCatalogTreeViewItem : public uiBasicTreeViewItem {
   {
   }
 
+  void onActivate() override
+  {
+    const AssetCatalogTreeView &tree_view = static_cast<const AssetCatalogTreeView &>(
+        get_tree_view());
+    tree_view.params_->asset_catalog_visibility = FILE_SHOW_ASSETS_FROM_CATALOG;
+    tree_view.params_->catalog_id = catalog_.get_catalog_id().data();
+    WM_main_add_notifier(NC_SPACE | ND_SPACE_ASSET_PARAMS, NULL);
+  }
+
   void build_row(uiLayout &row) override
   {
     uiBasicTreeViewItem::build_row(row);
@@ -66,48 +93,57 @@ class AssetCatalogTreeViewItem : public uiBasicTreeViewItem {
   }
 };
 
-class AssetCatalogTreeView : public uiAbstractTreeView {
-  bke::AssetLibrary *library_;
+AssetCatalogTreeView::AssetCatalogTreeView(bke::AssetLibrary *library,
+                                           FileAssetSelectParams *params)
+    : library_(lib

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list