Blender  V3.3
asset_library_reference_enum.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
12 #include "BLI_listbase.h"
13 
14 #include "BKE_preferences.h"
15 
16 #include "DNA_userdef_types.h"
17 
18 #include "UI_resources.h"
19 
20 #include "RNA_define.h"
21 
22 #include "ED_asset_library.h"
23 
25 {
26  /* Simple case: Predefined repository, just set the value. */
27  if (library->type < ASSET_LIBRARY_CUSTOM) {
28  return library->type;
29  }
30 
31  /* Note that the path isn't checked for validity here. If an invalid library path is used, the
32  * Asset Browser can give a nice hint on what's wrong. */
34  &U, library->custom_library_index);
35  if (user_library) {
36  return ASSET_LIBRARY_CUSTOM + library->custom_library_index;
37  }
38 
39  return ASSET_LIBRARY_LOCAL;
40 }
41 
43 {
45 
46  /* Simple case: Predefined repository, just set the value. */
47  if (value < ASSET_LIBRARY_CUSTOM) {
48  library.type = value;
49  library.custom_library_index = -1;
51  return library;
52  }
53 
55  &U, value - ASSET_LIBRARY_CUSTOM);
56 
57  /* Note that there is no check if the path exists here. If an invalid library path is used, the
58  * Asset Browser can give a nice hint on what's wrong. */
59  if (!user_library) {
61  library.custom_library_index = -1;
62  }
63  else {
64  const bool is_valid = (user_library->name[0] && user_library->path[0]);
65  if (is_valid) {
66  library.custom_library_index = value - ASSET_LIBRARY_CUSTOM;
68  }
69  }
70  return library;
71 }
72 
74  const bool include_local_library)
75 {
76  EnumPropertyItem *item = nullptr;
77  int totitem = 0;
78 
79  if (include_local_library) {
80  const EnumPropertyItem predefined_items[] = {
81  /* For the future. */
82  // {ASSET_REPO_BUNDLED, "BUNDLED", 0, "Bundled", "Show the default user assets"},
84  "LOCAL",
85  ICON_CURRENT_FILE,
86  "Current File",
87  "Show the assets currently available in this Blender session"},
88  {0, nullptr, 0, nullptr, nullptr},
89  };
90 
91  /* Add predefined items. */
92  RNA_enum_items_add(&item, &totitem, predefined_items);
93  }
94 
95  /* Add separator if needed. */
96  if (!BLI_listbase_is_empty(&U.asset_libraries)) {
97  RNA_enum_item_add_separator(&item, &totitem);
98  }
99 
100  int i = 0;
101  for (bUserAssetLibrary *user_library = (bUserAssetLibrary *)U.asset_libraries.first;
102  user_library;
103  user_library = user_library->next, i++) {
104  /* Note that the path itself isn't checked for validity here. If an invalid library path is
105  * used, the Asset Browser can give a nice hint on what's wrong. */
106  const bool is_valid = (user_library->name[0] && user_library->path[0]);
107  if (!is_valid) {
108  continue;
109  }
110 
111  AssetLibraryReference library_reference;
112  library_reference.type = ASSET_LIBRARY_CUSTOM;
113  library_reference.custom_library_index = i;
114 
115  const int enum_value = ED_asset_library_reference_to_enum_value(&library_reference);
116  /* Use library path as description, it's a nice hint for users. */
117  EnumPropertyItem tmp = {
118  enum_value, user_library->name, ICON_NONE, user_library->name, user_library->path};
119  RNA_enum_item_add(&item, &totitem, &tmp);
120  }
121 
122  RNA_enum_item_end(&item, &totitem);
123  return item;
124 }
struct bUserAssetLibrary * BKE_preferences_asset_library_find_from_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
#define BLI_assert(a)
Definition: BLI_assert.h:46
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:269
#define ELEM(...)
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_LOCAL
int ED_asset_library_reference_to_enum_value(const AssetLibraryReference *library)
AssetLibraryReference ED_asset_library_reference_from_enum_value(int value)
const EnumPropertyItem * ED_asset_library_reference_to_rna_enum_itemf(const bool include_local_library)
unsigned int U
Definition: btGjkEpa3.h:78
bool is_valid
void RNA_enum_items_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
Definition: rna_define.c:4465
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
Definition: rna_define.c:4487
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
Definition: rna_define.c:4436
void RNA_enum_item_add_separator(EnumPropertyItem **items, int *totitem)
Definition: rna_define.c:4459
const char * name
Definition: RNA_types.h:465
struct bUserAssetLibrary * next
static FT_Library library