Blender V5.3
asset_shelf_settings.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
10
12#include "AS_asset_library.hh"
13
14#include "DNA_defs.h"
15#include "DNA_screen_types.h"
16#include "DNA_userdef_types.h"
17
18#include "BLO_read_write.hh"
19
20#include "BLI_listbase.hh"
21#include "BLI_string.hh"
22#include "BLI_string_utf8.hh"
23
24#include "BKE_asset.hh"
25#include "BKE_preferences.h"
26#include "BKE_screen.hh"
27
28#include "asset_shelf.hh"
29
30namespace blender {
31
32using namespace blender::ed::asset;
33
34AssetShelfSettings::AssetShelfSettings() = default;
35
36AssetShelfSettings::AssetShelfSettings(const AssetShelfSettings &other)
37{
38 operator=(other);
39}
40
41AssetShelfSettings &AssetShelfSettings::operator=(const AssetShelfSettings &other)
42{
43 if (this == &other) {
44 return *this; /* Handle self-assignment safely. */
45 }
46
47 /* Free existing properties. Check if they point to the same memory first, #AssetShelfSettings
48 * might have been shallow copied before. */
51 }
52 if (this->active_catalog_path != other.active_catalog_path) {
53 MEM_SAFE_DELETE(this->active_catalog_path);
54 }
55
56 /* Copy from 'other'. */
59 this->preview_size = other.preview_size;
60 this->display_flag = other.display_flag;
61
62 if (other.active_catalog_path) {
64 }
66
67 return *this;
68}
69
70AssetShelfSettings::~AssetShelfSettings()
71{
73 MEM_SAFE_DELETE(active_catalog_path);
74}
75
76namespace ed::asset::shelf {
77
79{
80 writer->write_struct(&settings);
81
83 writer->write_string(settings.active_catalog_path);
84}
85
91
93{
95 /* Nothing to validate, all good. */
96 return settings.asset_library_reference;
97 }
98
101
102 /* If the library wasn't found, fall back to the "All" library. */
103 if (!user_library || !BKE_preferences_asset_library_is_available(&U, user_library)) {
105 }
106 return settings.asset_library_reference;
107}
108
111{
112 MEM_delete(settings.active_catalog_path);
113 settings.active_catalog_path = BLI_strdupn(path.c_str(), path.length());
114}
115
117{
118 MEM_delete(settings.active_catalog_path);
119 settings.active_catalog_path = nullptr;
120}
121
124{
125 return settings.active_catalog_path && settings.active_catalog_path == path.str();
126}
127
129{
130 return !settings.active_catalog_path || !settings.active_catalog_path[0];
131}
132
134{
135 return shelf.type && (shelf.type->flag & ASSET_SHELF_TYPE_FLAG_STORE_CATALOGS_IN_PREFS);
136}
137
138static const ListBaseT<AssetCatalogPathLink> *get_enabled_catalog_path_list(
139 const AssetShelf &shelf)
140{
143 &U, shelf.idname);
144 return pref_settings ? &pref_settings->enabled_catalog_paths : nullptr;
145 }
146 return &shelf.settings.enabled_catalog_paths;
147}
148
149static ListBaseT<AssetCatalogPathLink> *get_enabled_catalog_path_list(AssetShelf &shelf)
150{
151 return const_cast<ListBaseT<AssetCatalogPathLink> *>(
152 get_enabled_catalog_path_list(const_cast<const AssetShelf &>(shelf)));
153}
154
156{
157 ListBaseT<AssetCatalogPathLink> *enabled_catalog_paths = get_enabled_catalog_path_list(shelf);
158 if (enabled_catalog_paths) {
159 BKE_asset_catalog_path_list_free(*enabled_catalog_paths);
160 BLI_assert(enabled_catalog_paths->is_empty());
161 }
162}
163
166{
167 const ListBaseT<AssetCatalogPathLink> *enabled_catalog_paths = get_enabled_catalog_path_list(
168 shelf);
169 if (!enabled_catalog_paths) {
170 return false;
171 }
172
173 return BKE_asset_catalog_path_list_has_path(*enabled_catalog_paths, path.c_str());
174}
175
178{
181 &U, shelf.idname, path.c_str()))
182 {
183 U.runtime.is_dirty = true;
184 }
185 }
186 else {
187 if (!BKE_asset_catalog_path_list_has_path(shelf.settings.enabled_catalog_paths, path.c_str()))
188 {
189 BKE_asset_catalog_path_list_add_path(shelf.settings.enabled_catalog_paths, path.c_str());
190 }
191 }
192}
193
195 const AssetShelf &shelf,
196 FunctionRef<void(const asset_system::AssetCatalogPath &catalog_path)> fn)
197{
198 const ListBaseT<AssetCatalogPathLink> *enabled_catalog_paths = get_enabled_catalog_path_list(
199 shelf);
200 if (!enabled_catalog_paths) {
201 return;
202 }
203
204 for (const AssetCatalogPathLink &path_link : *enabled_catalog_paths) {
205 fn(asset_system::AssetCatalogPath(path_link.path));
206 }
207}
208
209} // namespace ed::asset::shelf
210} // namespace blender
#define STRNCPY_UTF8(dst, src)
btGeneric6DofConstraint & operator=(btGeneric6DofConstraint &other)
AssetLibraryReference all_library_reference()
void settings_clear_enabled_catalogs(AssetShelf &shelf)
static bool use_enabled_catalogs_from_prefs(const AssetShelf &shelf)
bool settings_is_all_catalog_active(const AssetShelfSettings &settings)
AssetLibraryReference & settings_ensure_valid_library_ref(AssetShelfSettings &settings)
void settings_blend_read_data(BlendDataReader *reader, AssetShelfSettings &settings)
void settings_foreach_enabled_catalog_path(const AssetShelf &shelf, FunctionRef< void(const asset_system::AssetCatalogPath &catalog_path)> fn)
void settings_set_all_catalog_active(AssetShelfSettings &settings)
bool settings_is_catalog_path_enabled(const AssetShelf &shelf, const asset_system::AssetCatalogPath &path)
bool settings_is_active_catalog(const AssetShelfSettings &settings, const asset_system::AssetCatalogPath &path)
void settings_set_catalog_path_enabled(AssetShelf &shelf, const asset_system::AssetCatalogPath &path)
static const ListBaseT< AssetCatalogPathLink > * get_enabled_catalog_path_list(const AssetShelf &shelf)
void settings_blend_write(BlendWriter *writer, const AssetShelfSettings &settings)
void settings_set_active_catalog(AssetShelfSettings &settings, const asset_system::AssetCatalogPath &path)
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_asset_catalog_path_list_free(ListBaseT< AssetCatalogPathLink > &catalog_path_list)
bool BKE_asset_catalog_path_list_has_path(const ListBaseT< AssetCatalogPathLink > &catalog_path_list, const char *catalog_path)
struct bUserAssetLibrary * BKE_preferences_asset_library_find_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BKE_preferences_asset_shelf_settings_ensure_catalog_path_enabled(UserDef *userdef, const char *shelf_idname, const char *catalog_path)
@ ASSET_LIBRARY_CUSTOM
bUserAssetShelfSettings * BKE_preferences_asset_shelf_settings_get(const UserDef *userdef, const char *shelf_idname)
void BKE_asset_catalog_path_list_blend_read_data(BlendDataReader *reader, ListBaseT< AssetCatalogPathLink > &catalog_path_list)
bool BKE_preferences_asset_library_is_available(const struct UserDef *userdef, const struct bUserAssetLibrary *library) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ ASSET_SHELF_TYPE_FLAG_STORE_CATALOGS_IN_PREFS
void BKE_asset_catalog_path_list_blend_write(BlendWriter *writer, const ListBaseT< AssetCatalogPathLink > &catalog_path_list)
void BKE_asset_catalog_path_list_add_path(ListBaseT< AssetCatalogPathLink > &catalog_path_list, const char *catalog_path)
ListBaseT< AssetCatalogPathLink > BKE_asset_catalog_path_list_duplicate(const ListBaseT< AssetCatalogPathLink > &catalog_path_list)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
AssetLibraryReference asset_library_reference
ListBaseT< AssetCatalogPathLink > enabled_catalog_paths
AssetShelfSettings_DisplayFlag display_flag
void write_string(const char *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
ListBaseT< AssetCatalogPathLink > enabled_catalog_paths