Cleanup: style
[blender.git] / source / blender / editors / gpencil / gpencil_fill.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2017, Blender Foundation
17  * This is a new part of Blender
18  */
19
20 /** \file
21  * \ingroup edgpencil
22  */
23
24 #include <stdio.h>
25
26 #include "MEM_guardedalloc.h"
27
28 #include "BLI_utildefines.h"
29 #include "BLI_blenlib.h"
30 #include "BLI_math.h"
31 #include "BLI_stack.h"
32
33 #include "BLT_translation.h"
34
35 #include "DNA_brush_types.h"
36 #include "DNA_gpencil_types.h"
37 #include "DNA_image_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_windowmanager_types.h"
41
42 #include "BKE_main.h"
43 #include "BKE_brush.h"
44 #include "BKE_deform.h"
45 #include "BKE_image.h"
46 #include "BKE_gpencil.h"
47 #include "BKE_material.h"
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50 #include "BKE_paint.h"
51
52 #include "ED_gpencil.h"
53 #include "ED_screen.h"
54 #include "ED_space_api.h"
55 #include "ED_view3d.h"
56
57 #include "RNA_access.h"
58 #include "RNA_define.h"
59
60 #include "IMB_imbuf.h"
61 #include "IMB_imbuf_types.h"
62
63 #include "GPU_immediate.h"
64 #include "GPU_draw.h"
65 #include "GPU_matrix.h"
66 #include "GPU_framebuffer.h"
67 #include "GPU_state.h"
68
69 #include "UI_interface.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "DEG_depsgraph.h"
75 #include "DEG_depsgraph_query.h"
76
77 #include "gpencil_intern.h"
78
79 #define LEAK_HORZ 0
80 #define LEAK_VERT 1
81
82
83   /* Temporary fill operation data (op->customdata) */
84 typedef struct tGPDfill {
85         bContext *C;
86         struct Main *bmain;
87         struct Depsgraph *depsgraph;
88         /** window where painting originated */
89         struct wmWindow *win;
90         /** current scene from context */
91         struct Scene *scene;
92         /** current active gp object */
93         struct Object *ob;
94         /** area where painting originated */
95         struct ScrArea *sa;
96         /** region where painting originated */
97         struct RegionView3D *rv3d;
98         /** view3 where painting originated */
99         struct View3D *v3d;
100         /** region where painting originated */
101         struct ARegion *ar;
102         /** current GP datablock */
103         struct bGPdata *gpd;
104         /** current material */
105         struct Material *mat;
106         /** layer */
107         struct bGPDlayer *gpl;
108         /** frame */
109         struct bGPDframe *gpf;
110
111         /** flags */
112         short flag;
113         /** avoid too fast events */
114         short oldkey;
115         /** send to back stroke */
116         bool on_back;
117
118         /** mouse fill center position */
119         int center[2];
120         /** windows width */
121         int sizex;
122         /** window height */
123         int sizey;
124         /** lock to viewport axis */
125         int lock_axis;
126
127         /** number of pixel to consider the leak is too small (x 2) */
128         short fill_leak;
129         /** factor for transparency */
130         float fill_threshold;
131         /** number of simplify steps */
132         int fill_simplylvl;
133         /** boundary limits drawing mode */
134         int fill_draw_mode;
135         /* scaling factor */
136         short fill_factor;
137
138         /** number of elements currently in cache */
139         short sbuffer_size;
140         /** temporary points */
141         void *sbuffer;
142         /** depth array for reproject */
143         float *depth_arr;
144
145         /** temp image */
146         Image *ima;
147         /** temp points data */
148         BLI_Stack *stack;
149         /** handle for drawing strokes while operator is running 3d stuff */
150         void *draw_handle_3d;
151
152         /* tmp size x */
153         int bwinx;
154         /* tmp size y */
155         int bwiny;
156         rcti brect;
157
158 } tGPDfill;
159
160
161 /* draw a given stroke using same thickness and color for all points */
162 static void gp_draw_basic_stroke(
163         tGPDfill *tgpf, bGPDstroke *gps, const float diff_mat[4][4],
164         const bool cyclic, const float ink[4], const int flag, const float thershold)
165 {
166         bGPDspoint *points = gps->points;
167
168         Material *ma = tgpf->mat;
169         MaterialGPencilStyle *gp_style = ma->gp_style;
170
171         int totpoints = gps->totpoints;
172         float fpt[3];
173         float col[4];
174
175         copy_v4_v4(col, ink);
176
177         /* if cyclic needs more vertex */
178         int cyclic_add = (cyclic) ? 1 : 0;
179
180         GPUVertFormat *format = immVertexFormat();
181         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
182         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
183
184         immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR);
185
186         /* draw stroke curve */
187         GPU_line_width(1.0f);
188         immBeginAtMost(GPU_PRIM_LINE_STRIP, totpoints + cyclic_add);
189         const bGPDspoint *pt = points;
190
191         for (int i = 0; i < totpoints; i++, pt++) {
192
193                 if (flag & GP_BRUSH_FILL_HIDE) {
194                         float alpha = gp_style->stroke_rgba[3] * pt->strength;
195                         CLAMP(alpha, 0.0f, 1.0f);
196                         col[3] = alpha <= thershold ? 0.0f : 1.0f;
197                 }
198                 else {
199                         col[3] = 1.0f;
200                 }
201                 /* set point */
202                 immAttr4fv(color, col);
203                 mul_v3_m4v3(fpt, diff_mat, &pt->x);
204                 immVertex3fv(pos, fpt);
205         }
206
207         if (cyclic && totpoints > 2) {
208                 /* draw line to first point to complete the cycle */
209                 immAttr4fv(color, col);
210                 mul_v3_m4v3(fpt, diff_mat, &points->x);
211                 immVertex3fv(pos, fpt);
212         }
213
214         immEnd();
215         immUnbindProgram();
216 }
217
218 /* loop all layers */
219 static void gp_draw_datablock(tGPDfill *tgpf, const float ink[4])
220 {
221         /* duplicated: etempFlags */
222         enum {
223                 GP_DRAWFILLS_NOSTATUS = (1 << 0),   /* don't draw status info */
224                 GP_DRAWFILLS_ONLY3D = (1 << 1),   /* only draw 3d-strokes */
225         };
226
227         Object *ob = tgpf->ob;
228         bGPdata *gpd = tgpf->gpd;
229         int cfra_eval = (int)DEG_get_ctime(tgpf->depsgraph);
230
231         tGPDdraw tgpw;
232         tgpw.rv3d = tgpf->rv3d;
233         tgpw.depsgraph = tgpf->depsgraph;
234         tgpw.ob = ob;
235         tgpw.gpd = gpd;
236         tgpw.offsx = 0;
237         tgpw.offsy = 0;
238         tgpw.winx = tgpf->ar->winx;
239         tgpw.winy = tgpf->ar->winy;
240         tgpw.dflag = 0;
241         tgpw.disable_fill = 1;
242         tgpw.dflag |= (GP_DRAWFILLS_ONLY3D | GP_DRAWFILLS_NOSTATUS);
243
244         GPU_blend(true);
245
246         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
247                 /* calculate parent position */
248                 ED_gpencil_parent_location(tgpw.depsgraph, ob, gpd, gpl, tgpw.diff_mat);
249
250                 /* do not draw layer if hidden */
251                 if (gpl->flag & GP_LAYER_HIDE)
252                         continue;
253
254                 /* if active layer and no keyframe, create a new one */
255                 if (gpl == tgpf->gpl) {
256                         if ((gpl->actframe == NULL) || (gpl->actframe->framenum != cfra_eval)) {
257                                 BKE_gpencil_layer_getframe(gpl, cfra_eval, GP_GETFRAME_ADD_NEW);
258                         }
259                 }
260
261                 /* get frame to draw */
262                 bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, cfra_eval, GP_GETFRAME_USE_PREV);
263                 if (gpf == NULL)
264                         continue;
265
266                 for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
267                         /* check if stroke can be drawn */
268                         if ((gps->points == NULL) || (gps->totpoints < 2)) {
269                                 continue;
270                         }
271                         /* check if the color is visible */
272                         MaterialGPencilStyle *gp_style = BKE_material_gpencil_settings_get(ob, gps->mat_nr + 1);
273                         if ((gp_style == NULL) || (gp_style->flag & GP_STYLE_COLOR_HIDE)) {
274                                 continue;
275                         }
276
277                         tgpw.gps = gps;
278                         tgpw.gpl = gpl;
279                         tgpw.gpf = gpf;
280                         tgpw.t_gpf = gpf;
281
282                         /* reduce thickness to avoid gaps */
283                         tgpw.is_fill_stroke = (tgpf->fill_draw_mode == GP_FILL_DMODE_CONTROL) ? false : true ;
284                         tgpw.lthick = gpl->line_change;
285                         tgpw.opacity = 1.0;
286                         copy_v4_v4(tgpw.tintcolor, ink);
287                         tgpw.onion = true;
288                         tgpw.custonion = true;
289
290                         bool textured_stroke = (gp_style->stroke_style == GP_STYLE_STROKE_STYLE_TEXTURE);
291
292                         /* normal strokes */
293                         if (((tgpf->fill_draw_mode == GP_FILL_DMODE_STROKE) ||
294                              (tgpf->fill_draw_mode == GP_FILL_DMODE_BOTH)) &&
295                             !textured_stroke)
296                         {
297                                 ED_gp_draw_fill(&tgpw);
298                         }
299
300                         /* 3D Lines with basic shapes and invisible lines */
301                         if ((tgpf->fill_draw_mode == GP_FILL_DMODE_CONTROL) ||
302                             (tgpf->fill_draw_mode == GP_FILL_DMODE_BOTH) ||
303                             textured_stroke)
304                         {
305                                 gp_draw_basic_stroke(
306                                         tgpf, gps, tgpw.diff_mat, gps->flag & GP_STROKE_CYCLIC, ink,
307                                         tgpf->flag, tgpf->fill_threshold);
308                         }
309                 }
310         }
311
312         GPU_blend(false);
313 }
314
315 /* draw strokes in offscreen buffer */
316 static bool gp_render_offscreen(tGPDfill *tgpf)
317 {
318         bool is_ortho = false;
319         float winmat[4][4];
320
321         if (!tgpf->gpd) {
322                 return false;
323         }
324
325         /* set temporary new size */
326         tgpf->bwinx = tgpf->ar->winx;
327         tgpf->bwiny = tgpf->ar->winy;
328         tgpf->brect = tgpf->ar->winrct;
329
330         /* resize ar */
331         tgpf->ar->winrct.xmin = 0;
332         tgpf->ar->winrct.ymin = 0;
333         tgpf->ar->winrct.xmax = (int)tgpf->ar->winx * tgpf->fill_factor;
334         tgpf->ar->winrct.ymax = (int)tgpf->ar->winy * tgpf->fill_factor;
335         tgpf->ar->winx = (short)abs(tgpf->ar->winrct.xmax - tgpf->ar->winrct.xmin);
336         tgpf->ar->winy = (short)abs(tgpf->ar->winrct.ymax - tgpf->ar->winrct.ymin);
337
338         /* save new size */
339         tgpf->sizex = (int)tgpf->ar->winx;
340         tgpf->sizey = (int)tgpf->ar->winy;
341
342         /* adjust center */
343         float center[2];
344         center[0] = (float)tgpf->center[0] * ((float)tgpf->ar->winx / (float)tgpf->bwinx);
345         center[1] = (float)tgpf->center[1] * ((float)tgpf->ar->winy / (float)tgpf->bwiny);
346         round_v2i_v2fl(tgpf->center, center);
347
348         char err_out[256] = "unknown";
349         GPUOffScreen *offscreen = GPU_offscreen_create(tgpf->sizex, tgpf->sizey, 0, true, false, err_out);
350         if (offscreen == NULL) {
351                 printf("GPencil - Fill - Unable to create fill buffer\n");
352                 return false;
353         }
354
355         GPU_offscreen_bind(offscreen, true);
356         uint flag = IB_rect | IB_rectfloat;
357         ImBuf *ibuf = IMB_allocImBuf(tgpf->sizex, tgpf->sizey, 32, flag);
358
359         rctf viewplane;
360         float clip_start, clip_end;
361
362         is_ortho = ED_view3d_viewplane_get(
363                 tgpf->depsgraph, tgpf->v3d, tgpf->rv3d, tgpf->sizex, tgpf->sizey,
364                 &viewplane, &clip_start, &clip_end, NULL);
365         if (is_ortho) {
366                 orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clip_end, clip_end);
367         }
368         else {
369                 perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clip_start, clip_end);
370         }
371
372         GPU_matrix_push_projection();
373         GPU_matrix_identity_set();
374         GPU_matrix_push();
375         GPU_matrix_identity_set();
376
377         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
379
380         ED_view3d_update_viewmat(
381                 tgpf->depsgraph, tgpf->scene, tgpf->v3d, tgpf->ar,
382                 NULL, winmat, NULL);
383         /* set for opengl */
384         GPU_matrix_projection_set(tgpf->rv3d->winmat);
385         GPU_matrix_set(tgpf->rv3d->viewmat);
386
387         /* draw strokes */
388         float ink[4] = {1.0f, 0.0f, 0.0f, 1.0f};
389         gp_draw_datablock(tgpf, ink);
390
391         GPU_matrix_pop_projection();
392         GPU_matrix_pop();
393
394         /* create a image to see result of template */
395         if (ibuf->rect_float) {
396                 GPU_offscreen_read_pixels(offscreen, GL_FLOAT, ibuf->rect_float);
397         }
398         else if (ibuf->rect) {
399                 GPU_offscreen_read_pixels(offscreen, GL_UNSIGNED_BYTE, ibuf->rect);
400         }
401         if (ibuf->rect_float && ibuf->rect) {
402                 IMB_rect_from_float(ibuf);
403         }
404
405         tgpf->ima = BKE_image_add_from_imbuf(tgpf->bmain, ibuf, "GP_fill");
406         tgpf->ima->id.tag |= LIB_TAG_DOIT;
407
408         BKE_image_release_ibuf(tgpf->ima, ibuf, NULL);
409
410         /* switch back to window-system-provided framebuffer */
411         GPU_offscreen_unbind(offscreen, true);
412         GPU_offscreen_free(offscreen);
413
414         return true;
415 }
416
417 /* return pixel data (rgba) at index */
418 static void get_pixel(const ImBuf *ibuf, const int idx, float r_col[4])
419 {
420         if (ibuf->rect_float) {
421                 const float *frgba = &ibuf->rect_float[idx * 4];
422                 copy_v4_v4(r_col, frgba);
423         }
424         else {
425                 /* XXX: This case probably doesn't happen, as we only write to the float buffer,
426                  * but we get compiler warnings about uninitialised vars otherwise
427                  */
428                 BLI_assert(!"gpencil_fill.c - get_pixel() non-float case is used!");
429                 zero_v4(r_col);
430         }
431 }
432
433 /* set pixel data (rgba) at index */
434 static void set_pixel(ImBuf *ibuf, int idx, const float col[4])
435 {
436         //BLI_assert(idx <= ibuf->x * ibuf->y);
437         if (ibuf->rect) {
438                 uint *rrect = &ibuf->rect[idx];
439                 uchar ccol[4];
440
441                 rgba_float_to_uchar(ccol, col);
442                 *rrect = *((uint *)ccol);
443         }
444
445         if (ibuf->rect_float) {
446                 float *rrectf = &ibuf->rect_float[idx * 4];
447                 copy_v4_v4(rrectf, col);
448         }
449 }
450
451 /* check if the size of the leak is narrow to determine if the stroke is closed
452  * this is used for strokes with small gaps between them to get a full fill
453  * and do not get a full screen fill.
454  *
455  * \param ibuf: Image pixel data
456  * \param maxpixel: Maximum index
457  * \param limit: Limit of pixels to analyze
458  * \param index: Index of current pixel
459  * \param type: 0-Horizontal 1-Vertical
460  */
461 static bool is_leak_narrow(ImBuf *ibuf, const int maxpixel, int limit, int index, int type)
462 {
463         float rgba[4];
464         int i;
465         int pt;
466         bool t_a = false;
467         bool t_b = false;
468
469         /* Horizontal leak (check vertical pixels)
470          * X
471          * X
472          * xB7
473          * X
474          * X
475          */
476         if (type == LEAK_HORZ) {
477                 /* pixels on top */
478                 for (i = 1; i <= limit; i++) {
479                         pt = index + (ibuf->x * i);
480                         if (pt <= maxpixel) {
481                                 get_pixel(ibuf, pt, rgba);
482                                 if (rgba[0] == 1.0f) {
483                                         t_a = true;
484                                         break;
485                                 }
486                         }
487                         else {
488                                 /* edge of image*/
489                                 t_a = true;
490                                 break;
491                         }
492                 }
493                 /* pixels on bottom */
494                 for (i = 1; i <= limit; i++) {
495                         pt = index - (ibuf->x * i);
496                         if (pt >= 0) {
497                                 get_pixel(ibuf, pt, rgba);
498                                 if (rgba[0] == 1.0f) {
499                                         t_b = true;
500                                         break;
501                                 }
502                         }
503                         else {
504                                 /* edge of image*/
505                                 t_b = true;
506                                 break;
507                         }
508                 }
509         }
510
511         /* Vertical leak (check horizontal pixels)
512          *
513          * XXXxB7XX
514          */
515         if (type == LEAK_VERT) {
516                 /* get pixel range of the row */
517                 int row = index / ibuf->x;
518                 int lowpix = row * ibuf->x;
519                 int higpix = lowpix + ibuf->x - 1;
520
521                 /* pixels to right */
522                 for (i = 0; i < limit; i++) {
523                         pt = index - (limit - i);
524                         if (pt >= lowpix) {
525                                 get_pixel(ibuf, pt, rgba);
526                                 if (rgba[0] == 1.0f) {
527                                         t_a = true;
528                                         break;
529                                 }
530                         }
531                         else {
532                                 t_a = true; /* edge of image*/
533                                 break;
534                         }
535                 }
536                 /* pixels to left */
537                 for (i = 0; i < limit; i++) {
538                         pt = index + (limit - i);
539                         if (pt <= higpix) {
540                                 get_pixel(ibuf, pt, rgba);
541                                 if (rgba[0] == 1.0f) {
542                                         t_b = true;
543                                         break;
544                                 }
545                         }
546                         else {
547                                 t_b = true; /* edge of image */
548                                 break;
549                         }
550                 }
551         }
552         return (bool)(t_a && t_b);
553 }
554
555 /* Boundary fill inside strokes
556  * Fills the space created by a set of strokes using the stroke color as the boundary
557  * of the shape to fill.
558  *
559  * \param tgpf: Temporary fill data
560  */
561 static void gpencil_boundaryfill_area(tGPDfill *tgpf)
562 {
563         ImBuf *ibuf;
564         float rgba[4];
565         void *lock;
566         const float fill_col[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
567         ibuf = BKE_image_acquire_ibuf(tgpf->ima, NULL, &lock);
568         const int maxpixel = (ibuf->x * ibuf->y) - 1;
569
570         BLI_Stack *stack = BLI_stack_new(sizeof(int), __func__);
571
572         /* calculate index of the seed point using the position of the mouse */
573         int index = (tgpf->sizex * tgpf->center[1]) + tgpf->center[0];
574         if ((index >= 0) && (index <= maxpixel)) {
575                 BLI_stack_push(stack, &index);
576         }
577
578         /* the fill use a stack to save the pixel list instead of the common recursive
579          * 4-contact point method.
580          * The problem with recursive calls is that for big fill areas, we can get max limit
581          * of recursive calls and STACK_OVERFLOW error.
582          *
583          * The 4-contact point analyze the pixels to the left, right, bottom and top
584          *      -----------
585          *      |    X    |
586          *      |   XoX   |
587          *      |    X    |
588          *      -----------
589          */
590         while (!BLI_stack_is_empty(stack)) {
591                 int v;
592
593                 BLI_stack_pop(stack, &v);
594
595                 get_pixel(ibuf, v, rgba);
596
597                 if (true) { /* Was: 'rgba' */
598                         /* check if no border(red) or already filled color(green) */
599                         if ((rgba[0] != 1.0f) && (rgba[1] != 1.0f)) {
600                                 /* fill current pixel with green */
601                                 set_pixel(ibuf, v, fill_col);
602
603                                 /* add contact pixels */
604                                 /* pixel left */
605                                 if (v - 1 >= 0) {
606                                         index = v - 1;
607                                         if (!is_leak_narrow(ibuf, maxpixel, tgpf->fill_leak, v, LEAK_HORZ)) {
608                                                 BLI_stack_push(stack, &index);
609                                         }
610                                 }
611                                 /* pixel right */
612                                 if (v + 1 <= maxpixel) {
613                                         index = v + 1;
614                                         if (!is_leak_narrow(ibuf, maxpixel, tgpf->fill_leak, v, LEAK_HORZ)) {
615                                                 BLI_stack_push(stack, &index);
616                                         }
617                                 }
618                                 /* pixel top */
619                                 if (v + ibuf->x <= maxpixel) {
620                                         index = v + ibuf->x;
621                                         if (!is_leak_narrow(ibuf, maxpixel, tgpf->fill_leak, v, LEAK_VERT)) {
622                                                 BLI_stack_push(stack, &index);
623                                         }
624                                 }
625                                 /* pixel bottom */
626                                 if (v - ibuf->x >= 0) {
627                                         index = v - ibuf->x;
628                                         if (!is_leak_narrow(ibuf, maxpixel, tgpf->fill_leak, v, LEAK_VERT)) {
629                                                 BLI_stack_push(stack, &index);
630                                         }
631                                 }
632                         }
633                 }
634         }
635
636         /* release ibuf */
637         if (ibuf) {
638                 BKE_image_release_ibuf(tgpf->ima, ibuf, lock);
639         }
640
641         tgpf->ima->id.tag |= LIB_TAG_DOIT;
642         /* free temp stack data */
643         BLI_stack_free(stack);
644 }
645
646 /* clean external border of image to avoid infinite loops */
647 static void gpencil_clean_borders(tGPDfill *tgpf)
648 {
649         ImBuf *ibuf;
650         void *lock;
651         const float fill_col[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
652         ibuf = BKE_image_acquire_ibuf(tgpf->ima, NULL, &lock);
653         int idx;
654         int pixel = 0;
655
656         /* horizontal lines */
657         for (idx = 0; idx < ibuf->x; idx++) {
658                 /* bottom line */
659                 set_pixel(ibuf, idx, fill_col);
660                 /* top line */
661                 pixel = idx + (ibuf->x * (ibuf->y - 1));
662                 set_pixel(ibuf, pixel, fill_col);
663         }
664         /* vertical lines */
665         for (idx = 0; idx < ibuf->y; idx++) {
666                 /* left line */
667                 set_pixel(ibuf, ibuf->x * idx, fill_col);
668                 /* right line */
669                 pixel = ibuf->x * idx + (ibuf->x - 1);
670                 set_pixel(ibuf, pixel, fill_col);
671         }
672
673         /* release ibuf */
674         if (ibuf) {
675                 BKE_image_release_ibuf(tgpf->ima, ibuf, lock);
676         }
677
678         tgpf->ima->id.tag |= LIB_TAG_DOIT;
679 }
680
681 /* Naive dilate
682  *
683  * Expand green areas into enclosing red areas.
684  * Using stack prevents creep when replacing colors directly.
685  * -----------
686  *  XXXXXXX
687  *  XoooooX
688  *  XXooXXX
689  *   XXXX
690  * -----------
691  */
692 static void dilate(ImBuf *ibuf)
693 {
694         BLI_Stack *stack = BLI_stack_new(sizeof(int), __func__);
695         const float green[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
696         const int maxpixel = (ibuf->x * ibuf->y) - 1;
697         /* detect pixels and expand into red areas */
698         for (int v = maxpixel; v != 0; v--) {
699                 float color[4];
700                 int index;
701                 int tp = 0;
702                 int bm = 0;
703                 int lt = 0;
704                 int rt = 0;
705                 get_pixel(ibuf, v, color);
706                 if (color[1] == 1.0f) {
707                         /* pixel left */
708                         if (v - 1 >= 0) {
709                                 index = v - 1;
710                                 get_pixel(ibuf, index, color);
711                                 if (color[0] == 1.0f) {
712                                         BLI_stack_push(stack, &index);
713                                         lt = index;
714                                 }
715                         }
716                         /* pixel right */
717                         if (v + 1 <= maxpixel) {
718                                 index = v + 1;
719                                 get_pixel(ibuf, index, color);
720                                 if (color[0] == 1.0f) {
721                                         BLI_stack_push(stack, &index);
722                                         rt = index;
723                                 }
724                         }
725                         /* pixel top */
726                         if (v + ibuf->x <= maxpixel) {
727                                 index = v + ibuf->x;
728                                 get_pixel(ibuf, index, color);
729                                 if (color[0] == 1.0f) {
730                                         BLI_stack_push(stack, &index);
731                                         tp = index;
732                                 }
733                         }
734                         /* pixel bottom */
735                         if (v - ibuf->x >= 0) {
736                                 index = v - ibuf->x;
737                                 get_pixel(ibuf, index, color);
738                                 if (color[0] == 1.0f) {
739                                         BLI_stack_push(stack, &index);
740                                         bm = index;
741                                 }
742                         }
743                         /* pixel top-left */
744                         if (tp && lt) {
745                                 index = tp - 1;
746                                 get_pixel(ibuf, index, color);
747                                 if (color[0] == 1.0f) {
748                                         BLI_stack_push(stack, &index);
749                                 }
750                         }
751                         /* pixel top-right */
752                         if (tp && rt) {
753                                 index = tp + 1;
754                                 get_pixel(ibuf, index, color);
755                                 if (color[0] == 1.0f) {
756                                         BLI_stack_push(stack, &index);
757                                 }
758                         }
759                         /* pixel bottom-left */
760                         if (bm && lt) {
761                                 index = bm - 1;
762                                 get_pixel(ibuf, index, color);
763                                 if (color[0] == 1.0f) {
764                                         BLI_stack_push(stack, &index);
765                                 }
766                         }
767                         /* pixel bottom-right */
768                         if (bm && rt) {
769                                 index = bm + 1;
770                                 get_pixel(ibuf, index, color);
771                                 if (color[0] == 1.0f) {
772                                         BLI_stack_push(stack, &index);
773                                 }
774                         }
775                 }
776         }
777         /* set dilated pixels */
778         while (!BLI_stack_is_empty(stack)) {
779                 int v;
780                 BLI_stack_pop(stack, &v);
781                 set_pixel(ibuf, v, green);
782         }
783         BLI_stack_free(stack);
784 }
785
786 /* Get the outline points of a shape using Moore Neighborhood algorithm
787  *
788  * This is a Blender customized version of the general algorithm described
789  * in https://en.wikipedia.org/wiki/Moore_neighborhood
790  */
791 static void gpencil_get_outline_points(tGPDfill *tgpf)
792 {
793         ImBuf *ibuf;
794         float rgba[4];
795         void *lock;
796         int v[2];
797         int boundary_co[2];
798         int start_co[2];
799         int backtracked_co[2];
800         int current_check_co[2];
801         int prev_check_co[2];
802         int backtracked_offset[1][2] = {{0, 0}};
803         // bool boundary_found = false;
804         bool start_found = false;
805         const int NEIGHBOR_COUNT = 8;
806
807         const int offset[8][2] = {
808                 {-1, -1},
809                 {0, -1},
810                 {1, -1},
811                 {1, 0},
812                 {1, 1},
813                 {0, 1},
814                 {-1, 1},
815                 {-1, 0},
816         };
817
818         tgpf->stack = BLI_stack_new(sizeof(int[2]), __func__);
819
820         ibuf = BKE_image_acquire_ibuf(tgpf->ima, NULL, &lock);
821         int imagesize = ibuf->x * ibuf->y;
822
823         /* dilate */
824         dilate(ibuf);
825
826         /* find the initial point to start outline analysis */
827         for (int idx = imagesize - 1; idx != 0; idx--) {
828                 get_pixel(ibuf, idx, rgba);
829                 if (rgba[1] == 1.0f) {
830                         boundary_co[0] = idx % ibuf->x;
831                         boundary_co[1] = idx / ibuf->x;
832                         copy_v2_v2_int(start_co, boundary_co);
833                         backtracked_co[0] = (idx - 1) % ibuf->x;
834                         backtracked_co[1] = (idx - 1) / ibuf->x;
835                         backtracked_offset[0][0] = backtracked_co[0] - boundary_co[0];
836                         backtracked_offset[0][1] = backtracked_co[1] - boundary_co[1];
837                         copy_v2_v2_int(prev_check_co, start_co);
838
839                         BLI_stack_push(tgpf->stack, &boundary_co);
840                         start_found = true;
841                         break;
842                 }
843         }
844
845         while (start_found) {
846                 int cur_back_offset = -1;
847                 for (int i = 0; i < NEIGHBOR_COUNT; i++) {
848                         if (backtracked_offset[0][0] == offset[i][0] &&
849                             backtracked_offset[0][1] == offset[i][1])
850                         {
851                                 /* Finding the bracktracked pixel offset index */
852                                 cur_back_offset = i;
853                                 break;
854                         }
855                 }
856
857                 int loop = 0;
858                 while (loop < (NEIGHBOR_COUNT - 1) && cur_back_offset != -1) {
859                         int offset_idx = (cur_back_offset + 1) % NEIGHBOR_COUNT;
860                         current_check_co[0] = boundary_co[0] + offset[offset_idx][0];
861                         current_check_co[1] = boundary_co[1] + offset[offset_idx][1];
862
863                         int image_idx = ibuf->x * current_check_co[1] + current_check_co[0];
864                         get_pixel(ibuf, image_idx, rgba);
865
866                         /* find next boundary pixel */
867                         if (rgba[1] == 1.0f) {
868                                 copy_v2_v2_int(boundary_co, current_check_co);
869                                 copy_v2_v2_int(backtracked_co, prev_check_co);
870                                 backtracked_offset[0][0] = backtracked_co[0] - boundary_co[0];
871                                 backtracked_offset[0][1] = backtracked_co[1] - boundary_co[1];
872
873                                 BLI_stack_push(tgpf->stack, &boundary_co);
874
875                                 break;
876                         }
877                         copy_v2_v2_int(prev_check_co, current_check_co);
878                         cur_back_offset++;
879                         loop++;
880                 }
881                 /* current pixel is equal to starting pixel */
882                 if (boundary_co[0] == start_co[0] &&
883                     boundary_co[1] == start_co[1])
884                 {
885                         BLI_stack_pop(tgpf->stack, &v);
886                         // boundary_found = true;
887                         break;
888                 }
889         }
890
891         /* release ibuf */
892         if (ibuf) {
893                 BKE_image_release_ibuf(tgpf->ima, ibuf, lock);
894         }
895 }
896
897 /* get z-depth array to reproject on surface */
898 static void gpencil_get_depth_array(tGPDfill *tgpf)
899 {
900         tGPspoint *ptc;
901         ToolSettings *ts = tgpf->scene->toolsettings;
902         int totpoints = tgpf->sbuffer_size;
903         int i = 0;
904
905         if (totpoints == 0) {
906                 return;
907         }
908
909         /* for surface sketching, need to set the right OpenGL context stuff so that
910          * the conversions will project the values correctly...
911          */
912         if (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_VIEW) {
913                 /* need to restore the original projection settings before packing up */
914                 view3d_region_operator_needs_opengl(tgpf->win, tgpf->ar);
915                 ED_view3d_autodist_init(tgpf->depsgraph, tgpf->ar, tgpf->v3d, 0);
916
917                 /* since strokes are so fine, when using their depth we need a margin otherwise they might get missed */
918                 int depth_margin = 0;
919
920                 /* get an array of depths, far depths are blended */
921                 int mval_prev[2] = { 0 };
922                 int interp_depth = 0;
923                 int found_depth = 0;
924
925                 tgpf->depth_arr = MEM_mallocN(sizeof(float) * totpoints, "depth_points");
926
927                 for (i = 0, ptc = tgpf->sbuffer; i < totpoints; i++, ptc++) {
928
929                         int mval_i[2];
930                         round_v2i_v2fl(mval_i, &ptc->x);
931
932                         if ((ED_view3d_autodist_depth(
933                                      tgpf->ar, mval_i, depth_margin, tgpf->depth_arr + i) == 0) &&
934                             (i && (ED_view3d_autodist_depth_seg(
935                                            tgpf->ar, mval_i, mval_prev, depth_margin + 1, tgpf->depth_arr + i) == 0)))
936                         {
937                                 interp_depth = true;
938                         }
939                         else {
940                                 found_depth = true;
941                         }
942
943                         copy_v2_v2_int(mval_prev, mval_i);
944                 }
945
946                 if (found_depth == false) {
947                         /* eeh... not much we can do.. :/, ignore depth in this case */
948                         for (i = totpoints - 1; i >= 0; i--)
949                                 tgpf->depth_arr[i] = 0.9999f;
950                 }
951                 else {
952                         if (interp_depth) {
953                                 interp_sparse_array(tgpf->depth_arr, totpoints, FLT_MAX);
954                         }
955                 }
956         }
957 }
958
959 /* create array of points using stack as source */
960 static void gpencil_points_from_stack(tGPDfill *tgpf)
961 {
962         tGPspoint *point2D;
963         int totpoints = BLI_stack_count(tgpf->stack);
964         if (totpoints == 0) {
965                 return;
966         }
967
968         tgpf->sbuffer_size = (short)totpoints;
969         tgpf->sbuffer = MEM_callocN(sizeof(tGPspoint) * totpoints, __func__);
970
971         point2D = tgpf->sbuffer;
972         while (!BLI_stack_is_empty(tgpf->stack)) {
973                 int v[2];
974                 BLI_stack_pop(tgpf->stack, &v);
975                 copy_v2fl_v2i(&point2D->x, v);
976                 /* shift points to center of pixel */
977                 add_v2_fl(&point2D->x, 0.5f);
978                 point2D->pressure = 1.0f;
979                 point2D->strength = 1.0f;
980                 point2D->time = 0.0f;
981                 point2D++;
982         }
983 }
984
985 /* create a grease pencil stroke using points in buffer */
986 static void gpencil_stroke_from_buffer(tGPDfill *tgpf)
987 {
988         const int cfra_eval = (int)DEG_get_ctime(tgpf->depsgraph);
989
990         ToolSettings *ts = tgpf->scene->toolsettings;
991         const char *align_flag = &ts->gpencil_v3d_align;
992         const bool is_depth = (bool)(*align_flag & (GP_PROJECT_DEPTH_VIEW | GP_PROJECT_DEPTH_STROKE));
993         const bool is_camera = (bool)(ts->gp_sculpt.lock_axis == 0) &&
994                 (tgpf->rv3d->persp == RV3D_CAMOB) && (!is_depth);
995         Brush *brush = BKE_paint_brush(&ts->gp_paint->paint);
996         if (brush == NULL) {
997                 return;
998         }
999
1000         bGPDspoint *pt;
1001         MDeformVert *dvert = NULL;
1002         tGPspoint *point2D;
1003
1004         if (tgpf->sbuffer_size == 0) {
1005                 return;
1006         }
1007
1008         /* get frame or create a new one */
1009         tgpf->gpf = BKE_gpencil_layer_getframe(tgpf->gpl, cfra_eval, GP_GETFRAME_ADD_NEW);
1010
1011         /* create new stroke */
1012         bGPDstroke *gps = MEM_callocN(sizeof(bGPDstroke), "bGPDstroke");
1013         gps->thickness = brush->size;
1014         gps->inittime = 0.0f;
1015
1016         /* the polygon must be closed, so enabled cyclic */
1017         gps->flag |= GP_STROKE_CYCLIC;
1018         gps->flag |= GP_STROKE_3DSPACE;
1019
1020         gps->mat_nr = BKE_gpencil_handle_material(tgpf->bmain, tgpf->ob, tgpf->mat);
1021
1022         /* allocate memory for storage points */
1023         gps->totpoints = tgpf->sbuffer_size;
1024         gps->points = MEM_callocN(sizeof(bGPDspoint) * tgpf->sbuffer_size, "gp_stroke_points");
1025
1026         /* initialize triangle memory to dummy data */
1027         gps->tot_triangles = 0;
1028         gps->triangles = NULL;
1029         gps->flag |= GP_STROKE_RECALC_GEOMETRY;
1030
1031         /* add stroke to frame */
1032         if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) || (tgpf->on_back == true)) {
1033                 BLI_addhead(&tgpf->gpf->strokes, gps);
1034         }
1035         else {
1036                 BLI_addtail(&tgpf->gpf->strokes, gps);
1037         }
1038
1039         /* add points */
1040         pt = gps->points;
1041         point2D = (tGPspoint *)tgpf->sbuffer;
1042
1043         const int def_nr = tgpf->ob->actdef - 1;
1044         const bool have_weight = (bool)BLI_findlink(&tgpf->ob->defbase, def_nr);
1045
1046         if ((ts->gpencil_flags & GP_TOOL_FLAG_CREATE_WEIGHTS) && (have_weight)) {
1047                 BKE_gpencil_dvert_ensure(gps);
1048                 dvert = gps->dvert;
1049         }
1050
1051         for (int i = 0; i < tgpf->sbuffer_size && point2D; i++, point2D++, pt++) {
1052                 /* convert screen-coordinates to 3D coordinates */
1053                 gp_stroke_convertcoords_tpoint(
1054                         tgpf->scene, tgpf->ar, tgpf->ob,
1055                         tgpf->gpl, point2D,
1056                         tgpf->depth_arr ? tgpf->depth_arr + i : NULL,
1057                         &pt->x);
1058
1059                 pt->pressure = 1.0f;
1060                 pt->strength = 1.0f;
1061                 pt->time = 0.0f;
1062
1063                 if ((ts->gpencil_flags & GP_TOOL_FLAG_CREATE_WEIGHTS) && (have_weight)) {
1064                         MDeformWeight *dw = defvert_verify_index(dvert, def_nr);
1065                         if (dw) {
1066                                 dw->weight = ts->vgroup_weight;
1067                         }
1068
1069                         dvert++;
1070                 }
1071                 else {
1072                         if (dvert != NULL) {
1073                                 dvert->totweight = 0;
1074                                 dvert->dw = NULL;
1075                                 dvert++;
1076                         }
1077                 }
1078         }
1079
1080         /* smooth stroke */
1081         float reduce = 0.0f;
1082         float smoothfac = 1.0f;
1083         for (int r = 0; r < 1; r++) {
1084                 for (int i = 0; i < gps->totpoints; i++) {
1085                         BKE_gpencil_smooth_stroke(gps, i, smoothfac - reduce);
1086                 }
1087                 reduce += 0.25f;  // reduce the factor
1088         }
1089
1090         /* if axis locked, reproject to plane locked */
1091         if ((tgpf->lock_axis > GP_LOCKAXIS_VIEW) && ((ts->gpencil_v3d_align & GP_PROJECT_DEPTH_VIEW) == 0)) {
1092                 float origin[3];
1093                 ED_gp_get_drawing_reference(
1094                         tgpf->scene, tgpf->ob, tgpf->gpl,
1095                         ts->gpencil_v3d_align, origin);
1096                 ED_gp_project_stroke_to_plane(
1097                         tgpf->scene, tgpf->ob, tgpf->rv3d, gps, origin,
1098                         tgpf->lock_axis - 1);
1099         }
1100
1101         /* if parented change position relative to parent object */
1102         for (int a = 0; a < tgpf->sbuffer_size; a++) {
1103                 pt = &gps->points[a];
1104                 gp_apply_parent_point(tgpf->depsgraph, tgpf->ob, tgpf->gpd, tgpf->gpl, pt);
1105         }
1106
1107         /* if camera view, reproject flat to view to avoid perspective effect */
1108         if (is_camera) {
1109                 ED_gpencil_project_stroke_to_view(tgpf->C, tgpf->gpl, gps);
1110         }
1111
1112         /* simplify stroke */
1113         for (int b = 0; b < tgpf->fill_simplylvl; b++) {
1114                 BKE_gpencil_simplify_fixed(gps);
1115         }
1116 }
1117
1118 /* ----------------------- */
1119 /* Drawing                 */
1120 /* Helper: Draw status message while the user is running the operator */
1121 static void gpencil_fill_status_indicators(bContext *C, tGPDfill *UNUSED(tgpf))
1122 {
1123         const char *status_str = IFACE_("Fill: ESC/RMB cancel, LMB Fill, Shift Draw on Back");
1124         ED_workspace_status_text(C, status_str);
1125 }
1126
1127 /* draw boundary lines to see fill limits */
1128 static void gpencil_draw_boundary_lines(const bContext *UNUSED(C), tGPDfill *tgpf)
1129 {
1130         if (!tgpf->gpd) {
1131                 return;
1132         }
1133         const float ink[4] = {1.0f, 0.0f, 0.0f, 1.0f};
1134         gp_draw_datablock(tgpf, ink);
1135 }
1136
1137 /* Drawing callback for modal operator in 3d mode */
1138 static void gpencil_fill_draw_3d(const bContext *C, ARegion *UNUSED(ar), void *arg)
1139 {
1140         tGPDfill *tgpf = (tGPDfill *)arg;
1141         /* draw only in the region that originated operator. This is required for multiwindow */
1142         ARegion *ar = CTX_wm_region(C);
1143         if (ar != tgpf->ar) {
1144                 return;
1145         }
1146
1147         gpencil_draw_boundary_lines(C, tgpf);
1148 }
1149
1150 /* check if context is suitable for filling */
1151 static bool gpencil_fill_poll(bContext *C)
1152 {
1153         Object *obact = CTX_data_active_object(C);
1154
1155         if (ED_operator_regionactive(C)) {
1156                 ScrArea *sa = CTX_wm_area(C);
1157                 if (sa->spacetype == SPACE_VIEW3D) {
1158                         if ((obact == NULL) ||
1159                             (obact->type != OB_GPENCIL) ||
1160                             (obact->mode != OB_MODE_PAINT_GPENCIL))
1161                         {
1162                                 return false;
1163                         }
1164
1165                         return true;
1166                 }
1167                 else {
1168                         CTX_wm_operator_poll_msg_set(C, "Active region not valid for filling operator");
1169                         return false;
1170                 }
1171         }
1172         else {
1173                 CTX_wm_operator_poll_msg_set(C, "Active region not set");
1174                 return false;
1175         }
1176 }
1177
1178 /* Allocate memory and initialize values */
1179 static tGPDfill *gp_session_init_fill(bContext *C, wmOperator *UNUSED(op))
1180 {
1181         tGPDfill *tgpf = MEM_callocN(sizeof(tGPDfill), "GPencil Fill Data");
1182
1183         /* define initial values */
1184         ToolSettings *ts = CTX_data_tool_settings(C);
1185         bGPdata *gpd = CTX_data_gpencil_data(C);
1186         Main *bmain = CTX_data_main(C);
1187
1188         /* set current scene and window info */
1189         tgpf->C = C;
1190         tgpf->bmain = CTX_data_main(C);
1191         tgpf->scene = CTX_data_scene(C);
1192         tgpf->ob = CTX_data_active_object(C);
1193         tgpf->sa = CTX_wm_area(C);
1194         tgpf->ar = CTX_wm_region(C);
1195         tgpf->rv3d = tgpf->ar->regiondata;
1196         tgpf->v3d = tgpf->sa->spacedata.first;
1197         tgpf->depsgraph = CTX_data_depsgraph(C);
1198         tgpf->win = CTX_wm_window(C);
1199
1200         /* set GP datablock */
1201         tgpf->gpd = gpd;
1202         tgpf->gpl = BKE_gpencil_layer_getactive(gpd);
1203         if (tgpf->gpl == NULL) {
1204                 tgpf->gpl = BKE_gpencil_layer_addnew(tgpf->gpd, DATA_("GP_Layer"), true);
1205         }
1206         tgpf->lock_axis = ts->gp_sculpt.lock_axis;
1207
1208         tgpf->oldkey = -1;
1209         tgpf->sbuffer_size = 0;
1210         tgpf->sbuffer = NULL;
1211         tgpf->depth_arr = NULL;
1212
1213         /* save filling parameters */
1214         Brush *brush = BKE_paint_brush(&ts->gp_paint->paint);
1215         tgpf->flag = brush->gpencil_settings->flag;
1216         tgpf->fill_leak = brush->gpencil_settings->fill_leak;
1217         tgpf->fill_threshold = brush->gpencil_settings->fill_threshold;
1218         tgpf->fill_simplylvl = brush->gpencil_settings->fill_simplylvl;
1219         tgpf->fill_draw_mode = brush->gpencil_settings->fill_draw_mode;
1220         tgpf->fill_factor = (short)max_ii(1, min_ii((int)brush->gpencil_settings->fill_factor, 8));
1221
1222         int totcol = tgpf->ob->totcol;
1223
1224         /* get color info */
1225         Material *ma = BKE_gpencil_current_input_brush_material(bmain, tgpf->ob, brush);
1226
1227         tgpf->mat = ma;
1228
1229         /* check whether the material was newly added */
1230         if (totcol != tgpf->ob->totcol) {
1231                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_PROPERTIES, NULL);
1232         }
1233
1234         /* init undo */
1235         gpencil_undo_init(tgpf->gpd);
1236
1237         /* return context data for running operator */
1238         return tgpf;
1239 }
1240
1241 /* end operator */
1242 static void gpencil_fill_exit(bContext *C, wmOperator *op)
1243 {
1244         Main *bmain = CTX_data_main(C);
1245         Object *ob = CTX_data_active_object(C);
1246
1247         /* clear undo stack */
1248         gpencil_undo_finish();
1249
1250         /* restore cursor to indicate end of fill */
1251         WM_cursor_modal_restore(CTX_wm_window(C));
1252
1253         tGPDfill *tgpf = op->customdata;
1254
1255         /* don't assume that operator data exists at all */
1256         if (tgpf) {
1257                 /* clear status message area */
1258                 ED_workspace_status_text(C, NULL);
1259
1260                 MEM_SAFE_FREE(tgpf->sbuffer);
1261                 MEM_SAFE_FREE(tgpf->depth_arr);
1262
1263                 /* remove drawing handler */
1264                 if (tgpf->draw_handle_3d) {
1265                         ED_region_draw_cb_exit(tgpf->ar->type, tgpf->draw_handle_3d);
1266                 }
1267
1268                 /* delete temp image */
1269                 if (tgpf->ima) {
1270                         for (Image *ima = bmain->images.first; ima; ima = ima->id.next) {
1271                                 if (ima == tgpf->ima) {
1272                                         BLI_remlink(&bmain->images, ima);
1273                                         BKE_image_free(tgpf->ima);
1274                                         MEM_SAFE_FREE(tgpf->ima);
1275                                         break;
1276                                 }
1277                         }
1278                 }
1279
1280                 /* finally, free memory used by temp data */
1281                 MEM_freeN(tgpf);
1282         }
1283
1284         /* clear pointer */
1285         op->customdata = NULL;
1286
1287         /* drawing batch cache is dirty now */
1288         if ((ob) && (ob->type == OB_GPENCIL) && (ob->data)) {
1289                 bGPdata *gpd2 = ob->data;
1290                 DEG_id_tag_update(&gpd2->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
1291                 gpd2->flag |= GP_DATA_CACHE_IS_DIRTY;
1292         }
1293
1294         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
1295 }
1296
1297 static void gpencil_fill_cancel(bContext *C, wmOperator *op)
1298 {
1299         /* this is just a wrapper around exit() */
1300         gpencil_fill_exit(C, op);
1301 }
1302
1303 /* Init: Allocate memory and set init values */
1304 static int gpencil_fill_init(bContext *C, wmOperator *op)
1305 {
1306         tGPDfill *tgpf;
1307         /* cannot paint in locked layer */
1308         bGPdata *gpd = CTX_data_gpencil_data(C);
1309         bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
1310         if ((gpl) && (gpl->flag & GP_LAYER_LOCKED)) {
1311                 return 0;
1312         }
1313
1314         /* check context */
1315         tgpf = op->customdata = gp_session_init_fill(C, op);
1316         if (tgpf == NULL) {
1317                 /* something wasn't set correctly in context */
1318                 gpencil_fill_exit(C, op);
1319                 return 0;
1320         }
1321
1322         /* everything is now setup ok */
1323         return 1;
1324 }
1325
1326 /* start of interactive part of operator */
1327 static int gpencil_fill_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1328 {
1329         tGPDfill *tgpf = NULL;
1330
1331         /* try to initialize context data needed */
1332         if (!gpencil_fill_init(C, op)) {
1333                 gpencil_fill_exit(C, op);
1334                 if (op->customdata)
1335                         MEM_freeN(op->customdata);
1336                 return OPERATOR_CANCELLED;
1337         }
1338         else {
1339                 tgpf = op->customdata;
1340         }
1341
1342         /* Enable custom drawing handlers to show help lines */
1343         if (tgpf->flag & GP_BRUSH_FILL_SHOW_HELPLINES) {
1344                 tgpf->draw_handle_3d = ED_region_draw_cb_activate(tgpf->ar->type, gpencil_fill_draw_3d, tgpf, REGION_DRAW_POST_VIEW);
1345         }
1346
1347         WM_cursor_modal_set(CTX_wm_window(C), BC_PAINTBRUSHCURSOR);
1348
1349         gpencil_fill_status_indicators(C, tgpf);
1350
1351         DEG_id_tag_update(&tgpf->gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
1352         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
1353
1354         /* add a modal handler for this operator*/
1355         WM_event_add_modal_handler(C, op);
1356
1357         return OPERATOR_RUNNING_MODAL;
1358 }
1359
1360 /* events handling during interactive part of operator */
1361 static int gpencil_fill_modal(bContext *C, wmOperator *op, const wmEvent *event)
1362 {
1363         tGPDfill *tgpf = op->customdata;
1364
1365         int estate = OPERATOR_PASS_THROUGH; /* default exit state - pass through */
1366
1367         switch (event->type) {
1368                 case ESCKEY:
1369                 case RIGHTMOUSE:
1370                         estate = OPERATOR_CANCELLED;
1371                         break;
1372                 case LEFTMOUSE:
1373                         tgpf->on_back = RNA_boolean_get(op->ptr, "on_back");
1374                         /* first time the event is not enabled to show help lines */
1375                         if ((tgpf->oldkey != -1) || ((tgpf->flag & GP_BRUSH_FILL_SHOW_HELPLINES) == 0)) {
1376                                 ARegion *ar = BKE_area_find_region_xy(CTX_wm_area(C), RGN_TYPE_ANY, event->x, event->y);
1377                                 if (ar) {
1378                                         bool in_bounds = false;
1379
1380                                         /* Perform bounds check */
1381                                         in_bounds = BLI_rcti_isect_pt(&ar->winrct, event->x, event->y);
1382
1383                                         if ((in_bounds) && (ar->regiontype == RGN_TYPE_WINDOW)) {
1384                                                 /* TODO GPXX: Verify the mouse click is right for any window size */
1385                                                 tgpf->center[0] = event->mval[0];
1386                                                 tgpf->center[1] = event->mval[1];
1387
1388                                                 /* render screen to temp image */
1389                                                 if ( gp_render_offscreen(tgpf) ) {
1390
1391                                                         /* apply boundary fill */
1392                                                         gpencil_boundaryfill_area(tgpf);
1393
1394                                                         /* clean borders to avoid infinite loops */
1395                                                         gpencil_clean_borders(tgpf);
1396
1397                                                         /* analyze outline */
1398                                                         gpencil_get_outline_points(tgpf);
1399
1400                                                         /* create array of points from stack */
1401                                                         gpencil_points_from_stack(tgpf);
1402
1403                                                         /* create z-depth array for reproject */
1404                                                         gpencil_get_depth_array(tgpf);
1405
1406                                                         /* create stroke and reproject */
1407                                                         gpencil_stroke_from_buffer(tgpf);
1408
1409                                                 }
1410
1411                                                 /* restore size */
1412                                                 tgpf->ar->winx = (short)tgpf->bwinx;
1413                                                 tgpf->ar->winy = (short)tgpf->bwiny;
1414                                                 tgpf->ar->winrct = tgpf->brect;
1415
1416                                                 /* free temp stack data */
1417                                                 if (tgpf->stack) {
1418                                                         BLI_stack_free(tgpf->stack);
1419                                                 }
1420
1421                                                 /* push undo data */
1422                                                 gpencil_undo_push(tgpf->gpd);
1423
1424                                                 estate = OPERATOR_FINISHED;
1425                                         }
1426                                         else {
1427                                                 estate = OPERATOR_CANCELLED;
1428                                         }
1429                                 }
1430                                 else {
1431                                         estate = OPERATOR_CANCELLED;
1432                                 }
1433                         }
1434                         tgpf->oldkey = event->type;
1435                         break;
1436         }
1437         /* process last operations before exiting */
1438         switch (estate) {
1439                 case OPERATOR_FINISHED:
1440                         gpencil_fill_exit(C, op);
1441                         WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL);
1442                         break;
1443
1444                 case OPERATOR_CANCELLED:
1445                         gpencil_fill_exit(C, op);
1446                         break;
1447
1448                 case OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH:
1449                         break;
1450         }
1451
1452         /* return status code */
1453         return estate;
1454 }
1455
1456 void GPENCIL_OT_fill(wmOperatorType *ot)
1457 {
1458         PropertyRNA *prop;
1459
1460         /* identifiers */
1461         ot->name = "Grease Pencil Fill";
1462         ot->idname = "GPENCIL_OT_fill";
1463         ot->description = "Fill with color the shape formed by strokes";
1464
1465         /* api callbacks */
1466         ot->invoke = gpencil_fill_invoke;
1467         ot->modal = gpencil_fill_modal;
1468         ot->poll = gpencil_fill_poll;
1469         ot->cancel = gpencil_fill_cancel;
1470
1471         /* flags */
1472         ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING;
1473
1474         prop = RNA_def_boolean(ot->srna, "on_back", false, "Draw On Back", "Send new stroke to Back");
1475         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1476 }