Blender V5.3
interface_template_preview.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 "BKE_context.hh"
10#include "BKE_idtype.hh"
11#include "BKE_linestyle.h"
12#include "BKE_scene.hh"
13
14#include "BLI_listbase.hh"
15#include "BLI_string_utf8.hh"
16
17#include "BLT_translation.hh"
18
19#include "DNA_light_types.h"
20#include "DNA_material_types.h"
21#include "DNA_texture_types.h"
22#include "DNA_world_types.h"
23
24#include "ED_render.hh"
25
26#include "RNA_access.hh"
27
28#include "WM_api.hh"
29
30#include "UI_interface.hh"
32
33namespace blender::ui {
34
36 bContext *C,
37 ID *id,
38 bool show_buttons,
39 ID *parent,
40 MTex *slot,
41 const char *preview_id)
42{
43 Material *ma = nullptr;
44 short *pr_texture = nullptr;
45
46 char _preview_id[sizeof(uiPreview::preview_id)];
47
48 if (id && !ELEM(GS(id->name), ID_MA, ID_TE, ID_WO, ID_LA, ID_LS)) {
49 RNA_warning("Expected ID of type material, texture, light, world or line style");
50 return;
51 }
52
53 /* decide what to render */
54 ID *pid = id;
55 ID *pparent = nullptr;
56
57 if (id && (GS(id->name) == ID_TE)) {
58 if (parent && (GS(parent->name) == ID_MA)) {
59 pr_texture = &(id_cast<Material *>(parent))->pr_texture;
60 }
61 else if (parent && (GS(parent->name) == ID_WO)) {
62 pr_texture = &(id_cast<World *>(parent))->pr_texture;
63 }
64 else if (parent && (GS(parent->name) == ID_LA)) {
65 pr_texture = &(id_cast<Light *>(parent))->pr_texture;
66 }
67 else if (parent && (GS(parent->name) == ID_LS)) {
68 pr_texture = &(id_cast<FreestyleLineStyle *>(parent))->pr_texture;
69 }
70
71 if (pr_texture) {
72 if (*pr_texture == TEX_PR_OTHER) {
73 pid = parent;
74 }
75 else if (*pr_texture == TEX_PR_BOTH) {
76 pparent = parent;
77 }
78 }
79 }
80
81 if (!preview_id || (preview_id[0] == '\0')) {
82 /* If no identifier given, generate one from ID type. */
83 SNPRINTF_UTF8(_preview_id, "uiPreview_%s", BKE_idtype_idcode_to_name(GS(id->name)));
84 preview_id = _preview_id;
85 }
86
87 /* Find or add the uiPreview to the current Region. */
88 ARegion *region = CTX_wm_region(C);
89 uiPreview *ui_preview = static_cast<uiPreview *>(
90 BLI_findstring(&region->ui_previews, preview_id, offsetof(uiPreview, preview_id)));
91
92 if (!ui_preview) {
93 ui_preview = MEM_new<uiPreview>(__func__);
94 STRNCPY_UTF8(ui_preview->preview_id, preview_id);
95 ui_preview->height = short(UI_UNIT_Y * 7.6f);
96 ui_preview->id_session_uid = pid->session_uid;
97 ui_preview->tag = UI_PREVIEW_TAG_DIRTY;
98 BLI_addtail(&region->ui_previews, ui_preview);
99 }
100 else if (ui_preview->id_session_uid != pid->session_uid) {
101 ui_preview->id_session_uid = pid->session_uid;
102 ui_preview->tag |= UI_PREVIEW_TAG_DIRTY;
103 }
104
105 if (ui_preview->height < UI_UNIT_Y) {
106 ui_preview->height = UI_UNIT_Y;
107 }
108 else if (ui_preview->height > UI_UNIT_Y * 50) { /* Rather high upper limit, yet not insane! */
109 ui_preview->height = UI_UNIT_Y * 50;
110 }
111
112 /* layout */
113 Block *block = layout->block();
114 Layout *row = &layout->row(false);
115 Layout *col = &row->column(false);
116
117 /* add preview */
118 uiDefBut(
119 block, ButtonType::Extra, "", 0, 0, UI_UNIT_X * 10, ui_preview->height, pid, 0.0, 0.0, "");
121 [pid, pparent, slot, ui_preview](const bContext *C, rcti *rect) {
122 ED_preview_draw(C, pid, pparent, slot, ui_preview, rect);
123 });
124 uiDefIconButV(block,
126 ICON_GRIP,
127 0,
128 0,
129 UI_UNIT_X * 10,
130 short(UI_UNIT_Y * 0.3f),
131 &ui_preview->height,
132 UI_UNIT_Y,
133 UI_UNIT_Y * 50.0f,
134 "");
135
136 /* add buttons */
137 if (pid && show_buttons) {
138 if (GS(pid->name) == ID_MA || (pparent && GS(pparent->name) == ID_MA)) {
139 if (GS(pid->name) == ID_MA) {
140 ma = id_cast<Material *>(pid);
141 }
142 else {
143 ma = id_cast<Material *>(pparent);
144 }
145
146 /* Create RNA Pointer */
147 PointerRNA material_ptr = RNA_id_pointer_create(&ma->id);
148
149 col = &row->column(true);
150 col->scale_x_set(1.5);
151 col->prop(&material_ptr, "preview_render_type", ITEM_R_EXPAND, "", ICON_NONE);
152
153 /* EEVEE preview file has baked lighting so use_preview_world has no effect,
154 * just hide the option until this feature is supported. */
156 col->separator();
157 col->prop(&material_ptr, "use_preview_world", UI_ITEM_NONE, "", ICON_WORLD);
158 }
159 }
160
161 if (pr_texture) {
162 /* Create RNA Pointer */
163 PointerRNA texture_ptr = RNA_id_pointer_create(id);
164
165 layout->row(true);
166 Button *but = uiDefButV(block,
168 IFACE_("Texture"),
169 0,
170 0,
171 UI_UNIT_X * 10,
172 UI_UNIT_Y,
173 pr_texture,
174 10,
176 "");
177 button_func_set(but, [](bContext &C) {
179 });
180 if (GS(parent->name) == ID_MA) {
181 but = uiDefButV(block,
183 IFACE_("Material"),
184 0,
185 0,
186 UI_UNIT_X * 10,
187 UI_UNIT_Y,
188 pr_texture,
189 10,
191 "");
192 button_func_set(but, [](bContext &C) {
194 });
195 }
196 else if (GS(parent->name) == ID_LA) {
197 but = uiDefButV(block,
200 0,
201 0,
202 UI_UNIT_X * 10,
203 UI_UNIT_Y,
204 pr_texture,
205 10,
207 "");
208 button_func_set(but, [](bContext &C) {
210 });
211 }
212 else if (GS(parent->name) == ID_WO) {
213 but = uiDefButV(block,
216 0,
217 0,
218 UI_UNIT_X * 10,
219 UI_UNIT_Y,
220 pr_texture,
221 10,
223 "");
224 button_func_set(but, [](bContext &C) {
226 });
227 }
228 else if (GS(parent->name) == ID_LS) {
229 but = uiDefButV(block,
231 IFACE_("Line Style"),
232 0,
233 0,
234 UI_UNIT_X * 10,
235 UI_UNIT_Y,
236 pr_texture,
237 10,
239 "");
240 button_func_set(but, [](bContext &C) {
242 });
243 }
244 but = uiDefButV(block,
246 IFACE_("Both"),
247 0,
248 0,
249 UI_UNIT_X * 10,
250 UI_UNIT_Y,
251 pr_texture,
252 10,
254 "");
255 button_func_set(but, [](bContext &C) {
257 });
258
259 /* Alpha button for texture preview */
260 if (*pr_texture != TEX_PR_OTHER) {
261 row = &layout->row(false);
262 row->prop(&texture_ptr, "use_preview_alpha", UI_ITEM_NONE, std::nullopt, ICON_NONE);
263 }
264 }
265 }
266}
267
268} // namespace blender::ui
Blender kernel freestyle line style functionality.
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define BLT_I18NCONTEXT_ID_WORLD
#define BLT_I18NCONTEXT_ID_LIGHT
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_ITEM_NONE
#define ND_SHADING_PREVIEW
Definition WM_types.hh:523
#define NC_MATERIAL
Definition WM_types.hh:393
#define ELEM(...)
#define offsetof(t, d)
void button_func_set(Button *but, std::function< void(bContext &)> func)
Button * uiDefIconButV(Block *block, ButtonType type, int icon, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
Button * uiDefButV(Block *block, ButtonType type, StringRef str, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
void template_preview(Layout *layout, bContext *C, ID *id, bool show_buttons, ID *parent, MTex *slot, const char *preview_id)
void button_func_drawextra_set(Block *block, std::function< void(const bContext *C, rcti *rect)> func)
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
PointerRNA RNA_id_pointer_create(ID *id)
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
bool BKE_scene_uses_blender_eevee(const Scene *scene)
Definition scene.cc:3077
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Scene * CTX_data_scene(const bContext *C)
void ED_preview_draw(const bContext *C, void *idp, void *parentp, void *slotp, uiPreview *ui_preview, rcti *rect)
ARegion * CTX_wm_region(const bContext *C)
ListBaseT< uiPreview > ui_previews
char name[258]
Definition DNA_ID.h:433
unsigned int session_uid
Definition DNA_ID.h:463
unsigned int id_session_uid
void scale_x_set(float scale)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)