1 /* This program is free software; you can redistribute it and/or
2  * modify it under the terms of the GNU General Public License
3  * as published by the Free Software Foundation; either version 2
4  * of the License, or (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software  Foundation,
13  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14  */
15 
16 /** \file
17  * \ingroup modifiers
18  */
19 
20 #include "BLI_listbase.h"
21 #include "BLI_string.h"
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "BKE_context.h"
26 #include "BKE_gpencil_modifier.h"
27 #include "BKE_material.h"
28 #include "BKE_object.h"
29 #include "BKE_screen.h"
30 
31 #include "DNA_object_force_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_particle_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 
37 #include "ED_object.h"
38 
39 #include "BLT_translation.h"
40 
41 #include "UI_interface.h"
42 #include "UI_resources.h"
43 
44 #include "RNA_access.h"
45 
46 #include "WM_api.h"
47 #include "WM_types.h"
48 
49 #include "MOD_gpencil_ui_common.h" /* Self include */
50 
51 /**
52  * Poll function so these modifier panels only show for grease pencil objects.
53  */
gpencil_modifier_ui_poll(const bContext * C,PanelType * UNUSED (pt))54 static bool gpencil_modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
55 {
56   Object *ob = ED_object_active_context(C);
57 
58   return (ob != NULL) && (ob->type == OB_GPENCIL);
59 }
60 
61 /* -------------------------------------------------------------------- */
62 /** \name Panel Drag and Drop, Expansion Saving
63  * \{ */
64 
65 /**
66  * Move a modifier to the index it's moved to after a drag and drop.
67  */
gpencil_modifier_reorder(bContext * C,Panel * panel,int new_index)68 static void gpencil_modifier_reorder(bContext *C, Panel *panel, int new_index)
69 {
70   PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
71   GpencilModifierData *md = (GpencilModifierData *)md_ptr->data;
72 
73   PointerRNA props_ptr;
74   wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_gpencil_modifier_move_to_index", false);
75   WM_operator_properties_create_ptr(&props_ptr, ot);
76   RNA_string_set(&props_ptr, "modifier", md->name);
77   RNA_int_set(&props_ptr, "index", new_index);
78   WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &props_ptr);
79   WM_operator_properties_free(&props_ptr);
80 }
81 
get_gpencil_modifier_expand_flag(const bContext * UNUSED (C),Panel * panel)82 static short get_gpencil_modifier_expand_flag(const bContext *UNUSED(C), Panel *panel)
83 {
84   PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
85   GpencilModifierData *md = (GpencilModifierData *)md_ptr->data;
86   return md->ui_expand_flag;
87 }
88 
set_gpencil_modifier_expand_flag(const bContext * UNUSED (C),Panel * panel,short expand_flag)89 static void set_gpencil_modifier_expand_flag(const bContext *UNUSED(C),
90                                              Panel *panel,
91                                              short expand_flag)
92 {
93   PointerRNA *md_ptr = UI_panel_custom_data_get(panel);
94   GpencilModifierData *md = (GpencilModifierData *)md_ptr->data;
95   md->ui_expand_flag = expand_flag;
96 }
97 
98 /** \} */
99 
100 /* -------------------------------------------------------------------- */
101 /** \name Modifier Panel Layouts
102  * \{ */
103 
gpencil_modifier_masking_panel_draw(Panel * panel,bool use_material,bool use_vertex)104 void gpencil_modifier_masking_panel_draw(Panel *panel, bool use_material, bool use_vertex)
105 {
106   uiLayout *row, *col, *sub;
107   uiLayout *layout = panel->layout;
108 
109   PointerRNA ob_ptr;
110   PointerRNA *ptr = gpencil_modifier_panel_get_property_pointers(panel, &ob_ptr);
111 
112   PointerRNA obj_data_ptr = RNA_pointer_get(&ob_ptr, "data");
113   bool has_layer = RNA_string_length(ptr, "layer") != 0;
114 
115   uiLayoutSetPropSep(layout, true);
116 
117   col = uiLayoutColumn(layout, true);
118   row = uiLayoutRow(col, true);
119   uiItemPointerR(row, ptr, "layer", &obj_data_ptr, "layers", NULL, ICON_GREASEPENCIL);
120   sub = uiLayoutRow(row, true);
121   uiLayoutSetActive(sub, has_layer);
122   uiLayoutSetPropDecorate(sub, false);
123   uiItemR(sub, ptr, "invert_layers", 0, "", ICON_ARROW_LEFTRIGHT);
124 
125   row = uiLayoutRow(col, true);
126   uiItemR(row, ptr, "layer_pass", 0, NULL, ICON_NONE);
127   sub = uiLayoutRow(row, true);
128   uiLayoutSetActive(sub, RNA_int_get(ptr, "layer_pass") != 0);
129   uiLayoutSetPropDecorate(sub, false);
130   uiItemR(sub, ptr, "invert_layer_pass", 0, "", ICON_ARROW_LEFTRIGHT);
131 
132   if (use_material) {
133     PointerRNA material_ptr = RNA_pointer_get(ptr, "material");
134     bool has_material = !RNA_pointer_is_null(&material_ptr);
135 
136     /* Because the Gpencil modifier material property used to be a string in an earlier version of
137      * Blender, we need to check if the material is valid and display it differently if so. */
138     bool valid = false;
139     {
140       if (!has_material) {
141         valid = true;
142       }
143       else {
144         Material *current_material = material_ptr.data;
145         Object *ob = ob_ptr.data;
146         for (int i = 0; i <= ob->totcol; i++) {
147           Material *mat = BKE_object_material_get(ob, i);
148           if (mat == current_material) {
149             valid = true;
150             break;
151           }
152         }
153       }
154     }
155 
156     col = uiLayoutColumn(layout, true);
157     row = uiLayoutRow(col, true);
158     uiLayoutSetRedAlert(row, !valid);
159     uiItemPointerR(row,
160                    ptr,
161                    "material",
162                    &obj_data_ptr,
163                    "materials",
164                    NULL,
165                    valid ? ICON_SHADING_TEXTURE : ICON_ERROR);
166     sub = uiLayoutRow(row, true);
167     uiLayoutSetActive(sub, has_material);
168     uiLayoutSetPropDecorate(sub, false);
169     uiItemR(sub, ptr, "invert_materials", 0, "", ICON_ARROW_LEFTRIGHT);
170 
171     row = uiLayoutRow(col, true);
172     uiItemR(row, ptr, "pass_index", 0, NULL, ICON_NONE);
173     sub = uiLayoutRow(row, true);
174     uiLayoutSetActive(sub, RNA_int_get(ptr, "pass_index") != 0);
175     uiLayoutSetPropDecorate(sub, false);
176     uiItemR(sub, ptr, "invert_material_pass", 0, "", ICON_ARROW_LEFTRIGHT);
177   }
178 
179   if (use_vertex) {
180     bool has_vertex_group = RNA_string_length(ptr, "vertex_group") != 0;
181 
182     row = uiLayoutRow(layout, true);
183     uiItemPointerR(row, ptr, "vertex_group", &ob_ptr, "vertex_groups", NULL, ICON_NONE);
184     sub = uiLayoutRow(row, true);
185     uiLayoutSetActive(sub, has_vertex_group);
186     uiLayoutSetPropDecorate(sub, false);
187     uiItemR(sub, ptr, "invert_vertex", 0, "", ICON_ARROW_LEFTRIGHT);
188   }
189 }
190 
gpencil_modifier_curve_header_draw(const bContext * UNUSED (C),Panel * panel)191 void gpencil_modifier_curve_header_draw(const bContext *UNUSED(C), Panel *panel)
192 {
193   uiLayout *layout = panel->layout;
194 
195   PointerRNA *ptr = gpencil_modifier_panel_get_property_pointers(panel, NULL);
196 
197   uiItemR(layout, ptr, "use_custom_curve", 0, NULL, ICON_NONE);
198 }
199 
gpencil_modifier_curve_panel_draw(const bContext * UNUSED (C),Panel * panel)200 void gpencil_modifier_curve_panel_draw(const bContext *UNUSED(C), Panel *panel)
201 {
202   uiLayout *layout = panel->layout;
203 
204   PointerRNA *ptr = gpencil_modifier_panel_get_property_pointers(panel, NULL);
205 
206   uiTemplateCurveMapping(layout, ptr, "curve", 0, false, false, false, false);
207 }
208 
209 /**
210  * Draw modifier error message.
211  */
gpencil_modifier_panel_end(uiLayout * layout,PointerRNA * ptr)212 void gpencil_modifier_panel_end(uiLayout *layout, PointerRNA *ptr)
213 {
214   GpencilModifierData *md = ptr->data;
215   if (md->error) {
216     uiLayout *row = uiLayoutRow(layout, false);
217     uiItemL(row, IFACE_(md->error), ICON_ERROR);
218   }
219 }
220 
221 /**
222  * Gets RNA pointers for the active object and the panel's modifier data.
223  */
224 #define ERROR_LIBDATA_MESSAGE TIP_("External library data")
gpencil_modifier_panel_get_property_pointers(Panel * panel,PointerRNA * r_ob_ptr)225 PointerRNA *gpencil_modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
226 {
227   PointerRNA *ptr = UI_panel_custom_data_get(panel);
228   BLI_assert(RNA_struct_is_a(ptr->type, &RNA_GpencilModifier));
229 
230   if (r_ob_ptr != NULL) {
231     RNA_pointer_create(ptr->owner_id, &RNA_Object, ptr->owner_id, r_ob_ptr);
232   }
233 
234   uiBlock *block = uiLayoutGetBlock(panel->layout);
235   UI_block_lock_clear(block);
236   UI_block_lock_set(block, ID_IS_LINKED((Object *)ptr->owner_id), ERROR_LIBDATA_MESSAGE);
237 
238   uiLayoutSetContextPointer(panel->layout, "modifier", ptr);
239 
240   return ptr;
241 }
242 
gpencil_modifier_ops_extra_draw(bContext * C,uiLayout * layout,void * md_v)243 static void gpencil_modifier_ops_extra_draw(bContext *C, uiLayout *layout, void *md_v)
244 {
245   PointerRNA op_ptr;
246   uiLayout *row;
247   GpencilModifierData *md = (GpencilModifierData *)md_v;
248   const GpencilModifierTypeInfo *mti = BKE_gpencil_modifier_get_info(md->type);
249 
250   PointerRNA ptr;
251   Object *ob = ED_object_active_context(C);
252   RNA_pointer_create(&ob->id, &RNA_GpencilModifier, md, &ptr);
253   uiLayoutSetContextPointer(layout, "modifier", &ptr);
254   uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
255 
256   uiLayoutSetUnitsX(layout, 4.0f);
257 
258   /* Apply. */
259   if (!(mti->flags & eGpencilModifierTypeFlag_NoApply)) {
260     uiItemO(layout,
261             CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Apply"),
262             ICON_CHECKMARK,
263             "OBJECT_OT_gpencil_modifier_apply");
264   }
265 
266   /* Duplicate. */
267   uiItemO(layout,
268           CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Duplicate"),
269           ICON_DUPLICATE,
270           "OBJECT_OT_gpencil_modifier_copy");
271 
272   uiItemS(layout);
273 
274   /* Move to first. */
275   row = uiLayoutColumn(layout, false);
276   uiItemFullO(row,
277               "OBJECT_OT_gpencil_modifier_move_to_index",
278               IFACE_("Move to First"),
279               ICON_TRIA_UP,
280               NULL,
281               WM_OP_INVOKE_DEFAULT,
282               0,
283               &op_ptr);
284   RNA_int_set(&op_ptr, "index", 0);
285   if (!md->prev) {
286     uiLayoutSetEnabled(row, false);
287   }
288 
289   /* Move to last. */
290   row = uiLayoutColumn(layout, false);
291   uiItemFullO(row,
292               "OBJECT_OT_gpencil_modifier_move_to_index",
293               IFACE_("Move to Last"),
294               ICON_TRIA_DOWN,
295               NULL,
296               WM_OP_INVOKE_DEFAULT,
297               0,
298               &op_ptr);
299   RNA_int_set(&op_ptr, "index", BLI_listbase_count(&ob->greasepencil_modifiers) - 1);
300   if (!md->next) {
301     uiLayoutSetEnabled(row, false);
302   }
303 }
304 
gpencil_modifier_panel_header(const bContext * UNUSED (C),Panel * panel)305 static void gpencil_modifier_panel_header(const bContext *UNUSED(C), Panel *panel)
306 {
307   uiLayout *row, *sub;
308   uiLayout *layout = panel->layout;
309 
310   PointerRNA *ptr = UI_panel_custom_data_get(panel);
311   GpencilModifierData *md = (GpencilModifierData *)ptr->data;
312 
313   uiLayoutSetContextPointer(panel->layout, "modifier", ptr);
314 
315   const GpencilModifierTypeInfo *mti = BKE_gpencil_modifier_get_info(md->type);
316   bool narrow_panel = (panel->sizex < UI_UNIT_X * 9 && panel->sizex != 0);
317 
318   /* Modifier Icon. */
319   row = uiLayoutRow(layout, false);
320   if (mti->isDisabled && mti->isDisabled(md, 0)) {
321     uiLayoutSetRedAlert(row, true);
322   }
323   uiItemL(row, "", RNA_struct_ui_icon(ptr->type));
324 
325   /* Modifier name. */
326   row = uiLayoutRow(layout, true);
327   if (!narrow_panel) {
328     uiItemR(row, ptr, "name", 0, "", ICON_NONE);
329   }
330   else {
331     uiLayoutSetAlignment(row, UI_LAYOUT_ALIGN_RIGHT);
332   }
333 
334   /* Display mode buttons. */
335   if (mti->flags & eGpencilModifierTypeFlag_SupportsEditmode) {
336     sub = uiLayoutRow(row, true);
337     uiItemR(sub, ptr, "show_in_editmode", 0, "", ICON_NONE);
338   }
339   uiItemR(row, ptr, "show_viewport", 0, "", ICON_NONE);
340   uiItemR(row, ptr, "show_render", 0, "", ICON_NONE);
341 
342   /* Extra operators. */
343   // row = uiLayoutRow(layout, true);
344   uiItemMenuF(row, "", ICON_DOWNARROW_HLT, gpencil_modifier_ops_extra_draw, md);
345 
346   /* Remove button. */
347   sub = uiLayoutRow(row, true);
348   uiLayoutSetEmboss(sub, UI_EMBOSS_NONE);
349   uiItemO(sub, "", ICON_X, "OBJECT_OT_gpencil_modifier_remove");
350 
351   /* Extra padding. */
352   uiItemS(layout);
353 }
354 
355 /** \} */
356 
357 /* -------------------------------------------------------------------- */
358 /** \name Modifier Registration Helpers
359  * \{ */
360 
361 /**
362  * Create a panel in the context's region
363  */
gpencil_modifier_panel_register(ARegionType * region_type,GpencilModifierType type,PanelDrawFn draw)364 PanelType *gpencil_modifier_panel_register(ARegionType *region_type,
365                                            GpencilModifierType type,
366                                            PanelDrawFn draw)
367 {
368 
369   /* Get the name for the modifier's panel. */
370   char panel_idname[BKE_ST_MAXNAME];
371   BKE_gpencil_modifierType_panel_id(type, panel_idname);
372 
373   PanelType *panel_type = MEM_callocN(sizeof(PanelType), panel_idname);
374 
375   BLI_strncpy(panel_type->idname, panel_idname, BKE_ST_MAXNAME);
376   BLI_strncpy(panel_type->label, "", BKE_ST_MAXNAME);
377   BLI_strncpy(panel_type->context, "modifier", BKE_ST_MAXNAME);
378   BLI_strncpy(panel_type->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA, BKE_ST_MAXNAME);
379 
380   panel_type->draw_header = gpencil_modifier_panel_header;
381   panel_type->draw = draw;
382   panel_type->poll = gpencil_modifier_ui_poll;
383 
384   /* Give the panel the special flag that says it was built here and corresponds to a
385    * modifier rather than a #PanelType. */
386   panel_type->flag = PNL_LAYOUT_HEADER_EXPAND | PNL_DRAW_BOX | PNL_INSTANCED;
387   panel_type->reorder = gpencil_modifier_reorder;
388   panel_type->get_list_data_expand_flag = get_gpencil_modifier_expand_flag;
389   panel_type->set_list_data_expand_flag = set_gpencil_modifier_expand_flag;
390 
391   BLI_addtail(&region_type->paneltypes, panel_type);
392 
393   return panel_type;
394 }
395 
396 /**
397  * Add a child panel to the parent.
398  *
399  * \note To create the panel type's idname, it appends the \a name argument to the \a parent's
400  * idname.
401  */
gpencil_modifier_subpanel_register(ARegionType * region_type,const char * name,const char * label,PanelDrawFn draw_header,PanelDrawFn draw,PanelType * parent)402 PanelType *gpencil_modifier_subpanel_register(ARegionType *region_type,
403                                               const char *name,
404                                               const char *label,
405                                               PanelDrawFn draw_header,
406                                               PanelDrawFn draw,
407                                               PanelType *parent)
408 {
409   /* Create the subpanel's ID name. */
410   char panel_idname[BKE_ST_MAXNAME];
411   BLI_snprintf(panel_idname, BKE_ST_MAXNAME, "%s_%s", parent->idname, name);
412 
413   PanelType *panel_type = MEM_callocN(sizeof(PanelType), panel_idname);
414 
415   BLI_strncpy(panel_type->idname, panel_idname, BKE_ST_MAXNAME);
416   BLI_strncpy(panel_type->label, label, BKE_ST_MAXNAME);
417   BLI_strncpy(panel_type->context, "modifier", BKE_ST_MAXNAME);
418   BLI_strncpy(panel_type->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA, BKE_ST_MAXNAME);
419 
420   panel_type->draw_header = draw_header;
421   panel_type->draw = draw;
422   panel_type->poll = gpencil_modifier_ui_poll;
423   panel_type->flag = (PNL_DEFAULT_CLOSED | PNL_DRAW_BOX);
424 
425   BLI_assert(parent != NULL);
426   BLI_strncpy(panel_type->parent_id, parent->idname, BKE_ST_MAXNAME);
427   panel_type->parent = parent;
428   BLI_addtail(&parent->children, BLI_genericNodeN(panel_type));
429   BLI_addtail(&region_type->paneltypes, panel_type);
430 
431   return panel_type;
432 }
433 
434 /** \} */
435