Commit: ad5bfecf5ac22752f6fe5d46f07ee903bde86d5f
Author: Julian Eisel
Date:   Thu Oct 1 18:29:26 2020 +0200
Branches: asset-browser
https://developer.blender.org/rBad5bfecf5ac22752f6fe5d46f07ee903bde86d5f

Support switching between custom repositories in the Asset Browser

Now the repository list in Asset Browers includes the custom
repositories. They can be selected and should load fine (making the
Browser show assets stored in the repository's .blend file).
Some sanity checks (e.g. test if the repository path is valid and points
to a .blend file) could be added still.

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

M       source/blender/blenkernel/BKE_preferences.h
M       source/blender/blenkernel/intern/preferences.c
M       source/blender/editors/space_file/filelist.c
M       source/blender/editors/space_file/filelist.h
M       source/blender/editors/space_file/filesel.c
M       source/blender/editors/space_file/space_file.c
M       source/blender/makesdna/DNA_space_types.h
M       source/blender/makesrna/intern/rna_space.c
M       source/blender/makesrna/intern/rna_userdef.c

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

diff --git a/source/blender/blenkernel/BKE_preferences.h 
b/source/blender/blenkernel/BKE_preferences.h
index f9e1013e45b..26df568f5f2 100644
--- a/source/blender/blenkernel/BKE_preferences.h
+++ b/source/blender/blenkernel/BKE_preferences.h
@@ -39,6 +39,14 @@ void BKE_preferences_asset_repository_remove(struct UserDef 
*userdef,
                                              struct bUserAssetRepository 
*repository)
     ATTR_NONNULL();
 
+struct bUserAssetRepository *BKE_preferences_asset_repository_find_from_index(
+    const struct UserDef *userdef, int index) ATTR_NONNULL() 
ATTR_WARN_UNUSED_RESULT;
+struct bUserAssetRepository *BKE_preferences_asset_repository_find_from_name(
+    const struct UserDef *userdef, const char *name) ATTR_NONNULL() 
ATTR_WARN_UNUSED_RESULT;
+int BKE_preferences_asset_repository_get_index(const struct UserDef *userdef,
+                                               const struct 
bUserAssetRepository *repository)
+    ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
+
 void BKE_preferences_asset_repository_default_add(struct UserDef *userdef) 
ATTR_NONNULL();
 
 #ifdef __cplusplus
diff --git a/source/blender/blenkernel/intern/preferences.c 
b/source/blender/blenkernel/intern/preferences.c
index da337e06823..2faca033e2c 100644
--- a/source/blender/blenkernel/intern/preferences.c
+++ b/source/blender/blenkernel/intern/preferences.c
@@ -68,6 +68,24 @@ void BKE_preferences_asset_repository_remove(UserDef 
*userdef, bUserAssetReposit
   BLI_freelinkN(&userdef->asset_repositories, repository);
 }
 
+bUserAssetRepository *BKE_preferences_asset_repository_find_from_index(const 
UserDef *userdef,
+                                                                       int 
index)
+{
+  return BLI_findlink(&userdef->asset_repositories, index);
+}
+
+bUserAssetRepository *BKE_preferences_asset_repository_find_from_name(const 
UserDef *userdef,
+                                                                      const 
char *name)
+{
+  return BLI_findstring(&userdef->asset_repositories, name, 
offsetof(bUserAssetRepository, name));
+}
+
+int BKE_preferences_asset_repository_get_index(const UserDef *userdef,
+                                               const bUserAssetRepository 
*repository)
+{
+  return BLI_findindex(&userdef->asset_repositories, repository);
+}
+
 void BKE_preferences_asset_repository_default_add(UserDef *userdef)
 {
   const char *asset_blend_name = "assets.blend";
diff --git a/source/blender/editors/space_file/filelist.c 
b/source/blender/editors/space_file/filelist.c
index b83b7bc24d9..8c784bee367 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -309,6 +309,8 @@ typedef struct FileList {
   FileDirEntryArr filelist;
 
   eFileSelectType type;
+  /* The repository this list was created for. Stored here so we know when to 
re-read. */
+  FileSelectAssetRepositoryID asset_repository;
 
   short prv_w;
   short prv_h;
@@ -1562,7 +1564,7 @@ static void filelist_cache_clear(FileListEntryCache 
*cache, size_t new_size)
   BLI_listbase_clear(&cache->cached_entries);
 }
 
-FileList *filelist_new(short type)
+FileList *filelist_new(short type, const FileSelectAssetRepositoryID 
*asset_repository)
 {
   FileList *p = MEM_callocN(sizeof(*p), __func__);
 
@@ -1572,6 +1574,9 @@ FileList *filelist_new(short type)
   p->selection_state = BLI_ghash_new(
       BLI_ghashutil_uinthash_v4_p, BLI_ghashutil_uinthash_v4_cmp, __func__);
   p->filelist.nbr_entries = -1;
+  if (asset_repository) {
+    p->asset_repository = *asset_repository;
+  }
 
   switch (p->type) {
     case FILE_MAIN:
@@ -1709,6 +1714,13 @@ bool filelist_matches_type(const FileList *filelist, 
short type)
   return filelist->type == (eFileSelectType)type;
 }
 
+bool filelist_matches_asset_repository(const FileList *filelist,
+                                       const FileSelectAssetRepositoryID 
*repository)
+{
+  return (filelist->asset_repository.type == repository->type) &&
+         STREQ(filelist->asset_repository.idname, repository->idname);
+}
+
 const char *filelist_dir(struct FileList *filelist)
 {
   return filelist->filelist.root;
diff --git a/source/blender/editors/space_file/filelist.h 
b/source/blender/editors/space_file/filelist.h
index 8925a051149..d9fea15fdc2 100644
--- a/source/blender/editors/space_file/filelist.h
+++ b/source/blender/editors/space_file/filelist.h
@@ -30,6 +30,7 @@ extern "C" {
 struct BlendHandle;
 struct FileList;
 struct FileSelection;
+struct FileSelectAssetRepositoryID;
 struct wmWindowManager;
 
 struct FileDirEntry;
@@ -75,7 +76,8 @@ struct ImBuf *filelist_getimage(struct FileList *filelist, 
const int index);
 struct ImBuf *filelist_geticon_image(struct FileList *filelist, const int 
index);
 int filelist_geticon(struct FileList *filelist, const int index, const bool 
is_main);
 
-struct FileList *filelist_new(short type);
+struct FileList *filelist_new(short type,
+                              const struct FileSelectAssetRepositoryID 
*asset_repository);
 void filelist_clear(struct FileList *filelist);
 void filelist_clear_ex(struct FileList *filelist,
                        const bool do_cache,
@@ -84,6 +86,8 @@ void filelist_clear_ex(struct FileList *filelist,
 void filelist_free(struct FileList *filelist);
 
 bool filelist_matches_type(const struct FileList *filelist, short type);
+bool filelist_matches_asset_repository(const struct FileList *filelist,
+                                       const struct 
FileSelectAssetRepositoryID *repository);
 
 const char *filelist_dir(struct FileList *filelist);
 bool filelist_is_dir(struct FileList *filelist, const char *path);
diff --git a/source/blender/editors/space_file/filesel.c 
b/source/blender/editors/space_file/filesel.c
index e84753a52e7..e724d928ad5 100644
--- a/source/blender/editors/space_file/filesel.c
+++ b/source/blender/editors/space_file/filesel.c
@@ -58,6 +58,7 @@
 #include "BKE_context.h"
 #include "BKE_idtype.h"
 #include "BKE_main.h"
+#include "BKE_preferences.h"
 
 #include "BLF_api.h"
 
@@ -80,7 +81,7 @@
 static eFileSelectType fileselect_type_from_params_get(const FileSelectParams 
*params)
 {
 
-  return (params->asset_repository == FILE_ASSET_REPO_LOCAL) ? FILE_MAIN_ASSET 
: FILE_LOADLIB;
+  return (params->asset_repository.type == FILE_ASSET_REPO_LOCAL) ? 
FILE_MAIN_ASSET : FILE_LOADLIB;
 }
 
 static bool fileselect_needs_refresh(const SpaceFile *sfile)
@@ -102,6 +103,10 @@ static bool fileselect_needs_refresh(const SpaceFile 
*sfile)
       !filelist_matches_type(sfile->files, 
fileselect_type_from_params_get(sfile->params))) {
     return true;
   }
+  if (sfile->files &&
+      !filelist_matches_asset_repository(sfile->files, 
&sfile->params->asset_repository)) {
+    return true;
+  }
 
   return false;
 }
@@ -140,6 +145,8 @@ short ED_fileselect_set_params(SpaceFile *sfile)
     sfile->params->thumbnail_size = U_default.file_space_data.thumbnail_size;
     sfile->params->details_flags = U_default.file_space_data.details_flags;
     sfile->params->filter_id = U_default.file_space_data.filter_id;
+    /* Sane default. */
+    sfile->params->asset_repository.type = FILE_ASSET_REPO_LOCAL;
   }
 
   params = sfile->params;
@@ -331,27 +338,31 @@ short ED_fileselect_set_params(SpaceFile *sfile)
     params->filter_glob[0] = '\0';
 
     if (ED_fileselect_is_asset_browser(params)) {
-      if (params->asset_repository == FILE_ASSET_REPO_LOCAL) {
-        params->dir[0] = '\0';
-        params->file[0] = '\0';
-        allow_null_dir = true;
-      }
-      else {
-        /* TODO Fixed file path. */
-        const char *doc_path = BKE_appdir_folder_default();
-
-        if (doc_path) {
-          const char *asset_blend_name = "assets.blend";
-          // const char *id_group_name = BKE_idtype_idcode_to_name(ID_OB);
-
-          BLI_join_dirfile(params->dir, sizeof(params->dir), doc_path, 
asset_blend_name);
-          // BLI_path_join(
-          //     params->dir, sizeof(params->dir), doc_path, asset_blend_name, 
id_group_name,
-          //     NULL);
-          params->file[0] = '\0';
+      /* Ensure valid repo, or fall-back to local one. */
+      eFileAssetReporitory_Type repo_type = params->asset_repository.type;
+      bUserAssetRepository *user_repository = NULL;
+
+      if (repo_type == FILE_ASSET_REPO_CUSTOM) {
+        user_repository = BKE_preferences_asset_repository_find_from_name(
+            &U, params->asset_repository.idname);
+        if (!user_repository) {
+          repo_type = FILE_ASSET_REPO_LOCAL;
         }
       }
 
+      switch (repo_type) {
+        case FILE_ASSET_REPO_LOCAL:
+          params->dir[0] = '\0';
+          allow_null_dir = true;
+          break;
+        case FILE_ASSET_REPO_CUSTOM:
+          BLI_assert(user_repository);
+          BLI_strncpy(params->dir, user_repository->path, sizeof(params->dir));
+          break;
+      }
+      params->file[0] = '\0';
+      params->asset_repository.type = repo_type;
+
       params->type = fileselect_type_from_params_get(params);
       /* TODO this way of using filters to realize categories is noticably 
slower than
        * specifying a "group" to read. That's because all types are read and 
filtering is applied
diff --git a/source/blender/editors/space_file/space_file.c 
b/source/blender/editors/space_file/space_file.c
index 56f74e47c5b..c4746a06897 100644
--- a/source/blender/editors/space_file/space_file.c
+++ b/source/blender/editors/space_file/space_file.c
@@ -225,7 +225,7 @@ static SpaceLink *file_duplicate(SpaceLink *sl)
   sfilen->smoothscroll_timer = NULL;
 
   if (sfileo->params) {
-    sfilen->files = filelist_new(sfileo->params->type);
+    sfilen->files = filelist_new(sfileo->params->type, 
&sfileo->params->asset_repository);
     sfilen->params = MEM_dupallocN(sfileo->params);
     filelist_setdir(sfilen->files, sfilen->params->dir);
   }
@@ -317,12 +317,16 @@ static void file_refresh(const bContext *C, ScrArea *area)
   if (!sfile->folders_prev) {
     sfile->folders_prev = folderlist_new();
   }
-  if (sfile->files && !filelist_matches_type(sfile->files, params->type)) {
+  /* TODO this filelist regeneration could be done better. Also kinda 
duplicated from
+   * fileselect_needs_refresh(). */
+  if (sfile->f

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to