Improve grease pencil stroke quality
[blender.git] / source / blender / editors / gpencil / gpencil_brush.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2015, Blender Foundation
19  * This is a new part of Blender
20  *
21  * Contributor(s): Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  * Brush based operators for editing Grease Pencil strokes
26  */
27
28 /** \file blender/editors/gpencil/gpencil_brush.c
29  *  \ingroup edgpencil
30  */
31
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stddef.h>
37 #include <math.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_ghash.h"
43 #include "BLI_math.h"
44 #include "BLI_rand.h"
45 #include "BLI_utildefines.h"
46
47 #include "BLT_translation.h"
48
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_view3d_types.h"
53 #include "DNA_gpencil_types.h"
54
55 #include "BKE_context.h"
56 #include "BKE_global.h"
57 #include "BKE_gpencil.h"
58 #include "BKE_library.h"
59 #include "BKE_report.h"
60 #include "BKE_screen.h"
61
62 #include "UI_interface.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "RNA_access.h"
68 #include "RNA_define.h"
69 #include "RNA_enum_types.h"
70
71 #include "UI_view2d.h"
72
73 #include "ED_gpencil.h"
74 #include "ED_screen.h"
75 #include "ED_view3d.h"
76
77 #include "BIF_gl.h"
78 #include "BIF_glutil.h"
79
80 #include "gpencil_intern.h"
81
82 /* ************************************************ */
83 /* General Brush Editing Context */
84
85 /* Context for brush operators */
86 typedef struct tGP_BrushEditData {
87         /* Current editor/region/etc. */
88         /* NOTE: This stuff is mainly needed to handle 3D view projection stuff... */
89         Scene *scene;
90         
91         ScrArea *sa;
92         ARegion *ar;
93         
94         /* Current GPencil datablock */
95         bGPdata *gpd;
96         
97         /* Brush Settings */
98         GP_BrushEdit_Settings *settings;
99         GP_EditBrush_Data *brush;
100         
101         eGP_EditBrush_Types brush_type;
102         eGP_EditBrush_Flag  flag;
103         
104         /* Space Conversion Data */
105         GP_SpaceConversion gsc;
106         
107         
108         /* Is the brush currently painting? */
109         bool is_painting;
110         
111         /* Start of new sculpt stroke */
112         bool first;
113         
114         /* Current frame */
115         int cfra;
116         
117         
118         /* Brush Runtime Data: */
119         /* - position and pressure
120          * - the *_prev variants are the previous values
121          */
122         int   mval[2], mval_prev[2];
123         float pressure, pressure_prev;
124         
125         /* - effect vector (e.g. 2D/3D translation for grab brush) */
126         float dvec[3];
127         
128         /* brush geometry (bounding box) */
129         rcti brush_rect;
130         
131         /* Custom data for certain brushes */
132         /* - map from bGPDstroke's to structs containing custom data about those strokes */
133         GHash *stroke_customdata;
134         /* - general customdata */
135         void *customdata;
136         
137         
138         /* Timer for in-place accumulation of brush effect */
139         wmTimer *timer;
140         bool timerTick; /* is this event from a timer */
141 } tGP_BrushEditData;
142
143
144 /* Callback for performing some brush operation on a single point */
145 typedef bool (*GP_BrushApplyCb)(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
146                                 const int radius, const int co[2]);
147
148 /* ************************************************ */
149 /* Utility Functions */
150
151 /* Context ---------------------------------------- */
152
153 /* Get the sculpting settings */
154 static GP_BrushEdit_Settings *gpsculpt_get_settings(Scene *scene)
155 {
156         return &scene->toolsettings->gp_sculpt;
157 }
158
159 /* Get the active brush */
160 static GP_EditBrush_Data *gpsculpt_get_brush(Scene *scene)
161 {
162         GP_BrushEdit_Settings *gset = &scene->toolsettings->gp_sculpt;
163         return &gset->brush[gset->brushtype];
164 }
165
166 /* Brush Operations ------------------------------- */
167
168 /* Invert behaviour of brush? */
169 static bool gp_brush_invert_check(tGP_BrushEditData *gso)
170 {
171         /* The basic setting is the brush's setting (from the panel) */
172         bool invert = ((gso->brush->flag & GP_EDITBRUSH_FLAG_INVERT) != 0);
173         
174         /* During runtime, the user can hold down the Ctrl key to invert the basic behaviour */
175         if (gso->flag & GP_EDITBRUSH_FLAG_INVERT) {
176                 invert ^= true;
177         }
178                 
179         return invert;
180 }
181
182 /* Compute strength of effect */
183 static float gp_brush_influence_calc(tGP_BrushEditData *gso, const int radius, const int co[2])
184 {
185         GP_EditBrush_Data *brush = gso->brush;
186         
187         /* basic strength factor from brush settings */
188         float influence = brush->strength;
189         
190         /* use pressure? */
191         if (brush->flag & GP_EDITBRUSH_FLAG_USE_PRESSURE) {
192                 influence *= gso->pressure;
193         }
194         
195         /* distance fading */
196         if (brush->flag & GP_EDITBRUSH_FLAG_USE_FALLOFF) {
197                 float distance = (float)len_v2v2_int(gso->mval, co);
198                 float fac;
199                 
200                 CLAMP(distance, 0.0f, (float)radius);
201                 fac = 1.0f - (distance / (float)radius);
202                 
203                 influence *= fac;
204         }
205         
206         /* return influence */
207         return influence;
208 }
209
210 /* ************************************************ */
211 /* Brush Callbacks */
212 /* This section defines the callbacks used by each brush to perform their magic.
213  * These are called on each point within the brush's radius.
214  */
215
216 /* ----------------------------------------------- */
217 /* Smooth Brush */
218
219 /* A simple (but slower + inaccurate) smooth-brush implementation to test the algorithm for stroke smoothing */
220 static bool gp_brush_smooth_apply(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
221                                   const int radius, const int co[2])
222 {
223         GP_EditBrush_Data *brush = gso->brush;
224         bGPDspoint *pt = &gps->points[i];
225         float inf = gp_brush_influence_calc(gso, radius, co);
226         
227         /* perform smoothing */
228         return gp_smooth_stroke(gps, i, inf);
229 }
230
231 /* ----------------------------------------------- */
232 /* Line Thickness Brush */
233
234 /* Make lines thicker or thinner by the specified amounts */
235 static bool gp_brush_thickness_apply(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
236                                      const int radius, const int co[2])
237 {
238         bGPDspoint *pt = gps->points + i;
239         float inf;
240         
241         /* Compute strength of effect
242          * - We divide the strength by 10, so that users can set "sane" values.
243          *   Otherwise, good default values are in the range of 0.093
244          */
245         inf = gp_brush_influence_calc(gso, radius, co) / 10.0f;
246         
247         /* apply */
248         // XXX: this is much too strong, and it should probably do some smoothing with the surrounding stuff
249         if (gp_brush_invert_check(gso)) {
250                 /* make line thinner - reduce stroke pressure */
251                 pt->pressure -= inf;
252         }
253         else {
254                 /* make line thicker - increase stroke pressure */
255                 pt->pressure += inf;
256         }
257         
258         /* Pressure should stay within [0.0, 1.0]
259          * However, it is nice for volumetric strokes to be able to exceed
260          * the upper end of this range. Therefore, we don't actually clamp
261          * down on the upper end.
262          */
263         if (pt->pressure < 0.0f)
264                 pt->pressure = 0.0f;
265         
266         return true;
267 }
268
269
270 /* ----------------------------------------------- */
271 /* Grab Brush */
272
273 /* Custom data per stroke for the Grab Brush
274  *
275  * This basically defines the strength of the effect for each
276  * affected stroke point that was within the initial range of
277  * the brush region.
278  */
279 typedef struct tGPSB_Grab_StrokeData {
280         /* array of indices to corresponding points in the stroke */
281         int   *points;
282         /* array of influence weights for each of the included points */
283         float *weights;
284         
285         /* capacity of the arrays */
286         int capacity;
287         /* actual number of items currently stored */
288         int size;
289 } tGPSB_Grab_StrokeData;
290
291 /* initialise custom data for handling this stroke */
292 static void gp_brush_grab_stroke_init(tGP_BrushEditData *gso, bGPDstroke *gps)
293 {
294         tGPSB_Grab_StrokeData *data = NULL;
295         
296         BLI_assert(gps->totpoints > 0);
297         
298         /* Check if there are buffers already (from a prior run) */
299         if (BLI_ghash_haskey(gso->stroke_customdata, gps)) {
300                 /* Ensure that the caches are empty
301                  * - Since we reuse these between different strokes, we don't
302                  *   want the previous invocation's data polluting the arrays
303                  */
304                 data = BLI_ghash_lookup(gso->stroke_customdata, gps);
305                 BLI_assert(data != NULL);
306                 
307                 data->size = 0; /* minimum requirement - so that we can repopulate again */
308                 
309                 memset(data->points, 0, sizeof(int) * data->capacity);
310                 memset(data->weights, 0, sizeof(float) * data->capacity);
311         }
312         else {
313                 /* Create new instance */
314                 data = MEM_callocN(sizeof(tGPSB_Grab_StrokeData), "GP Stroke Grab Data");
315                 
316                 data->capacity = gps->totpoints;
317                 data->size = 0;
318                 
319                 data->points  = MEM_callocN(sizeof(int) * data->capacity, "GP Stroke Grab Indices");
320                 data->weights = MEM_callocN(sizeof(float) * data->capacity, "GP Stroke Grab Weights");
321                 
322                 /* hook up to the cache */
323                 BLI_ghash_insert(gso->stroke_customdata, gps, data);
324         }       
325 }
326
327 /* store references to stroke points in the initial stage */
328 static bool gp_brush_grab_store_points(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
329                                        const int radius, const int co[2])
330 {
331         tGPSB_Grab_StrokeData *data = BLI_ghash_lookup(gso->stroke_customdata, gps);
332         float inf = gp_brush_influence_calc(gso, radius, co);
333         
334         BLI_assert(data != NULL);
335         BLI_assert(data->size < data->capacity);
336         
337         /* insert this point into the set of affected points */
338         data->points[data->size]  = i;
339         data->weights[data->size] = inf;
340         data->size++;
341         
342         /* done */
343         return true;
344 }
345
346 /* Compute effect vector for grab brush */
347 static void gp_brush_grab_calc_dvec(tGP_BrushEditData *gso)
348 {
349         /* Convert mouse-movements to movement vector */
350         // TODO: incorporate pressure into this?
351         // XXX: screen-space strokes in 3D space will suffer!
352         if (gso->sa->spacetype == SPACE_VIEW3D) {
353                 View3D *v3d = gso->sa->spacedata.first;
354                 RegionView3D *rv3d = gso->ar->regiondata;
355                 float *rvec = ED_view3d_cursor3d_get(gso->scene, v3d);
356                 float zfac = ED_view3d_calc_zfac(rv3d, rvec, NULL);
357                 
358                 float mval_f[2];
359                 
360                 /* convert from 2D screenspace to 3D... */
361                 mval_f[0] = (float)(gso->mval[0] - gso->mval_prev[0]);
362                 mval_f[1] = (float)(gso->mval[1] - gso->mval_prev[1]);
363                 
364                 ED_view3d_win_to_delta(gso->ar, mval_f, gso->dvec, zfac);
365         }
366         else {
367                 /* 2D - just copy */
368                 // XXX: view2d?
369                 gso->dvec[0] = (float)(gso->mval[0] - gso->mval_prev[0]);
370                 gso->dvec[1] = (float)(gso->mval[1] - gso->mval_prev[1]);
371                 gso->dvec[2] = 0.0f;  /* unused */
372         }
373 }
374
375 /* Apply grab transform to all relevant points of the affected strokes */
376 static void gp_brush_grab_apply_cached(tGP_BrushEditData *gso, bGPDstroke *gps)
377 {
378         tGPSB_Grab_StrokeData *data = BLI_ghash_lookup(gso->stroke_customdata, gps);
379         int i;
380         
381         /* Apply dvec to all of the stored points */
382         for (i = 0; i < data->size; i++) {
383                 bGPDspoint *pt = &gps->points[data->points[i]];
384                 float delta[3] = {0.0f};
385                 
386                 /* adjust the amount of displacement to apply */
387                 mul_v3_v3fl(delta, gso->dvec, data->weights[i]);
388                 
389                 /* apply */
390                 add_v3_v3(&pt->x, delta);
391         }
392 }
393
394 /* free customdata used for handling this stroke */
395 static void gp_brush_grab_stroke_free(void *ptr)
396 {
397         tGPSB_Grab_StrokeData *data = (tGPSB_Grab_StrokeData *)ptr;
398         
399         /* free arrays */
400         MEM_freeN(data->points);
401         MEM_freeN(data->weights);
402         
403         /* ... and this item itself, since it was also allocated */
404         MEM_freeN(data);
405 }
406
407 /* ----------------------------------------------- */
408 /* Push Brush */
409 /* NOTE: Depends on gp_brush_grab_calc_dvec() */
410
411 static bool gp_brush_push_apply(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
412                                 const int radius, const int co[2])
413 {
414         bGPDspoint *pt = gps->points + i;
415         float inf = gp_brush_influence_calc(gso, radius, co);
416         float delta[3] = {0.0f};
417                 
418         /* adjust the amount of displacement to apply */
419         mul_v3_v3fl(delta, gso->dvec, inf);
420         
421         /* apply */
422         add_v3_v3(&pt->x, delta);
423
424         /* done */
425         return true;
426 }
427
428 /* ----------------------------------------------- */
429 /* Pinch Brush */
430
431 /* Compute reference midpoint for the brush - this is what we'll be moving towards */
432 static void gp_brush_calc_midpoint(tGP_BrushEditData *gso)
433 {
434         if (gso->sa->spacetype == SPACE_VIEW3D) {
435                 /* Convert mouse position to 3D space
436                  * See: gpencil_paint.c :: gp_stroke_convertcoords()
437                  */
438                 View3D *v3d = gso->sa->spacedata.first;
439                 RegionView3D *rv3d = gso->ar->regiondata;
440                 float *rvec = ED_view3d_cursor3d_get(gso->scene, v3d);
441                 float zfac = ED_view3d_calc_zfac(rv3d, rvec, NULL);
442                 
443                 float mval_f[2] = {UNPACK2(gso->mval)};
444                 float mval_prj[2];
445                 float dvec[3];
446                 
447                 
448                 if (ED_view3d_project_float_global(gso->ar, rvec, mval_prj, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
449                         sub_v2_v2v2(mval_f, mval_prj, mval_f);
450                         ED_view3d_win_to_delta(gso->ar, mval_f, dvec, zfac);
451                         sub_v3_v3v3(gso->dvec, rvec, dvec);
452                 }
453                 else {
454                         zero_v3(gso->dvec);
455                 }
456         }
457         else {
458                 /* Just 2D coordinates */
459                 // XXX: fix View2D offsets later
460                 gso->dvec[0] = (float)gso->mval[0];
461                 gso->dvec[1] = (float)gso->mval[1];
462                 gso->dvec[2] = 0.0f;
463         }
464 }
465
466 /* Shrink distance between midpoint and this point... */
467 static bool gp_brush_pinch_apply(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
468                                  const int radius, const int co[2])
469 {
470         bGPDspoint *pt = gps->points + i;
471         float fac, inf;
472         float vec[3];
473         
474         /* Scale down standard influence value to get it more manageable...
475          *  - No damping = Unmanageable at > 0.5 strength
476          *  - Div 10     = Not enough effect
477          *  - Div 5      = Happy medium... (by trial and error)
478          */
479         inf = gp_brush_influence_calc(gso, radius, co) / 5.0f;
480         
481         /* 1) Make this point relative to the cursor/midpoint (dvec) */
482         sub_v3_v3v3(vec, &pt->x, gso->dvec);
483         
484         /* 2) Shrink the distance by pulling the point towards the midpoint
485          *    (0.0 = at midpoint, 1 = at edge of brush region)
486          *                         OR
487          *    Increase the distance (if inverting the brush action!)
488          */
489         if (gp_brush_invert_check(gso)) {
490                 /* Inflate (inverse) */
491                 fac = 1.0f + (inf * inf); /* squared to temper the effect... */
492         }
493         else {
494                 /* Shrink (default) */
495                 fac = 1.0f - (inf * inf); /* squared to temper the effect... */
496         }
497         mul_v3_fl(vec, fac);
498         
499         /* 3) Translate back to original space, with the shrinkage applied */
500         add_v3_v3v3(&pt->x, gso->dvec, vec);
501         
502         /* done */
503         return true;
504 }
505
506 /* ----------------------------------------------- */
507 /* Twist Brush - Rotate Around midpoint */
508
509 /* Take the screenspace coordinates of the point, rotate this around the brush midpoint,
510  * convert the rotated point and convert it into "data" space
511  */
512
513 static bool gp_brush_twist_apply(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
514                                  const int radius, const int co[2])
515 {
516         bGPDspoint *pt = gps->points + i;
517         float angle, inf;
518         
519         /* Angle to rotate by */
520         inf = gp_brush_influence_calc(gso, radius, co);
521         angle = DEG2RADF(1.0f) * inf;
522         
523         if (gp_brush_invert_check(gso)) {
524                 /* invert angle that we rotate by */
525                 angle *= -1;
526         }
527         
528         /* Rotate in 2D or 3D space? */
529         if (gps->flag & GP_STROKE_3DSPACE) {
530                 /* Perform rotation in 3D space... */
531                 RegionView3D *rv3d = gso->ar->regiondata;
532                 float rmat[3][3];
533                 float axis[3];
534                 float vec[3];
535                 
536                 /* Compute rotation matrix - rotate around view vector by angle */
537                 negate_v3_v3(axis, rv3d->persinv[2]);
538                 normalize_v3(axis);
539                 
540                 axis_angle_normalized_to_mat3(rmat, axis, angle);
541                 
542                 /* Rotate point (no matrix-space transforms needed, as GP points are in world space) */
543                 sub_v3_v3v3(vec, &pt->x, gso->dvec); /* make relative to center (center is stored in dvec) */
544                 mul_m3_v3(rmat, vec);
545                 add_v3_v3v3(&pt->x, vec, gso->dvec); /* restore */
546         }
547         else {
548                 const float axis[3] = {0.0f, 0.0f, 1.0f};
549                 float vec[3] = {0.0f};
550                 float rmat[3][3];
551                 
552                 /* Express position of point relative to cursor, ready to rotate */
553                 // XXX: There is still some offset here, but it's close to working as expected...
554                 vec[0] = (float)(co[0] - gso->mval[0]);
555                 vec[1] = (float)(co[1] - gso->mval[1]);
556                 
557                 /* rotate point */
558                 axis_angle_normalized_to_mat3(rmat, axis, angle);
559                 mul_m3_v3(rmat, vec);
560                 
561                 /* Convert back to screen-coordinates */
562                 vec[0] += (float)gso->mval[0];
563                 vec[1] += (float)gso->mval[1];
564                 
565                 /* Map from screen-coordinates to final coordinate space */
566                 if (gps->flag & GP_STROKE_2DSPACE) {
567                         View2D *v2d = gso->gsc.v2d;
568                         UI_view2d_region_to_view(v2d, vec[0], vec[1], &pt->x, &pt->y);
569                 }
570                 else {
571                         // XXX
572                         copy_v2_v2(&pt->x, vec);
573                 }
574         }
575         
576         /* done */
577         return true;
578 }
579
580
581 /* ----------------------------------------------- */
582 /* Randomize Brush */
583
584 /* Apply some random jitter to the point */
585 static bool gp_brush_randomize_apply(tGP_BrushEditData *gso, bGPDstroke *gps, int i,
586                                      const int radius, const int co[2])
587 {
588         bGPDspoint *pt = gps->points + i;
589         
590         /* Amount of jitter to apply depends on the distance of the point to the cursor,
591          * as well as the strength of the brush
592          */
593         const float inf = gp_brush_influence_calc(gso, radius, co) / 2.0f;
594         const float fac = BLI_frand() * inf;
595         
596         /* Jitter is applied perpendicular to the mouse movement vector
597          * - We compute all effects in screenspace (since it's easier)
598          *   and then project these to get the points/distances in
599          *   viewspace as needed
600          */
601         float mvec[2], svec[2];
602         
603         /* mouse movement in ints -> floats */
604         mvec[0] = (float)(gso->mval[0] - gso->mval_prev[0]);
605         mvec[1] = (float)(gso->mval[1] - gso->mval_prev[1]);
606         
607         /* rotate mvec by 90 degrees... */
608         svec[0] = -mvec[1];
609         svec[1] =  mvec[0];
610         
611         //printf("svec = %f %f, ", svec[0], svec[1]);
612         
613         /* scale the displacement by the random displacement, and apply */
614         if (BLI_frand() > 0.5f) {
615                 mul_v2_fl(svec, -fac);
616         }
617         else {
618                 mul_v2_fl(svec, fac);
619         }
620         
621         //printf("%f %f (%f), nco = {%f %f}, co = %d %d\n", svec[0], svec[1], fac, nco[0], nco[1], co[0], co[1]);
622         
623         /* convert to dataspace */
624         if (gps->flag & GP_STROKE_3DSPACE) {
625                 /* 3D: Project to 3D space */
626                 if (gso->sa->spacetype == SPACE_VIEW3D) {
627                         bool flip;
628                         RegionView3D *rv3d = gso->ar->regiondata;
629                         float zfac = ED_view3d_calc_zfac(rv3d, &pt->x, &flip);
630                         if (flip == false) {
631                                 float dvec[3];
632                                 ED_view3d_win_to_delta(gso->gsc.ar, svec, dvec, zfac);
633                                 add_v3_v3(&pt->x, dvec);
634                         }
635                 }
636                 else {
637                         /* ERROR */
638                         BLI_assert("3D stroke being sculpted in non-3D view");
639                 }
640         }
641         else {
642                 /* 2D: As-is */
643                 // XXX: v2d scaling/offset?
644                 float nco[2];
645                 nco[0] = (float)co[0] + svec[0];
646                 nco[1] = (float)co[1] + svec[1];
647
648                 copy_v2_v2(&pt->x, nco);
649         }
650         
651         /* done */
652         return true;
653 }
654
655 /* ************************************************ */
656 /* Non Callback-Based Brushes */
657
658 /* Clone Brush ------------------------------------- */
659 /* How this brush currently works:
660  * - If this is start of the brush stroke, paste immediately under the cursor
661  *   by placing the midpoint of the buffer strokes under the cursor now
662  *
663  * - Otherwise, in:
664  *   "Stamp Mode" - Move the newly pasted strokes so that their center follows the cursor
665  *   "Continuous" - Repeatedly just paste new copies for where the brush is now
666  */
667
668 /* Custom state data for clone brush */
669 typedef struct tGPSB_CloneBrushData {
670         /* midpoint of the strokes on the clipboard */
671         float buffer_midpoint[3];
672         
673         /* number of strokes in the paste buffer (and/or to be created each time) */
674         size_t totitems;
675         
676         /* for "stamp" mode, the currently pasted brushes */
677         bGPDstroke **new_strokes;
678 } tGPSB_CloneBrushData;
679
680 /* Initialise "clone" brush data */
681 static void gp_brush_clone_init(bContext *C, tGP_BrushEditData *gso)
682 {
683         tGPSB_CloneBrushData *data;
684         bGPDstroke *gps;
685         
686         /* init custom data */
687         gso->customdata = data = MEM_callocN(sizeof(tGPSB_CloneBrushData), "CloneBrushData");
688         
689         /* compute midpoint of strokes on clipboard */
690         for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) {
691                 if (ED_gpencil_stroke_can_use(C, gps)) {
692                         const float dfac = 1.0f / ((float)gps->totpoints);
693                         float mid[3] = {0.0f};
694                         
695                         bGPDspoint *pt;
696                         int i;
697                         
698                         /* compute midpoint of this stroke */
699                         for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
700                                 float co[3];
701                                 
702                                 mul_v3_v3fl(co, &pt->x, dfac);
703                                 add_v3_v3(mid, co);
704                         }
705                         
706                         /* combine this stroke's data with the main data */
707                         add_v3_v3(data->buffer_midpoint, mid);
708                         data->totitems++;
709                 }
710         }
711         
712         /* Divide the midpoint by the number of strokes, to finish averaging it */
713         if (data->totitems > 1) {
714                 mul_v3_fl(data->buffer_midpoint, 1.0f / (float)data->totitems);
715         }
716         
717         /* Create a buffer for storing the current strokes */
718         if (1 /*gso->brush->mode == GP_EDITBRUSH_CLONE_MODE_STAMP*/) {
719                 data->new_strokes = MEM_callocN(sizeof(bGPDstroke *) * data->totitems, "cloned strokes ptr array");
720         }
721 }
722
723 /* Free custom data used for "clone" brush */
724 static void gp_brush_clone_free(tGP_BrushEditData *gso)
725 {
726         tGPSB_CloneBrushData *data = gso->customdata;
727         
728         /* free strokes array */
729         if (data->new_strokes) {
730                 MEM_freeN(data->new_strokes);
731                 data->new_strokes = NULL;
732         }
733         
734         /* free the customdata itself */
735         MEM_freeN(data);
736         gso->customdata = NULL;
737 }
738
739 /* Create new copies of the strokes on the clipboard */
740 static void gp_brush_clone_add(bContext *C, tGP_BrushEditData *gso)
741 {
742         tGPSB_CloneBrushData *data = gso->customdata;
743         
744         Scene *scene = gso->scene;
745         bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
746         bGPDframe *gpf = gpencil_layer_getframe(gpl, CFRA, true);
747         bGPDstroke *gps;
748         
749         float delta[3];
750         size_t strokes_added = 0;
751         
752         /* Compute amount to offset the points by */
753         /* NOTE: This assumes that screenspace strokes are NOT used in the 3D view... */
754         
755         gp_brush_calc_midpoint(gso); /* this puts the cursor location into gso->dvec */
756         sub_v3_v3v3(delta, gso->dvec, data->buffer_midpoint);
757         
758         /* Copy each stroke into the layer */
759         for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) {
760                 if (ED_gpencil_stroke_can_use(C, gps)) {
761                         bGPDstroke *new_stroke;
762                         bGPDspoint *pt;
763                         int i;
764                         
765                         /* Make a new stroke */
766                         new_stroke = MEM_dupallocN(gps);
767                         
768                         new_stroke->points = MEM_dupallocN(gps->points);
769                         new_stroke->next = new_stroke->prev = NULL;
770                         
771                         BLI_addtail(&gpf->strokes, new_stroke);
772                         
773                         /* Adjust all the stroke's points, so that the strokes
774                          * get pasted relative to where the cursor is now
775                          */
776                         for (i = 0, pt = new_stroke->points; i < new_stroke->totpoints; i++, pt++) {
777                                 /* assume that the delta can just be applied, and then everything works */
778                                 add_v3_v3(&pt->x, delta);
779                         }
780                         
781                         /* Store ref for later */
782                         if ((data->new_strokes) && (strokes_added < data->totitems)) {
783                                 data->new_strokes[strokes_added] = new_stroke;
784                                 strokes_added++;
785                         }
786                 }
787         }
788 }
789
790 /* Move newly-added strokes around - "Stamp" mode of the Clone brush */
791 static void gp_brush_clone_adjust(tGP_BrushEditData *gso)
792 {
793         tGPSB_CloneBrushData *data = gso->customdata;
794         size_t snum;
795         
796         /* Compute the amount of movement to apply (overwrites dvec) */
797         gp_brush_grab_calc_dvec(gso);
798         
799         /* For each of the stored strokes, apply the offset to each point */
800         /* NOTE: Again this assumes that in the 3D view, we only have 3d space and not screenspace strokes... */
801         for (snum = 0; snum < data->totitems; snum++) {
802                 bGPDstroke *gps = data->new_strokes[snum];
803                 bGPDspoint *pt;
804                 int i;
805                 
806                 for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) {
807                         if (gso->brush->flag & GP_EDITBRUSH_FLAG_USE_FALLOFF) {
808                                 /* "Smudge" Effect when falloff is enabled */
809                                 float delta[3] = {0.0f};
810                                 int sco[2] = {0};
811                                 float influence;
812                                 
813                                 /* compute influence on point */
814                                 gp_point_to_xy(&gso->gsc, gps, pt, &sco[0], &sco[1]);
815                                 influence = gp_brush_influence_calc(gso, gso->brush->size, sco);
816                                 
817                                 /* adjust the amount of displacement to apply */
818                                 mul_v3_v3fl(delta, gso->dvec, influence);
819                                 
820                                 /* apply */
821                                 add_v3_v3(&pt->x, delta);
822                         }
823                         else {
824                                 /* Just apply the offset - All points move perfectly in sync with the cursor */
825                                 add_v3_v3(&pt->x, gso->dvec);
826                         }
827                 }
828         }
829 }
830
831 /* Entrypoint for applying "clone" brush */
832 static bool gpsculpt_brush_apply_clone(bContext *C, tGP_BrushEditData *gso)
833 {
834         /* Which "mode" are we operating in? */
835         if (gso->first) {
836                 /* Create initial clones */
837                 gp_brush_clone_add(C, gso);
838         }
839         else {
840                 /* Stamp or Continous Mode */
841                 if (1 /*gso->brush->mode == GP_EDITBRUSH_CLONE_MODE_STAMP*/) {
842                         /* Stamp - Proceed to translate the newly added strokes */
843                         gp_brush_clone_adjust(gso);
844                 }
845                 else {
846                         /* Continuous - Just keep pasting everytime we move */
847                         /* TODO: The spacing of repeat should be controlled using a "stepsize" or similar property? */
848                         gp_brush_clone_add(C, gso);
849                 }
850         }
851         
852         return true;
853 }
854
855 /* ************************************************ */
856 /* Cursor drawing */
857
858 /* Helper callback for drawing the cursor itself */
859 static void gp_brush_drawcursor(bContext *C, int x, int y, void *UNUSED(customdata))
860 {
861         GP_EditBrush_Data *brush = gpsculpt_get_brush(CTX_data_scene(C));
862         
863         if (brush) {
864                 glPushMatrix();
865                 
866                 glTranslatef((float)x, (float)y, 0.0f);
867                 
868                 /* TODO: toggle between add and remove? */
869                 glColor4ub(255, 255, 255, 128);
870                 
871                 glEnable(GL_LINE_SMOOTH);
872                 glEnable(GL_BLEND);
873                 
874                 glutil_draw_lined_arc(0.0, M_PI * 2.0, brush->size, 40);
875                 
876                 glDisable(GL_BLEND);
877                 glDisable(GL_LINE_SMOOTH);
878                 
879                 glPopMatrix();
880         }
881 }
882
883 /* Turn brush cursor in on/off */
884 static void gpencil_toggle_brush_cursor(bContext *C, bool enable)
885 {
886         GP_BrushEdit_Settings *gset = gpsculpt_get_settings(CTX_data_scene(C));
887         
888         if (gset->paintcursor && !enable) {
889                 /* clear cursor */
890                 WM_paint_cursor_end(CTX_wm_manager(C), gset->paintcursor);
891                 gset->paintcursor = NULL;
892         }
893         else if (enable) {
894                 /* enable cursor */
895                 gset->paintcursor = WM_paint_cursor_activate(CTX_wm_manager(C), 
896                                                              NULL, 
897                                                              gp_brush_drawcursor, NULL);
898         }
899 }
900
901
902 /* ************************************************ */
903 /* Header Info for GPencil Sculpt */
904
905 static void gpsculpt_brush_header_set(bContext *C, tGP_BrushEditData *gso)
906 {
907         const char *brush_name = NULL;
908         char str[256] = "";
909         
910         RNA_enum_name(rna_enum_gpencil_sculpt_brush_items, gso->brush_type, &brush_name);
911         
912         BLI_snprintf(str, sizeof(str),
913                      IFACE_("GPencil Sculpt: %s Stroke  | LMB to paint | RMB/Escape to Exit"
914                             " | Ctrl to Invert Action | Wheel Up/Down for Size "
915                             " | Shift-Wheel Up/Down for Strength"),
916                      (brush_name) ? brush_name : "<?>");
917         
918         ED_area_headerprint(CTX_wm_area(C), str);
919 }
920
921 /* ************************************************ */
922 /* Grease Pencil Sculpting Operator */
923
924 /* Init/Exit ----------------------------------------------- */
925
926 static bool gpsculpt_brush_init(bContext *C, wmOperator *op)
927 {
928         Scene *scene = CTX_data_scene(C);
929         tGP_BrushEditData *gso;
930         
931         /* setup operator data */
932         gso = MEM_callocN(sizeof(tGP_BrushEditData), "tGP_BrushEditData");
933         op->customdata = gso;
934         
935         /* store state */
936         gso->settings = gpsculpt_get_settings(scene);
937         gso->brush = gpsculpt_get_brush(scene);
938         
939         gso->brush_type = gso->settings->brushtype;
940         
941         
942         gso->is_painting = false;
943         gso->first = true;
944         
945         gso->gpd = ED_gpencil_data_get_active(C);
946         gso->cfra = INT_MAX; /* NOTE: So that first stroke will get handled in init_stroke() */
947         
948         gso->scene = scene;
949         
950         gso->sa = CTX_wm_area(C);
951         gso->ar = CTX_wm_region(C);
952         
953         /* initialise custom data for brushes */
954         switch (gso->brush_type) {
955                 case GP_EDITBRUSH_TYPE_CLONE:
956                 {
957                         bGPDstroke *gps;
958                         bool found = false;
959                         
960                         /* check that there are some usable strokes in the buffer */
961                         for (gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) {
962                                 if (ED_gpencil_stroke_can_use(C, gps)) {
963                                         found = true;
964                                         break;
965                                 }
966                         }
967                         
968                         if (found == false) {
969                                 /* STOP HERE! Nothing to paste! */
970                                 BKE_report(op->reports, RPT_ERROR, 
971                                            "Copy some strokes to the clipboard before using the Clone brush to paste copies of them");
972                                            
973                                 MEM_freeN(gso);
974                                 op->customdata = NULL;
975                                 return false;
976                         }
977                         else {
978                                 /* initialise customdata */
979                                 gp_brush_clone_init(C, gso);
980                         }
981                         break;
982                 }
983                 
984                 case GP_EDITBRUSH_TYPE_GRAB:
985                 {
986                         /* initialise the cache needed for this brush */
987                         gso->stroke_customdata = BLI_ghash_ptr_new("GP Grab Brush - Strokes Hash");
988                         break;
989                 }
990                         
991                 /* Others - No customdata needed */
992                 default:
993                         break;
994         }
995         
996         
997         /* setup space conversions */
998         gp_point_conversion_init(C, &gso->gsc);
999         
1000         /* update header */
1001         gpsculpt_brush_header_set(C, gso);
1002         
1003         /* setup cursor drawing */
1004         WM_cursor_modal_set(CTX_wm_window(C), BC_CROSSCURSOR);
1005         gpencil_toggle_brush_cursor(C, true);
1006         
1007         return true;
1008 }
1009
1010 static void gpsculpt_brush_exit(bContext *C, wmOperator *op)
1011 {
1012         tGP_BrushEditData *gso = op->customdata;
1013         wmWindow *win = CTX_wm_window(C);
1014         
1015         /* free brush-specific data */
1016         switch (gso->brush_type) {
1017                 case GP_EDITBRUSH_TYPE_GRAB:
1018                 {
1019                         /* Free per-stroke customdata
1020                          * - Keys don't need to be freed, as those are the strokes
1021                          * - Values assigned to those keys do, as they are custom structs
1022                          */
1023                         BLI_ghash_free(gso->stroke_customdata, NULL, gp_brush_grab_stroke_free);
1024                         break;
1025                 }
1026                 
1027                 case GP_EDITBRUSH_TYPE_CLONE:
1028                 {
1029                         /* Free customdata */
1030                         gp_brush_clone_free(gso);
1031                         break;
1032                 }
1033                 
1034                 default:
1035                         break;
1036         }
1037         
1038         /* unregister timer (only used for realtime) */
1039         if (gso->timer) {
1040                 WM_event_remove_timer(CTX_wm_manager(C), win, gso->timer);
1041         }
1042
1043         /* disable cursor and headerprints */
1044         ED_area_headerprint(CTX_wm_area(C), NULL);
1045         WM_cursor_modal_restore(win);
1046         gpencil_toggle_brush_cursor(C, false);
1047         
1048         /* free operator data */
1049         MEM_freeN(gso);
1050         op->customdata = NULL;
1051 }
1052
1053 /* poll callback for stroke sculpting operator(s) */
1054 static int gpsculpt_brush_poll(bContext *C)
1055 {
1056         /* NOTE: this is a bit slower, but is the most accurate... */
1057         return CTX_DATA_COUNT(C, editable_gpencil_strokes) != 0;
1058 }
1059
1060 /* Init Sculpt Stroke ---------------------------------- */
1061
1062 static void gpsculpt_brush_init_stroke(tGP_BrushEditData *gso)
1063 {
1064         Scene *scene = gso->scene;
1065         bGPdata *gpd = gso->gpd;
1066         bGPDlayer *gpl;
1067         int cfra = CFRA;
1068         
1069         /* only try to add a new frame if this is the first stroke, or the frame has changed */
1070         if ((gpd == NULL) || (cfra == gso->cfra))
1071                 return;
1072         
1073         /* go through each layer, and ensure that we've got a valid frame to use */
1074         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1075                 /* only editable and visible layers are considered */
1076                 if (gpencil_layer_is_editable(gpl) && (gpl->actframe != NULL)) {
1077                         bGPDframe *gpf = gpl->actframe;
1078                         
1079                         /* Make a new frame to work on if the layer's frame and the current scene frame don't match up 
1080                          * - This is useful when animating as it saves that "uh-oh" moment when you realize you've
1081                          *   spent too much time editing the wrong frame...
1082                          */
1083                         // XXX: should this be allowed when framelock is enabled?
1084                         if (gpf->framenum != cfra) {
1085                                 gpencil_frame_addcopy(gpl, cfra);
1086                         }
1087                 }
1088         }
1089         
1090         /* save off new current frame, so that next update works fine */
1091         gso->cfra = cfra;
1092 }
1093
1094 /* Apply ----------------------------------------------- */
1095
1096 /* Apply brush operation to points in this stroke */
1097 static bool gpsculpt_brush_do_stroke(tGP_BrushEditData *gso, bGPDstroke *gps, GP_BrushApplyCb apply)
1098 {
1099         GP_SpaceConversion *gsc = &gso->gsc;
1100         rcti *rect = &gso->brush_rect;
1101         const int radius = gso->brush->size;
1102         
1103         bGPDspoint *pt1, *pt2;
1104         int pc1[2] = {0};
1105         int pc2[2] = {0};
1106         int i;
1107         bool include_last = false;
1108         bool changed = false;
1109         
1110         if (gps->totpoints == 1) {
1111                 gp_point_to_xy(gsc, gps, gps->points, &pc1[0], &pc1[1]);
1112                 
1113                 /* do boundbox check first */
1114                 if ((!ELEM(V2D_IS_CLIPPED, pc1[0], pc1[1])) && BLI_rcti_isect_pt(rect, pc1[0], pc1[1])) {
1115                         /* only check if point is inside */
1116                         if (len_v2v2_int(gso->mval, pc1) <= radius) {
1117                                 /* apply operation to this point */
1118                                 changed = apply(gso, gps, 0, radius, pc1);
1119                         }
1120                 }
1121         }
1122         else {
1123                 /* Loop over the points in the stroke, checking for intersections 
1124                  *  - an intersection means that we touched the stroke
1125                  */
1126                 for (i = 0; (i + 1) < gps->totpoints; i++) {
1127                         /* Get points to work with */
1128                         pt1 = gps->points + i;
1129                         pt2 = gps->points + i + 1;
1130                         
1131                         /* Skip if neither one is selected (and we are only allowed to edit/consider selected points) */
1132                         if (gso->settings->flag & GP_BRUSHEDIT_FLAG_SELECT_MASK) {
1133                                 if (!(pt1->flag & GP_SPOINT_SELECT) && !(pt2->flag & GP_SPOINT_SELECT)) {
1134                                         include_last = false;
1135                                         continue;
1136                                 }
1137                         }
1138                         
1139                         gp_point_to_xy(gsc, gps, pt1, &pc1[0], &pc1[1]);
1140                         gp_point_to_xy(gsc, gps, pt2, &pc2[0], &pc2[1]);
1141                         
1142                         /* Check that point segment of the boundbox of the selection stroke */
1143                         if (((!ELEM(V2D_IS_CLIPPED, pc1[0], pc1[1])) && BLI_rcti_isect_pt(rect, pc1[0], pc1[1])) ||
1144                             ((!ELEM(V2D_IS_CLIPPED, pc2[0], pc2[1])) && BLI_rcti_isect_pt(rect, pc2[0], pc2[1])))
1145                         {
1146                                 /* Check if point segment of stroke had anything to do with
1147                                  * brush region  (either within stroke painted, or on its lines)
1148                                  *  - this assumes that linewidth is irrelevant
1149                                  */
1150                                 if (gp_stroke_inside_circle(gso->mval, gso->mval_prev, radius, pc1[0], pc1[1], pc2[0], pc2[1])) {
1151                                         /* Apply operation to these points */
1152                                         bool ok = false;
1153                                         
1154                                         /* To each point individually... */
1155                                         ok = apply(gso, gps, i, radius, pc1);
1156                                         
1157                                         /* Only do the second point if this is the last segment,
1158                                          * and it is unlikely that the point will get handled
1159                                          * otherwise. 
1160                                          * 
1161                                          * NOTE: There is a small risk here that the second point wasn't really
1162                                          *       actually in-range. In that case, it only got in because
1163                                          *       the line linking the points was!
1164                                          */
1165                                         if (i + 1 == gps->totpoints - 1) {
1166                                                 ok |= apply(gso, gps, i + 1, radius, pc2);
1167                                                 include_last = false;
1168                                         }
1169                                         else {
1170                                                 include_last = true;
1171                                         }
1172                                         
1173                                         changed |= ok;
1174                                 }
1175                                 else if (include_last) {
1176                                         /* This case is for cases where for whatever reason the second vert (1st here) doesn't get included
1177                                          * because the whole edge isn't in bounds, but it would've qualified since it did with the
1178                                          * previous step (but wasn't added then, to avoid double-ups) 
1179                                          */
1180                                         changed |= apply(gso, gps, i, radius, pc1);
1181                                         include_last = false;
1182                                 }
1183                         }
1184                 }
1185         }
1186         
1187         return changed;
1188 }
1189
1190 /* Perform two-pass brushes which modify the existing strokes */
1191 static bool gpsculpt_brush_apply_standard(bContext *C, tGP_BrushEditData *gso)
1192 {
1193         bool changed = false;
1194         
1195         /* Calculate brush-specific data which applies equally to all points */
1196         switch (gso->brush_type) {
1197                 case GP_EDITBRUSH_TYPE_GRAB: /* Grab points */
1198                 case GP_EDITBRUSH_TYPE_PUSH: /* Push points */
1199                 {
1200                         /* calculate amount of displacement to apply */
1201                         gp_brush_grab_calc_dvec(gso);
1202                         break;
1203                 }
1204                 
1205                 case GP_EDITBRUSH_TYPE_PINCH: /* Pinch points */
1206                 case GP_EDITBRUSH_TYPE_TWIST: /* Twist points around midpoint */
1207                 {
1208                         /* calculate midpoint of the brush (in data space) */
1209                         gp_brush_calc_midpoint(gso);
1210                         break;
1211                 }
1212                 
1213                 case GP_EDITBRUSH_TYPE_RANDOMIZE: /* Random jitter */
1214                 {
1215                         /* compute the displacement vector for the cursor (in data space) */
1216                         gp_brush_grab_calc_dvec(gso);
1217                         break;
1218                 }
1219                 
1220                 default:
1221                         break;
1222         }
1223         
1224         
1225         /* Find visible strokes, and perform operations on those if hit */
1226         CTX_DATA_BEGIN(C, bGPDstroke *, gps, editable_gpencil_strokes)
1227         {
1228                 switch (gso->brush_type) {
1229                         case GP_EDITBRUSH_TYPE_SMOOTH: /* Smooth strokes */
1230                         {
1231                                 changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_smooth_apply);
1232                                 break;
1233                         }
1234                         
1235                         case GP_EDITBRUSH_TYPE_THICKNESS: /* Adjust stroke thickness */
1236                         {
1237                                 changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_thickness_apply);
1238                                 break;
1239                         }
1240                         
1241                         case GP_EDITBRUSH_TYPE_GRAB: /* Grab points */
1242                         {
1243                                 if (gso->first) {
1244                                         /* First time this brush stroke is being applied:
1245                                          * 1) Prepare data buffers (init/clear) for this stroke
1246                                          * 2) Use the points now under the cursor
1247                                          */
1248                                         gp_brush_grab_stroke_init(gso, gps);
1249                                         changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_grab_store_points);
1250                                 }
1251                                 else {
1252                                         /* Apply effect to the stored points */
1253                                         gp_brush_grab_apply_cached(gso, gps);
1254                                         changed |= true;
1255                                 }
1256                                 break;
1257                         }
1258                         
1259                         case GP_EDITBRUSH_TYPE_PUSH: /* Push points */
1260                         {
1261                                 changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_push_apply);
1262                                 break;
1263                         }
1264                         
1265                         case GP_EDITBRUSH_TYPE_PINCH: /* Pinch points */
1266                         {
1267                                 changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_pinch_apply);
1268                                 break;
1269                         }
1270                         
1271                         case GP_EDITBRUSH_TYPE_TWIST: /* Twist points around midpoint */
1272                         {
1273                                 changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_twist_apply);
1274                                 break;
1275                         }
1276                         
1277                         case GP_EDITBRUSH_TYPE_RANDOMIZE: /* Apply jitter */
1278                         {
1279                                 changed |= gpsculpt_brush_do_stroke(gso, gps, gp_brush_randomize_apply);
1280                                 break;
1281                         }
1282                         
1283                         default:
1284                                 printf("ERROR: Unknown type of GPencil Sculpt brush - %u\n", gso->brush_type);
1285                                 break;
1286                 }
1287         }
1288         CTX_DATA_END;
1289         
1290         return changed;
1291 }
1292
1293 /* Calculate settings for applying brush */
1294 static void gpsculpt_brush_apply(bContext *C, wmOperator *op, PointerRNA *itemptr)
1295 {
1296         tGP_BrushEditData *gso = op->customdata;
1297         const int radius = gso->brush->size;
1298         float mousef[2];
1299         int mouse[2];
1300         bool changed = false;
1301         
1302         /* Get latest mouse coordinates */
1303         RNA_float_get_array(itemptr, "mouse", mousef);
1304         gso->mval[0] = mouse[0] = (int)(mousef[0]);
1305         gso->mval[1] = mouse[1] = (int)(mousef[1]);
1306         
1307         gso->pressure = RNA_float_get(itemptr, "pressure");
1308         
1309         if (RNA_boolean_get(itemptr, "pen_flip"))
1310                 gso->flag |= GP_EDITBRUSH_FLAG_INVERT;
1311         else
1312                 gso->flag &= ~GP_EDITBRUSH_FLAG_INVERT;
1313         
1314         
1315         /* Store coordinates as reference, if operator just started running */
1316         if (gso->first) {
1317                 gso->mval_prev[0]  = gso->mval[0];
1318                 gso->mval_prev[1]  = gso->mval[1];
1319                 gso->pressure_prev = gso->pressure;
1320         }
1321         
1322         /* Update brush_rect, so that it represents the bounding rectangle of brush */
1323         gso->brush_rect.xmin = mouse[0] - radius;
1324         gso->brush_rect.ymin = mouse[1] - radius;
1325         gso->brush_rect.xmax = mouse[0] + radius;
1326         gso->brush_rect.ymax = mouse[1] + radius;
1327         
1328         
1329         /* Apply brush */
1330         if (gso->brush_type == GP_EDITBRUSH_TYPE_CLONE) {
1331                 changed = gpsculpt_brush_apply_clone(C, gso);
1332         }
1333         else {
1334                 changed = gpsculpt_brush_apply_standard(C, gso);
1335         }
1336         
1337         
1338         /* Updates */
1339         if (changed) {
1340                 WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
1341         }
1342         
1343         /* Store values for next step */
1344         gso->mval_prev[0]  = gso->mval[0];
1345         gso->mval_prev[1]  = gso->mval[1];
1346         gso->pressure_prev = gso->pressure;
1347         gso->first = false;
1348 }
1349
1350 /* Running --------------------------------------------- */
1351
1352 /* helper - a record stroke, and apply paint event */
1353 static void gpsculpt_brush_apply_event(bContext *C, wmOperator *op, const wmEvent *event)
1354 {
1355         tGP_BrushEditData *gso = op->customdata;
1356         PointerRNA itemptr;
1357         float mouse[2];
1358         int tablet = 0;
1359         
1360         mouse[0] = event->mval[0] + 1;
1361         mouse[1] = event->mval[1] + 1;
1362         
1363         /* fill in stroke */
1364         RNA_collection_add(op->ptr, "stroke", &itemptr);
1365         
1366         RNA_float_set_array(&itemptr, "mouse", mouse);
1367         RNA_boolean_set(&itemptr, "pen_flip", event->ctrl != false);
1368         RNA_boolean_set(&itemptr, "is_start", gso->first);
1369         
1370         /* handle pressure sensitivity (which is supplied by tablets) */
1371         if (event->tablet_data) {
1372                 const wmTabletData *wmtab = event->tablet_data;
1373                 float pressure = wmtab->Pressure;
1374                 
1375                 tablet = (wmtab->Active != EVT_TABLET_NONE);
1376                 
1377                 /* special exception here for too high pressure values on first touch in
1378                  * windows for some tablets: clamp the values to be sane
1379                  */
1380                 if (tablet && (pressure >= 0.99f)) {
1381                         pressure = 1.0f;
1382                 }
1383                 RNA_float_set(&itemptr, "pressure", pressure);
1384         }
1385         else {
1386                 RNA_float_set(&itemptr, "pressure", 1.0f);
1387         }
1388         
1389         /* apply */
1390         gpsculpt_brush_apply(C, op, &itemptr);
1391 }
1392
1393 /* reapply */
1394 static int gpsculpt_brush_exec(bContext *C, wmOperator *op)
1395 {
1396         if (!gpsculpt_brush_init(C, op))
1397                 return OPERATOR_CANCELLED;
1398         
1399         RNA_BEGIN(op->ptr, itemptr, "stroke") 
1400         {
1401                 gpsculpt_brush_apply(C, op, &itemptr);
1402         }
1403         RNA_END;
1404         
1405         gpsculpt_brush_exit(C, op);
1406         
1407         return OPERATOR_FINISHED;
1408 }
1409
1410
1411 /* start modal painting */
1412 static int gpsculpt_brush_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1413 {
1414         tGP_BrushEditData *gso = NULL;
1415         const bool is_modal = RNA_boolean_get(op->ptr, "wait_for_input");
1416         bool needs_timer = false;
1417         float brush_rate = 0.0f;
1418         
1419         /* init painting data */
1420         if (!gpsculpt_brush_init(C, op))
1421                 return OPERATOR_CANCELLED;
1422         
1423         gso = op->customdata;
1424         
1425         /* initialise type-specific data (used for the entire session) */
1426         switch (gso->brush_type) {
1427                 /* Brushes requiring timer... */
1428                 case GP_EDITBRUSH_TYPE_THICKNESS:
1429                         brush_rate = 0.01f; // XXX: hardcoded
1430                         needs_timer = true;
1431                         break;
1432                         
1433                 case GP_EDITBRUSH_TYPE_PINCH:
1434                         brush_rate = 0.001f; // XXX: hardcoded
1435                         needs_timer = true;
1436                         break;
1437                 
1438                 case GP_EDITBRUSH_TYPE_TWIST:
1439                         brush_rate = 0.01f; // XXX: hardcoded
1440                         needs_timer = true;
1441                         break;
1442                         
1443                 default:
1444                         break;
1445         }
1446         
1447         /* register timer for increasing influence by hovering over an area */
1448         if (needs_timer) {
1449                 gso->timer = WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, brush_rate);
1450         }
1451         
1452         /* register modal handler */
1453         WM_event_add_modal_handler(C, op);
1454         
1455         /* start drawing immediately? */
1456         if (is_modal == false) {
1457                 ARegion *ar = CTX_wm_region(C);
1458                 
1459                 /* ensure that we'll have a new frame to draw on */
1460                 gpsculpt_brush_init_stroke(gso);
1461                 
1462                 /* apply first dab... */
1463                 gso->is_painting = true;
1464                 gpsculpt_brush_apply_event(C, op, event);
1465                 
1466                 /* redraw view with feedback */
1467                 ED_region_tag_redraw(ar);
1468         }
1469         
1470         return OPERATOR_RUNNING_MODAL;
1471 }
1472
1473 /* painting - handle events */
1474 static int gpsculpt_brush_modal(bContext *C, wmOperator *op, const wmEvent *event)
1475 {
1476         tGP_BrushEditData *gso = op->customdata;
1477         const bool is_modal = RNA_boolean_get(op->ptr, "wait_for_input");
1478         bool redraw_region = false;
1479         bool redraw_toolsettings = false;
1480         
1481         /* The operator can be in 2 states: Painting and Idling */
1482         if (gso->is_painting) {
1483                 /* Painting  */
1484                 switch (event->type) {
1485                         /* Mouse Move = Apply somewhere else */
1486                         case MOUSEMOVE:
1487                         case INBETWEEN_MOUSEMOVE:
1488                                 /* apply brush effect at new position */
1489                                 gpsculpt_brush_apply_event(C, op, event);
1490                                 
1491                                 /* force redraw, so that the cursor will at least be valid */
1492                                 redraw_region = true;
1493                                 break;
1494                         
1495                         /* Timer Tick - Only if this was our own timer */
1496                         case TIMER:
1497                                 if (event->customdata == gso->timer) {
1498                                         gso->timerTick = true;
1499                                         gpsculpt_brush_apply_event(C, op, event);
1500                                         gso->timerTick = false;
1501                                 }
1502                                 break;
1503                                 
1504                         /* Adjust brush settings */
1505                         /* FIXME: Step increments and modifier keys are hardcoded here! */
1506                         case WHEELUPMOUSE:
1507                         case PADPLUSKEY:
1508                                 if (event->shift) {
1509                                         /* increase strength */
1510                                         gso->brush->strength += 0.05f;
1511                                         CLAMP_MAX(gso->brush->strength, 1.0f);
1512                                 }
1513                                 else {
1514                                         /* increase brush size */
1515                                         gso->brush->size += 3;
1516                                         CLAMP_MAX(gso->brush->size, 300);
1517                                 }
1518                                 
1519                                 redraw_region = true;
1520                                 redraw_toolsettings = true;
1521                                 break;
1522                         
1523                         case WHEELDOWNMOUSE: 
1524                         case PADMINUS:
1525                                 if (event->shift) {
1526                                         /* decrease strength */
1527                                         gso->brush->strength -= 0.05f;
1528                                         CLAMP_MIN(gso->brush->strength, 0.0f);
1529                                 }
1530                                 else {
1531                                         /* decrease brush size */
1532                                         gso->brush->size -= 3;
1533                                         CLAMP_MIN(gso->brush->size, 1);
1534                                 }
1535                                 
1536                                 redraw_region = true;
1537                                 redraw_toolsettings = true;
1538                                 break;
1539                         
1540                         /* Painting mbut release = Stop painting (back to idle) */
1541                         case LEFTMOUSE:
1542                                 //BLI_assert(event->val == KM_RELEASE);
1543                                 if (is_modal) {
1544                                         /* go back to idling... */
1545                                         gso->is_painting = false;
1546                                 }
1547                                 else {
1548                                         /* end sculpt session, since we're not modal */
1549                                         gso->is_painting = false;
1550                                         
1551                                         gpsculpt_brush_exit(C, op);
1552                                         return OPERATOR_FINISHED;
1553                                 }
1554                                 break;
1555                                 
1556                         /* Abort painting if any of the usual things are tried */
1557                         case MIDDLEMOUSE:
1558                         case RIGHTMOUSE:
1559                         case ESCKEY:
1560                                 gpsculpt_brush_exit(C, op);
1561                                 return OPERATOR_FINISHED;
1562                 }
1563         }
1564         else {
1565                 /* Idling */
1566                 BLI_assert(is_modal == true);
1567                 
1568                 switch (event->type) {
1569                         /* Painting mbut press = Start painting (switch to painting state) */
1570                         case LEFTMOUSE:
1571                                 /* do initial "click" apply */
1572                                 gso->is_painting = true;
1573                                 gso->first = true;
1574                                 
1575                                 gpsculpt_brush_init_stroke(gso);
1576                                 gpsculpt_brush_apply_event(C, op, event);
1577                                 break;
1578                                 
1579                         /* Exit modal operator, based on the "standard" ops */
1580                         case RIGHTMOUSE:
1581                         case ESCKEY:
1582                                 gpsculpt_brush_exit(C, op);
1583                                 return OPERATOR_FINISHED;
1584                                 
1585                         /* MMB is often used for view manipulations */
1586                         case MIDDLEMOUSE:
1587                                 return OPERATOR_PASS_THROUGH;
1588                         
1589                         /* Mouse movements should update the brush cursor - Just redraw the active region */
1590                         case MOUSEMOVE:
1591                         case INBETWEEN_MOUSEMOVE:
1592                                 redraw_region = true;
1593                                 break;
1594                         
1595                         /* Adjust brush settings */
1596                         /* FIXME: Step increments and modifier keys are hardcoded here! */
1597                         case WHEELUPMOUSE:
1598                         case PADPLUSKEY:
1599                                 if (event->shift) {
1600                                         /* increase strength */
1601                                         gso->brush->strength += 0.05f;
1602                                         CLAMP_MAX(gso->brush->strength, 1.0f);
1603                                 }
1604                                 else {
1605                                         /* increase brush size */
1606                                         gso->brush->size += 3;
1607                                         CLAMP_MAX(gso->brush->size, 300);
1608                                 }
1609                                 
1610                                 redraw_region = true;
1611                                 redraw_toolsettings = true;
1612                                 break;
1613                         
1614                         case WHEELDOWNMOUSE: 
1615                         case PADMINUS:
1616                                 if (event->shift) {
1617                                         /* decrease strength */
1618                                         gso->brush->strength -= 0.05f;
1619                                         CLAMP_MIN(gso->brush->strength, 0.0f);
1620                                 }
1621                                 else {
1622                                         /* decrease brush size */
1623                                         gso->brush->size -= 3;
1624                                         CLAMP_MIN(gso->brush->size, 1);
1625                                 }
1626                                 
1627                                 redraw_region = true;
1628                                 redraw_toolsettings = true;
1629                                 break;
1630                         
1631                         /* Change Frame - Allowed */
1632                         case LEFTARROWKEY:
1633                         case RIGHTARROWKEY:
1634                         case UPARROWKEY:
1635                         case DOWNARROWKEY:
1636                                 return OPERATOR_PASS_THROUGH;
1637                         
1638                         /* Unhandled event */
1639                         default:
1640                                 break;
1641                 }
1642         }
1643         
1644         /* Redraw region? */
1645         if (redraw_region) {
1646                 ARegion *ar = CTX_wm_region(C);
1647                 ED_region_tag_redraw(ar);
1648         }
1649         
1650         /* Redraw toolsettings (brush settings)? */
1651         if (redraw_toolsettings) {
1652                 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
1653         }
1654         
1655         return OPERATOR_RUNNING_MODAL;
1656 }
1657
1658
1659 /* Operator --------------------------------------------- */
1660
1661 void GPENCIL_OT_brush_paint(wmOperatorType *ot)
1662 {
1663         PropertyRNA *prop;
1664         
1665         /* identifiers */
1666         ot->name = "Stroke Sculpt";
1667         ot->idname = "GPENCIL_OT_brush_paint";
1668         ot->description = "Apply tweaks to strokes by painting over the strokes"; // XXX
1669         
1670         /* api callbacks */
1671         ot->exec = gpsculpt_brush_exec;
1672         ot->invoke = gpsculpt_brush_invoke;
1673         ot->modal = gpsculpt_brush_modal;
1674         ot->cancel = gpsculpt_brush_exit;
1675         ot->poll = gpsculpt_brush_poll;
1676
1677         /* flags */
1678         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
1679
1680         /* properties */
1681         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1682         
1683         prop = RNA_def_boolean(ot->srna, "wait_for_input", true, "Wait for Input",
1684                                "Enter a mini 'sculpt-mode' if enabled, otherwise, exit after drawing a single stroke");
1685         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1686 }
1687
1688 /* ************************************************ */