Blender V5.0
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.h"
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) {
42 }
43
45}
46
48{
50
51 /* Simple case: Predefined repository, just set the value. */
52 if (value < ASSET_LIBRARY_CUSTOM) {
53 library.type = value;
54 library.custom_library_index = -1;
56 return library;
57 }
58
60 &U, value - ASSET_LIBRARY_CUSTOM);
61
62 /* Note that there is no check if the path exists here. If an invalid library path is used, the
63 * Asset Browser can give a nice hint on what's wrong. */
64 if (!user_library) {
65 library.type = ASSET_LIBRARY_ALL;
66 library.custom_library_index = -1;
67 }
68 else {
69 const bool is_valid = (user_library->name[0] && user_library->dirpath[0]);
70 if (is_valid) {
72 library.type = ASSET_LIBRARY_CUSTOM;
73 }
74 }
75 return library;
76}
77
78static void rna_enum_add_custom_libraries(EnumPropertyItem **item, int *totitem)
79{
80 int i;
81 LISTBASE_FOREACH_INDEX (bUserAssetLibrary *, user_library, &U.asset_libraries, i) {
82 /* Note that the path itself isn't checked for validity here. If an invalid library path is
83 * used, the Asset Browser can give a nice hint on what's wrong. */
84 const bool is_valid = (user_library->name[0] && user_library->dirpath[0]);
85 if (!is_valid) {
86 continue;
87 }
88
89 AssetLibraryReference library_reference;
90 library_reference.type = ASSET_LIBRARY_CUSTOM;
91 library_reference.custom_library_index = i;
92
93 const int enum_value = library_reference_to_enum_value(&library_reference);
94 /* Use library path as description, it's a nice hint for users. */
95 EnumPropertyItem tmp = {
96 enum_value, user_library->name, ICON_NONE, user_library->name, user_library->dirpath};
97 RNA_enum_item_add(item, totitem, &tmp);
98 }
99}
100
101const EnumPropertyItem *library_reference_to_rna_enum_itemf(const bool include_readonly,
102 const bool include_current_file)
103{
104 EnumPropertyItem *item = nullptr;
105 int totitem = 0;
106
107 if (include_readonly) {
110 RNA_enum_item_add_separator(&item, &totitem);
111 }
112 if (include_current_file) {
115 }
116 if (include_readonly) {
119 }
120
121 /* Add separator if needed. */
122 if (!BLI_listbase_is_empty(&U.asset_libraries) && (include_readonly || include_current_file)) {
123 RNA_enum_item_add_separator(&item, &totitem);
124 }
125 rna_enum_add_custom_libraries(&item, &totitem);
126
127 RNA_enum_item_end(&item, &totitem);
128 return item;
129}
130
132{
133 EnumPropertyItem *item = nullptr;
134 int totitem = 0;
135
136 rna_enum_add_custom_libraries(&item, &totitem);
137
138 RNA_enum_item_end(&item, &totitem);
139 return item;
140}
141
142} // namespace blender::ed::asset
struct bUserAssetLibrary * BKE_preferences_asset_library_find_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 ListBase *lb)
#define LISTBASE_FOREACH_INDEX(type, var, list, index_var)
#define ELEM(...)
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
@ ASSET_LIBRARY_ALL
#define U
int library_reference_to_enum_value(const AssetLibraryReference *library)
const EnumPropertyItem * library_reference_to_rna_enum_itemf(bool include_readonly, bool include_current_file)
AssetLibraryReference library_reference_from_enum_value(int value)
const EnumPropertyItem * custom_libraries_rna_enum_itemf()
static void rna_enum_add_custom_libraries(EnumPropertyItem **item, int *totitem)
const EnumPropertyItem rna_enum_asset_library_type_items[]
Definition rna_asset.cc:21
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
void RNA_enum_item_add_separator(EnumPropertyItem **items, int *totitem)
i
Definition text_draw.cc:230