Blender V5.3
annotate_paint.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008-2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_enum_flags.hh"
17#include "BLI_listbase.hh"
18#include "BLI_math_geom_c.hh"
19#include "BLI_math_matrix_c.hh"
20#include "BLI_time.hh"
21
22#include "BLT_translation.hh"
23
24#include "BKE_annotations.h"
26#include "BKE_callbacks.hh"
27#include "BKE_camera.h"
28#include "BKE_context.hh"
29#include "BKE_report.hh"
30#include "BKE_screen.hh"
31#include "BKE_tracking.hh"
32
34#include "DNA_meshdata_types.h"
35#include "DNA_scene_types.h"
37
38#include "UI_resources.hh"
39#include "UI_view2d.hh"
40
41#include "ED_clip.hh"
42#include "ED_gpencil_legacy.hh"
43#include "ED_screen.hh"
44#include "ED_view3d.hh"
45
46#include "GPU_immediate.hh"
47#include "GPU_immediate_util.hh"
48#include "GPU_state.hh"
49
50#include "RNA_access.hh"
51#include "RNA_define.hh"
52#include "RNA_prototypes.hh"
53
54#include "WM_api.hh"
55#include "WM_types.hh"
56
57#include "gpencil_intern.hh"
58
59namespace blender {
60
61/* ******************************************* */
62/* 'Globals' and Defines */
63
64#define DEPTH_INVALID 1.0f
65
66/* values for tGPsdata->status */
68 GP_STATUS_IDLING = 0, /* stroke isn't in progress yet */
69 GP_STATUS_PAINTING, /* a stroke is in progress */
70 GP_STATUS_ERROR, /* something wasn't correctly set up */
71 GP_STATUS_DONE, /* painting done */
72 GP_STATUS_CAPTURE /* capture event, but cancel */
73};
74
75/* Return flags for adding points to stroke buffer */
77 GP_STROKEADD_INVALID = -2, /* error occurred - insufficient info to do so */
78 GP_STROKEADD_OVERFLOW = -1, /* error occurred - cannot fit any more points */
79 GP_STROKEADD_NORMAL, /* point was successfully added */
80 GP_STROKEADD_FULL, /* cannot add any more points to buffer */
81};
82
83/* Runtime flags */
85 GP_PAINTFLAG_FIRSTRUN = (1 << 0), /* operator just started */
89 /* Flags used to indicate if stabilization is being used. */
92};
94
95/* Temporary 'Stroke' Operation data
96 * "p" = op->customdata
97 */
98struct tGPsdata {
102 Depsgraph *depsgraph;
103
115 const rctf *subrect;
117
120
129
132
139
141 short radius;
142
143 /* Stabilizer. */
147
149 float mval[2];
151 float mvalo[2];
152
154 float pressure;
157
158 /* These need to be doubles, as (at least under unix) they are in seconds since epoch,
159 * float (and its 7 digits precision) is definitively not enough here!
160 * double, with its 15 digits precision,
161 * ensures us millisecond precision for a few centuries at least.
162 */
164 double inittime;
166 double curtime;
168 double ocurtime;
169
174 float imat[4][4];
175 float mat[4][4];
176
178 float custom_color[4];
179
182
184 short straight[2];
185
188};
189
190/* ------ */
191
192/* Macros for accessing sensitivity thresholds... */
193/* minimum number of pixels mouse should move before new point created */
194#define MIN_MANHATTAN_PX (U.gp_manhattandist)
195/* minimum length of new segment before new point can be added */
196#define MIN_EUCLIDEAN_PX (U.gp_euclideandist)
197
199{
200 return (p->gpf && p->gpf->strokes.last() && p->flags & GP_PAINTFLAG_STROKEADDED);
201}
202
204{
205 BLI_assert(p->gpf->strokes.last() != nullptr);
207}
208
209/* ------ */
210/* Forward defines for some functions... */
211
212static void annotation_session_validatebuffer(tGPsdata *p);
213
214/* ******************************************* */
215/* Context Wrangling... */
216
217/* check if context is suitable for drawing */
219{
221 /* check if current context can support GPencil data */
222 if (ED_annotation_data_get_pointers(C, nullptr) != nullptr) {
223 return true;
224 }
225 CTX_wm_operator_poll_msg_set(C, "Failed to find Annotation data to draw into");
226 }
227 else {
228 CTX_wm_operator_poll_msg_set(C, "Active region not set");
229 }
230
231 return false;
232}
233
234/* check if projecting strokes into 3d-geometry in the 3D-View */
236{
237 bGPdata *gpd = p->gpd;
238 return ((gpd->runtime.sbuffer_sflag & GP_STROKE_3DSPACE) &&
240}
241
242/* ******************************************* */
243/* Calculations/Conversions */
244
245/* Utilities --------------------------------- */
246
247/* get the reference point for stroke-point conversions */
248static void annotation_get_3d_reference(tGPsdata *p, float vec[3])
249{
250 const float *fp = p->scene->cursor.location;
251
252 /* use 3D-cursor */
253 copy_v3_v3(vec, fp);
254}
255
256/* Stroke Editing ---------------------------- */
257
258/* check if the current mouse position is suitable for adding a new point */
259static bool annotation_stroke_filtermval(tGPsdata *p, const float mval[2], const float pmval[2])
260{
261 int dx = int(fabsf(mval[0] - pmval[0]));
262 int dy = int(fabsf(mval[1] - pmval[1]));
263
264 /* if buffer is empty, just let this go through (i.e. so that dots will work) */
265 if (p->gpd->runtime.sbuffer_used == 0) {
266 return true;
267 }
268
269 /* check if mouse moved at least certain distance on both axes (best case)
270 * - aims to eliminate some jitter-noise from input when trying to draw straight lines freehand
271 */
272
273 /* If lazy mouse, check minimum distance. */
275 if ((dx * dx + dy * dy) > (p->stabilizer_radius * p->stabilizer_radius)) {
276 return true;
277 }
278
279 /* If the mouse is moving within the radius of the last move,
280 * don't update the mouse position. This allows sharp turns. */
281 copy_v2_v2(p->mval, p->mvalo);
282 return false;
283 }
284
285 if ((dx > MIN_MANHATTAN_PX) && (dy > MIN_MANHATTAN_PX)) {
286 return true;
287 }
288
289 /* Check if the distance since the last point is significant enough:
290 * - Prevents points being added too densely
291 * - Distance here doesn't use `sqrt` to prevent slowness.
292 * We should still be safe from overflows though.
293 */
294 if ((dx * dx + dy * dy) > MIN_EUCLIDEAN_PX * MIN_EUCLIDEAN_PX) {
295 return true;
296 }
297
298 /* mouse 'didn't move' */
299 return false;
300}
301
302/* convert screen-coordinates to buffer-coordinates */
304 const float2 mval,
305 float out[3],
306 const float *depth)
307{
308 bGPdata *gpd = p->gpd;
309 if (depth && (*depth == DEPTH_INVALID)) {
310 depth = nullptr;
311 }
312
313 /* in 3d-space - pt->x/y/z are 3 side-by-side floats */
315 int2 mval_i = int2(mval);
316 if (annotation_project_check(p) && ED_view3d_autodist_simple(p->region, mval_i, out, 0, depth))
317 {
318 /* projecting onto 3D-Geometry
319 * - nothing more needs to be done here, since view_autodist_simple() has already done it
320 */
321 }
322 else {
323 float mval_prj[2];
324 float rvec[3];
325
326 /* Current method just converts each point in screen-coordinates to
327 * 3D-coordinates using the 3D-cursor as reference. In general, this
328 * works OK, but it could of course be improved.
329 *
330 * TODO:
331 * - investigate using nearest point(s) on a previous stroke as
332 * reference point instead or as offset, for easier stroke matching
333 */
334
336 const float zfac = ED_view3d_calc_zfac(
337 static_cast<const RegionView3D *>(p->region->regiondata), rvec);
338
341 {
342 float dvec[3];
343 float xy_delta[2];
344 sub_v2_v2v2(xy_delta, mval_prj, mval);
345 ED_view3d_win_to_delta(p->region, xy_delta, zfac, dvec);
346 sub_v3_v3v3(out, rvec, dvec);
347 }
348 else {
349 zero_v3(out);
350 }
351 }
352 }
353
354 /* 2d - on 'canvas' (assume that p->v2d is set) */
355 else if ((gpd->runtime.sbuffer_sflag & GP_STROKE_2DSPACE) && (p->v2d)) {
356 ui::view2d_region_to_view(p->v2d, mval[0], mval[1], &out[0], &out[1]);
357 mul_v3_m4v3(out, p->imat, out);
358 }
359
360 /* 2d - relative to screen (viewport area) */
361 else {
362 if (p->subrect == nullptr) { /* normal 3D view */
363 out[0] = float(mval[0]) / float(p->region->winx) * 100;
364 out[1] = float(mval[1]) / float(p->region->winy) * 100;
365 }
366 else { /* camera view, use subrect */
367 out[0] = ((mval[0] - p->subrect->xmin) / BLI_rctf_size_x(p->subrect)) * 100;
368 out[1] = ((mval[1] - p->subrect->ymin) / BLI_rctf_size_y(p->subrect)) * 100;
369 }
370 }
371}
372
383static void annotation_smooth_buffer(tGPsdata *p, float inf, int idx)
384{
385 bGPdata *gpd = p->gpd;
386 short num_points = gpd->runtime.sbuffer_used;
387
388 /* Do nothing if not enough points to smooth out */
389 if ((num_points < 3) || (idx < 3) || (inf == 0.0f)) {
390 return;
391 }
392
393 tGPspoint *points = gpd->runtime.sbuffer;
394 float steps = 4.0f;
395 if (idx < 4) {
396 steps--;
397 }
398
399 tGPspoint *pta = idx >= 4 ? &points[idx - 4] : nullptr;
400 tGPspoint *ptb = idx >= 3 ? &points[idx - 3] : nullptr;
401 tGPspoint *ptc = idx >= 2 ? &points[idx - 2] : nullptr;
402 tGPspoint *ptd = &points[idx - 1];
403
404 float sco[2] = {0.0f};
405 float a[2], b[2], c[2], d[2];
406 const float average_fac = 1.0f / steps;
407
408 /* Compute smoothed coordinate by taking the ones nearby */
409 if (pta) {
410 copy_v2_v2(a, pta->m_xy);
411 madd_v2_v2fl(sco, a, average_fac);
412 }
413 if (ptb) {
414 copy_v2_v2(b, ptb->m_xy);
415 madd_v2_v2fl(sco, b, average_fac);
416 }
417 if (ptc) {
418 copy_v2_v2(c, ptc->m_xy);
419 madd_v2_v2fl(sco, c, average_fac);
420 }
421 if (ptd) {
422 copy_v2_v2(d, ptd->m_xy);
423 madd_v2_v2fl(sco, d, average_fac);
424 }
425
426 /* Based on influence factor, blend between original and optimal smoothed coordinate */
427 interp_v2_v2v2(c, c, sco, inf);
428 copy_v2_v2(ptc->m_xy, c);
429}
430
431static void annotation_stroke_arrow_calc_points_segment(float stroke_points[8],
432 const float ref_point[2],
433 const float dir_cw[2],
434 const float dir_ccw[2],
435 const float length,
436 const float sign)
437{
438 stroke_points[0] = ref_point[0] + dir_cw[0] * length * sign;
439 stroke_points[1] = ref_point[1] + dir_cw[1] * length * sign;
440 stroke_points[2] = ref_point[0] + dir_ccw[0] * length * sign;
441 stroke_points[3] = ref_point[1] + dir_ccw[1] * length * sign;
442}
443
445 const float stroke_dir[2],
446 float corner[2],
447 float stroke_points[8],
448 const int arrow_style)
449{
450 const int arrow_length = 8;
451 float norm_dir[2];
452 copy_v2_v2(norm_dir, stroke_dir);
453 normalize_v2(norm_dir);
454 const float inv_norm_dir_clockwise[2] = {norm_dir[1], -norm_dir[0]};
455 const float inv_norm_dir_counterclockwise[2] = {-norm_dir[1], norm_dir[0]};
456
457 switch (arrow_style) {
459 mul_v2_fl(norm_dir, arrow_length);
460 stroke_points[0] = corner[0] + inv_norm_dir_clockwise[0] * arrow_length + norm_dir[0];
461 stroke_points[1] = corner[1] + inv_norm_dir_clockwise[1] * arrow_length + norm_dir[1];
462 stroke_points[2] = corner[0] + inv_norm_dir_counterclockwise[0] * arrow_length + norm_dir[0];
463 stroke_points[3] = corner[1] + inv_norm_dir_counterclockwise[1] * arrow_length + norm_dir[1];
464 break;
467 corner,
468 inv_norm_dir_clockwise,
469 inv_norm_dir_counterclockwise,
470 arrow_length,
471 1.0f);
472 break;
474 mul_v2_fl(norm_dir, arrow_length);
475 if (point != nullptr) {
476 add_v2_v2(point->m_xy, norm_dir);
477 copy_v2_v2(corner, point->m_xy);
478 }
480 corner,
481 inv_norm_dir_clockwise,
482 inv_norm_dir_counterclockwise,
483 arrow_length,
484 -1.0f);
485 stroke_points[4] = corner[0] - norm_dir[0];
486 stroke_points[5] = corner[1] - norm_dir[1];
487 break;
489 mul_v2_fl(norm_dir, arrow_length * 1.5f);
490 if (point != nullptr) {
491 add_v2_v2(point->m_xy, norm_dir);
492 copy_v2_v2(corner, point->m_xy);
493 }
495 corner,
496 inv_norm_dir_clockwise,
497 inv_norm_dir_counterclockwise,
498 arrow_length * 0.75f,
499 -1.0f);
500 stroke_points[4] = stroke_points[0] - norm_dir[0];
501 stroke_points[5] = stroke_points[1] - norm_dir[1];
502 stroke_points[6] = stroke_points[2] - norm_dir[0];
503 stroke_points[7] = stroke_points[3] - norm_dir[1];
504 break;
505 default:
506 break;
507 }
508}
509
510/* add current stroke-point to buffer (returns whether point was successfully added) */
512 const float mval[2],
513 float pressure,
514 double curtime)
515{
516 bGPdata *gpd = p->gpd;
517 tGPspoint *pt;
519
520 /* check painting mode */
522 /* straight lines only - i.e. only store start and end point in buffer */
523 if (gpd->runtime.sbuffer_used == 0) {
524 /* first point in buffer (start point) */
525 pt = gpd->runtime.sbuffer;
526
527 /* store settings */
528 copy_v2_v2(pt->m_xy, mval);
529 /* Pressure values are unreliable, so ignore for now, see #44932. */
530 pt->pressure = 1.0f;
531 pt->strength = 1.0f;
532 pt->time = float(curtime - p->inittime);
533
534 /* increment buffer size */
535 gpd->runtime.sbuffer_used++;
536 }
537 else {
538 /* just reset the endpoint to the latest value
539 * - assume that pointers for this are always valid...
540 */
541 pt = (gpd->runtime.sbuffer + 1);
542
543 /* store settings */
544 copy_v2_v2(pt->m_xy, mval);
545 /* Pressure values are unreliable, so ignore for now, see #44932. */
546 pt->pressure = 1.0f;
547 pt->strength = 1.0f;
548 pt->time = float(curtime - p->inittime);
549
550 /* now the buffer has 2 points (and shouldn't be allowed to get any larger) */
551 gpd->runtime.sbuffer_used = 2;
552
553 /* Arrows. */
555 /* Store start and end point coords for arrows. */
556 float end[2];
557 copy_v2_v2(end, pt->m_xy);
558 pt = gpd->runtime.sbuffer;
559 float start[2];
560 copy_v2_v2(start, pt->m_xy);
561
562 /* Arrow end corner. */
564 pt++;
565 const float e_heading[2] = {start[0] - end[0], start[1] - end[1]};
566 /* Calculate points for ending arrow. */
568 pt, e_heading, end, gpd->runtime.arrow_end, gpd->runtime.arrow_end_style);
569 }
570 /* Arrow start corner. */
572 const float s_heading[2] = {end[0] - start[0], end[1] - start[1]};
573 /* Calculate points for starting arrow. */
575 nullptr, s_heading, start, gpd->runtime.arrow_start, gpd->runtime.arrow_start_style);
576 }
577 }
578 }
579
580 /* can keep carrying on this way :) */
581 return GP_STROKEADD_NORMAL;
582 }
583
584 if (p->paintmode == GP_PAINTMODE_DRAW) { /* normal drawing */
585 /* check if still room in buffer or add more */
587 gpd->runtime.sbuffer, &gpd->runtime.sbuffer_size, &gpd->runtime.sbuffer_used, false);
588
589 /* get pointer to destination point */
590 pt = gpd->runtime.sbuffer + gpd->runtime.sbuffer_used;
591
592 /* store settings */
593 copy_v2_v2(pt->m_xy, mval);
594 pt->pressure = pressure;
595 /* Unused for annotations, but initialize for easier conversions to GP Object. */
596 pt->strength = 1.0f;
597
598 /* point time */
599 pt->time = float(curtime - p->inittime);
600
601 /* increment counters */
602 gpd->runtime.sbuffer_used++;
603
604 /* Don't smooth if stabilizer is on. */
605 if ((p->flags & GP_PAINTFLAG_USE_STABILIZER_TEMP) == 0) {
606 /* smooth while drawing previous points with a reduction factor for previous */
607 for (int s = 0; s < 3; s++) {
608 annotation_smooth_buffer(p, 0.5f * ((3.0f - s) / 3.0f), gpd->runtime.sbuffer_used - s);
609 }
610 }
611
612 return GP_STROKEADD_NORMAL;
613 }
614
616 /* get pointer to destination point */
617 pt = gpd->runtime.sbuffer;
618
619 /* store settings */
620 copy_v2_v2(pt->m_xy, mval);
621 /* Pressure values are unreliable, so ignore for now, see #44932. */
622 pt->pressure = 1.0f;
623 pt->strength = 1.0f;
624 pt->time = float(curtime - p->inittime);
625
626 /* if there's stroke for this poly line session add (or replace last) point
627 * to stroke. This allows to draw lines more interactively (see new segment
628 * during mouse slide, e.g.)
629 */
631 bGPDstroke *gps = p->gpf->strokes.last();
632 bGPDspoint *pts;
633
634 /* first time point is adding to temporary buffer -- need to allocate new point in stroke */
635 if (gpd->runtime.sbuffer_used == 0) {
636 gps->points = static_cast<bGPDspoint *>(
637 MEM_realloc_uninitialized(gps->points, sizeof(bGPDspoint) * (gps->totpoints + 1)));
638 gps->totpoints++;
639 }
640
641 pts = &gps->points[gps->totpoints - 1];
642
643 /* special case for poly lines: normally,
644 * depth is needed only when creating new stroke from buffer,
645 * but poly lines are converting to stroke instantly,
646 * so initialize depth buffer before converting coordinates
647 */
649 View3D *v3d = p->area->spacedata.first_as<View3D>();
650
652
656 }
657 else {
658 mode = V3D_DEPTH_ALL;
659 }
660 }
661
663 ED_view3d_depth_override(p->depsgraph, p->region, v3d, nullptr, mode, false, nullptr);
664 }
665
666 /* convert screen-coordinates to appropriate coordinates (and store them) */
667 annotation_stroke_convertcoords(p, pt->m_xy, &pts->x, nullptr);
668
669 /* copy pressure and time */
670 pts->pressure = pt->pressure;
671 pts->strength = pt->strength;
672 pts->time = pt->time;
673 gps->tot_triangles = 0;
674 }
675
676 /* increment counters */
677 if (gpd->runtime.sbuffer_used == 0) {
678 gpd->runtime.sbuffer_used++;
679 }
680
681 return GP_STROKEADD_NORMAL;
682 }
683
684 /* return invalid state for now... */
686}
687
689{
690 pt->pressure = 1.0f;
691 pt->strength = 1.0f;
692 pt->time = 1.0f;
693}
694
695static void annotation_stroke_arrow_init_conv_point(bGPDspoint *pt, const float point[3])
696{
697 copy_v3_v3(&pt->x, point);
699}
700
702 tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, const float co[8], const int co_idx)
703{
704 /* NOTE: provided co_idx should be always pair number as it's [x1, y1, x2, y2, x3, y3]. */
705 const float real_co[2] = {co[co_idx], co[co_idx + 1]};
706 copy_v2_v2(ptc->m_xy, real_co);
707 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, nullptr);
709}
710
711static void annotation_stroke_arrow_allocate(bGPDstroke *gps, const int totpoints)
712{
713 /* Copy appropriate settings for stroke. */
714 gps->totpoints = totpoints;
715 /* Allocate enough memory for a continuous array for storage points. */
716 gps->points = MEM_new_array<bGPDspoint>(gps->totpoints, "annotation_stroke_points");
717}
718
720 tGPspoint *ptc,
721 bGPDspoint *pt,
722 const float corner_point[3],
723 const float arrow_points[8])
724{
725 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 0);
726 pt++;
728 pt++;
729 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 2);
730}
731
733 tGPspoint *ptc,
734 bGPDspoint *pt,
735 const float arrow_points[8])
736{
737 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 0);
738 pt++;
739 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 2);
740}
741
743 tGPspoint *ptc,
744 bGPDspoint *pt,
745 const float arrow_points[8])
746{
747 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 0);
748 pt++;
749 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 2);
750 pt++;
751 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 4);
752 pt++;
753 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 0);
754}
755
757 tGPspoint *ptc,
758 bGPDspoint *pt,
759 const float corner_point[3],
760 const float arrow_points[8])
761{
763 pt++;
764 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 0);
765 pt++;
766 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 4);
767 pt++;
768 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 6);
769 pt++;
770 annotation_stroke_arrow_init_point(p, ptc, pt, arrow_points, 2);
771 pt++;
773}
774
776 tGPspoint *ptc,
777 bGPDspoint *pt,
778 bGPDstroke *arrow_stroke,
779 const float arrow_points[8],
780 const int style)
781{
782 float corner_conv[3];
783 copy_v3_v3(corner_conv, &pt->x);
784
785 switch (style) {
787 annotation_arrow_create_segm(p, ptc, pt, arrow_points);
788 break;
790 annotation_arrow_create_closed(p, ptc, pt, arrow_points);
791 break;
793 annotation_arrow_create_open(p, ptc, pt, corner_conv, arrow_points);
794 break;
796 annotation_arrow_create_square(p, ptc, pt, corner_conv, arrow_points);
797 break;
798 default:
799 break;
800 }
801 /* Link stroke to frame. */
802 BLI_addtail(&p->gpf->strokes, arrow_stroke);
803}
804
805/* make a new stroke from the buffer data */
807{
808 bGPdata *gpd = p->gpd;
809 bGPDlayer *gpl = p->gpl;
810 bGPDstroke *gps;
811 bGPDspoint *pt;
812 tGPspoint *ptc;
814
815 int i, totelem;
816 /* Since strokes are so fine, when using their depth we need a margin
817 * otherwise they might get missed. */
818 int depth_margin = (ts->annotate_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 4 : 0;
819
820 /* get total number of points to allocate space for
821 * - drawing straight-lines only requires the endpoints
822 */
824 totelem = (gpd->runtime.sbuffer_used >= 2) ? 2 : gpd->runtime.sbuffer_used;
825 }
826 else {
827 totelem = gpd->runtime.sbuffer_used;
828 }
829
830 /* exit with error if no valid points from this stroke */
831 if (totelem == 0) {
832 return;
833 }
834
835 /* special case for poly line -- for already added stroke during session
836 * coordinates are getting added to stroke immediately to allow more
837 * interactive behavior
838 */
841 return;
842 }
843 }
844
845 /* allocate memory for a new stroke */
846 gps = MEM_new<bGPDstroke>("annotation_stroke");
847
848 /* copy appropriate settings for stroke */
849 gps->totpoints = totelem;
850 gps->thickness = gpl->thickness;
851 gps->fill_opacity_fac = 1.0f;
852 gps->hardness = 1.0f;
853 copy_v2_fl(gps->aspect_ratio, 1.0f);
854 gps->uv_scale = 1.0f;
855 gps->flag = gpd->runtime.sbuffer_sflag;
856 gps->inittime = p->inittime;
857 gps->tot_triangles = 0;
858
859 /* allocate enough memory for a continuous array for storage points */
860 gps->points = MEM_new_array<bGPDspoint>(gps->totpoints, "annotation_stroke_points");
861 gps->tot_triangles = 0;
862
863 /* set pointer to first non-initialized point */
864 pt = gps->points + (gps->totpoints - totelem);
865
866 /* copy points from the buffer to the stroke */
868 /* straight lines only -> only endpoints */
869 {
870 /* first point */
871 ptc = gpd->runtime.sbuffer;
872
873 /* convert screen-coordinates to appropriate coordinates (and store them) */
874 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, nullptr);
875
876 /* copy pressure and time */
877 pt->pressure = ptc->pressure;
878 pt->strength = ptc->strength;
880 pt->time = ptc->time;
881
882 pt++;
883 }
884
885 if (totelem == 2) {
887
888 /* Last point if applicable. */
889 ptc = runtime.sbuffer + (runtime.sbuffer_used - 1);
890
891 /* Convert screen-coordinates to appropriate coordinates (and store them). */
892 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, nullptr);
893
894 /* Copy pressure and time. */
895 pt->pressure = ptc->pressure;
896 pt->strength = ptc->strength;
898 pt->time = ptc->time;
899
901 /* End arrow stroke. */
902 if ((runtime.sbuffer_sflag & GP_STROKE_USE_ARROW_END) &&
904 {
905 int totarrowpoints = runtime.arrow_end_style;
906
907 /* Setting up arrow stroke. */
908 bGPDstroke *e_arrow_gps = BKE_gpencil_stroke_duplicate(gps, false, false);
909 annotation_stroke_arrow_allocate(e_arrow_gps, totarrowpoints);
910
911 /* Set pointer to first non-initialized point. */
912 pt = e_arrow_gps->points + (e_arrow_gps->totpoints - totarrowpoints);
913
914 /* End point. */
915 ptc = runtime.sbuffer + (runtime.sbuffer_used - 1);
916 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, nullptr);
918
919 /* Fill and convert arrow points to create arrow shape. */
921 p, ptc, pt, e_arrow_gps, runtime.arrow_end, runtime.arrow_end_style);
922 }
923 /* Start arrow stroke. */
926 {
927 int totarrowpoints = runtime.arrow_start_style;
928
929 /* Setting up arrow stroke. */
930 bGPDstroke *s_arrow_gps = BKE_gpencil_stroke_duplicate(gps, false, false);
931 annotation_stroke_arrow_allocate(s_arrow_gps, totarrowpoints);
932
933 /* Set pointer to first non-initialized point. */
934 pt = s_arrow_gps->points + (s_arrow_gps->totpoints - totarrowpoints);
935
936 /* Start point. */
937 ptc = runtime.sbuffer;
938 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, nullptr);
940
941 /* Fill and convert arrow points to create arrow shape. */
943 p, ptc, pt, s_arrow_gps, runtime.arrow_start, runtime.arrow_start_style);
944 }
945 }
946 }
947 else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) {
948 /* first point */
949 ptc = gpd->runtime.sbuffer;
950
951 /* convert screen-coordinates to appropriate coordinates (and store them) */
952 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, nullptr);
953
954 /* copy pressure and time */
955 pt->pressure = ptc->pressure;
956 pt->strength = ptc->strength;
957 pt->time = ptc->time;
958 }
959 else {
960 float *depth_arr = nullptr;
961
962 /* get an array of depths, far depths are blended */
964 int2 mval_i, mval_prev = {0, 0};
965 int interp_depth = 0;
966 int found_depth = 0;
967
968 depth_arr = MEM_new_array_uninitialized<float>(gpd->runtime.sbuffer_used, "depth_points");
969
970 const ViewDepths *depths = p->depths;
971 for (i = 0, ptc = gpd->runtime.sbuffer; i < gpd->runtime.sbuffer_used; i++, ptc++, pt++) {
972 mval_i = int2(ptc->m_xy);
973 if ((ED_view3d_depth_read_cached(depths, mval_i, depth_margin, depth_arr + i) == 0) &&
975 depths, mval_i, mval_prev, depth_margin + 1, depth_arr + i) == 0)))
976 {
977 interp_depth = true;
978 }
979 else {
980 found_depth = true;
981 }
982
983 copy_v2_v2_int(mval_prev, mval_i);
984 }
985
986 if (found_depth == false) {
987 /* Unfortunately there is not much we can do when the depth isn't found,
988 * ignore depth in this case, use the 3D cursor. */
989 for (i = gpd->runtime.sbuffer_used - 1; i >= 0; i--) {
990 depth_arr[i] = 0.9999f;
991 }
992 }
993 else {
995 /* remove all info between the valid endpoints */
996 int first_valid = 0;
997 int last_valid = 0;
998
999 for (i = 0; i < gpd->runtime.sbuffer_used; i++) {
1000 if (depth_arr[i] != DEPTH_INVALID) {
1001 break;
1002 }
1003 }
1004 first_valid = i;
1005
1006 for (i = gpd->runtime.sbuffer_used - 1; i >= 0; i--) {
1007 if (depth_arr[i] != DEPTH_INVALID) {
1008 break;
1009 }
1010 }
1011 last_valid = i;
1012
1013 /* invalidate non-endpoints, so only blend between first and last */
1014 for (i = first_valid + 1; i < last_valid; i++) {
1015 depth_arr[i] = DEPTH_INVALID;
1016 }
1017
1018 interp_depth = true;
1019 }
1020
1021 if (interp_depth) {
1023 }
1024 }
1025 }
1026
1027 pt = gps->points;
1028
1029 /* convert all points (normal behavior) */
1030 for (i = 0, ptc = gpd->runtime.sbuffer; i < gpd->runtime.sbuffer_used && ptc; i++, ptc++, pt++)
1031 {
1032 /* convert screen-coordinates to appropriate coordinates (and store them) */
1033 annotation_stroke_convertcoords(p, ptc->m_xy, &pt->x, depth_arr ? depth_arr + i : nullptr);
1034
1035 /* copy pressure and time */
1036 pt->pressure = ptc->pressure;
1037 pt->strength = ptc->strength;
1039 pt->time = ptc->time;
1040 }
1041
1042 if (depth_arr) {
1043 MEM_delete(depth_arr);
1044 }
1045 }
1046
1047 /* add stroke to frame */
1048 BLI_addtail(&p->gpf->strokes, gps);
1050}
1051
1052/* --- 'Eraser' for 'Paint' Tool ------ */
1053
1054/* helper to free a stroke
1055 * NOTE: gps->dvert and gps->triangles should be nullptr, but check anyway for good measure
1056 */
1058{
1059 if (gps->points) {
1060 MEM_delete(gps->points);
1061 }
1062
1063 if (gps->dvert) {
1065 MEM_delete(gps->dvert);
1066 }
1067
1068 if (gps->triangles) {
1069 MEM_delete(gps->triangles);
1070 }
1071
1072 BLI_freelinkN(&gpf->strokes, gps);
1073}
1074
1075/* only erase stroke points that are visible (3d view) */
1077 const bGPDspoint *pt,
1078 const int x,
1079 const int y)
1080{
1082 RegionView3D *rv3d = static_cast<RegionView3D *>(p->region->regiondata);
1083 const int mval_i[2] = {x, y};
1084 float mval_3d[3];
1085
1086 float p_depth;
1087 if (ED_view3d_depth_read_cached(p->depths, mval_i, 0, &p_depth)) {
1088 ED_view3d_depth_unproject_v3(p->region, mval_i, double(p_depth), mval_3d);
1089
1090 const float depth_mval = ED_view3d_calc_depth_for_comparison(rv3d, mval_3d);
1091 const float depth_pt = ED_view3d_calc_depth_for_comparison(rv3d, &pt->x);
1092
1093 if (depth_pt > depth_mval) {
1094 return true;
1095 }
1096 }
1097 }
1098 return false;
1099}
1100
1101/* Temp data for storing information about an "island" of points
1102 * that should be kept when splitting up a stroke. Used in:
1103 * gpencil_stroke_delete_tagged_points()
1104 */
1109
1111 bGPDframe *gpf,
1112 bGPDstroke *gps_first,
1113 bGPDstroke *gps_last)
1114{
1115 bGPDspoint *pt = nullptr;
1116 bGPDspoint *pt_final = nullptr;
1117 const int totpoints = gps_first->totpoints + gps_last->totpoints;
1118
1119 /* create new stroke */
1120 bGPDstroke *join_stroke = BKE_gpencil_stroke_duplicate(gps_first, false, true);
1121
1122 join_stroke->points = MEM_new_array<bGPDspoint>(totpoints, __func__);
1123 join_stroke->totpoints = totpoints;
1124 join_stroke->flag &= ~GP_STROKE_CYCLIC;
1125
1126 /* copy points (last before) */
1127 int e1 = 0;
1128 int e2 = 0;
1129 float delta = 0.0f;
1130
1131 for (int i = 0; i < totpoints; i++) {
1132 pt_final = &join_stroke->points[i];
1133 if (i < gps_last->totpoints) {
1134 pt = &gps_last->points[e1];
1135 e1++;
1136 }
1137 else {
1138 pt = &gps_first->points[e2];
1139 e2++;
1140 }
1141
1142 /* copy current point */
1143 copy_v3_v3(&pt_final->x, &pt->x);
1144 pt_final->pressure = pt->pressure;
1145 pt_final->strength = pt->strength;
1146 pt_final->time = delta;
1147 pt_final->flag = pt->flag;
1148 copy_v4_v4(pt_final->vert_color, pt->vert_color);
1149
1150 /* retiming with fixed time interval (we cannot determine real time) */
1151 delta += 0.01f;
1152 }
1153
1154 /* Copy over vertex weight data (if available) */
1155 if ((gps_first->dvert != nullptr) || (gps_last->dvert != nullptr)) {
1156 join_stroke->dvert = MEM_new_array_zeroed<MDeformVert>(totpoints, __func__);
1157 MDeformVert *dvert_src = nullptr;
1158 MDeformVert *dvert_dst = nullptr;
1159
1160 /* Copy weights (last before). */
1161 e1 = 0;
1162 e2 = 0;
1163 for (int i = 0; i < totpoints; i++) {
1164 dvert_dst = &join_stroke->dvert[i];
1165 dvert_src = nullptr;
1166 if (i < gps_last->totpoints) {
1167 if (gps_last->dvert) {
1168 dvert_src = &gps_last->dvert[e1];
1169 e1++;
1170 }
1171 }
1172 else {
1173 if (gps_first->dvert) {
1174 dvert_src = &gps_first->dvert[e2];
1175 e2++;
1176 }
1177 }
1178
1179 if ((dvert_src) && (dvert_src->dw)) {
1180 dvert_dst->dw = MEM_dupalloc(dvert_src->dw);
1181 }
1182 }
1183 }
1184
1185 /* add new stroke at head */
1186 BLI_addhead(&gpf->strokes, join_stroke);
1187 /* Calc geometry data. */
1188 BKE_annotation_stroke_geometry_update(gpd, join_stroke);
1189
1190 /* remove first stroke */
1191 BLI_remlink(&gpf->strokes, gps_first);
1192 BKE_annotations_free_stroke(gps_first);
1193
1194 /* remove last stroke */
1195 BLI_remlink(&gpf->strokes, gps_last);
1197}
1198
1200 bGPDframe *gpf,
1201 bGPDstroke *gps,
1202 bGPDstroke *next_stroke,
1203 int tag_flags,
1204 const bool select,
1205 const bool flat_cap,
1206 const int limit)
1207{
1208 /* The algorithm used here is as follows:
1209 * 1) We firstly identify the number of "islands" of non-tagged points
1210 * which will all end up being in new strokes.
1211 * - In the most extreme case (i.e. every other vert is a 1-vert island),
1212 * we have at most `n / 2` islands
1213 * - Once we start having larger islands than that, the number required
1214 * becomes much less
1215 * 2) Each island gets converted to a new stroke
1216 * If the number of points is <= limit, the stroke is deleted. */
1217
1219 "gp_point_islands");
1220 bool in_island = false;
1221 int num_islands = 0;
1222
1223 bGPDstroke *new_stroke = nullptr;
1224 bGPDstroke *gps_first = nullptr;
1225 const bool is_cyclic = bool(gps->flag & GP_STROKE_CYCLIC);
1226
1227 /* First Pass: Identify start/end of islands */
1228 bGPDspoint *pt = gps->points;
1229 for (int i = 0; i < gps->totpoints; i++, pt++) {
1230 if (pt->flag & tag_flags) {
1231 /* selected - stop accumulating to island */
1232 in_island = false;
1233 }
1234 else {
1235 /* unselected - start of a new island? */
1236 int idx;
1237
1238 if (in_island) {
1239 /* extend existing island */
1240 idx = num_islands - 1;
1241 islands[idx].end_idx = i;
1242 }
1243 else {
1244 /* start of new island */
1245 in_island = true;
1246 num_islands++;
1247
1248 idx = num_islands - 1;
1249 islands[idx].start_idx = islands[idx].end_idx = i;
1250 }
1251 }
1252 }
1253
1254 /* Watch out for special case where No islands = All points selected = Delete Stroke only */
1255 if (num_islands) {
1256 /* There are islands, so create a series of new strokes,
1257 * adding them before the "next" stroke. */
1258 int idx;
1259
1260 /* Create each new stroke... */
1261 for (idx = 0; idx < num_islands; idx++) {
1262 tGPDeleteIsland *island = &islands[idx];
1263 new_stroke = BKE_gpencil_stroke_duplicate(gps, false, true);
1264 if (flat_cap) {
1265 new_stroke->caps[1 - (idx % 2)] = GP_STROKE_CAP_FLAT;
1266 }
1267
1268 /* if cyclic and first stroke, save to join later */
1269 if ((is_cyclic) && (gps_first == nullptr)) {
1270 gps_first = new_stroke;
1271 }
1272
1273 new_stroke->flag &= ~GP_STROKE_CYCLIC;
1274
1275 /* Compute new buffer size (+ 1 needed as the endpoint index is "inclusive") */
1276 new_stroke->totpoints = island->end_idx - island->start_idx + 1;
1277
1278 /* Copy over the relevant point data */
1279 new_stroke->points = MEM_new_array<bGPDspoint>(new_stroke->totpoints,
1280 "gp delete stroke fragment");
1281 memcpy(static_cast<void *>(new_stroke->points),
1282 gps->points + island->start_idx,
1283 sizeof(bGPDspoint) * new_stroke->totpoints);
1284
1285 /* Copy over vertex weight data (if available) */
1286 if (gps->dvert != nullptr) {
1287 /* Copy over the relevant vertex-weight points */
1288 new_stroke->dvert = MEM_new_array_zeroed<MDeformVert>(new_stroke->totpoints,
1289 "gp delete stroke fragment weight");
1290 memcpy(new_stroke->dvert,
1291 gps->dvert + island->start_idx,
1292 sizeof(MDeformVert) * new_stroke->totpoints);
1293
1294 /* Copy weights */
1295 int e = island->start_idx;
1296 for (int i = 0; i < new_stroke->totpoints; i++) {
1297 MDeformVert *dvert_src = &gps->dvert[e];
1298 MDeformVert *dvert_dst = &new_stroke->dvert[i];
1299 if (dvert_src->dw) {
1300 dvert_dst->dw = MEM_dupalloc(dvert_src->dw);
1301 }
1302 e++;
1303 }
1304 }
1305 /* Each island corresponds to a new stroke.
1306 * We must adjust the timings of these new strokes:
1307 *
1308 * Each point's timing data is a delta from stroke's inittime, so as we erase some points
1309 * from the start of the stroke, we have to offset this inittime and all remaining points'
1310 * delta values. This way we get a new stroke with exactly the same timing as if user had
1311 * started drawing from the first non-removed point.
1312 */
1313 {
1314 bGPDspoint *pts;
1315 float delta = gps->points[island->start_idx].time;
1316 int j;
1317
1318 new_stroke->inittime += double(delta);
1319
1320 pts = new_stroke->points;
1321 for (j = 0; j < new_stroke->totpoints; j++, pts++) {
1322 /* Some points have time = 0, so check to not get negative time values. */
1323 pts->time = max_ff(pts->time - delta, 0.0f);
1324 /* set flag for select again later */
1325 if (select == true) {
1326 pts->flag &= ~GP_SPOINT_SELECT;
1327 pts->flag |= GP_SPOINT_TAG;
1328 }
1329 }
1330 }
1331
1332 /* Add new stroke to the frame or delete if below limit */
1333 if ((limit > 0) && (new_stroke->totpoints <= limit)) {
1334 if (gps_first == new_stroke) {
1335 gps_first = nullptr;
1336 }
1337 BKE_annotations_free_stroke(new_stroke);
1338 }
1339 else {
1340 /* Calc geometry data. */
1342
1343 if (next_stroke) {
1344 BLI_insertlinkbefore(&gpf->strokes, next_stroke, new_stroke);
1345 }
1346 else {
1347 BLI_addtail(&gpf->strokes, new_stroke);
1348 }
1349 }
1350 }
1351 /* if cyclic, need to join last stroke with first stroke */
1352 if ((is_cyclic) && (gps_first != nullptr) && (gps_first != new_stroke)) {
1353 stroke_join_islands(gpd, gpf, gps_first, new_stroke);
1354 }
1355 }
1356
1357 /* free islands */
1358 MEM_delete(islands);
1359
1360 /* Delete the old stroke */
1361 BLI_remlink(&gpf->strokes, gps);
1363
1364 return new_stroke;
1365}
1366
1367/* Eraser tool - evaluation per stroke. */
1369 bGPDframe *gpf,
1370 bGPDstroke *gps,
1371 const float mval[2],
1372 const int radius,
1373 const rcti *rect)
1374{
1375 bGPDspoint *pt1, *pt2;
1376 int pc1[2] = {0};
1377 int pc2[2] = {0};
1378 int2 mval_i = int2(mval);
1379
1380 if (gps->totpoints == 0) {
1381 /* just free stroke */
1382 annotation_free_stroke(gpf, gps);
1383 }
1384 else if (gps->totpoints == 1) {
1385 /* only process if it hasn't been masked out... */
1386 if (!(p->flags & GP_PAINTFLAG_SELECTMASK) || (gps->points->flag & GP_SPOINT_SELECT)) {
1387 gpencil_point_to_xy(&p->gsc, gps, gps->points, &pc1[0], &pc1[1]);
1388
1389 /* Do bound-box check first. */
1390 if (!ELEM(V2D_IS_CLIPPED, pc1[0], pc1[1]) && BLI_rcti_isect_pt(rect, pc1[0], pc1[1])) {
1391 /* only check if point is inside */
1392 if (len_v2v2_int(mval_i, pc1) <= radius) {
1393 /* free stroke */
1394 annotation_free_stroke(gpf, gps);
1395 }
1396 }
1397 }
1398 }
1399 else {
1400 /* Perform culling? */
1401 bool do_cull = false;
1402
1403 /* Clear Tags
1404 *
1405 * NOTE: It's better this way, as we are sure that
1406 * we don't miss anything, though things will be
1407 * slightly slower as a result
1408 */
1409 for (int i = 0; i < gps->totpoints; i++) {
1410 bGPDspoint *pt = &gps->points[i];
1411 pt->flag &= ~GP_SPOINT_TAG;
1412 }
1413
1414 /* First Pass: Loop over the points in the stroke
1415 * 1) Thin out parts of the stroke under the brush
1416 * 2) Tag "too thin" parts for removal (in second pass)
1417 */
1418 for (int i = 0; (i + 1) < gps->totpoints; i++) {
1419 /* get points to work with */
1420 pt1 = gps->points + i;
1421 pt2 = gps->points + i + 1;
1422
1423 /* only process if it hasn't been masked out... */
1424 if ((p->flags & GP_PAINTFLAG_SELECTMASK) && !(gps->points->flag & GP_SPOINT_SELECT)) {
1425 continue;
1426 }
1427
1428 gpencil_point_to_xy(&p->gsc, gps, pt1, &pc1[0], &pc1[1]);
1429 gpencil_point_to_xy(&p->gsc, gps, pt2, &pc2[0], &pc2[1]);
1430
1431 /* Check that point segment of the bound-box of the eraser stroke. */
1432 if ((!ELEM(V2D_IS_CLIPPED, pc1[0], pc1[1]) && BLI_rcti_isect_pt(rect, pc1[0], pc1[1])) ||
1433 (!ELEM(V2D_IS_CLIPPED, pc2[0], pc2[1]) && BLI_rcti_isect_pt(rect, pc2[0], pc2[1])))
1434 {
1435 /* Check if point segment of stroke had anything to do with
1436 * eraser region (either within stroke painted, or on its lines)
1437 * - this assumes that line-width is irrelevant.
1438 */
1439 if (gpencil_stroke_inside_circle(mval, radius, pc1[0], pc1[1], pc2[0], pc2[1])) {
1440 if ((annotation_stroke_eraser_is_occluded(p, pt1, pc1[0], pc1[1]) == false) ||
1441 (annotation_stroke_eraser_is_occluded(p, pt2, pc2[0], pc2[1]) == false))
1442 {
1443 /* Edge is affected - Check individual points now */
1444 if (len_v2v2_int(mval_i, pc1) <= radius) {
1445 pt1->flag |= GP_SPOINT_TAG;
1446 }
1447 if (len_v2v2_int(mval_i, pc2) <= radius) {
1448 pt2->flag |= GP_SPOINT_TAG;
1449 }
1450 do_cull = true;
1451 }
1452 }
1453 }
1454 }
1455
1456 /* Second Pass: Remove any points that are tagged */
1457 if (do_cull) {
1458 stroke_delete_tagged_points(p->gpd, gpf, gps, gps->next, GP_SPOINT_TAG, false, false, 0);
1459 }
1460 }
1461}
1462
1463/* erase strokes which fall under the eraser strokes */
1465{
1466 bGPDframe *gpf = p->gpf;
1467 bGPDstroke *gps, *gpn;
1468 rcti rect;
1469
1470 /* rect is rectangle of eraser */
1471 rect.xmin = p->mval[0] - p->radius;
1472 rect.ymin = p->mval[1] - p->radius;
1473 rect.xmax = p->mval[0] + p->radius;
1474 rect.ymax = p->mval[1] + p->radius;
1475
1476 if (p->area->spacetype == SPACE_VIEW3D) {
1478 View3D *v3d = p->area->spacedata.first_as<View3D>();
1481 p->depsgraph, p->region, v3d, nullptr, V3D_DEPTH_NO_GPENCIL, false, &p->depths);
1482 }
1483 }
1484
1485 /* loop over strokes of active layer only (session init already took care of ensuring validity),
1486 * checking segments for intersections to remove
1487 */
1488 for (gps = gpf->strokes.first(); gps; gps = gpn) {
1489 gpn = gps->next;
1490 /* Not all strokes in the datablock may be valid in the current editor/context
1491 * (e.g. 2D space strokes in the 3D view, if the same datablock is shared)
1492 */
1494 annotation_stroke_eraser_dostroke(p, gpf, gps, p->mval, p->radius, &rect);
1495 }
1496 }
1497}
1498
1499/* ******************************************* */
1500/* Sketching Operator */
1501
1502/* clear the session buffers (call this before AND after a paint operation) */
1504{
1505 bGPdata *gpd = p->gpd;
1506
1508 gpd->runtime.sbuffer, &gpd->runtime.sbuffer_size, &gpd->runtime.sbuffer_used, true);
1509
1510 /* reset flags */
1512
1513 /* reset inittime */
1514 p->inittime = 0.0;
1515}
1516
1517/* (re)init new painting data */
1519{
1520 Main *bmain = CTX_data_main(C);
1521 bGPdata **gpd_ptr = nullptr;
1522 ScrArea *curarea = CTX_wm_area(C);
1523 ARegion *region = CTX_wm_region(C);
1525
1526 /* make sure the active view (at the starting time) is a 3d-view */
1527 if (curarea == nullptr) {
1529 return false;
1530 }
1531
1532 /* pass on current scene and window */
1533 p->bmain = CTX_data_main(C);
1534 p->scene = CTX_data_scene(C);
1536 p->win = CTX_wm_window(C);
1537
1538 unit_m4(p->imat);
1539 unit_m4(p->mat);
1540
1541 switch (curarea->spacetype) {
1542 /* supported views first */
1543 case SPACE_VIEW3D: {
1544 // View3D *v3d = curarea->spacedata.first;
1545 // RegionView3D *rv3d = region->regiondata;
1546
1547 /* set current area
1548 * - must verify that region data is 3D-view (and not something else)
1549 */
1550 /* CAUTION: If this is the "toolbar", then this will change on the first stroke */
1551 p->area = curarea;
1552 p->region = region;
1554
1555 if (region->regiondata == nullptr) {
1557 return false;
1558 }
1559 break;
1560 }
1561 case SPACE_NODE: {
1562 // SpaceNode *snode = curarea->spacedata.first;
1563
1564 /* set current area */
1565 p->area = curarea;
1566 p->region = region;
1567 p->v2d = &region->v2d;
1568 p->align_flag = &ts->gpencil_v2d_align;
1569 break;
1570 }
1571 case SPACE_SEQ: {
1572 SpaceSeq *sseq = curarea->spacedata.first_as<SpaceSeq>();
1573
1574 /* set current area */
1575 p->area = curarea;
1576 p->region = region;
1577 p->v2d = &region->v2d;
1578 p->align_flag = &ts->gpencil_v2d_align;
1579
1580 /* check that gpencil data is allowed to be drawn */
1581 if (!((sseq->mainb == SEQ_DRAW_IMG_IMBUF) && (region->regiontype == RGN_TYPE_PREVIEW))) {
1583 return false;
1584 }
1585 break;
1586 }
1587 case SPACE_IMAGE: {
1588 // SpaceImage *sima = curarea->spacedata.first;
1589
1590 /* set the current area */
1591 p->area = curarea;
1592 p->region = region;
1593 p->v2d = &region->v2d;
1594 p->align_flag = &ts->gpencil_v2d_align;
1595 break;
1596 }
1597 case SPACE_CLIP: {
1598 SpaceClip *sc = curarea->spacedata.first_as<SpaceClip>();
1600
1601 if (clip == nullptr) {
1603 return false;
1604 }
1605
1606 /* set the current area */
1607 p->area = curarea;
1608 p->region = region;
1609 p->v2d = &region->v2d;
1610 p->align_flag = &ts->gpencil_v2d_align;
1611
1612 invert_m4_m4(p->imat, sc->unistabmat);
1613
1614 /* custom color for new layer */
1615 p->custom_color[0] = 1.0f;
1616 p->custom_color[1] = 0.0f;
1617 p->custom_color[2] = 0.5f;
1618 p->custom_color[3] = 0.9f;
1619
1620 if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
1621 int framenr = ED_space_clip_get_clip_frame_number(sc);
1622 const MovieTrackingObject *tracking_object = BKE_tracking_object_get_active(
1623 &clip->tracking);
1624 MovieTrackingTrack *track = tracking_object->active_track;
1625 MovieTrackingMarker *marker = track ? BKE_tracking_marker_get(track, framenr) : nullptr;
1626
1627 if (marker) {
1628 p->imat[3][0] -= marker->pos[0];
1629 p->imat[3][1] -= marker->pos[1];
1630 }
1631 else {
1633 return false;
1634 }
1635 }
1636
1637 invert_m4_m4(p->mat, p->imat);
1638 copy_m4_m4(p->gsc.mat, p->mat);
1639 break;
1640 }
1641 /* unsupported views */
1642 default: {
1644 return false;
1645 }
1646 }
1647
1648 /* get gp-data */
1650 if (gpd_ptr == nullptr) {
1652 return false;
1653 }
1654
1655 /* if no existing GPencil block exists, add one */
1656 if (*gpd_ptr == nullptr) {
1657 bGPdata *gpd = BKE_annotations_data_addnew(bmain, "Annotations");
1658 *gpd_ptr = gpd;
1659
1660 /* mark datablock as being used for annotations */
1661 gpd->flag |= GP_DATA_ANNOTATIONS;
1662 }
1663 p->gpd = *gpd_ptr;
1664
1665 /* clear out buffer (stored in gp-data), in case something contaminated it */
1667
1668 return true;
1669}
1670
1671/* Enable the annotations in the current space. */
1673{
1674 ScrArea *area = p->area;
1675 switch (area->spacetype) {
1676 case SPACE_VIEW3D: {
1677 View3D *v3d = area->spacedata.first_as<View3D>();
1678 v3d->flag2 |= V3D_SHOW_ANNOTATION;
1679 break;
1680 }
1681 case SPACE_SEQ: {
1682 SpaceSeq *sseq = area->spacedata.first_as<SpaceSeq>();
1684 break;
1685 }
1686 case SPACE_IMAGE: {
1687 SpaceImage *sima = area->spacedata.first_as<SpaceImage>();
1688 sima->flag |= SI_SHOW_GPENCIL;
1689 break;
1690 }
1691 case SPACE_NODE: {
1692 SpaceNode *snode = area->spacedata.first_as<SpaceNode>();
1693 snode->flag |= SNODE_SHOW_GPENCIL;
1694 break;
1695 }
1696 case SPACE_CLIP: {
1697 SpaceClip *sclip = area->spacedata.first_as<SpaceClip>();
1698 sclip->flag |= SC_SHOW_ANNOTATION;
1699 break;
1700 }
1701 default:
1702 break;
1703 }
1704}
1705
1706/* init new painting session */
1708{
1709 tGPsdata *p = nullptr;
1710
1711 /* create new context data */
1712 p = MEM_new<tGPsdata>("Annotation Drawing Data");
1713
1714 /* Try to initialize context data
1715 * WARNING: This may not always succeed (e.g. using GP in an annotation-only context)
1716 */
1717 if (annotation_session_initdata(C, p) == 0) {
1718 /* Invalid state - Exit
1719 * NOTE: It should be safe to just free the data, since failing context checks should
1720 * only happen when no data has been allocated.
1721 */
1722 MEM_delete(p);
1723 return nullptr;
1724 }
1725
1726 /* Radius for eraser circle is defined in user-preferences. */
1727 /* NOTE: we do this here, so that if we exit immediately,
1728 * erase size won't get lost
1729 */
1730 p->radius = U.gp_eraser;
1731
1732 /* Annotations must be always visible when use it. */
1734
1735 /* return context data for running paint operator */
1736 return p;
1737}
1738
1739/* cleanup after a painting session */
1741{
1742 bGPdata *gpd = (p) ? p->gpd : nullptr;
1743
1744 /* error checking */
1745 if (gpd == nullptr) {
1746 return;
1747 }
1748
1749 /* free stroke buffer */
1750 if (gpd->runtime.sbuffer) {
1751 MEM_delete(gpd->runtime.sbuffer);
1752 gpd->runtime.sbuffer = nullptr;
1753 }
1754
1755 /* clear flags */
1756 gpd->runtime.sbuffer_used = 0;
1757 gpd->runtime.sbuffer_size = 0;
1759 p->inittime = 0.0;
1760}
1761
1763{
1764 if (p->depths) {
1766 }
1767 MEM_delete(p);
1768}
1769
1770/* init new stroke */
1772 eGPencil_PaintModes paintmode,
1773 Depsgraph *depsgraph)
1774{
1775 Scene *scene = p->scene;
1776 ToolSettings *ts = scene->toolsettings;
1777
1778 /* Call to the annotation pre handler to notify python the annotation starts. */
1780
1781 /* get active layer (or add a new one if non-existent) */
1783 if (p->gpl == nullptr) {
1784 /* tag for annotations */
1786 p->gpl = BKE_annotations_layer_addnew(p->gpd, DATA_("Note"), true, false);
1787
1788 if (p->custom_color[3]) {
1790 }
1791 }
1792 if (p->gpl->flag & GP_LAYER_LOCKED) {
1794 return;
1795 }
1796
1797 /* get active frame (add a new one if not matching frame) */
1798 if (paintmode == GP_PAINTMODE_ERASER) {
1799 /* Eraser mode:
1800 * 1) Only allow erasing on the active layer (unlike for 3d-art Grease Pencil),
1801 * since we won't be exposing layer locking in the UI
1802 * 2) Ensure that p->gpf refers to the frame used for the active layer
1803 * (to avoid problems with other tools which expect it to exist)
1804 */
1805 bool has_layer_to_erase = false;
1806
1808 /* Ensure that there's stuff to erase here (not including selection mask below)... */
1809 if (p->gpl->actframe && p->gpl->actframe->strokes.first_) {
1810 has_layer_to_erase = true;
1811 }
1812 }
1813
1814 /* Ensure active frame is set correctly... */
1815 p->gpf = p->gpl->actframe;
1816
1817 if (has_layer_to_erase == false) {
1819 printf("Error: Eraser will not be affecting anything (gpencil_paint_init)\n");
1820 return;
1821 }
1822 }
1823 else {
1824 /* Drawing Modes - Add a new frame if needed on the active layer */
1825 short add_frame_mode = GP_GETFRAME_ADD_NEW;
1826
1828 add_frame_mode = GP_GETFRAME_ADD_COPY;
1829 }
1830 else {
1831 add_frame_mode = GP_GETFRAME_ADD_NEW;
1832 }
1833
1835 p->gpl, scene->r.cfra, eGP_GetFrame_Mode(add_frame_mode));
1836
1837 if (p->gpf == nullptr) {
1839 return;
1840 }
1841
1842 p->gpf->flag |= GP_FRAME_PAINT;
1843 }
1844
1845 /* set 'eraser' for this stroke if using eraser */
1846 p->paintmode = paintmode;
1847 if (p->paintmode == GP_PAINTMODE_ERASER) {
1849
1850 /* check if we should respect depth while erasing */
1851 if (p->area->spacetype == SPACE_VIEW3D) {
1852 if (p->gpl->flag & GP_LAYER_NO_XRAY) {
1854 }
1855 }
1856 }
1857 else {
1858 /* disable eraser flags - so that we can switch modes during a session */
1860
1861 if (p->area->spacetype == SPACE_VIEW3D) {
1862 if (p->gpl->flag & GP_LAYER_NO_XRAY) {
1864 }
1865 }
1866 }
1867
1868 /* set 'initial run' flag, which is only used to denote when a new stroke is starting */
1870
1871 /* when drawing in the camera view, in 2D space, set the subrect */
1872 p->subrect = nullptr;
1873 if ((*p->align_flag & GP_PROJECT_VIEWSPACE) == 0) {
1874 if (p->area->spacetype == SPACE_VIEW3D) {
1875 View3D *v3d = p->area->spacedata.first_as<View3D>();
1876 RegionView3D *rv3d = static_cast<RegionView3D *>(p->region->regiondata);
1877
1878 /* for camera view set the subrect */
1879 if (rv3d->persp == RV3D_CAMOB) {
1880 /* no shift */
1882 p->scene, depsgraph, v3d, rv3d, p->region->winx, p->region->winy, true, false, true);
1883 p->subrect = &p->subrect_data;
1884 }
1885 }
1886 }
1887
1888 /* init stroke point space-conversion settings... */
1889 p->gsc.gpd = p->gpd;
1890 p->gsc.gpl = p->gpl;
1891
1892 p->gsc.area = p->area;
1893 p->gsc.region = p->region;
1894 p->gsc.v2d = p->v2d;
1895
1897 p->gsc.subrect = p->subrect;
1898
1899 copy_m4_m4(p->gsc.mat, p->mat);
1900
1901 /* check if points will need to be made in view-aligned space */
1902 if (*p->align_flag & GP_PROJECT_VIEWSPACE) {
1903 switch (p->area->spacetype) {
1904 case SPACE_VIEW3D: {
1906 break;
1907 }
1908 case SPACE_NODE:
1909 case SPACE_SEQ:
1910 case SPACE_IMAGE:
1911 case SPACE_CLIP: {
1913 break;
1914 }
1915 }
1916 }
1917}
1918
1919/* finish off a stroke (clears buffer, but doesn't finish the paint operation) */
1921{
1923 const bool is_eraser = (p->gpd->runtime.sbuffer_sflag & GP_STROKE_ERASER) != 0;
1924 /* for surface sketching, need to set the right OpenGL context stuff so that
1925 * the conversions will project the values correctly...
1926 */
1927 if (annotation_project_check(p)) {
1928 View3D *v3d = p->area->spacedata.first_as<View3D>();
1929
1931
1935 }
1936 else {
1937 mode = V3D_DEPTH_ALL;
1938 }
1939 }
1940 /* need to restore the original projection settings before packing up */
1943 p->depsgraph, p->region, v3d, nullptr, mode, false, is_eraser ? nullptr : &p->depths);
1944 }
1945
1946 /* check if doing eraser or not */
1947 if (!is_eraser) {
1948 /* transfer stroke to frame */
1950 }
1951
1952 /* Call to the annotation post handler to notify python the annotation is done. */
1954
1955 /* clean up buffer now */
1957}
1958
1959/* finish off stroke painting operation */
1961{
1962 /* p->gpd==nullptr happens when stroke failed to initialize,
1963 * for example when GP is hidden in current space (sergey)
1964 */
1965 if (p->gpd) {
1966 /* finish off a stroke */
1968 }
1969
1970 /* "unlock" frame */
1971 if (p->gpf) {
1972 p->gpf->flag &= ~GP_FRAME_PAINT;
1973 }
1974}
1975
1976/* ------------------------------- */
1977
1978/* Helper callback for drawing the cursor itself */
1980 const int2 &xy,
1981 const float2 & /*tilt*/,
1982 void *p_ptr)
1983{
1984 tGPsdata *p = static_cast<tGPsdata *>(p_ptr);
1985
1986 if (p->paintmode == GP_PAINTMODE_ERASER) {
1987 GPUVertFormat *format = immVertexFormat();
1988 const uint shdr_pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
1990
1991 GPU_line_smooth(true);
1993
1994 immUniformColor4ub(255, 100, 100, 20);
1995 imm_draw_circle_fill_2d(shdr_pos, xy[0], xy[1], p->radius, 40);
1996
1998
2000
2001 float viewport_size[4];
2002 GPU_viewport_size_get_f(viewport_size);
2003 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
2004
2005 immUniformColor4f(1.0f, 0.39f, 0.39f, 0.78f);
2006 immUniform1i("colors_len", 0); /* "simple" mode */
2007 immUniform1f("dash_width", 12.0f);
2008 immUniform1f("udash_factor", 0.5f);
2009
2010 imm_draw_circle_wire_2d(shdr_pos,
2011 xy.x,
2012 xy.y,
2013 p->radius,
2014 /* XXX Dashed shader gives bad results with sets of small segments
2015 * currently, temp hack around the issue. :( */
2016 max_ii(8, p->radius / 2)); /* was fixed 40 */
2017
2019
2021 GPU_line_smooth(false);
2022 }
2023}
2024
2025/* Turn brush cursor in 3D view on/off */
2027{
2028 if (p->erasercursor && !enable) {
2029 /* clear cursor */
2031 p->erasercursor = nullptr;
2032 }
2033 else if (enable && !p->erasercursor) {
2034 /* enable cursor */
2037 nullptr, /* XXX */
2039 p);
2040 }
2041}
2043 const int2 &xy,
2044 const float2 & /*tilt*/,
2045 void *p_ptr)
2046{
2047 ARegion *region = CTX_wm_region(C);
2048 tGPsdata *p = static_cast<tGPsdata *>(p_ptr);
2050 const tGPspoint *points = static_cast<const tGPspoint *>(runtime.sbuffer);
2051 int totpoints = runtime.sbuffer_used;
2052 if (totpoints < 2) {
2053 return;
2054 }
2055 const tGPspoint *pt = &points[totpoints - 1];
2056
2057 GPUVertFormat *format = immVertexFormat();
2058 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
2060 GPU_line_smooth(true);
2062 GPU_line_width(1.25f);
2063 const float color[3] = {1.0f, 0.39f, 0.39f};
2064
2065 /* default radius and color */
2066 float darkcolor[3];
2067 const float radius = 4.0f;
2068
2069 /* Inner Ring: Color from UI panel */
2070 immUniformColor4f(color[0], color[1], color[2], 0.8f);
2071 imm_draw_circle_wire_2d(pos, xy.x, xy.y, radius, 40);
2072
2073 /* Outer Ring: Dark color for contrast on light backgrounds (e.g. gray on white) */
2074 mul_v3_v3fl(darkcolor, color, 0.40f);
2075 immUniformColor4f(darkcolor[0], darkcolor[1], darkcolor[2], 0.8f);
2076 imm_draw_circle_wire_2d(pos, xy.x, xy.y, radius + 1, 40);
2077
2078 /* Rope Simple. */
2079 immUniformColor4f(color[0], color[1], color[2], 0.8f);
2081 immVertex2f(pos, pt->m_xy[0] + region->winrct.xmin, pt->m_xy[1] + region->winrct.ymin);
2082 immVertex2fv(pos, float2(xy));
2083 immEnd();
2084
2085 /* Returns back all GPU settings */
2087 GPU_line_smooth(false);
2088
2090}
2091
2092/* Turn *stabilizer* brush cursor in 3D view on/off */
2094{
2095 if (p->stabilizer_cursor && !enable) {
2096 /* clear cursor */
2098 p->stabilizer_cursor = nullptr;
2099 }
2100 else if (enable && !p->stabilizer_cursor) {
2101 /* enable cursor */
2104 }
2105}
2106
2107/* Check if tablet eraser is being used (when processing events) */
2109{
2110 return (event->tablet.active == EVT_TABLET_ERASER);
2111}
2112
2113/* ------------------------------- */
2114
2116{
2117 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2118
2119 /* restore cursor to indicate end of drawing */
2121
2122 /* don't assume that operator data exists at all */
2123 if (p) {
2124 /* check size of buffer before cleanup, to determine if anything happened here */
2125 if (p->paintmode == GP_PAINTMODE_ERASER) {
2126 /* turn off radial brush cursor */
2128 }
2129 else if (p->paintmode == GP_PAINTMODE_DRAW) {
2131 }
2132
2133 /* always store the new eraser size to be used again next time
2134 * NOTE: Do this even when not in eraser mode, as eraser may
2135 * have been toggled at some point.
2136 */
2137 U.gp_eraser = p->radius;
2138
2139 /* cleanup */
2143 p = nullptr;
2144 }
2145
2146 op->customdata = nullptr;
2147}
2148
2150{
2151 /* this is just a wrapper around exit() */
2153}
2154
2155/* ------------------------------- */
2156
2157static int annotation_draw_init(bContext *C, wmOperator *op, const wmEvent *event)
2158{
2159 tGPsdata *p;
2160 eGPencil_PaintModes paintmode = eGPencil_PaintModes(RNA_enum_get(op->ptr, "mode"));
2161
2162 /* check context */
2163 p = static_cast<tGPsdata *>(op->customdata = annotation_session_initpaint(C));
2164 if ((p == nullptr) || (p->status == GP_STATUS_ERROR)) {
2165 /* something wasn't set correctly in context */
2167 return 0;
2168 }
2169
2170 /* init painting data */
2172 if (p->status == GP_STATUS_ERROR) {
2174 return 0;
2175 }
2176
2177 if (event != nullptr) {
2178 p->keymodifier = event->keymodifier;
2179 }
2180 else {
2181 p->keymodifier = -1;
2182 }
2183
2184 /* everything is now setup ok */
2185 return 1;
2186}
2187
2188/* ------------------------------- */
2189
2190/* ensure that the correct cursor icon is set */
2192{
2193 if (p->paintmode == GP_PAINTMODE_ERASER) {
2195 }
2196 else {
2198 }
2199}
2200
2201/* update UI indicators of status, including cursor and header prints */
2203{
2204 WorkspaceStatus status(C);
2205
2206 /* header prints */
2207 switch (p->status) {
2208 case GP_STATUS_PAINTING:
2209 switch (p->paintmode) {
2211 status.item(IFACE_("End"), ICON_EVENT_ESC);
2212 status.item(IFACE_("Place Next Stroke Vertex"), ICON_MOUSE_LMB);
2213 break;
2215 status.item(IFACE_("End"), ICON_EVENT_ESC);
2216 status.item(IFACE_("Erase"), ICON_MOUSE_LMB);
2217 break;
2219 status.item(IFACE_("End"), ICON_EVENT_ESC);
2220 status.item(IFACE_("Draw"), ICON_MOUSE_LMB);
2221 break;
2222 case GP_PAINTMODE_DRAW:
2223 status.item(IFACE_("End"), ICON_EVENT_ESC);
2224 status.item(IFACE_("Draw"), ICON_MOUSE_LMB);
2225 break;
2226
2227 default: /* unhandled future cases */
2228 status.item(IFACE_("End"), ICON_EVENT_ESC);
2229 break;
2230 }
2231 break;
2232
2233 case GP_STATUS_IDLING:
2234 case GP_STATUS_ERROR:
2235 case GP_STATUS_DONE:
2236 case GP_STATUS_CAPTURE:
2237 break;
2238 }
2239}
2240
2241/* ------------------------------- */
2242
2243/* create a new stroke point at the point indicated by the painting context */
2244static void annotation_draw_apply(wmOperator *op, tGPsdata *p, Depsgraph *depsgraph)
2245{
2246 /* handle drawing/erasing -> test for erasing first */
2247 if (p->paintmode == GP_PAINTMODE_ERASER) {
2248 /* do 'live' erasing now */
2250
2251 /* store used values */
2252 p->mvalo[0] = p->mval[0];
2253 p->mvalo[1] = p->mval[1];
2254 p->opressure = p->pressure;
2255 }
2256 /* Only add current point to buffer if mouse moved
2257 * (even though we got an event, it might be just noise). */
2258 else if (annotation_stroke_filtermval(p, p->mval, p->mvalo)) {
2259 /* If lazy mouse, interpolate the last and current mouse positions. */
2261 float now_mouse[2];
2262 float last_mouse[2];
2263 copy_v2_v2(now_mouse, p->mval);
2264 copy_v2_v2(last_mouse, p->mvalo);
2265 interp_v2_v2v2(now_mouse, now_mouse, last_mouse, min_ff(p->stabilizer_factor, .995f));
2266 copy_v2_v2(p->mval, now_mouse);
2267 }
2268
2269 /* try to add point */
2270 short ok = annotation_stroke_addpoint(p, p->mval, p->pressure, p->curtime);
2271
2272 /* handle errors while adding point */
2274 /* finish off old stroke */
2276 /* And start a new one!!! Else, projection errors! */
2277 annotation_paint_initstroke(p, p->paintmode, depsgraph);
2278
2279 /* start a new stroke, starting from previous point */
2280 if (ok == GP_STROKEADD_OVERFLOW) {
2281 p->inittime = p->ocurtime;
2283 }
2284 else {
2285 p->inittime = p->curtime;
2286 }
2288 }
2289 else if (ok == GP_STROKEADD_INVALID) {
2290 /* the painting operation cannot continue... */
2291 BKE_report(op->reports, RPT_ERROR, "Cannot paint stroke");
2293
2294 return;
2295 }
2296
2297 /* store used values */
2298 p->mvalo[0] = p->mval[0];
2299 p->mvalo[1] = p->mval[1];
2300 p->opressure = p->pressure;
2301 p->ocurtime = p->curtime;
2302 }
2303}
2304
2305/* handle draw event */
2307 wmOperator *op, const wmEvent *event, Depsgraph *depsgraph, float x, float y)
2308{
2309 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2310 PointerRNA itemptr;
2311 float mousef[2];
2312
2313 /* Convert from window-space to area-space mouse coordinates
2314 * add any x,y override position for fake events. */
2315 if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
2316 /* The first run may be a drag event, see: #99368. */
2318 p->mval[0] -= x;
2319 p->mval[1] -= y;
2320 }
2321 else {
2322 p->mval[0] = float(event->mval[0]) - x;
2323 p->mval[1] = float(event->mval[1]) - y;
2324 }
2325
2326 /* Key to toggle stabilization. */
2327 if ((event->modifier & KM_SHIFT) && (p->paintmode == GP_PAINTMODE_DRAW)) {
2328 /* Using permanent stabilization, shift will deactivate the flag. */
2333 }
2334 }
2335 /* Not using any stabilization flag. Activate temporal one. */
2336 else if ((p->flags & GP_PAINTFLAG_USE_STABILIZER_TEMP) == 0) {
2339 }
2340 }
2341 /* verify key status for straight lines */
2342 else if (event->modifier & (KM_CTRL | KM_ALT)) {
2343 if (p->straight[0] == 0) {
2344 int dx = abs(int(p->mval[0] - p->mvalo[0]));
2345 int dy = abs(int(p->mval[1] - p->mvalo[1]));
2346 if ((dx > 0) || (dy > 0)) {
2347 /* check mouse direction to replace the other coordinate with previous values */
2348 if (dx >= dy) {
2349 /* horizontal */
2350 p->straight[0] = 1;
2351 p->straight[1] = p->mval[1]; /* save y */
2352 }
2353 else {
2354 /* vertical */
2355 p->straight[0] = 2;
2356 p->straight[1] = p->mval[0]; /* save x */
2357 }
2358 }
2359 }
2360 }
2361 else {
2362 p->straight[0] = 0;
2363 /* We were using shift while having permanent stabilization active,
2364 * so activate the temp flag back again. */
2366 if ((p->flags & GP_PAINTFLAG_USE_STABILIZER_TEMP) == 0) {
2369 }
2370 }
2371 /* We are using the temporal stabilizer flag at the moment,
2372 * but shift is not pressed as well as the permanent flag is not used,
2373 * so we don't need the cursor anymore. */
2375 /* Reset temporal stabilizer flag and remove cursor. */
2378 }
2379 }
2380
2382
2383 /* handle pressure sensitivity (which is supplied by tablets or otherwise 1.0) */
2384 p->pressure = event->tablet.pressure;
2385
2386 /* Hack for pressure sensitive eraser on D+RMB when using a tablet:
2387 * The pen has to float over the tablet surface, resulting in
2388 * zero pressure (#47101). Ignore pressure values if floating
2389 * (i.e. "effectively zero" pressure), and only when the "active"
2390 * end is the stylus (i.e. the default when not eraser)
2391 */
2392 if (p->paintmode == GP_PAINTMODE_ERASER) {
2393 if ((event->tablet.active != EVT_TABLET_ERASER) && (p->pressure < 0.001f)) {
2394 p->pressure = 1.0f;
2395 }
2396 }
2397
2398 /* special exception for start of strokes (i.e. maybe for just a dot) */
2399 if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
2401
2402 p->mvalo[0] = p->mval[0];
2403 p->mvalo[1] = p->mval[1];
2404 p->opressure = p->pressure;
2405 p->inittime = p->ocurtime = p->curtime;
2406 p->straight[0] = 0;
2407 p->straight[1] = 0;
2408
2409 /* special exception here for too high pressure values on first touch in
2410 * windows for some tablets, then we just skip first touch...
2411 */
2412 if ((event->tablet.active != EVT_TABLET_NONE) && (p->pressure >= 0.99f)) {
2413 return;
2414 }
2415 }
2416
2417 /* check if alt key is pressed and limit to straight lines */
2418 if ((p->paintmode != GP_PAINTMODE_ERASER) && (p->straight[0] != 0)) {
2419 if (p->straight[0] == 1) {
2420 /* horizontal */
2421 p->mval[1] = p->straight[1]; /* replace y */
2422 }
2423 else {
2424 /* vertical */
2425 p->mval[0] = p->straight[1]; /* replace x */
2426 }
2427 }
2428
2429 /* fill in stroke data (not actually used directly by gpencil_draw_apply) */
2430 RNA_collection_add(op->ptr, "stroke", &itemptr);
2431
2432 mousef[0] = p->mval[0];
2433 mousef[1] = p->mval[1];
2434 RNA_float_set_array(&itemptr, "mouse", mousef);
2435 RNA_float_set(&itemptr, "pressure", p->pressure);
2436 RNA_boolean_set(&itemptr, "is_start", (p->flags & GP_PAINTFLAG_FIRSTRUN) != 0);
2437
2438 RNA_float_set(&itemptr, "time", p->curtime - p->inittime);
2439
2440 /* apply the current latest drawing point */
2441 annotation_draw_apply(op, p, depsgraph);
2442
2443 /* force refresh */
2444 /* just active area for now, since doing whole screen is too slow */
2446}
2447
2448/* ------------------------------- */
2449
2450/* operator 'redo' (i.e. after changing some properties, but also for repeat last) */
2452{
2453 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2454
2455 /* try to initialize context data needed while drawing */
2456 if (!annotation_draw_init(C, op, nullptr)) {
2457 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2458 MEM_delete(p);
2459 op->customdata = nullptr;
2460 return OPERATOR_CANCELLED;
2461 }
2462
2463 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2464
2465 /* loop over the stroke RNA elements recorded (i.e. progress of mouse movement),
2466 * setting the relevant values in context at each step, then applying
2467 */
2468 RNA_BEGIN (op->ptr, itemptr, "stroke") {
2469 float mousef[2];
2470
2471 /* get relevant data for this point from stroke */
2472 RNA_float_get_array(&itemptr, "mouse", mousef);
2473 p->mval[0] = int(mousef[0]);
2474 p->mval[1] = int(mousef[1]);
2475 p->pressure = RNA_float_get(&itemptr, "pressure");
2476 p->curtime = double(RNA_float_get(&itemptr, "time")) + p->inittime;
2477
2478 if (RNA_boolean_get(&itemptr, "is_start")) {
2479 /* if first-run flag isn't set already (i.e. not true first stroke),
2480 * then we must terminate the previous one first before continuing
2481 */
2482 if ((p->flags & GP_PAINTFLAG_FIRSTRUN) == 0) {
2483 /* TODO: both of these ops can set error-status, but we probably don't need to worry */
2485 annotation_paint_initstroke(p, p->paintmode, depsgraph);
2486 }
2487 }
2488
2489 /* if first run, set previous data too */
2490 if (p->flags & GP_PAINTFLAG_FIRSTRUN) {
2492
2493 p->mvalo[0] = p->mval[0];
2494 p->mvalo[1] = p->mval[1];
2495 p->opressure = p->pressure;
2496 p->ocurtime = p->curtime;
2497 }
2498
2499 /* apply this data as necessary now (as per usual) */
2500 annotation_draw_apply(op, p, depsgraph);
2501 }
2502 RNA_END;
2503
2504 /* cleanup */
2506
2507 /* refreshes */
2509
2510 /* done */
2511 return OPERATOR_FINISHED;
2512}
2513
2514/* ------------------------------- */
2515
2516/* start of interactive drawing part of operator */
2518{
2519 /* support for tablets eraser pen */
2521 RNA_enum_set(op->ptr, "mode", GP_PAINTMODE_ERASER);
2522 }
2523
2524 /* try to initialize context data needed while drawing */
2525 if (!annotation_draw_init(C, op, event)) {
2526 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2527 MEM_delete(p);
2528 op->customdata = nullptr;
2529 return OPERATOR_CANCELLED;
2530 }
2531
2532 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2533
2534 /* if empty erase capture and finish */
2535 if (p->status == GP_STATUS_CAPTURE) {
2537
2538 BKE_report(op->reports, RPT_ERROR, "Nothing to erase");
2539 return OPERATOR_FINISHED;
2540 }
2541
2542 /* if eraser is on, draw radial aid */
2543 if (p->paintmode == GP_PAINTMODE_ERASER) {
2545 }
2546 else if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) {
2547 if (RNA_enum_get(op->ptr, "arrowstyle_start") != GP_STROKE_ARROWSTYLE_NONE) {
2550 RNA_enum_get(op->ptr, "arrowstyle_start"));
2551 }
2552 if (RNA_enum_get(op->ptr, "arrowstyle_end") != GP_STROKE_ARROWSTYLE_NONE) {
2555 RNA_enum_get(op->ptr, "arrowstyle_end"));
2556 }
2557 }
2558 else if (p->paintmode == GP_PAINTMODE_DRAW) {
2559 p->stabilizer_factor = RNA_float_get(op->ptr, "stabilizer_factor");
2560 p->stabilizer_radius = RNA_int_get(op->ptr, "stabilizer_radius");
2561 if (RNA_boolean_get(op->ptr, "use_stabilizer")) {
2564 }
2565 else if (event->modifier & KM_SHIFT) {
2568 }
2569 }
2570 /* set cursor
2571 * NOTE: This may change later (i.e. intentionally via brush toggle,
2572 * or unintentionally if the user scrolls outside the area)...
2573 */
2575
2576 /* only start drawing immediately if we're allowed to do so... */
2577 if (RNA_boolean_get(op->ptr, "wait_for_input") == false) {
2578 /* hotkey invoked - start drawing */
2580
2581 /* handle the initial drawing - i.e. for just doing a simple dot */
2584 }
2585 else {
2586 /* toolbar invoked - don't start drawing yet... */
2588 }
2589
2591 /* add a modal handler for this operator, so that we can then draw continuous strokes */
2594}
2595
2596/* gpencil modal operator stores area, which can be removed while using it (like full-screen). */
2597static bool annotation_area_exists(bContext *C, ScrArea *area_test)
2598{
2599 bScreen *screen = CTX_wm_screen(C);
2600 return (BLI_findindex(&screen->areabase, area_test) != -1);
2601}
2602
2604{
2605 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2606
2607 /* we must check that we're still within the area that we're set up to work from
2608 * otherwise we could crash (see bug #20586)
2609 */
2610 if (CTX_wm_area(C) != p->area) {
2611 printf("\t\t\tGP - wrong area execution abort!\n");
2613 }
2614
2615 /* we may need to set up paint env again if we're resuming */
2616 /* XXX: watch it with the paintmode! in future,
2617 * it'd be nice to allow changing paint-mode when in sketching-sessions */
2618
2621 }
2622
2623 if (p->status != GP_STATUS_ERROR) {
2626 }
2627
2628 return static_cast<tGPsdata *>(op->customdata);
2629}
2630
2632{
2633 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2634
2636
2638
2641
2642 p->gpd = nullptr;
2643 p->gpl = nullptr;
2644 p->gpf = nullptr;
2645}
2646
2647/* add events for missing mouse movements when the artist draw very fast */
2649 wmOperator *op,
2650 const wmEvent *event,
2651 tGPsdata *p)
2652{
2653 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2654 float pt[2], a[2], b[2];
2655 float factor = 10.0f;
2656
2657 copy_v2_v2(a, p->mvalo);
2658 b[0] = float(event->mval[0]) + 1.0f;
2659 b[1] = float(event->mval[1]) + 1.0f;
2660
2661 /* get distance in pixels */
2662 float dist = len_v2v2(a, b);
2663
2664 /* for very small distances, add a half way point */
2665 if (dist <= 2.0f) {
2666 interp_v2_v2v2(pt, a, b, 0.5f);
2667 sub_v2_v2v2(pt, b, pt);
2668 /* create fake event */
2669 annotation_draw_apply_event(op, event, depsgraph, pt[0], pt[1]);
2670 }
2671 else if (dist >= factor) {
2672 int slices = 2 + int((dist - 1.0) / factor);
2673 float n = 1.0f / slices;
2674 for (int i = 1; i < slices; i++) {
2675 interp_v2_v2v2(pt, a, b, n * i);
2676 sub_v2_v2v2(pt, b, pt);
2677 /* create fake event */
2678 annotation_draw_apply_event(op, event, depsgraph, pt[0], pt[1]);
2679 }
2680 }
2681}
2682
2683/* events handling during interactive drawing part of operator */
2685{
2686 tGPsdata *p = static_cast<tGPsdata *>(op->customdata);
2687 /* Default exit state - pass through to support MMB view navigation, etc. */
2689
2690/* NOTE(mike erwin): Not quite what I was looking for, but a good start!
2691 * grease-pencil continues to draw on the screen while the 3D mouse moves the viewpoint.
2692 * Problem is that the stroke is converted to 3D only after it is finished.
2693 * This approach should work better in tools that immediately apply in 3D space. */
2694#if 0
2695 if (event->type == NDOF_MOTION) {
2696 return OPERATOR_PASS_THROUGH;
2697 }
2698#endif
2699
2700 if (p->status == GP_STATUS_IDLING) {
2701 ARegion *region = CTX_wm_region(C);
2702 p->region = region;
2703 }
2704
2705 /* We don't pass on key events, GP is used with key-modifiers -
2706 * prevents D-key to insert drivers. */
2707 if (ISKEYBOARD(event->type)) {
2708 if (ELEM(event->type,
2713 EVT_ZKEY))
2714 {
2715 /* allow some keys:
2716 * - For frame changing #33412.
2717 * - For undo (during sketching sessions).
2718 */
2719 }
2720 else if (ELEM(event->type,
2721 EVT_PAD0,
2722 EVT_PAD1,
2723 EVT_PAD2,
2724 EVT_PAD3,
2725 EVT_PAD4,
2726 EVT_PAD5,
2727 EVT_PAD6,
2728 EVT_PAD7,
2729 EVT_PAD8,
2730 EVT_PAD9))
2731 {
2732 /* Allow numpad keys so that camera/view manipulations can still take place
2733 * - #EVT_PAD0 in particular is really important for Grease Pencil drawing,
2734 * as animators may be working "to camera", so having this working
2735 * is essential for ensuring that they can quickly return to that view.
2736 */
2737 }
2738 else if ((event->type == EVT_BKEY) && (event->val == KM_RELEASE)) {
2739 /* Add Blank Frame
2740 * - Since this operator is non-modal, we can just call it here, and keep going...
2741 * - This operator is especially useful when animating
2742 */
2744 C, "GPENCIL_OT_layer_annotation_add", wm::OpCallContext::ExecDefault, nullptr, event);
2745 estate = OPERATOR_RUNNING_MODAL;
2746 }
2747 else {
2748 estate = OPERATOR_RUNNING_MODAL;
2749 }
2750 }
2751
2752 /* Exit painting mode (and/or end current stroke)
2753 *
2754 * NOTE: cannot do RIGHTMOUSE (as is standard for canceling)
2755 * as that would break polyline #32647.
2756 */
2757 if (event->val == KM_PRESS &&
2759 {
2760 /* exit() ends the current stroke before cleaning up */
2762 estate = OPERATOR_FINISHED;
2763 }
2764
2765 /* Toggle painting mode upon mouse-button movement
2766 * - #RIGHTMOUSE: eraser (all).
2767 * (Disabling #RIGHTMOUSE case here results in bugs like #32647)
2768 * - Others (typically LMB): standard drawing (all) / straight line drawing (all).
2769 * Also making sure we have a valid event value, to not exit too early. */
2770
2771 if (ISMOUSE_BUTTON(event->type) && ELEM(event->val, KM_PRESS, KM_RELEASE)) {
2772 if (event->type == MIDDLEMOUSE) {
2773 /* Pass middle mouse event to viewport navigation, see: #151982 */
2774 return estate;
2775 }
2776
2777 /* if painting, end stroke */
2778 if (p->status == GP_STATUS_PAINTING) {
2779 int sketch = 0;
2780
2781 /* basically, this should be mouse-button up = end stroke
2782 * BUT, polyline drawing is an exception -- all knots should be added during one session
2783 */
2784 sketch |= (p->paintmode == GP_PAINTMODE_DRAW_POLY);
2785
2786 if (sketch) {
2787 /* end stroke only, and then wait to resume painting soon */
2789
2790 /* If eraser mode is on, turn it off after the stroke finishes
2791 * NOTE: This just makes it nicer to work with drawing sessions
2792 */
2793 if (p->paintmode == GP_PAINTMODE_ERASER) {
2794 p->paintmode = eGPencil_PaintModes(RNA_enum_get(op->ptr, "mode"));
2795
2796 /* if the original mode was *still* eraser,
2797 * we'll let it say for now, since this gives
2798 * users an opportunity to have visual feedback
2799 * when adjusting eraser size
2800 */
2801 if (p->paintmode != GP_PAINTMODE_ERASER) {
2802 /* turn off cursor...
2803 * NOTE: this should be enough for now
2804 * Just hiding this makes it seem like
2805 * you can paint again...
2806 */
2808 }
2809 }
2810
2811 /* we've just entered idling state, so this event was processed (but no others yet) */
2812 estate = OPERATOR_RUNNING_MODAL;
2813
2814 /* stroke could be smoothed, send notifier to refresh screen */
2816 }
2817 else {
2819 estate = OPERATOR_FINISHED;
2820 }
2821 }
2822 else if (event->val == KM_PRESS) {
2823 bool in_bounds = false;
2824
2825 /* Check if we're outside the bounds of the active region
2826 * NOTE: An exception here is that if launched from the toolbar,
2827 * whatever region we're now in should become the new region
2828 */
2829 if ((p->region) && (p->region->regiontype == RGN_TYPE_TOOLS)) {
2830 /* Change to whatever region is now under the mouse */
2831 ARegion *current_region = BKE_area_find_region_xy(p->area, RGN_TYPE_ANY, event->xy);
2832
2833 if (current_region) {
2834 /* Assume that since we found the cursor in here, it is in bounds
2835 * and that this should be the region that we begin drawing in
2836 */
2837 p->region = current_region;
2838 in_bounds = true;
2839 }
2840 else {
2841 /* Out of bounds, or invalid in some other way */
2843 estate = OPERATOR_CANCELLED;
2844 }
2845 }
2846 else if (p->region) {
2847 /* Perform bounds check. */
2848 const rcti *region_rect = ED_region_visible_rect(p->region);
2849 in_bounds = BLI_rcti_isect_pt_v(region_rect, event->mval);
2850 }
2851 else {
2852 /* No region */
2854 estate = OPERATOR_CANCELLED;
2855 }
2856
2857 if (in_bounds) {
2858 /* Switch paintmode (temporarily if need be) based on which button was used
2859 * NOTE: This is to make it more convenient to erase strokes when using drawing sessions
2860 */
2861 if ((event->type == RIGHTMOUSE) || annotation_is_tablet_eraser_active(event)) {
2862 /* turn on eraser */
2864 }
2865 else { /* Any mouse button besides right. */
2866 /* restore drawmode to default */
2867 p->paintmode = eGPencil_PaintModes(RNA_enum_get(op->ptr, "mode"));
2868 }
2869
2871
2872 /* not painting, so start stroke (this should be mouse-button down) */
2873 p = annotation_stroke_begin(C, op);
2874
2875 if (p->status == GP_STATUS_ERROR) {
2876 estate = OPERATOR_CANCELLED;
2877 }
2878 }
2879 else if (p->status != GP_STATUS_ERROR) {
2880 /* User clicked outside bounds of window while idling, so exit paintmode
2881 * NOTE: Don't enter this case if an error occurred while finding the
2882 * region (as above)
2883 */
2885 estate = OPERATOR_FINISHED;
2886 }
2887 }
2888 else if (event->val == KM_RELEASE) {
2891 }
2892 }
2893
2894 /* handle mode-specific events */
2895 if (p->status == GP_STATUS_PAINTING) {
2896 /* handle painting mouse-movements? */
2897 if (ISMOUSE_MOTION(event->type) || (p->flags & GP_PAINTFLAG_FIRSTRUN)) {
2898 /* handle drawing event */
2899 if ((p->flags & GP_PAINTFLAG_FIRSTRUN) == 0) {
2900 annotation_add_missing_events(C, op, event, p);
2901 }
2902
2903 /* TODO(sergey): Possibly evaluating dependency graph from modal operator? */
2905
2906 /* finish painting operation if anything went wrong just now */
2907 if (p->status == GP_STATUS_ERROR) {
2908 printf("\t\t\t\tGP - add error done!\n");
2909 estate = OPERATOR_CANCELLED;
2910 }
2911 else {
2912 /* event handled, so just tag as running modal */
2913 estate = OPERATOR_RUNNING_MODAL;
2914 }
2915 }
2916 /* eraser size */
2917 else if ((p->paintmode == GP_PAINTMODE_ERASER) &&
2919 {
2920 /* just resize the brush (local version)
2921 * TODO: fix the hardcoded size jumps (set to make a visible difference) and hardcoded keys
2922 */
2923 switch (event->type) {
2924 case WHEELDOWNMOUSE: /* larger */
2925 case EVT_PADPLUSKEY:
2926 p->radius += 5;
2927 break;
2928
2929 case WHEELUPMOUSE: /* smaller */
2930 case EVT_PADMINUS:
2931 p->radius -= 5;
2932
2933 if (p->radius <= 0) {
2934 p->radius = 1;
2935 }
2936 break;
2937 default: {
2938 break;
2939 }
2940 }
2941
2942 /* force refresh */
2943 /* just active area for now, since doing whole screen is too slow */
2945
2946 /* event handled, so just tag as running modal */
2947 estate = OPERATOR_RUNNING_MODAL;
2948 }
2949 /* there shouldn't be any other events, but just in case there are, let's swallow them
2950 * (i.e. to prevent problems with undo)
2951 */
2952 else {
2953 /* swallow event to save ourselves trouble */
2954 estate = OPERATOR_RUNNING_MODAL;
2955 }
2956 }
2957
2958 /* gpencil modal operator stores area, which can be removed while using it (like full-screen). */
2959 if (0 == annotation_area_exists(C, p->area)) {
2960 estate = OPERATOR_CANCELLED;
2961 }
2962 else {
2963 /* update status indicators - cursor, header, etc. */
2965 /* cursor may have changed outside our control - #44084 */
2967 }
2968
2969 /* process last operations before exiting */
2970 switch (estate) {
2971 case OPERATOR_FINISHED:
2972 /* one last flush before we're done */
2975 break;
2976
2977 case OPERATOR_CANCELLED:
2979 break;
2980
2981 /* Event doesn't need to be handled. */
2982 /* `OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH` */
2983 default:
2984 /* Quiet warnings. */
2985 break;
2986 }
2987
2988 /* return status code */
2989 return estate;
2990}
2991
2992/* ------------------------------- */
2993
2995 {GP_PAINTMODE_DRAW, "DRAW", 0, "Draw Freehand", "Draw freehand stroke(s)"},
2997 "DRAW_STRAIGHT",
2998 0,
2999 "Draw Straight Lines",
3000 "Draw straight line segment(s)"},
3002 "DRAW_POLY",
3003 0,
3004 "Draw Poly Line",
3005 "Click to place endpoints of straight line segments (connected)"},
3006 {GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", "Erase Annotation strokes"},
3007 {0, nullptr, 0, nullptr, nullptr},
3008};
3009
3011 {GP_STROKE_ARROWSTYLE_NONE, "NONE", 0, "None", "Don't use any arrow/style in corner"},
3012 {GP_STROKE_ARROWSTYLE_CLOSED, "ARROW", 0, "Arrow", "Use closed arrow style"},
3013 {GP_STROKE_ARROWSTYLE_OPEN, "ARROW_OPEN", 0, "Open Arrow", "Use open arrow style"},
3015 "ARROW_OPEN_INVERTED",
3016 0,
3017 "Segment",
3018 "Use perpendicular segment style"},
3019 {GP_STROKE_ARROWSTYLE_SQUARE, "DIAMOND", 0, "Square", "Use square style"},
3020 {0, nullptr, 0, nullptr, nullptr},
3021};
3022
3024{
3025 PropertyRNA *prop;
3026
3027 /* identifiers */
3028 ot->name = "Annotation Draw";
3029 ot->idname = "GPENCIL_OT_annotate";
3030 ot->description = "Make annotations on the active data";
3031
3032 /* API callbacks. */
3038
3039 /* flags */
3041
3042 /* settings for drawing */
3043 ot->prop = RNA_def_enum(
3044 ot->srna, "mode", prop_gpencil_drawmodes, 0, "Mode", "Way to interpret mouse movements");
3045
3046 /* properties */
3047 prop = RNA_def_enum(
3048 ot->srna, "arrowstyle_start", arrow_types, 0, "Start Arrow Style", "Stroke start style");
3049 prop = RNA_def_enum(
3050 ot->srna, "arrowstyle_end", arrow_types, 0, "End Arrow Style", "Stroke end style");
3051 prop = RNA_def_boolean(ot->srna,
3052 "use_stabilizer",
3053 false,
3054 "Stabilize Stroke",
3055 "Helper to draw smooth and clean lines. Press Shift for an invert effect "
3056 "(even if this option is not active)");
3057 prop = RNA_def_float(ot->srna,
3058 "stabilizer_factor",
3059 0.75f,
3060 0.0f,
3061 1.0f,
3062 "Stabilizer Stroke Factor",
3063 "Higher values give a smoother stroke",
3064 0.0f,
3065 1.0f);
3066 prop = RNA_def_int(ot->srna,
3067 "stabilizer_radius",
3068 35,
3069 0,
3070 200,
3071 "Stabilizer Stroke Radius",
3072 "Minimum distance from last point before stroke continues",
3073 1,
3074 100);
3076
3077 prop = RNA_def_collection_runtime(ot->srna, "stroke", RNA_OperatorStrokeElement, "Stroke", "");
3079
3080 /* NOTE: wait for input is enabled by default,
3081 * so that all UI code can work properly without needing users to know about this */
3082 prop = RNA_def_boolean(ot->srna,
3083 "wait_for_input",
3084 true,
3085 "Wait for Input",
3086 "Wait for first click instead of painting immediately");
3088}
3089
3090} // namespace blender
#define GPENCIL_STRENGTH_MIN
Camera data-block and utility functions.
#define ENUM_OPERATORS(_enum_type)
Platform independent time functions.
#define IFACE_(msgid)
#define DATA_(msgid)
#define RGN_TYPE_ANY
#define SPACE_TYPE_ANY
Read Blender memory allocation and freeing.
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
#define C
Definition RandGen.cpp:29
#define V2D_IS_CLIPPED
Definition UI_view2d.hh:46
#define NA_EDITED
Definition WM_types.hh:634
#define NC_GPENCIL
Definition WM_types.hh:412
#define MIN_EUCLIDEAN_PX
#define MIN_MANHATTAN_PX
#define DEPTH_INVALID
#define CLAMP(a, b, c)
#define ELEM(...)
#define MEM_realloc_uninitialized(vmemh, len)
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
constexpr T sign(T) RET
#define abs
float length(VecOp< float, D >) RET
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
BMesh Iterators.
MINLINE float min_ff(float a, float b)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool ED_view3d_depth_read_cached_seg(const ViewDepths *vd, const int mval_sta[2], const int mval_end[2], int margin, float *r_depth)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
void BKE_callback_exec_id_depsgraph(Main *bmain, ID *id, Depsgraph *depsgraph, eCbEvent evt)
Definition callbacks.cc:54
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
static void annotation_stroke_added_enable(tGPsdata *p)
static void annotation_draw_exit(bContext *C, wmOperator *op)
ScrArea * CTX_wm_area(const bContext *C)
static void annotation_stroke_arrow_calc_points_segment(float stroke_points[8], const float ref_point[2], const float dir_cw[2], const float dir_ccw[2], const float length, const float sign)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
int RNA_enum_get(PointerRNA *ptr, const char *name)
void WM_event_drag_start_mval_fl(const wmEvent *event, const ARegion *region, float r_mval[2])
static void annotation_arrow_create_segm(tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, const float arrow_points[8])
static void annotation_add_missing_events(bContext *C, wmOperator *op, const wmEvent *event, tGPsdata *p)
void immEnd()
static void annotation_draw_status_indicators(bContext *C, tGPsdata *p)
static bool annotation_stroke_added_check(tGPsdata *p)
MINLINE void copy_v2_fl(float r[2], float f)
static bool annotation_area_exists(bContext *C, ScrArea *area_test)
static void annotation_stroke_arrow_calc_points(tGPspoint *point, const float stroke_dir[2], float corner[2], float stroke_points[8], const int arrow_style)
static void annotation_paint_cleanup(tGPsdata *p)
struct bGPDframe * BKE_annotations_layer_frame_get(struct bGPDlayer *gpl, int cframe, eGP_GetFrame_Mode addnew)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
int interp_sparse_array(float *array, int list_size, float skipval)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ BKE_CB_EVT_ANNOTATION_PRE
@ BKE_CB_EVT_ANNOTATION_POST
static void annotation_session_validatebuffer(tGPsdata *p)
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
void ED_view3d_depths_free(ViewDepths *depths)
void immUniformColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
@ GP_STROKEADD_INVALID
@ GP_STROKEADD_OVERFLOW
@ GP_STROKEADD_NORMAL
MINLINE void mul_v2_fl(float r[2], float f)
void immUniform2f(const char *name, float x, float y)
bGPdata ** ED_annotation_data_get_pointers(const bContext *C, PointerRNA *r_ptr)
static bGPDstroke * stroke_delete_tagged_points(bGPdata *gpd, bGPDframe *gpf, bGPDstroke *gps, bGPDstroke *next_stroke, int tag_flags, const bool select, const bool flat_cap, const int limit)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
MovieTrackingObject * BKE_tracking_object_get_active(const MovieTracking *tracking)
Definition tracking.cc:1982
GPUVertFormat * immVertexFormat()
static const EnumPropertyItem arrow_types[]
MINLINE float len_v2v2_int(const int v1[2], const int v2[2])
void BKE_annotations_free_stroke_weights(struct bGPDstroke *gps)
struct bGPDlayer * BKE_annotations_layer_addnew(struct bGPdata *gpd, const char *name, bool setactive, bool add_to_header)
static void annotation_paint_strokeend(tGPsdata *p)
void WM_cursor_modal_set(wmWindow *win, int val)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void immBegin(GPUPrimType, uint vertex_len)
void BKE_annotation_stroke_geometry_update(struct bGPdata *gpd, struct bGPDstroke *gps)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static void annotation_arrow_create(tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, bGPDstroke *arrow_stroke, const float arrow_points[8], const int style)
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
int RNA_int_get(PointerRNA *ptr, const char *name)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
static void annotation_draw_cursor_set(tGPsdata *p)
wmWindow * CTX_wm_window(const bContext *C)
MINLINE float normalize_v2(float n[2])
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
MINLINE int max_ii(int a, int b)
static bool annotation_is_tablet_eraser_active(const wmEvent *event)
const rcti * ED_region_visible_rect(ARegion *region)
Definition area.cc:4791
void BKE_annotations_free_stroke(struct bGPDstroke *gps)
bool gpencil_stroke_inside_circle(const float mval[2], int rad, int x0, int y0, int x1, int y1)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
static void annotation_stroke_arrow_allocate(bGPDstroke *gps, const int totpoints)
struct bGPdata * BKE_annotations_data_addnew(struct Main *bmain, const char name[])
void immUnbindProgram()
static short annotation_stroke_addpoint(tGPsdata *p, const float mval[2], float pressure, double curtime)
static bool annotation_draw_poll(bContext *C)
static void annotation_draw_apply(wmOperator *op, tGPsdata *p, Depsgraph *depsgraph)
bool ED_gpencil_stroke_can_use_direct(const ScrArea *area, const bGPDstroke *gps)
void GPU_line_width(float width)
Definition gpu_state.cc:151
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
static tGPsdata * annotation_session_initpaint(bContext *C)
static bool annotation_stroke_filtermval(tGPsdata *p, const float mval[2], const float pmval[2])
static void annotation_session_free(tGPsdata *p)
static void annotation_arrow_create_square(tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, const float corner_point[3], const float arrow_points[8])
static void annotation_draw_eraser(bContext *, const int2 &xy, const float2 &, void *p_ptr)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void imm_draw_circle_wire_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
static void annotation_stroke_arrow_init_conv_point(bGPDspoint *pt, const float point[3])
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
@ GP_TOOL_FLAG_RETAIN_LAST
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
static void annotation_draw_toggle_stabilizer_cursor(tGPsdata *p, short enable)
static void annotation_stroke_newfrombuffer(tGPsdata *p)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
void immVertex2f(uint attr_id, float x, float y)
MINLINE void add_v2_v2(float r[2], const float a[2])
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
@ SEQ_PREVIEW_SHOW_GPENCIL
int ED_space_clip_get_clip_frame_number(const SpaceClip *sc)
rctf BKE_camera_view_border(const struct Scene *scene, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, int winx, int winy, bool no_shift, bool no_zoom, bool no_roll)
eV3DProjStatus ED_view3d_project_float_global(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
static void stroke_join_islands(bGPdata *gpd, bGPDframe *gpf, bGPDstroke *gps_first, bGPDstroke *gps_last)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
void WM_cursor_modal_restore(wmWindow *win)
static bool annotation_session_initdata(bContext *C, tGPsdata *p)
void immUniformColor4f(float r, float g, float b, float a)
bool ED_operator_regionactive(bContext *C)
static void annotation_stroke_arrow_init_point_default(bGPDspoint *pt)
void unit_m4(float m[4][4])
float ED_view3d_calc_zfac(const RegionView3D *rv3d, const float co[3])
ToolSettings * CTX_data_tool_settings(const bContext *C)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
struct bGPDstroke * BKE_gpencil_stroke_duplicate(struct bGPDstroke *gps_src, bool dup_points, bool dup_curve)
static void annotation_stroke_convertcoords(tGPsdata *p, const float2 mval, float out[3], const float *depth)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
struct bGPDlayer * BKE_annotations_layer_active_get(struct bGPdata *gpd)
MINLINE void zero_v3(float r[3])
void GPENCIL_OT_annotate(wmOperatorType *ot)
bool ED_view3d_autodist_simple(ARegion *region, const int mval[2], float mouse_worldloc[3], int margin, const float *force_depth)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
static void annotation_draw_cancel(bContext *C, wmOperator *op)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void annotation_get_3d_reference(tGPsdata *p, float vec[3])
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
static void annotation_stroke_eraser_dostroke(tGPsdata *p, bGPDframe *gpf, bGPDstroke *gps, const float mval[2], const int radius, const rcti *rect)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
static void annotation_stroke_end(wmOperator *op)
wmPaintCursor * WM_paint_cursor_activate(short space_type, short region_type, wmPaintCursorPoll poll, wmPaintCursorDraw draw, void *customdata)
static wmOperatorStatus annotation_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
static void annotation_arrow_create_open(tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, const float corner_point[3], const float arrow_points[8])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void gpencil_point_to_xy(const GP_SpaceConversion *gsc, const bGPDstroke *gps, const bGPDspoint *pt, int *r_x, int *r_y)
@ WM_CURSOR_PAINT_BRUSH
Definition wm_cursors.hh:36
@ WM_CURSOR_ERASER
Definition wm_cursors.hh:37
static void annotation_stroke_doeraser(tGPsdata *p)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
MINLINE void madd_v2_v2fl(float r[2], const float a[2], float f)
Main * CTX_data_main(const bContext *C)
static void annotation_smooth_buffer(tGPsdata *p, float inf, int idx)
MovieClip * ED_space_clip_get_clip(const SpaceClip *sc)
bool BKE_annotations_layer_is_editable(const struct bGPDlayer *gpl)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
static void annotation_stroke_arrow_init_point(tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, const float co[8], const int co_idx)
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
static void annotation_visible_on_space(tGPsdata *p)
static void annotation_draw_apply_event(wmOperator *op, const wmEvent *event, Depsgraph *depsgraph, float x, float y)
MINLINE float max_ff(float a, float b)
void ED_view3d_depth_override(Depsgraph *depsgraph, ARegion *region, View3D *v3d, Object *obact, eV3DDepthOverrideMode mode, bool use_overlay, ViewDepths **r_depths)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
bool ED_view3d_depth_read_cached(const ViewDepths *vd, const int mval[2], int margin, float *r_depth)
tGPspoint * ED_gpencil_sbuffer_ensure(tGPspoint *buffer_array, int *buffer_size, int *buffer_used, const bool clear)
static tGPsdata * annotation_stroke_begin(bContext *C, wmOperator *op)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
static void annotation_paint_initstroke(tGPsdata *p, eGPencil_PaintModes paintmode, Depsgraph *depsgraph)
@ V3D_PROJ_TEST_NOP
Definition ED_view3d.hh:295
eV3DDepthOverrideMode
Definition ED_view3d.hh:205
@ V3D_DEPTH_ALL
Definition ED_view3d.hh:207
@ V3D_DEPTH_GPENCIL_ONLY
Definition ED_view3d.hh:211
@ V3D_DEPTH_NO_GPENCIL
Definition ED_view3d.hh:209
@ V3D_DEPTH_SELECTED_ONLY
Definition ED_view3d.hh:215
@ GP_PAINTMODE_DRAW_STRAIGHT
@ GP_PAINTMODE_DRAW_POLY
@ KM_RELEASE
Definition WM_types.hh:325
static bool annotation_stroke_eraser_is_occluded(tGPsdata *p, const bGPDspoint *pt, const int x, const int y)
static void annotation_arrow_create_closed(tGPsdata *p, tGPspoint *ptc, bGPDspoint *pt, const float arrow_points[8])
double BLI_time_now_seconds()
Definition time.cc:30
Scene * CTX_data_scene(const bContext *C)
static void annotation_session_cleanup(tGPsdata *p)
bScreen * CTX_wm_screen(const bContext *C)
bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2])
MINLINE void copy_v4_v4(float r[4], const float a[4])
@ GP_PROJECT_DEPTH_STROKE_ENDPOINTS
@ GP_PROJECT_DEPTH_STROKE
@ GP_PROJECT_DEPTH_ONLY_SELECTED
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
bool BLI_rcti_isect_pt(const struct rcti *rect, int x, int y)
bool ED_view3d_depth_unproject_v3(const ARegion *region, const int mval[2], double depth, float r_location_world[3])
@ GP_PAINTFLAG_SELECTMASK
@ GP_PAINTFLAG_V3D_ERASER_DEPTH
@ GP_PAINTFLAG_FIRSTRUN
@ GP_PAINTFLAG_STROKEADDED
@ GP_PAINTFLAG_USE_STABILIZER_TEMP
@ GP_PAINTFLAG_USE_STABILIZER
void copy_m4_m4(float m1[4][4], const float m2[4][4])
static void annotation_free_stroke(bGPDframe *gpf, bGPDstroke *gps)
void immUniform1i(const char *name, int x)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static void annotation_draw_toggle_eraser_cursor(tGPsdata *p, short enable)
static int annotation_draw_init(bContext *C, wmOperator *op, const wmEvent *event)
void immVertex2fv(uint attr_id, const float data[2])
void imm_draw_circle_fill_2d(uint shdr_pos, float x, float y, float radius, int nsegments)
static const EnumPropertyItem prop_gpencil_drawmodes[]
void view3d_region_operator_needs_gpu(ARegion *region)
void RNA_collection_add(PointerRNA *ptr, const char *name, PointerRNA *r_value)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
static wmOperatorStatus annotation_draw_exec(bContext *C, wmOperator *op)
static void annotation_draw_stabilizer(bContext *C, const int2 &xy, const float2 &, void *p_ptr)
ARegion * CTX_wm_region(const bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
void ED_view3d_win_to_delta(const ARegion *region, const float xy_delta[2], float zfac, float r_out[3], bool precise=false)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static bool annotation_project_check(tGPsdata *p)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool WM_paint_cursor_end(wmPaintCursor *handle)
ARegion * BKE_area_find_region_xy(const ScrArea *area, int regiontype, const int xy[2]) ATTR_NONNULL(3)
Definition screen.cc:1053
MovieTrackingMarker * BKE_tracking_marker_get(MovieTrackingTrack *track, int framenr)
Definition tracking.cc:1363
float ED_view3d_calc_depth_for_comparison(const RegionView3D *rv3d, const float co[3])
static wmOperatorStatus annotation_draw_modal(bContext *C, wmOperator *op, const wmEvent *event)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
struct MDeformWeight * dw
MovieTrackingTrack * active_track
View3DCursor cursor
struct RenderData r
struct ToolSettings * toolsettings
ListBaseT< SpaceLink > spacedata
eSpaceSeq_SequencerPreviewOverlay_Flag flag
eSpaceClip_Flag flag
eSpaceClip_GPencil_Source gpencil_src
eSpaceImage_Flag flag
eSpaceNode_Flag flag
struct SequencerPreviewOverlay preview_overlay
eSpaceSeq_RegionType mainb
eGPencil_Flags gpencil_flags
eGPencil_Placement_Flags annotate_v3d_align
eGPencil_Placement_Flags gpencil_v2d_align
eView3D_Flag2 flag2
ListBaseT< bGPDstroke > strokes
eGPDstroke_Arrowstyle arrow_end_style
eGPDstroke_Arrowstyle arrow_start_style
ListBaseT< struct ScrArea > areabase
eGPencil_Placement_Flags * align_flag
GP_SpaceConversion gsc
eGPencil_PaintFlags flags
eGPencil_PaintStatus status
eGPencil_PaintModes paintmode
wmTabletData tablet
Definition WM_types.hh:846
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
#define ISMOUSE_BUTTON(event_type)
#define ISMOUSE_MOTION(event_type)
#define ISKEYBOARD(event_type)