Blender  V3.3
MOD_gpencil_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_gpencil_modifier.h"
14 #include "BKE_material.h"
15 #include "BKE_screen.h"
16 
17 #include "DNA_material_types.h"
18 #include "DNA_object_types.h"
19 #include "DNA_screen_types.h"
20 
21 #include "ED_object.h"
22 
23 #include "BLT_translation.h"
24 
25 #include "UI_interface.h"
26 #include "UI_resources.h"
27 
28 #include "RNA_access.h"
29 #include "RNA_prototypes.h"
30 
31 #include "WM_api.h"
32 #include "WM_types.h"
33 
34 #include "MOD_gpencil_ui_common.h" /* Self include */
35 
40 {
42 
43  return (ob != NULL) && (ob->type == OB_GPENCIL);
44 }
45 
46 /* -------------------------------------------------------------------- */
53 static void gpencil_modifier_reorder(bContext *C, Panel *panel, int new_index)
54 {
55  PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
57 
58  PointerRNA props_ptr;
59  wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_gpencil_modifier_move_to_index", false);
61  RNA_string_set(&props_ptr, "modifier", md->name);
62  RNA_int_set(&props_ptr, "index", new_index);
64  WM_operator_properties_free(&props_ptr);
65 }
66 
68 {
69  PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
71  return md->ui_expand_flag;
72 }
73 
75  Panel *panel,
76  short expand_flag)
77 {
78  PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
80  md->ui_expand_flag = expand_flag;
81 }
82 
85 /* -------------------------------------------------------------------- */
89 void gpencil_modifier_masking_panel_draw(Panel *panel, bool use_material, bool use_vertex)
90 {
91  uiLayout *row, *col, *sub;
92  uiLayout *layout = panel->layout;
93 
94  PointerRNA ob_ptr;
96 
97  PointerRNA obj_data_ptr = RNA_pointer_get(&ob_ptr, "data");
98  bool has_layer = RNA_string_length(ptr, "layer") != 0;
99 
100  uiLayoutSetPropSep(layout, true);
101 
102  col = uiLayoutColumn(layout, true);
103  row = uiLayoutRow(col, true);
104  uiItemPointerR(row, ptr, "layer", &obj_data_ptr, "layers", NULL, ICON_GREASEPENCIL);
105  sub = uiLayoutRow(row, true);
106  uiLayoutSetActive(sub, has_layer);
107  uiLayoutSetPropDecorate(sub, false);
108  uiItemR(sub, ptr, "invert_layers", 0, "", ICON_ARROW_LEFTRIGHT);
109 
110  row = uiLayoutRow(col, true);
111  uiItemR(row, ptr, "layer_pass", 0, NULL, ICON_NONE);
112  sub = uiLayoutRow(row, true);
113  uiLayoutSetActive(sub, RNA_int_get(ptr, "layer_pass") != 0);
114  uiLayoutSetPropDecorate(sub, false);
115  uiItemR(sub, ptr, "invert_layer_pass", 0, "", ICON_ARROW_LEFTRIGHT);
116 
117  if (use_material) {
118  PointerRNA material_ptr = RNA_pointer_get(ptr, "material");
119  bool has_material = !RNA_pointer_is_null(&material_ptr);
120 
121  /* Because the Gpencil modifier material property used to be a string in an earlier version of
122  * Blender, we need to check if the material is valid and display it differently if so. */
123  bool valid = false;
124  {
125  if (!has_material) {
126  valid = true;
127  }
128  else {
129  Material *current_material = material_ptr.data;
130  Object *ob = ob_ptr.data;
131  for (int i = 0; i <= ob->totcol; i++) {
132  Material *mat = BKE_object_material_get(ob, i);
133  if (mat == current_material) {
134  valid = true;
135  break;
136  }
137  }
138  }
139  }
140 
141  col = uiLayoutColumn(layout, true);
142  row = uiLayoutRow(col, true);
143  uiLayoutSetRedAlert(row, !valid);
144  uiItemPointerR(row,
145  ptr,
146  "material",
147  &obj_data_ptr,
148  "materials",
149  NULL,
150  valid ? ICON_SHADING_TEXTURE : ICON_ERROR);
151  sub = uiLayoutRow(row, true);
152  uiLayoutSetActive(sub, has_material);
153  uiLayoutSetPropDecorate(sub, false);
154  uiItemR(sub, ptr, "invert_materials", 0, "", ICON_ARROW_LEFTRIGHT);
155 
156  row = uiLayoutRow(col, true);
157  uiItemR(row, ptr, "pass_index", 0, NULL, ICON_NONE);
158  sub = uiLayoutRow(row, true);
159  uiLayoutSetActive(sub, RNA_int_get(ptr, "pass_index") != 0);
160  uiLayoutSetPropDecorate(sub, false);
161  uiItemR(sub, ptr, "invert_material_pass", 0, "", ICON_ARROW_LEFTRIGHT);
162  }
163 
164  if (use_vertex) {
165  bool has_vertex_group = RNA_string_length(ptr, "vertex_group") != 0;
166 
167  row = uiLayoutRow(layout, true);
168  uiItemPointerR(row, ptr, "vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
169  sub = uiLayoutRow(row, true);
170  uiLayoutSetActive(sub, has_vertex_group);
171  uiLayoutSetPropDecorate(sub, false);
172  uiItemR(sub, ptr, "invert_vertex", 0, "", ICON_ARROW_LEFTRIGHT);
173  }
174 }
175 
177 {
178  uiLayout *layout = panel->layout;
179 
181 
182  uiItemR(layout, ptr, "use_custom_curve", 0, NULL, ICON_NONE);
183 }
184 
186 {
187  uiLayout *layout = panel->layout;
188 
190 
191  uiTemplateCurveMapping(layout, ptr, "curve", 0, false, false, false, false);
192 }
193 
195 {
197  if (md->error) {
198  uiLayout *row = uiLayoutRow(layout, false);
199  uiItemL(row, IFACE_(md->error), ICON_ERROR);
200  }
201 }
202 
206 #define ERROR_LIBDATA_MESSAGE TIP_("External library data")
208 {
210  BLI_assert(RNA_struct_is_a(ptr->type, &RNA_GpencilModifier));
211 
212  if (r_ob_ptr != NULL) {
213  RNA_pointer_create(ptr->owner_id, &RNA_Object, ptr->owner_id, r_ob_ptr);
214  }
215 
216  uiBlock *block = uiLayoutGetBlock(panel->layout);
217  UI_block_lock_clear(block);
219 
220  UI_panel_context_pointer_set(panel, "modifier", ptr);
221 
222  return ptr;
223 }
224 
225 static void gpencil_modifier_ops_extra_draw(bContext *C, uiLayout *layout, void *md_v)
226 {
227  PointerRNA op_ptr;
228  uiLayout *row;
231 
232  PointerRNA ptr;
234  RNA_pointer_create(&ob->id, &RNA_GpencilModifier, md, &ptr);
235  uiLayoutSetContextPointer(layout, "modifier", &ptr);
237 
238  uiLayoutSetUnitsX(layout, 4.0f);
239 
241 
242  /* Apply. */
243  if (!(mti->flags & eGpencilModifierTypeFlag_NoApply)) {
244  uiItemO(layout,
246  ICON_CHECKMARK,
247  "OBJECT_OT_gpencil_modifier_apply");
248  }
249 
250  /* Duplicate. */
251  uiItemO(layout,
253  ICON_DUPLICATE,
254  "OBJECT_OT_gpencil_modifier_copy");
255 
256  uiItemO(layout,
257  CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Copy to Selected"),
258  0,
259  "OBJECT_OT_gpencil_modifier_copy_to_selected");
260 
261  uiItemS(layout);
262 
263  /* Move to first. */
264  row = uiLayoutColumn(layout, false);
265  uiItemFullO(row,
266  "OBJECT_OT_gpencil_modifier_move_to_index",
267  IFACE_("Move to First"),
268  ICON_TRIA_UP,
269  NULL,
271  0,
272  &op_ptr);
273  RNA_int_set(&op_ptr, "index", 0);
274  if (!md->prev) {
275  uiLayoutSetEnabled(row, false);
276  }
277 
278  /* Move to last. */
279  row = uiLayoutColumn(layout, false);
280  uiItemFullO(row,
281  "OBJECT_OT_gpencil_modifier_move_to_index",
282  IFACE_("Move to Last"),
283  ICON_TRIA_DOWN,
284  NULL,
286  0,
287  &op_ptr);
288  RNA_int_set(&op_ptr, "index", BLI_listbase_count(&ob->greasepencil_modifiers) - 1);
289  if (!md->next) {
290  uiLayoutSetEnabled(row, false);
291  }
292 }
293 
295 {
296  uiLayout *row, *sub;
297  uiLayout *layout = panel->layout;
298 
301 
302  UI_panel_context_pointer_set(panel, "modifier", ptr);
303 
305  bool narrow_panel = (panel->sizex < UI_UNIT_X * 9 && panel->sizex != 0);
306 
307  /* Modifier Icon. */
308  row = uiLayoutRow(layout, false);
309  if (mti->isDisabled && mti->isDisabled(md, 0)) {
310  uiLayoutSetRedAlert(row, true);
311  }
312  uiItemL(row, "", RNA_struct_ui_icon(ptr->type));
313 
314  /* Modifier name. */
315  row = uiLayoutRow(layout, true);
316  if (!narrow_panel) {
317  uiItemR(row, ptr, "name", 0, "", ICON_NONE);
318  }
319  else {
321  }
322 
323  /* Display mode buttons. */
325  sub = uiLayoutRow(row, true);
326  uiItemR(sub, ptr, "show_in_editmode", 0, "", ICON_NONE);
327  }
328  uiItemR(row, ptr, "show_viewport", 0, "", ICON_NONE);
329  uiItemR(row, ptr, "show_render", 0, "", ICON_NONE);
330 
331  /* Extra operators. */
332  // row = uiLayoutRow(layout, true);
333  uiItemMenuF(row, "", ICON_DOWNARROW_HLT, gpencil_modifier_ops_extra_draw, md);
334 
335  /* Remove button. */
336  sub = uiLayoutRow(row, false);
338  uiItemO(sub, "", ICON_X, "OBJECT_OT_gpencil_modifier_remove");
339 
340  /* Extra padding. */
341  uiItemS(layout);
342 }
343 
346 /* -------------------------------------------------------------------- */
352  PanelDrawFn draw)
353 {
354  PanelType *panel_type = MEM_callocN(sizeof(PanelType), __func__);
355 
357  BLI_strncpy(panel_type->label, "", BKE_ST_MAXNAME);
358  BLI_strncpy(panel_type->context, "modifier", BKE_ST_MAXNAME);
360 
362  panel_type->draw = draw;
363  panel_type->poll = gpencil_modifier_ui_poll;
364 
365  /* Give the panel the special flag that says it was built here and corresponds to a
366  * modifier rather than a #PanelType. */
368  panel_type->reorder = gpencil_modifier_reorder;
371 
372  BLI_addtail(&region_type->paneltypes, panel_type);
373 
374  return panel_type;
375 }
376 
378  const char *name,
379  const char *label,
380  PanelDrawFn draw_header,
381  PanelDrawFn draw,
382  PanelType *parent)
383 {
384  PanelType *panel_type = MEM_callocN(sizeof(PanelType), __func__);
385 
386  BLI_snprintf(panel_type->idname, BKE_ST_MAXNAME, "%s_%s", parent->idname, name);
387  BLI_strncpy(panel_type->label, label, BKE_ST_MAXNAME);
388  BLI_strncpy(panel_type->context, "modifier", BKE_ST_MAXNAME);
390 
391  panel_type->draw_header = draw_header;
392  panel_type->draw = draw;
393  panel_type->poll = gpencil_modifier_ui_poll;
394  panel_type->flag = PANEL_TYPE_DEFAULT_CLOSED;
395 
396  BLI_assert(parent != NULL);
397  BLI_strncpy(panel_type->parent_id, parent->idname, BKE_ST_MAXNAME);
398  panel_type->parent = parent;
399  BLI_addtail(&parent->children, BLI_genericNodeN(panel_type));
400  BLI_addtail(&region_type->paneltypes, panel_type);
401 
402  return panel_type;
403 }
404 
void BKE_gpencil_modifierType_panel_id(GpencilModifierType type, char *r_idname)
@ eGpencilModifierTypeFlag_SupportsEditmode
@ eGpencilModifierTypeFlag_NoApply
const GpencilModifierTypeInfo * BKE_gpencil_modifier_get_info(GpencilModifierType type)
General operations, lookup, etc. for materials.
struct Material * BKE_object_material_get(struct Object *ob, short act)
Definition: material.c:687
@ 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
#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 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
struct Object * ED_object_active_context(const struct bContext *C)
_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.
PointerRNA * gpencil_modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
static void gpencil_modifier_ops_extra_draw(bContext *C, uiLayout *layout, void *md_v)
void gpencil_modifier_masking_panel_draw(Panel *panel, bool use_material, bool use_vertex)
void gpencil_modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
static void set_gpencil_modifier_expand_flag(const bContext *UNUSED(C), Panel *panel, short expand_flag)
static void gpencil_modifier_panel_header(const bContext *UNUSED(C), Panel *panel)
PanelType * gpencil_modifier_subpanel_register(ARegionType *region_type, const char *name, const char *label, PanelDrawFn draw_header, PanelDrawFn draw, PanelType *parent)
PanelType * gpencil_modifier_panel_register(ARegionType *region_type, GpencilModifierType type, PanelDrawFn draw)
void gpencil_modifier_curve_header_draw(const bContext *UNUSED(C), Panel *panel)
static bool gpencil_modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
static void gpencil_modifier_reorder(bContext *C, Panel *panel, int new_index)
static short get_gpencil_modifier_expand_flag(const bContext *UNUSED(C), Panel *panel)
#define ERROR_LIBDATA_MESSAGE
void gpencil_modifier_curve_panel_draw(const bContext *UNUSED(C), Panel *panel)
#define C
Definition: RandGen.cpp:25
@ UI_LAYOUT_ALIGN_RIGHT
void uiLayoutSetActive(uiLayout *layout, bool active)
uiBlock * uiLayoutGetBlock(uiLayout *layout)
@ UI_EMBOSS_NONE
Definition: UI_interface.h:109
void uiTemplateCurveMapping(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int type, bool levels, bool brush, bool neg_slope, bool tone)
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 UI_block_lock_clear(uiBlock *block)
Definition: interface.cc:2281
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 uiLayoutSetAlignment(uiLayout *layout, char alignment)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
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 uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
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 uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon)
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
uint col
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
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5167
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
Definition: rna_access.c:4921
int RNA_int_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4910
int RNA_struct_ui_icon(const StructRNA *type)
Definition: rna_access.c:601
int RNA_string_length(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5144
bool RNA_pointer_is_null(const PointerRNA *ptr)
Definition: rna_access.c:164
ListBase paneltypes
Definition: BKE_screen.h:198
struct GpencilModifierData * next
struct GpencilModifierData * prev
GpencilModifierTypeFlag flags
bool(* isDisabled)(struct GpencilModifierData *md, int userRenderParams)
ListBase greasepencil_modifiers
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
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