Blender V5.3
asset_library_reference_enum.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include "BLI_listbase.hh"
15
16#include "BKE_preferences.h"
17
18#include "DNA_userdef_types.h"
19
20#include "UI_resources.hh"
21
22#include "RNA_define.hh"
23#include "RNA_enum_types.hh"
24
25#include "ED_asset_library.hh"
26
27namespace blender::ed::asset {
28
30{
31 /* Simple case: Predefined repository, just set the value. */
32 if (library->type < ASSET_LIBRARY_CUSTOM) {
33 return library->type;
34 }
35
36 /* Note that the path isn't checked for validity here. If an invalid library path is used, the
37 * Asset Browser can give a nice hint on what's wrong. */
39 &U, library->custom_library_index);
40 if (user_library) {
41 return ASSET_LIBRARY_CUSTOM + library->custom_library_index;
42 }
43
45}
46
47static bool custom_library_is_valid(const bUserAssetLibrary *user_library)
48{
49 if (user_library->flag & ASSET_LIBRARY_DISABLED) {
50 return false;
51 }
52 if (!user_library->name[0]) {
53 return false;
54 }
55
57 &U,
58 user_library,
59 /* Don't check if the path exists on disk. If an invalid library path is used, the Asset
60 * Browser can give a nice hint on what's wrong, so include such items in menus the user can
61 * choose from. */
62 /*check_directory_exists=*/false);
63}
64
66{
68
69 /* Simple case: Predefined repository, just set the value. */
70 if (value < ASSET_LIBRARY_CUSTOM) {
71 library.type = eAssetLibraryType(value);
72 library.custom_library_index = -1;
73 BLI_assert(ELEM(value,
78 return library;
79 }
80
82 &U, value - ASSET_LIBRARY_CUSTOM);
83
84 if (!user_library) {
86 library.custom_library_index = -1;
87 }
88 else if (custom_library_is_valid(user_library)) {
89 library.custom_library_index = value - ASSET_LIBRARY_CUSTOM;
91 }
92 return library;
93}
94
96 int *totitem,
97 const bool include_remote_libraries)
98{
99 /* Group by owner (user, project, extension), separated in the menu. */
100 const bUserAssetLibraryOwner owners[] = {
104 };
105 bool add_separator = false;
106 for (const bUserAssetLibraryOwner owner : owners) {
107 bool has_items = false;
108 for (const auto [i, user_library] : U.asset_libraries.enumerate()) {
109 if (BKE_preferences_asset_library_owner_get(&user_library) != owner) {
110 continue;
111 }
112 if (!include_remote_libraries && (user_library.flag & ASSET_LIBRARY_USE_REMOTE_URL)) {
113 continue;
114 }
115 if (!custom_library_is_valid(&user_library)) {
116 continue;
117 }
118 if (!has_items) {
119 if (add_separator) {
120 RNA_enum_item_add_separator(item, totitem);
121 }
122 has_items = true;
123 }
124
125 AssetLibraryReference library_reference;
126 library_reference.type = ASSET_LIBRARY_CUSTOM;
127 library_reference.custom_library_index = i;
128
129 const int enum_value = library_reference_to_enum_value(&library_reference);
130 EnumPropertyItem tmp = {
131 enum_value,
132 user_library.name,
133 ICON_NONE,
134 user_library.name,
135 /* Use library path or URL as description, it's a nice hint for users. */
136 (user_library.flag & ASSET_LIBRARY_USE_REMOTE_URL) ? user_library.remote_url :
137 (owner == bUserAssetLibraryOwner::Project) ? user_library.dirpath :
138 user_library.resolved_dirpath};
139 RNA_enum_item_add(item, totitem, &tmp);
140 }
141 if (has_items) {
142 add_separator = true;
143 }
144 }
145}
146
148 const bool include_readonly,
149 const bool include_current_file,
150 const bool include_remote_libraries,
151 const bool include_separate_online_essentials)
152{
153 EnumPropertyItem *item = nullptr;
154 int totitem = 0;
155
156 if (include_readonly) {
159 RNA_enum_item_add_separator(&item, &totitem);
160 }
161 if (include_current_file) {
164 }
165 if (include_readonly) {
168 }
169 if (include_separate_online_essentials) {
172 }
173
174 {
175 EnumPropertyItem *custom_item = nullptr;
176 int tot_custom_item = 0;
177 rna_enum_add_custom_libraries(&custom_item, &tot_custom_item, include_remote_libraries);
178
179 /* Add separator if needed. */
180 if ((tot_custom_item > 0) && (include_readonly || include_current_file)) {
181 RNA_enum_item_add_separator(&item, &totitem);
182 }
183 RNA_enum_item_end(&custom_item, &tot_custom_item);
184 RNA_enum_items_add(&item, &totitem, custom_item);
185
186 MEM_delete(custom_item);
187 }
188
189 RNA_enum_item_end(&item, &totitem);
190 return item;
191}
192
194{
195 EnumPropertyItem *item = nullptr;
196 int totitem = 0;
197
199 &item,
200 &totitem,
201 /* This function should return local/on-disk libraries only, so skip remote ones. */
202 /*include_remote_libraries=*/false);
203
204 RNA_enum_item_end(&item, &totitem);
205 return item;
206}
207
208} // namespace blender::ed::asset
#define ELEM(...)
int library_reference_to_enum_value(const AssetLibraryReference *library)
AssetLibraryReference library_reference_from_enum_value(int value)
static bool custom_library_is_valid(const bUserAssetLibrary *user_library)
const EnumPropertyItem * custom_libraries_rna_enum_itemf()
static void rna_enum_add_custom_libraries(EnumPropertyItem **item, int *totitem, const bool include_remote_libraries)
const EnumPropertyItem * library_reference_to_rna_enum_itemf(bool include_readonly, bool include_current_file, bool include_remote_libraries, bool include_separate_online_essentials)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
@ ASSET_LIBRARY_DISABLED
@ ASSET_LIBRARY_USE_REMOTE_URL
struct bUserAssetLibrary * BKE_preferences_asset_library_find_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
bool BKE_preferences_asset_library_is_valid(const UserDef *userdef, const struct bUserAssetLibrary *library, const bool check_directory_exists) ATTR_NONNULL()
const EnumPropertyItem rna_enum_asset_library_type_items[]
Definition rna_asset.cc:24
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
void RNA_enum_items_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
bUserAssetLibraryOwner BKE_preferences_asset_library_owner_get(const struct bUserAssetLibrary *library) ATTR_NONNULL()
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
void RNA_enum_item_add_separator(EnumPropertyItem **items, int *totitem)