Blender V4.5
interface_template_asset_view.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
8
10
11#include "DNA_space_types.h"
12
13#include "BKE_screen.hh"
14
15#include "BLI_string.h"
16#include "BLI_string_ref.hh"
17
18#include "ED_asset.hh"
19#include "ED_screen.hh"
20
21#include "MEM_guardedalloc.h"
22
23#include "RNA_access.hh"
24#include "RNA_prototypes.hh"
25
26#include "UI_interface.hh"
27
28#include "WM_types.hh"
29
30#include "interface_intern.hh"
31
32using namespace blender;
33using namespace blender::ed;
34
41
42static void asset_view_item_but_drag_set(uiBut *but, AssetHandle *asset_handle)
43{
45 asset_handle);
46
48
49 ID *id = asset->local_id();
50 if (id != nullptr) {
51 UI_but_drag_set_id(but, id);
52 return;
53 }
54
55 const eAssetImportMethod import_method = asset->get_import_method().value_or(
57 AssetImportSettings import_settings{};
58 import_settings.method = import_method;
59 import_settings.use_instance_collections = false;
60
62 asset,
63 import_settings,
66}
67
68static void asset_view_draw_item(uiList *ui_list,
69 const bContext * /*C*/,
70 uiLayout *layout,
71 PointerRNA * /*dataptr*/,
72 PointerRNA * /*itemptr*/,
73 int /*icon*/,
74 PointerRNA * /*active_dataptr*/,
75 const char * /*active_propname*/,
76 int index,
77 int /*flt_flag*/)
78{
80
82 index);
84
86 &list_data->screen->id,
87 &RNA_FileSelectEntry,
88 const_cast<FileDirEntry *>(asset_handle.file_data));
89 uiLayoutSetContextPointer(layout, "active_file", &file_ptr);
90
91 asset->ensure_previewable();
92
93 uiBlock *block = uiLayoutGetBlock(layout);
94 const bool show_names = list_data->show_names;
95 const float size_x = UI_preview_tile_size_x();
96 const float size_y = show_names ? UI_preview_tile_size_y() : UI_preview_tile_size_y_no_label();
97 uiBut *but = uiDefIconTextBut(block,
99 0,
101 show_names ? asset->get_name().c_str() : "",
102 0,
103 0,
104 size_x,
105 size_y,
106 nullptr,
107 0,
108 0,
109 "");
110 ui_def_but_icon(but,
112 /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */
115 if (!ui_list->dyn_data->custom_drag_optype) {
116 asset_view_item_but_drag_set(but, &asset_handle);
117 }
118}
119
120static void asset_view_filter_items(uiList *ui_list,
121 const bContext *C,
122 PointerRNA *dataptr,
123 const char *propname)
124{
125 AssetViewListData *list_data = (AssetViewListData *)ui_list->dyn_data->customdata;
126 asset::AssetFilterSettings &filter_settings = list_data->filter_settings;
127
128 uiListNameFilter name_filter(*ui_list);
129
131 ui_list,
132 C,
133 [&name_filter, list_data, &filter_settings](
134 const PointerRNA &itemptr, blender::StringRefNull name, int index) {
136 list_data->asset_library_ref, index);
137
138 if (!asset::filter_matches_asset(&filter_settings, *asset)) {
140 }
141 return name_filter(itemptr, name, index);
142 },
143 dataptr,
144 propname,
145 [list_data](const PointerRNA & /*itemptr*/, int index) -> std::string {
147 list_data->asset_library_ref, index);
148
149 return asset->get_name();
150 });
151}
152
154{
155 const wmNotifier *notifier = params->notifier;
156
157 switch (notifier->category) {
158 case NC_ID: {
159 if (ELEM(notifier->action, NA_RENAME)) {
161 }
162 break;
163 }
164 }
165
166 if (asset::list::listen(params->notifier)) {
168 }
169}
170
172{
173 uiListType *list_type = (uiListType *)MEM_callocN(sizeof(*list_type), __func__);
174
175 STRNCPY(list_type->idname, "UI_UL_asset_view");
176 list_type->draw_item = asset_view_draw_item;
178 list_type->listener = asset_view_listener;
179
180 return list_type;
181}
182
183static void populate_asset_collection(const AssetLibraryReference &asset_library_ref,
184 PointerRNA &assets_dataptr,
185 const char *assets_propname)
186{
187 PropertyRNA *assets_prop = RNA_struct_find_property(&assets_dataptr, assets_propname);
188 if (!assets_prop) {
189 RNA_warning("Asset collection not found");
190 return;
191 }
192 if (RNA_property_type(assets_prop) != PROP_COLLECTION) {
193 RNA_warning("Expected a collection property");
194 return;
195 }
196 if (!RNA_struct_is_a(RNA_property_pointer_type(&assets_dataptr, assets_prop), &RNA_AssetHandle))
197 {
198 RNA_warning("Expected a collection property for AssetHandle items");
199 return;
200 }
201
202 RNA_property_collection_clear(&assets_dataptr, assets_prop);
203
204 asset::list::iterate(asset_library_ref, [&](asset_system::AssetRepresentation & /*asset*/) {
205 /* XXX creating a dummy #RNA_AssetHandle collection item. It's #file_data will be null. This is
206 * because the #FileDirEntry may be freed while iterating, there's a cache for them with a
207 * maximum size. Further code will query as needed it using the collection index. */
208
209 PointerRNA itemptr;
210 RNA_property_collection_add(&assets_dataptr, assets_prop, &itemptr);
211 PointerRNA fileptr = RNA_pointer_create_discrete(nullptr, &RNA_FileSelectEntry, nullptr);
212 RNA_pointer_set(&itemptr, "file_data", fileptr);
213
214 return true;
215 });
216}
217
219 const bContext *C,
220 const char *list_id,
221 PointerRNA *asset_library_dataptr,
222 const char *asset_library_propname,
223 PointerRNA *assets_dataptr,
224 const char *assets_propname,
225 PointerRNA *active_dataptr,
226 const char *active_propname,
227 const asset::AssetFilterSettings *filter_settings,
228 const int display_flags,
229 const char *activate_opname,
230 PointerRNA *r_activate_op_properties,
231 const char *drag_opname,
232 PointerRNA *r_drag_op_properties)
233{
234 if (!list_id || !list_id[0]) {
235 RNA_warning("Asset view needs a valid identifier");
236 return;
237 }
238
239 uiLayout *col = &layout->column(false);
240
241 PropertyRNA *asset_library_prop = RNA_struct_find_property(asset_library_dataptr,
242 asset_library_propname);
244 RNA_property_enum_get(asset_library_dataptr, asset_library_prop));
245
246 uiLayout *row = &col->row(true);
247 if ((display_flags & UI_TEMPLATE_ASSET_DRAW_NO_LIBRARY) == 0) {
248 row->prop(
249 asset_library_dataptr, asset_library_prop, RNA_NO_INDEX, 0, UI_ITEM_NONE, "", ICON_NONE);
250 if (asset_library_ref.type != ASSET_LIBRARY_LOCAL) {
251 row->op("ASSET_OT_library_refresh", "", ICON_FILE_REFRESH);
252 }
253 }
254
255 asset::list::storage_fetch(&asset_library_ref, C);
256 const int tot_items = asset::list::size(&asset_library_ref);
257
258 populate_asset_collection(asset_library_ref, *assets_dataptr, assets_propname);
259
260 AssetViewListData *list_data = (AssetViewListData *)MEM_mallocN(sizeof(*list_data),
261 "AssetViewListData");
262 list_data->asset_library_ref = asset_library_ref;
263 list_data->filter_settings = *filter_settings;
264 list_data->screen = CTX_wm_screen(C);
265 list_data->show_names = (display_flags & UI_TEMPLATE_ASSET_DRAW_NO_NAMES) == 0;
266
267 uiTemplateListFlags template_list_flags = UI_TEMPLATE_LIST_NO_GRIP;
268 if ((display_flags & UI_TEMPLATE_ASSET_DRAW_NO_NAMES) != 0) {
269 template_list_flags |= UI_TEMPLATE_LIST_NO_NAMES;
270 }
271 if ((display_flags & UI_TEMPLATE_ASSET_DRAW_NO_FILTER) != 0) {
272 template_list_flags |= UI_TEMPLATE_LIST_NO_FILTER_OPTIONS;
273 }
274
275 uiLayout *subcol = &col->column(false);
276
277 uiLayoutSetScaleX(subcol, 0.8f);
278 uiLayoutSetScaleY(subcol, 0.8f);
279
280 /* TODO can we have some kind of model-view API to handle referencing, filtering and lazy loading
281 * (of previews) of the items? */
282 uiList *list = uiTemplateList_ex(subcol,
283 C,
284 "UI_UL_asset_view",
285 list_id,
286 assets_dataptr,
287 assets_propname,
288 active_dataptr,
289 active_propname,
290 nullptr,
291 tot_items,
292 0,
294 0,
295 template_list_flags,
296 list_data);
297 if (!list) {
298 /* List creation failed. */
299 MEM_freeN(list_data);
300 return;
301 }
302
303 if (activate_opname) {
305 list, activate_opname, r_activate_op_properties != nullptr);
306 if (r_activate_op_properties && ptr) {
307 *r_activate_op_properties = *ptr;
308 }
309 }
310 if (drag_opname) {
312 list, drag_opname, r_drag_op_properties != nullptr);
313 if (r_drag_op_properties && ptr) {
314 *r_drag_op_properties = *ptr;
315 }
316 }
317}
Main runtime representation of an asset.
bScreen * CTX_wm_screen(const bContext *C)
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
#define ELEM(...)
eAssetImportMethod
@ ASSET_IMPORT_APPEND_REUSE
@ ASSET_LIBRARY_LOCAL
@ UILST_LAYOUT_BIG_PREVIEW_GRID
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:639
Read Guarded memory(de)allocation.
#define RNA_warning(format,...)
@ PROP_COLLECTION
Definition RNA_types.hh:156
#define C
Definition RandGen.cpp:29
@ UI_LIST_ITEM_NEVER_SHOW
void UI_list_filter_and_sort_items(uiList *ui_list, const bContext *C, uiListItemFilterFn item_filter_fn, PointerRNA *dataptr, const char *propname, uiListItemGetNameFn get_name_fn=nullptr)
uiBut * uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, blender::StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< blender::StringRef > tip)
PointerRNA * UI_list_custom_drag_operator_set(uiList *ui_list, blender::StringRefNull opname, bool create_properties)
@ UI_TEMPLATE_ASSET_DRAW_NO_NAMES
@ UI_TEMPLATE_ASSET_DRAW_NO_LIBRARY
@ UI_TEMPLATE_ASSET_DRAW_NO_FILTER
int UI_preview_tile_size_y(const int size_px=96)
uiList * uiTemplateList_ex(uiLayout *layout, const bContext *C, const char *listtype_name, const char *list_id, PointerRNA *dataptr, blender::StringRefNull propname, PointerRNA *active_dataptr, blender::StringRefNull active_propname, const char *item_dyntip_propname, int rows, int maxrows, int layout_type, int columns, enum uiTemplateListFlags flags, void *customdata)
void UI_but_dragflag_enable(uiBut *but, int flag)
void UI_but_drag_set_asset(uiBut *but, const blender::asset_system::AssetRepresentation *asset, const AssetImportSettings &import_settings, int icon, int preview_icon)
PointerRNA * UI_list_custom_activate_operator_set(uiList *ui_list, blender::StringRefNull opname, bool create_properties)
@ UI_BUT_DRAG_FULL_BUT
int UI_preview_tile_size_x(const int size_px=96)
@ UI_BTYPE_PREVIEW_TILE
void UI_but_drag_set_id(uiBut *but, ID *id)
int UI_preview_tile_size_y_no_label(const int size_px=96)
@ UI_BUT_ICON_PREVIEW
uiTemplateListFlags
@ UI_TEMPLATE_LIST_NO_NAMES
@ UI_TEMPLATE_LIST_NO_FILTER_OPTIONS
@ UI_TEMPLATE_LIST_NO_GRIP
void uiLayoutSetContextPointer(uiLayout *layout, blender::StringRef name, PointerRNA *ptr)
void uiLayoutSetScaleY(uiLayout *layout, float scale)
uiBlock * uiLayoutGetBlock(uiLayout *layout)
void uiLayoutSetScaleX(uiLayout *layout, float scale)
#define UI_ITEM_NONE
#define NC_ID
Definition WM_types.hh:392
#define NA_RENAME
Definition WM_types.hh:585
uint col
void ui_def_but_icon(uiBut *but, const int icon, const int flag)
#define RNA_NO_INDEX
@ UI_HAS_ICON
static void asset_view_item_but_drag_set(uiBut *but, AssetHandle *asset_handle)
static void asset_view_draw_item(uiList *ui_list, const bContext *, uiLayout *layout, PointerRNA *, PointerRNA *, int, PointerRNA *, const char *, int index, int)
static void asset_view_listener(uiList *, wmRegionListenerParams *params)
uiListType * UI_UL_asset_view()
static void asset_view_filter_items(uiList *ui_list, const bContext *C, PointerRNA *dataptr, const char *propname)
static void populate_asset_collection(const AssetLibraryReference &asset_library_ref, PointerRNA &assets_dataptr, const char *assets_propname)
void uiTemplateAssetView(uiLayout *layout, const bContext *C, const char *list_id, PointerRNA *asset_library_dataptr, const char *asset_library_propname, PointerRNA *assets_dataptr, const char *assets_propname, PointerRNA *active_dataptr, const char *active_propname, const asset::AssetFilterSettings *filter_settings, const int display_flags, const char *activate_opname, PointerRNA *r_activate_op_properties, const char *drag_opname, PointerRNA *r_drag_op_properties)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
int size(const AssetLibraryReference *library_reference)
bool listen(const wmNotifier *notifier)
asset_system::AssetRepresentation * asset_get_by_index(const AssetLibraryReference &library_reference, int asset_index)
AssetHandle asset_handle_get_by_index(const AssetLibraryReference *library_reference, int asset_index)
void iterate(const AssetLibraryReference &library_reference, AssetListIndexIterFn fn)
AssetLibraryReference library_reference_from_enum_value(int value)
asset_system::AssetRepresentation * handle_get_representation(const AssetHandle *asset)
bool filter_matches_asset(const AssetFilterSettings *filter, const blender::asset_system::AssetRepresentation &asset)
BIFIconID asset_preview_or_icon(const asset_system::AssetRepresentation &asset)
BIFIconID asset_preview_icon_id(const asset_system::AssetRepresentation &asset)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void RNA_pointer_set(PointerRNA *ptr, const char *name, PointerRNA ptr_value)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
PropertyType RNA_property_type(PropertyRNA *prop)
void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
const struct FileDirEntry * file_data
eAssetImportMethod method
asset::AssetFilterSettings filter_settings
AssetLibraryReference asset_library_ref
Definition DNA_ID.h:404
blender::ui::EmbossType emboss
PointerRNA op(wmOperatorType *ot, std::optional< blender::StringRef > name, int icon, wmOperatorCallContext context, eUI_Item_Flag flag)
uiLayout & column(bool align)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
struct wmOperatorType * custom_drag_optype
char idname[BKE_ST_MAXNAME]
uiListFilterItemsFunc filter_items
uiListDrawItemFunc draw_item
uiListListener listener
uiListDyn * dyn_data
unsigned int action
Definition WM_types.hh:355
unsigned int category
Definition WM_types.hh:355
PointerRNA * ptr
Definition wm_files.cc:4226