Blender  V3.3
FX_ui_common.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_listbase.h"
8 #include "BLI_string.h"
9 
10 #include "MEM_guardedalloc.h"
11 
12 #include "BKE_context.h"
13 #include "BKE_object.h"
14 #include "BKE_screen.h"
15 #include "BKE_shader_fx.h"
16 
17 #include "DNA_object_types.h"
18 #include "DNA_scene_types.h"
19 #include "DNA_screen_types.h"
20 #include "DNA_shader_fx_types.h"
21 
22 #include "ED_object.h"
23 
24 #include "BLT_translation.h"
25 
26 #include "UI_interface.h"
27 #include "UI_resources.h"
28 
29 #include "RNA_access.h"
30 #include "RNA_prototypes.h"
31 
32 #include "WM_api.h"
33 #include "WM_types.h"
34 
35 #include "FX_ui_common.h" /* Self include */
36 
37 /* -------------------------------------------------------------------- */
44 static void shaderfx_reorder(bContext *C, Panel *panel, int new_index)
45 {
46  PointerRNA *fx_ptr = UI_panel_custom_data_get(panel);
47  ShaderFxData *fx = (ShaderFxData *)fx_ptr->data;
48 
49  PointerRNA props_ptr;
50  wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_shaderfx_move_to_index", false);
52  RNA_string_set(&props_ptr, "shaderfx", fx->name);
53  RNA_int_set(&props_ptr, "index", new_index);
55  WM_operator_properties_free(&props_ptr);
56 }
57 
61 static short get_shaderfx_expand_flag(const bContext *UNUSED(C), Panel *panel)
62 {
63  PointerRNA *fx_ptr = UI_panel_custom_data_get(panel);
64  ShaderFxData *fx = (ShaderFxData *)fx_ptr->data;
65  return fx->ui_expand_flag;
66 }
67 
71 static void set_shaderfx_expand_flag(const bContext *UNUSED(C), Panel *panel, short expand_flag)
72 {
73  PointerRNA *fx_ptr = UI_panel_custom_data_get(panel);
74  ShaderFxData *fx = (ShaderFxData *)fx_ptr->data;
75  fx->ui_expand_flag = expand_flag;
76 }
77 
80 /* -------------------------------------------------------------------- */
85 {
86  ShaderFxData *fx = ptr->data;
87  if (fx->error) {
88  uiLayout *row = uiLayoutRow(layout, false);
89  uiItemL(row, TIP_(fx->error), ICON_ERROR);
90  }
91 }
92 
94 {
96  BLI_assert(RNA_struct_is_a(ptr->type, &RNA_ShaderFx));
97 
98  if (r_ob_ptr != NULL) {
99  RNA_pointer_create(ptr->owner_id, &RNA_Object, ptr->owner_id, r_ob_ptr);
100  }
101 
102  UI_panel_context_pointer_set(panel, "shaderfx", ptr);
103 
104  return ptr;
105 }
106 
107 #define ERROR_LIBDATA_MESSAGE TIP_("External library data")
108 
109 static void gpencil_shaderfx_ops_extra_draw(bContext *C, uiLayout *layout, void *fx_v)
110 {
111  PointerRNA op_ptr;
112  uiLayout *row;
113  ShaderFxData *fx = (ShaderFxData *)fx_v;
114 
115  PointerRNA ptr;
117  RNA_pointer_create(&ob->id, &RNA_ShaderFx, fx, &ptr);
118  uiLayoutSetContextPointer(layout, "shaderfx", &ptr);
120 
121  uiLayoutSetUnitsX(layout, 4.0f);
122 
124 
125  /* Duplicate. */
126  uiItemO(layout,
128  ICON_DUPLICATE,
129  "OBJECT_OT_shaderfx_copy");
130 
131  uiItemS(layout);
132 
133  /* Move to first. */
134  row = uiLayoutColumn(layout, false);
135  uiItemFullO(row,
136  "OBJECT_OT_shaderfx_move_to_index",
137  IFACE_("Move to First"),
138  ICON_TRIA_UP,
139  NULL,
141  0,
142  &op_ptr);
143  RNA_int_set(&op_ptr, "index", 0);
144  if (!fx->prev) {
145  uiLayoutSetEnabled(row, false);
146  }
147 
148  /* Move to last. */
149  row = uiLayoutColumn(layout, false);
150  uiItemFullO(row,
151  "OBJECT_OT_shaderfx_move_to_index",
152  IFACE_("Move to Last"),
153  ICON_TRIA_DOWN,
154  NULL,
156  0,
157  &op_ptr);
158  RNA_int_set(&op_ptr, "index", BLI_listbase_count(&ob->shader_fx) - 1);
159  if (!fx->next) {
160  uiLayoutSetEnabled(row, false);
161  }
162 }
163 
164 static void shaderfx_panel_header(const bContext *UNUSED(C), Panel *panel)
165 {
166  uiLayout *layout = panel->layout;
167  bool narrow_panel = (panel->sizex < UI_UNIT_X * 7 && panel->sizex != 0);
168 
170  Object *ob = (Object *)ptr->owner_id;
171  ShaderFxData *fx = (ShaderFxData *)ptr->data;
172 
173  const ShaderFxTypeInfo *fxti = BKE_shaderfx_get_info(fx->type);
174 
176 
177  /* Effect type icon. */
178  uiLayout *row = uiLayoutRow(layout, false);
179  if (fxti->isDisabled && fxti->isDisabled(fx, 0)) {
180  uiLayoutSetRedAlert(row, true);
181  }
182  uiItemL(row, "", RNA_struct_ui_icon(ptr->type));
183 
184  /* Effect name. */
185  row = uiLayoutRow(layout, true);
186  if (!narrow_panel) {
187  uiItemR(row, ptr, "name", 0, "", ICON_NONE);
188  }
189 
190  /* Mode enabling buttons. */
192  uiLayout *sub = uiLayoutRow(row, true);
193  uiLayoutSetActive(sub, false);
194  uiItemR(sub, ptr, "show_in_editmode", 0, "", ICON_NONE);
195  }
196  uiItemR(row, ptr, "show_viewport", 0, "", ICON_NONE);
197  uiItemR(row, ptr, "show_render", 0, "", ICON_NONE);
198 
199  /* Extra operators. */
200  uiItemMenuF(row, "", ICON_DOWNARROW_HLT, gpencil_shaderfx_ops_extra_draw, fx);
201 
202  row = uiLayoutRow(row, false);
204  uiItemO(row, "", ICON_X, "OBJECT_OT_shaderfx_remove");
205 
206  /* Some padding so the X isn't too close to the drag icon. */
207  uiItemS(layout);
208 }
209 
212 /* -------------------------------------------------------------------- */
216 static bool shaderfx_ui_poll(const bContext *C, PanelType *UNUSED(pt))
217 {
219 
220  return (ob != NULL) && (ob->type == OB_GPENCIL);
221 }
222 
224 {
225  PanelType *panel_type = MEM_callocN(sizeof(PanelType), __func__);
226 
227  BKE_shaderfxType_panel_id(type, panel_type->idname);
228  BLI_strncpy(panel_type->label, "", BKE_ST_MAXNAME);
229  BLI_strncpy(panel_type->context, "shaderfx", BKE_ST_MAXNAME);
231 
232  panel_type->draw_header = shaderfx_panel_header;
233  panel_type->draw = draw;
234  panel_type->poll = shaderfx_ui_poll;
235 
236  /* Give the panel the special flag that says it was built here and corresponds to a
237  * shader effect rather than a PanelType. */
239  panel_type->reorder = shaderfx_reorder;
242 
243  BLI_addtail(&region_type->paneltypes, panel_type);
244 
245  return panel_type;
246 }
247 
249  const char *name,
250  const char *label,
251  PanelDrawFn draw_header,
252  PanelDrawFn draw,
253  PanelType *parent)
254 {
255  PanelType *panel_type = MEM_callocN(sizeof(PanelType), __func__);
256 
257  BLI_snprintf(panel_type->idname, BKE_ST_MAXNAME, "%s_%s", parent->idname, name);
258  BLI_strncpy(panel_type->label, label, BKE_ST_MAXNAME);
259  BLI_strncpy(panel_type->context, "shaderfx", BKE_ST_MAXNAME);
261 
262  panel_type->draw_header = draw_header;
263  panel_type->draw = draw;
264  panel_type->poll = shaderfx_ui_poll;
265  panel_type->flag = PANEL_TYPE_DEFAULT_CLOSED;
266 
267  BLI_assert(parent != NULL);
268  BLI_strncpy(panel_type->parent_id, parent->idname, BKE_ST_MAXNAME);
269  panel_type->parent = parent;
270  BLI_addtail(&parent->children, BLI_genericNodeN(panel_type));
271  BLI_addtail(&region_type->paneltypes, panel_type);
272 
273  return panel_type;
274 }
275 
General operations, lookup, etc. for blender objects.
@ PANEL_TYPE_INSTANCED
Definition: BKE_screen.h:285
@ PANEL_TYPE_DEFAULT_CLOSED
Definition: BKE_screen.h:279
@ PANEL_TYPE_HEADER_EXPAND
Definition: BKE_screen.h:282
#define BKE_ST_MAXNAME
Definition: BKE_screen.h:53
void BKE_shaderfxType_panel_id(ShaderFxType type, char *r_idname)
Definition: shader_fx.c:155
const ShaderFxTypeInfo * BKE_shaderfx_get_info(ShaderFxType type)
Definition: shader_fx.c:139
@ eShaderFxTypeFlag_SupportsEditmode
Definition: BKE_shader_fx.h:39
#define BLI_assert(a)
Definition: BLI_assert.h:46
struct LinkData * BLI_genericNodeN(void *data)
Definition: listbase.c:842
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define UNUSED(x)
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:566
Object is a sort of wrapper for general info.
@ OB_GPENCIL
ShaderFxType
struct Object * ED_object_active_context(const struct bContext *C)
static void set_shaderfx_expand_flag(const bContext *UNUSED(C), Panel *panel, short expand_flag)
Definition: FX_ui_common.c:71
PanelType * shaderfx_panel_register(ARegionType *region_type, ShaderFxType type, PanelDrawFn draw)
Definition: FX_ui_common.c:223
static void shaderfx_panel_header(const bContext *UNUSED(C), Panel *panel)
Definition: FX_ui_common.c:164
static short get_shaderfx_expand_flag(const bContext *UNUSED(C), Panel *panel)
Definition: FX_ui_common.c:61
static bool shaderfx_ui_poll(const bContext *C, PanelType *UNUSED(pt))
Definition: FX_ui_common.c:216
static void shaderfx_reorder(bContext *C, Panel *panel, int new_index)
Definition: FX_ui_common.c:44
PanelType * shaderfx_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
Definition: FX_ui_common.c:248
void shaderfx_panel_end(uiLayout *layout, PointerRNA *ptr)
Definition: FX_ui_common.c:84
static void gpencil_shaderfx_ops_extra_draw(bContext *C, uiLayout *layout, void *fx_v)
Definition: FX_ui_common.c:109
PointerRNA * shaderfx_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
Definition: FX_ui_common.c:93
#define ERROR_LIBDATA_MESSAGE
Definition: FX_ui_common.c:107
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void uiLayoutSetActive(uiLayout *layout, bool active)
uiBlock * uiLayoutGetBlock(uiLayout *layout)
@ UI_EMBOSS_NONE
Definition: UI_interface.h:109
void uiLayoutSetEnabled(uiLayout *layout, bool enabled)
void UI_panel_context_pointer_set(struct Panel *panel, const char *name, struct PointerRNA *ptr)
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiItemL(uiLayout *layout, const char *name, int icon)
void uiLayoutSetRedAlert(uiLayout *layout, bool redalert)
struct PointerRNA * UI_panel_custom_data_get(const struct Panel *panel)
void uiItemS(uiLayout *layout)
uiLayout * uiLayoutRow(uiLayout *layout, bool align)
void uiLayoutSetUnitsX(uiLayout *layout, float unit)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiLayoutSetEmboss(uiLayout *layout, eUIEmbossType emboss)
void uiItemO(uiLayout *layout, const char *name, int icon, const char *opname)
void uiLayoutSetContextPointer(uiLayout *layout, const char *name, struct PointerRNA *ptr)
void uiItemFullO(uiLayout *layout, const char *opname, const char *name, int icon, struct IDProperty *properties, wmOperatorCallContext context, int flag, struct PointerRNA *r_opptr)
void uiItemMenuF(uiLayout *layout, const char *name, int icon, uiMenuCreateFunc func, void *arg)
#define UI_UNIT_X
void UI_block_flag_enable(uiBlock *block, int flag)
Definition: interface.cc:5848
void uiLayoutSetOperatorContext(uiLayout *layout, wmOperatorCallContext opcontext)
void UI_block_lock_set(uiBlock *block, bool val, const char *lockstr)
Definition: interface.cc:2273
@ UI_BLOCK_IS_FLIP
Definition: UI_interface.h:136
@ WM_OP_INVOKE_DEFAULT
Definition: WM_types.h:201
const char * label
void(* PanelDrawFn)(const bContext *, struct Panel *)
Definition: fmodifier_ui.c:46
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
Definition: rna_access.c:695
void RNA_pointer_create(ID *id, StructRNA *type, void *data, PointerRNA *r_ptr)
Definition: rna_access.c:136
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:5155
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:4921
int RNA_struct_ui_icon(const StructRNA *type)
Definition: rna_access.c:601
ListBase paneltypes
Definition: BKE_screen.h:198
ListBase shader_fx
short(* get_list_data_expand_flag)(const struct bContext *C, struct Panel *pa)
Definition: BKE_screen.h:260
void(* draw)(const struct bContext *C, struct Panel *panel)
Definition: BKE_screen.h:248
bool(* poll)(const struct bContext *C, struct PanelType *pt)
Definition: BKE_screen.h:242
void(* draw_header)(const struct bContext *C, struct Panel *panel)
Definition: BKE_screen.h:244
char idname[BKE_ST_MAXNAME]
Definition: BKE_screen.h:223
void(* set_list_data_expand_flag)(const struct bContext *C, struct Panel *pa, short expand_flag)
Definition: BKE_screen.h:267
char context[BKE_ST_MAXNAME]
Definition: BKE_screen.h:227
void(* reorder)(struct bContext *C, struct Panel *pa, int new_index)
Definition: BKE_screen.h:253
char translation_context[BKE_ST_MAXNAME]
Definition: BKE_screen.h:226
ListBase children
Definition: BKE_screen.h:271
struct PanelType * parent
Definition: BKE_screen.h:270
char label[BKE_ST_MAXNAME]
Definition: BKE_screen.h:224
char parent_id[BKE_ST_MAXNAME]
Definition: BKE_screen.h:230
struct uiLayout * layout
struct StructRNA * type
Definition: RNA_types.h:37
void * data
Definition: RNA_types.h:38
struct ID * owner_id
Definition: RNA_types.h:36
struct ShaderFxData * prev
struct ShaderFxData * next
ShaderFxTypeFlag flags
Definition: BKE_shader_fx.h:75
bool(* isDisabled)(struct ShaderFxData *fx, int userRenderParams)
int WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wmOperatorCallContext context, PointerRNA *properties, const wmEvent *event)
PointerRNA * ptr
Definition: wm_files.c:3480
wmOperatorType * ot
Definition: wm_files.c:3479
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
Definition: wm_operators.c:661
void WM_operator_properties_free(PointerRNA *ptr)
Definition: wm_operators.c:783