Blender V5.3
asset_ui_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10#include <string>
11
12#include <fmt/format.h>
13
14#include "AS_asset_library.hh"
16
17#include "BKE_preferences.h"
18#include "BKE_preview_image.hh"
19
20#include "BLI_assert.hh"
21#include "BLI_listbase.hh"
22#include "BLI_path_utils.hh"
23#include "BLI_string.hh"
24
25#include "BLT_translation.hh"
26
27#include "UI_interface_c.hh"
28#include "UI_interface_icons.hh"
29#include "UI_resources.hh"
30
31#include "DNA_userdef_types.h"
32
33#include "RNA_access.hh"
34
35#include "ED_asset.hh"
36
37namespace blender::ed::asset {
38
40 ui::TooltipData &tip,
41 const bool include_name)
42{
43 if (include_name) {
44 tooltip_text_field_add(tip, asset.get_name(), {}, ui::TIP_STYLE_HEADER, ui::TIP_LC_MAIN);
45 tooltip_text_field_add(tip, {}, {}, ui::TIP_STYLE_SPACER, ui::TIP_LC_NORMAL, false);
46 }
47
48 const AssetMetaData &meta_data = asset.get_metadata();
49 if (meta_data.description) {
50 tooltip_text_field_add(tip, meta_data.description, {}, ui::TIP_STYLE_HEADER, ui::TIP_LC_MAIN);
51 }
52
53 if (asset.remote_file_status() == asset_system::RemoteAssetFileStatus::NO_MATCH) {
54 tooltip_text_field_add(
55 tip,
56 TIP_("This asset was previously downloaded, but it is outdated or inconsistent.\n"
57 "Downloading it again is recommended."),
58 {},
61 }
62
63 switch (asset.owner_asset_library().library_type()) {
65 if (asset.is_online_only()) {
66 /* Don't show file path or .blend name. Data on disk is just a cache. */
67 break;
68 }
69
70 tooltip_text_field_add(tip, {}, {}, ui::TIP_STYLE_SPACER, ui::TIP_LC_NORMAL, false);
71
72 const std::string full_blend_path = asset.full_library_path();
73
74 char dir[FILE_MAX], file[FILE_MAX];
75 BLI_path_split_dir_file(full_blend_path.c_str(), dir, sizeof(dir), file, sizeof(file));
76
77 if (file[0]) {
78 tooltip_text_field_add(tip, file, {}, ui::TIP_STYLE_NORMAL, ui::TIP_LC_VALUE);
79 }
80 if (dir[0]) {
81 tooltip_text_field_add(tip, dir, {}, ui::TIP_STYLE_NORMAL, ui::TIP_LC_VALUE);
82 }
83 break;
84 }
86 tooltip_text_field_add(tip, {}, {}, ui::TIP_STYLE_SPACER, ui::TIP_LC_NORMAL, false);
87 tooltip_text_field_add(
88 tip, TIP_("Asset Library: Current File"), {}, ui::TIP_STYLE_NORMAL, ui::TIP_LC_VALUE);
89 break;
92 tooltip_text_field_add(tip, {}, {}, ui::TIP_STYLE_SPACER, ui::TIP_LC_NORMAL, false);
93 tooltip_text_field_add(
94 tip, TIP_("Asset Library: Essentials"), {}, ui::TIP_STYLE_NORMAL, ui::TIP_LC_VALUE);
95 break;
96 default:
97 /* Intentionally empty. */
98 break;
99 }
100
101 if (asset.is_online_only()) {
102 if (std::optional<int64_t> combined_size = asset.online_asset_files_combined_size_in_bytes()) {
103 tooltip_text_field_add(tip, {}, {}, ui::TIP_STYLE_SPACER, ui::TIP_LC_NORMAL, false);
104
105 char size_ui_str[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE];
106 BLI_str_format_byte_unit(size_ui_str, *combined_size, true);
107 tooltip_text_field_add(tip,
108 fmt::format(fmt::runtime(TIP_("Download Size: {}")), size_ui_str),
109 {},
112 }
113 }
114}
115
117{
118 if (const PreviewImage *preview = asset.get_preview()) {
120 return preview->runtime->icon_id;
121 }
122 }
123
124 return ICON_NONE;
125}
126
128{
129 const BIFIconID preview_icon = asset_preview_icon_id(asset);
130 if (preview_icon != ICON_NONE) {
131 return preview_icon;
132 }
133
134 /* Preview image not found or invalid. Use type icon. */
135 return ui::icon_from_idcode(asset.get_id_type());
136}
137
139{
140 const int enum_value = RNA_enum_get(&ptr, "asset_library_reference");
143}
144
146{
147 const int enum_value = RNA_enum_get(&ptr, "asset_library_reference");
149}
150
151std::optional<AssetLibraryReference> get_user_library_ref_for_save(
152 const asset_system::AssetLibrary *preferred_library)
153{
154 if (preferred_library && !preferred_library->is_read_only()) {
155 if (std::optional<AssetLibraryReference> preferred_library_ref =
156 preferred_library->library_reference())
157 {
158 return preferred_library_ref;
159 }
161 }
162
163 /* Fallback to the first enabled on-disk user library. */
164 for (const bUserAssetLibrary &asset_library : U.asset_libraries) {
165 if (asset_library.flag & ASSET_LIBRARY_USE_REMOTE_URL) {
166 continue;
167 }
168 if (!BKE_preferences_asset_library_is_available(&U, &asset_library)) {
169 continue;
170 }
171 return asset::user_library_to_library_ref(asset_library);
172 }
173
174 /* No enabled user asset library found. */
175 return {};
176}
177
179 const Main &bmain,
180 const AssetLibraryReference lib,
181 const StringRef edit_text,
182 const FunctionRef<void(StringPropertySearchVisitParams)> visit_fn)
183{
185 if (!library) {
186 return;
187 }
188
189 if (!edit_text.is_empty()) {
190 const asset_system::AssetCatalogPath edit_path = edit_text;
191 if (!library->catalog_service().find_catalog_by_path(edit_path)) {
192 visit_fn(StringPropertySearchVisitParams{edit_path.str(), std::nullopt, ICON_ADD});
193 }
194 }
195
196 const std::shared_ptr<const asset_system::AssetCatalogTree> full_tree =
197 library->catalog_service().catalog_tree();
198 full_tree->foreach_item([&](const asset_system::AssetCatalogTreeItem &item) {
199 visit_fn(StringPropertySearchVisitParams{item.catalog_path().str(), std::nullopt});
200 });
201}
202
203} // namespace blender::ed::asset
Main runtime representation of an asset.
#define FILE_MAX
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE
Definition BLI_string.hh:31
#define TIP_(msgid)
#define BLI_assert_unreachable()
virtual std::optional< AssetLibraryReference > library_reference() const =0
constexpr bool is_empty() const
AssetLibraryReference get_asset_library_ref_from_opptr(PointerRNA &ptr)
AssetLibraryReference library_reference_from_enum_value(int value)
void asset_tooltip(const asset_system::AssetRepresentation &asset, ui::TooltipData &tip, const bool include_name)
void visit_library_catalogs_catalog_for_search(const Main &bmain, const AssetLibraryReference lib, const StringRef edit_text, const FunctionRef< void(StringPropertySearchVisitParams)> visit_fn)
std::optional< AssetLibraryReference > get_user_library_ref_for_save(const asset_system::AssetLibrary *preferred_library)
const bUserAssetLibrary * get_asset_library_from_opptr(PointerRNA &ptr)
BIFIconID asset_preview_or_icon(const asset_system::AssetRepresentation &asset)
AssetLibraryReference user_library_to_library_ref(const bUserAssetLibrary &user_library)
BIFIconID asset_preview_icon_id(const asset_system::AssetRepresentation &asset)
int icon_from_idcode(int idcode)
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
UserDef U
Definition blender.cc:58
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ ICON_SIZE_ICON
@ ASSET_LIBRARY_USE_REMOTE_URL
bool BKE_previewimg_is_invalid(const PreviewImage *prv, int size)
struct bUserAssetLibrary * BKE_preferences_asset_library_find_index(const struct UserDef *userdef, int index) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_ONLINE_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
asset_system::AssetLibrary * AS_asset_library_load(const Main *bmain, const AssetLibraryReference &library_reference)
int BIFIconID
Definition ED_asset.hh:30
bool BKE_preferences_asset_library_is_available(const struct UserDef *userdef, const struct bUserAssetLibrary *library) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
The meta-data of an asset. By creating and giving this for a data-block (ID.asset_data),...