[Bf-blender-cvs] [2d3e5eda3f2] master: Assets/UI: Resolve major asset view UI template limitation

Julian Eisel noreply at git.blender.org
Thu Jul 29 17:35:12 CEST 2021


Commit: 2d3e5eda3f26e59945454cbca0d6533db4d980a8
Author: Julian Eisel
Date:   Thu Jul 29 17:27:46 2021 +0200
Branches: master
https://developer.blender.org/rB2d3e5eda3f26e59945454cbca0d6533db4d980a8

Assets/UI: Resolve major asset view UI template limitation

Before this, all asset view templates showing the same asset library
would show the same assets, even if they should show different ID types.
That was a major limitation since the design did forsee that this
template can be put anywhere in the UI to display various sub-sets of
assets.

Initially I did the ID type filtering close to the asset-list reading,
because I wanted to optimize reading so that we would only actually read
asset information from disk of the ID type to be shown. But this will be
quite complex and I'm not sure if I'll get to work on this anytime soon.
So this commit moves the filtering to the template display level solving
this limitation.

Note: This also adds the code to filter by tags, together with the ID
type. But it's not actually used anywhere yet.

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

M	source/blender/editors/asset/CMakeLists.txt
A	source/blender/editors/asset/ED_asset_filter.h
M	source/blender/editors/asset/ED_asset_list.h
A	source/blender/editors/asset/intern/asset_filter.cc
M	source/blender/editors/asset/intern/asset_list.cc
M	source/blender/editors/include/ED_asset.h
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/makesdna/DNA_asset_types.h

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

diff --git a/source/blender/editors/asset/CMakeLists.txt b/source/blender/editors/asset/CMakeLists.txt
index 2e25be51781..7e578b78809 100644
--- a/source/blender/editors/asset/CMakeLists.txt
+++ b/source/blender/editors/asset/CMakeLists.txt
@@ -31,6 +31,7 @@ set(INC_SYS
 )
 
 set(SRC
+  intern/asset_filter.cc
   intern/asset_handle.cc
   intern/asset_library_reference.cc
   intern/asset_library_reference_enum.cc
@@ -39,6 +40,7 @@ set(SRC
   intern/asset_ops.cc
   intern/asset_temp_id_consumer.cc
 
+  ED_asset_filter.h
   ED_asset_handle.h
   ED_asset_library.h
   ED_asset_list.h
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/asset/ED_asset_filter.h
similarity index 58%
copy from source/blender/editors/include/ED_asset.h
copy to source/blender/editors/asset/ED_asset_filter.h
index 42faf716560..fb2d3bfb273 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/asset/ED_asset_filter.h
@@ -15,10 +15,7 @@
  */
 
 /** \file
- * \ingroup editors
- *
- * The public API for assets is defined in dedicated headers. This is a utility file that just
- * includes all of these.
+ * \ingroup edasset
  */
 
 #pragma once
@@ -27,22 +24,12 @@
 extern "C" {
 #endif
 
-/* Barely anything here. Just general editor level functions. Actual asset level code is in
- * dedicated headers. */
+struct AssetHandle;
+struct AssetFilterSettings;
 
-void ED_operatortypes_asset(void);
+bool ED_asset_filter_matches_asset(const struct AssetFilterSettings *filter,
+                                   const struct AssetHandle *asset);
 
 #ifdef __cplusplus
 }
 #endif
-
-#include "../asset/ED_asset_handle.h"
-#include "../asset/ED_asset_library.h"
-#include "../asset/ED_asset_list.h"
-#include "../asset/ED_asset_mark_clear.h"
-#include "../asset/ED_asset_temp_id_consumer.h"
-
-/* C++ only headers. */
-#ifdef __cplusplus
-#  include "../asset/ED_asset_list.hh"
-#endif
diff --git a/source/blender/editors/asset/ED_asset_list.h b/source/blender/editors/asset/ED_asset_list.h
index 1e7f0f0de55..55a0ac0b500 100644
--- a/source/blender/editors/asset/ED_asset_list.h
+++ b/source/blender/editors/asset/ED_asset_list.h
@@ -32,7 +32,6 @@ struct ID;
 struct wmNotifier;
 
 void ED_assetlist_storage_fetch(const struct AssetLibraryReference *library_reference,
-                                const struct AssetFilterSettings *filter_settings,
                                 const struct bContext *C);
 void ED_assetlist_ensure_previews_job(const struct AssetLibraryReference *library_reference,
                                       struct bContext *C);
diff --git a/source/blender/editors/asset/intern/asset_filter.cc b/source/blender/editors/asset/intern/asset_filter.cc
new file mode 100644
index 00000000000..397df33d596
--- /dev/null
+++ b/source/blender/editors/asset/intern/asset_filter.cc
@@ -0,0 +1,64 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup edasset
+ */
+
+#include "BKE_idtype.h"
+
+#include "BLI_listbase.h"
+
+#include "DNA_ID.h"
+#include "DNA_asset_types.h"
+
+#include "ED_asset_filter.h"
+#include "ED_asset_handle.h"
+
+/**
+ * Compare \a asset against the settings of \a filter.
+ *
+ * Individual filter parameters are ORed with the asset properties. That means:
+ * * The asset type must be one of the ID types filtered by, and
+ * * The asset must contain at least one of the tags filtered by.
+ * However for an asset to be matching it must have one match in each of the parameters. I.e. one
+ * matching type __and__ at least one matching tag.
+ *
+ * \returns True if the asset should be visible with these filter settings (parameters match).
+ *          Otherwise returns false (mismatch).
+ */
+bool ED_asset_filter_matches_asset(const AssetFilterSettings *filter, const AssetHandle *asset)
+{
+  ID_Type asset_type = ED_asset_handle_get_id_type(asset);
+  uint64_t asset_id_filter = BKE_idtype_idcode_to_idfilter(asset_type);
+
+  if ((filter->id_types & asset_id_filter) == 0) {
+    return false;
+  }
+  /* Not very efficient (O(n^2)), could be improved quite a bit. */
+  LISTBASE_FOREACH (const AssetTag *, filter_tag, &filter->tags) {
+    AssetMetaData *asset_data = ED_asset_handle_get_metadata(asset);
+
+    AssetTag *matched_tag = (AssetTag *)BLI_findstring(
+        &asset_data->tags, filter_tag->name, offsetof(AssetTag, name));
+    if (matched_tag == nullptr) {
+      return false;
+    }
+  }
+
+  /* Succesfully passed through all filters. */
+  return true;
+}
diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc
index d94a2bbf438..57c25e1614b 100644
--- a/source/blender/editors/asset/intern/asset_list.cc
+++ b/source/blender/editors/asset/intern/asset_list.cc
@@ -122,7 +122,7 @@ class AssetList : NonCopyable {
   AssetList(AssetList &&other) = default;
   ~AssetList() = default;
 
-  void setup(const AssetFilterSettings *filter_settings = nullptr);
+  void setup();
   void fetch(const bContext &C);
   void ensurePreviewsJob(bContext *C);
   void clear(bContext *C);
@@ -141,7 +141,7 @@ AssetList::AssetList(eFileSelectType filesel_type, const AssetLibraryReference &
 {
 }
 
-void AssetList::setup(const AssetFilterSettings *filter_settings)
+void AssetList::setup()
 {
   FileList *files = filelist_;
 
@@ -160,17 +160,13 @@ void AssetList::setup(const AssetFilterSettings *filter_settings)
   filelist_setrecursion(files, 1);
   filelist_setsorting(files, FILE_SORT_ALPHA, false);
   filelist_setlibrary(files, &library_ref_);
-  /* TODO different filtering settings require the list to be reread. That's a no-go for when we
-   * want to allow showing the same asset library with different filter settings (as in,
-   * different ID types). The filelist needs to be made smarter somehow, maybe goes together with
-   * the plan to separate the view (preview caching, filtering, etc. ) from the data. */
   filelist_setfilter_options(
       files,
-      filter_settings != nullptr,
+      false,
       true,
       true, /* Just always hide parent, prefer to not add an extra user option for this. */
       FILE_TYPE_BLENDERLIB,
-      filter_settings ? filter_settings->id_types : FILTER_ID_ALL,
+      FILTER_ID_ALL,
       true,
       "",
       "");
@@ -333,9 +329,7 @@ class AssetListStorage {
   /* Purely static class, can't instantiate this. */
   AssetListStorage() = delete;
 
-  static void fetch_library(const AssetLibraryReference &library_reference,
-                            const bContext &C,
-                            const AssetFilterSettings *filter_settings = nullptr);
+  static void fetch_library(const AssetLibraryReference &library_reference, const bContext &C);
   static void destruct();
   static AssetList *lookup_list(const AssetLibraryReference &library_ref);
   static void tagMainDataDirty();
@@ -353,8 +347,7 @@ class AssetListStorage {
 };
 
 void AssetListStorage::fetch_library(const AssetLibraryReference &library_reference,
-                                     const bContext &C,
-                                     const AssetFilterSettings *filter_settings)
+                                     const bContext &C)
 {
   std::optional filesel_type = asset_library_reference_to_fileselect_type(library_reference);
   if (!filesel_type) {
@@ -363,7 +356,7 @@ void AssetListStorage::fetch_library(const AssetLibraryReference &library_refere
 
   auto [list, is_new] = ensure_list_storage(library_reference, *filesel_type);
   if (is_new || list.needsRefetch()) {
-    list.setup(filter_settings);
+    list.setup();
     list.fetch(C);
   }
 }
@@ -440,11 +433,9 @@ using namespace blender::ed::asset;
  * Invoke asset list reading, potentially in a parallel job. Won't wait until the job is done,
  * and may return earlier.
  */
-void ED_assetlist_storage_fetch(const AssetLibraryReference *library_reference,
-                                const AssetFilterSettings *filter_settings,
-                                const bContext *C)
+void ED_assetlist_storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
 {
-  AssetListStorage::fetch_library(*library_reference, *C, filter_settings);
+  AssetListStorage::fetch_library(*library_reference, *C);
 }
 
 void ED_assetlist_ensure_previews_job(const AssetLibraryReference *library_reference, bContext *C)
diff --git a/source/blender/editors/include/ED_asset.h b/source/blender/editors/include/ED_asset.h
index 42faf716560..8f19c97e671 100644
--- a/source/blender/editors/include/ED_asset.h
+++ b/source/blender/editors/include/ED_asset.h
@@ -36,6 +36,7 @@ void ED_operatortypes_asset(void);
 }
 #endif
 
+#include "../asset/ED_asset_filter.h"
 #include "../asset/ED_asset_handle.h"
 #include "../asset/ED_asset_library.h"
 #include "../asset/ED_asset_list.h"
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
index 8babea5d18c..b4ba6a7feab 100644
--- a/source/blender/editors/interface/interface_template_asset_view.cc
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -154,6 +154,7 @@ uiListType *UI_UL_asset_view()
 
 static void asset_view_template_refresh_asset_collection(
     const AssetLibraryReference &asset_library,
+    const AssetFilterSettings &filter_settings,
     PointerRNA &assets_dataptr,
     const char *assets_propname)
 {
@@ -175,6 +176,11 @@ static void asset_view_template_refresh_asset_collection(
   RNA_property_collection_clear(&assets_dataptr, assets_prop

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list