Merging r50625 through r51896 from trunk into soc-2011-tomato
[blender-staging.git] / source / blender / editors / mask / mask_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/mask/mask_add.c
29  *  \ingroup edmask
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_math.h"
35
36 #include "BKE_context.h"
37 #include "BKE_depsgraph.h"
38 #include "BKE_mask.h"
39
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_mask_types.h"
43 #include "DNA_object_types.h"  /* SELECT */
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "ED_mask.h"  /* own include */
49 #include "ED_screen.h"
50
51 #include "RNA_access.h"
52 #include "RNA_define.h"
53
54 #include "mask_intern.h"  /* own include */
55
56
57 static int find_nearest_diff_point(const bContext *C, Mask *mask, const float normal_co[2], int threshold, int feather,
58                                    MaskLayer **masklay_r, MaskSpline **spline_r, MaskSplinePoint **point_r,
59                                    float *u_r, float tangent[2],
60                                    const short use_deform)
61 {
62         ScrArea *sa = CTX_wm_area(C);
63         ARegion *ar = CTX_wm_region(C);
64
65         MaskLayer *masklay, *point_masklay;
66         MaskSpline *point_spline;
67         MaskSplinePoint *point = NULL;
68         float dist = FLT_MAX, co[2];
69         int width, height;
70         float u;
71         float scalex, scaley;
72
73         ED_mask_get_size(sa, &width, &height);
74         ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley);
75
76         co[0] = normal_co[0] * scalex;
77         co[1] = normal_co[1] * scaley;
78
79         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
80                 MaskSpline *spline;
81
82                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
83                         continue;
84                 }
85
86                 for (spline = masklay->splines.first; spline; spline = spline->next) {
87                         int i;
88                         MaskSplinePoint *cur_point;
89
90                         for (i = 0, cur_point = use_deform ? spline->points_deform : spline->points;
91                              i < spline->tot_point;
92                              i++, cur_point++)
93                         {
94                                 float *diff_points;
95                                 unsigned int tot_diff_point;
96
97                                 diff_points = BKE_mask_point_segment_diff_with_resolution(spline, cur_point, width, height,
98                                                                                           &tot_diff_point);
99
100                                 if (diff_points) {
101                                         int j, tot_point;
102                                         unsigned int tot_feather_point;
103                                         float *feather_points = NULL, *points;
104
105                                         if (feather) {
106                                                 feather_points = BKE_mask_point_segment_feather_diff_with_resolution(spline, cur_point,
107                                                                                                                      width, height,
108                                                                                                                      &tot_feather_point);
109
110                                                 points = feather_points;
111                                                 tot_point = tot_feather_point;
112                                         }
113                                         else {
114                                                 points = diff_points;
115                                                 tot_point = tot_diff_point;
116                                         }
117
118                                         for (j = 0; j < tot_point - 1; j++) {
119                                                 float cur_dist, a[2], b[2];
120
121                                                 a[0] = points[2 * j] * scalex;
122                                                 a[1] = points[2 * j + 1] * scaley;
123
124                                                 b[0] = points[2 * j + 2] * scalex;
125                                                 b[1] = points[2 * j + 3] * scaley;
126
127                                                 cur_dist = dist_to_line_segment_v2(co, a, b);
128
129                                                 if (cur_dist < dist) {
130                                                         if (tangent)
131                                                                 sub_v2_v2v2(tangent, &diff_points[2 * j + 2], &diff_points[2 * j]);
132
133                                                         point_masklay = masklay;
134                                                         point_spline = spline;
135                                                         point = use_deform ? &spline->points[(cur_point - spline->points_deform)] : cur_point;
136                                                         dist = cur_dist;
137                                                         u = (float)j / tot_point;
138
139                                                 }
140                                         }
141
142                                         if (feather_points)
143                                                 MEM_freeN(feather_points);
144
145                                         MEM_freeN(diff_points);
146                                 }
147                         }
148                 }
149         }
150
151         if (point && dist < threshold) {
152                 if (masklay_r)
153                         *masklay_r = point_masklay;
154
155                 if (spline_r)
156                         *spline_r = point_spline;
157
158                 if (point_r)
159                         *point_r = point;
160
161                 if (u_r) {
162                         u = BKE_mask_spline_project_co(point_spline, point, u, normal_co, MASK_PROJ_ANY);
163
164                         *u_r = u;
165                 }
166
167                 return TRUE;
168         }
169
170         if (masklay_r)
171                 *masklay_r = NULL;
172
173         if (spline_r)
174                 *spline_r = NULL;
175
176         if (point_r)
177                 *point_r = NULL;
178
179         return FALSE;
180 }
181
182 /******************** add vertex *********************/
183
184 static void setup_vertex_point(Mask *mask, MaskSpline *spline, MaskSplinePoint *new_point,
185                                const float point_co[2], const float tangent[2], const float u,
186                                MaskSplinePoint *reference_point, const short reference_adjacent,
187                                const float view_zoom)
188 {
189         MaskSplinePoint *prev_point = NULL;
190         MaskSplinePoint *next_point = NULL;
191         BezTriple *bezt;
192         float co[3];
193         const float len = 10.0; /* default length of handle in pixel space */
194
195         copy_v2_v2(co, point_co);
196         co[2] = 0.0f;
197
198         /* point coordinate */
199         bezt = &new_point->bezt;
200
201         bezt->h1 = bezt->h2 = HD_ALIGN;
202
203         if (reference_point) {
204                 bezt->h1 = bezt->h2 = MAX2(reference_point->bezt.h2, reference_point->bezt.h1);
205         }
206         else if (reference_adjacent) {
207                 if (spline->tot_point != 1) {
208                         int index = (int)(new_point - spline->points);
209                         prev_point = &spline->points[(index - 1) % spline->tot_point];
210                         next_point = &spline->points[(index + 1) % spline->tot_point];
211
212                         bezt->h1 = bezt->h2 = MAX2(prev_point->bezt.h2, next_point->bezt.h1);
213
214                         /* note, we may want to copy other attributes later, radius? pressure? color? */
215                 }
216         }
217
218         copy_v3_v3(bezt->vec[0], co);
219         copy_v3_v3(bezt->vec[1], co);
220         copy_v3_v3(bezt->vec[2], co);
221
222         /* initial offset for handles */
223         if (spline->tot_point == 1) {
224                 /* first point of splien is aligned horizontally */
225                 bezt->vec[0][0] -= len * view_zoom;
226                 bezt->vec[2][0] += len * view_zoom;
227         }
228         else if (tangent) {
229                 float vec[2];
230
231                 copy_v2_v2(vec, tangent);
232
233                 mul_v2_fl(vec, len);
234
235                 sub_v2_v2(bezt->vec[0], vec);
236                 add_v2_v2(bezt->vec[2], vec);
237
238                 if (reference_adjacent) {
239                         BKE_mask_calc_handle_adjacent_interp(spline, new_point, u);
240                 }
241         }
242         else {
243
244                 /* calculating auto handles works much nicer */
245 #if 0
246                 /* next points are aligning in the direction of previous/next point */
247                 MaskSplinePoint *point;
248                 float v1[2], v2[2], vec[2];
249                 float dir = 1.0f;
250
251                 if (new_point == spline->points) {
252                         point = new_point + 1;
253                         dir = -1.0f;
254                 }
255                 else
256                         point = new_point - 1;
257
258                 if (spline->tot_point < 3) {
259                         v1[0] = point->bezt.vec[1][0] * width;
260                         v1[1] = point->bezt.vec[1][1] * height;
261
262                         v2[0] = new_point->bezt.vec[1][0] * width;
263                         v2[1] = new_point->bezt.vec[1][1] * height;
264                 }
265                 else {
266                         if (new_point == spline->points) {
267                                 v1[0] = spline->points[1].bezt.vec[1][0] * width;
268                                 v1[1] = spline->points[1].bezt.vec[1][1] * height;
269
270                                 v2[0] = spline->points[spline->tot_point - 1].bezt.vec[1][0] * width;
271                                 v2[1] = spline->points[spline->tot_point - 1].bezt.vec[1][1] * height;
272                         }
273                         else {
274                                 v1[0] = spline->points[0].bezt.vec[1][0] * width;
275                                 v1[1] = spline->points[0].bezt.vec[1][1] * height;
276
277                                 v2[0] = spline->points[spline->tot_point - 2].bezt.vec[1][0] * width;
278                                 v2[1] = spline->points[spline->tot_point - 2].bezt.vec[1][1] * height;
279                         }
280                 }
281
282                 sub_v2_v2v2(vec, v1, v2);
283                 mul_v2_fl(vec, len * dir / len_v2(vec));
284
285                 vec[0] /= width;
286                 vec[1] /= height;
287
288                 add_v2_v2(bezt->vec[0], vec);
289                 sub_v2_v2(bezt->vec[2], vec);
290 #else
291                 BKE_mask_calc_handle_point_auto(spline, new_point, TRUE);
292                 BKE_mask_calc_handle_adjacent_interp(spline, new_point, u);
293
294 #endif
295         }
296
297         BKE_mask_parent_init(&new_point->parent);
298
299         /* select new point */
300         MASKPOINT_SEL_ALL(new_point);
301         ED_mask_select_flush_all(mask);
302 }
303
304
305 /* **** add extrude vertex **** */
306
307 static void finSelectedSplinePoint(MaskLayer *masklay, MaskSpline **spline, MaskSplinePoint **point, short check_active)
308 {
309         MaskSpline *cur_spline = masklay->splines.first;
310
311         *spline = NULL;
312         *point = NULL;
313
314         if (check_active) {
315                 /* TODO, having an active point but no active spline is possible, why? */
316                 if (masklay->act_spline && masklay->act_point && MASKPOINT_ISSEL_ANY(masklay->act_point)) {
317                         *spline = masklay->act_spline;
318                         *point = masklay->act_point;
319                         return;
320                 }
321         }
322
323         while (cur_spline) {
324                 int i;
325
326                 for (i = 0; i < cur_spline->tot_point; i++) {
327                         MaskSplinePoint *cur_point = &cur_spline->points[i];
328
329                         if (MASKPOINT_ISSEL_ANY(cur_point)) {
330                                 if (*spline != NULL && *spline != cur_spline) {
331                                         *spline = NULL;
332                                         *point = NULL;
333                                         return;
334                                 }
335                                 else if (*point) {
336                                         *point = NULL;
337                                 }
338                                 else {
339                                         *spline = cur_spline;
340                                         *point = cur_point;
341                                 }
342                         }
343                 }
344
345                 cur_spline = cur_spline->next;
346         }
347 }
348
349 /* **** add subdivide vertex **** */
350
351 static void mask_spline_add_point_at_index(MaskSpline *spline, int point_index)
352 {
353         MaskSplinePoint *new_point_array;
354
355         new_point_array = MEM_callocN(sizeof(MaskSplinePoint) * (spline->tot_point + 1), "add mask vert points");
356
357         memcpy(new_point_array, spline->points, sizeof(MaskSplinePoint) * (point_index + 1));
358         memcpy(new_point_array + point_index + 2, spline->points + point_index + 1,
359                sizeof(MaskSplinePoint) * (spline->tot_point - point_index - 1));
360
361         MEM_freeN(spline->points);
362         spline->points = new_point_array;
363         spline->tot_point++;
364 }
365
366 static int add_vertex_subdivide(const bContext *C, Mask *mask, const float co[2])
367 {
368         MaskLayer *masklay;
369         MaskSpline *spline;
370         MaskSplinePoint *point = NULL;
371         const float threshold = 9;
372         float tangent[2];
373         float u;
374
375         if (find_nearest_diff_point(C, mask, co, threshold, FALSE, &masklay, &spline, &point, &u, tangent, TRUE)) {
376                 MaskSplinePoint *new_point;
377                 int point_index = point - spline->points;
378
379                 ED_mask_select_toggle_all(mask, SEL_DESELECT);
380
381                 mask_spline_add_point_at_index(spline, point_index);
382
383                 new_point = &spline->points[point_index + 1];
384
385                 setup_vertex_point(mask, spline, new_point, co, tangent, u, NULL, TRUE, 1.0f);
386
387                 /* TODO - we could pass the spline! */
388                 BKE_mask_layer_shape_changed_add(masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index + 1, TRUE, TRUE);
389
390                 masklay->act_point = new_point;
391
392                 WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
393
394                 return TRUE;
395         }
396
397         return FALSE;
398 }
399
400 static int add_vertex_extrude(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2])
401 {
402         MaskSpline *spline;
403         MaskSplinePoint *point;
404         MaskSplinePoint *new_point = NULL, *ref_point = NULL;
405
406         /* check on which side we want to add the point */
407         int point_index;
408         float tangent_point[2];
409         float tangent_co[2];
410         int do_cyclic_correct = FALSE;
411         int do_recalc_src = FALSE;  /* when extruding from endpoints only */
412         int do_prev;                /* use prev point rather then next?? */
413
414         if (!masklay) {
415                 return FALSE;
416         }
417         else {
418                 finSelectedSplinePoint(masklay, &spline, &point, TRUE);
419         }
420
421         ED_mask_select_toggle_all(mask, SEL_DESELECT);
422
423         point_index = (point - spline->points);
424
425         MASKPOINT_DESEL_ALL(point);
426
427         if ((spline->flag & MASK_SPLINE_CYCLIC) ||
428             (point_index > 0 && point_index != spline->tot_point - 1))
429         {
430                 BKE_mask_calc_tangent_polyline(spline, point, tangent_point);
431                 sub_v2_v2v2(tangent_co, co, point->bezt.vec[1]);
432
433                 if (dot_v2v2(tangent_point, tangent_co) < 0.0f) {
434                         do_prev = TRUE;
435                 }
436                 else {
437                         do_prev = FALSE;
438                 }
439         }
440         else if (((spline->flag & MASK_SPLINE_CYCLIC) == 0) && (point_index == 0)) {
441                 do_prev = TRUE;
442                 do_recalc_src = TRUE;
443         }
444         else if (((spline->flag & MASK_SPLINE_CYCLIC) == 0) && (point_index == spline->tot_point - 1)) {
445                 do_prev = FALSE;
446                 do_recalc_src = TRUE;
447         }
448         else {
449                 do_prev = FALSE;  /* quiet warning */
450                 /* should never get here */
451                 BLI_assert(0);
452         }
453
454         /* use the point before the active one */
455         if (do_prev) {
456                 point_index--;
457                 if (point_index < 0) {
458                         point_index += spline->tot_point; /* wrap index */
459                         if ((spline->flag & MASK_SPLINE_CYCLIC) == 0) {
460                                 do_cyclic_correct = TRUE;
461                                 point_index = 0;
462                         }
463                 }
464         }
465
466 //              print_v2("", tangent_point);
467 //              printf("%d\n", point_index);
468
469         mask_spline_add_point_at_index(spline, point_index);
470
471         if (do_cyclic_correct) {
472                 ref_point = &spline->points[point_index + 1];
473                 new_point = &spline->points[point_index];
474                 *ref_point = *new_point;
475                 memset(new_point, 0, sizeof(*new_point));
476         }
477         else {
478                 ref_point = &spline->points[point_index];
479                 new_point = &spline->points[point_index + 1];
480         }
481
482         masklay->act_point = new_point;
483
484         setup_vertex_point(mask, spline, new_point, co, NULL, 0.5f, ref_point, FALSE, 1.0f);
485
486         if (masklay->splines_shapes.first) {
487                 point_index = (((int)(new_point - spline->points) + 0) % spline->tot_point);
488                 BKE_mask_layer_shape_changed_add(masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index, TRUE, TRUE);
489         }
490
491         if (do_recalc_src) {
492                 /* TODO, update keyframes in time */
493                 BKE_mask_calc_handle_point_auto(spline, ref_point, FALSE);
494         }
495
496         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
497
498         return TRUE;
499 }
500
501 static int add_vertex_new(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2])
502 {
503         MaskSpline *spline;
504         MaskSplinePoint *point;
505         MaskSplinePoint *new_point = NULL, *ref_point = NULL;
506         float view_zoom;
507
508         if (!masklay) {
509                 /* if there's no masklay currently operationg on, create new one */
510                 masklay = BKE_mask_layer_new(mask, "");
511                 mask->masklay_act = mask->masklay_tot - 1;
512                 spline = NULL;
513                 point = NULL;
514         }
515         else {
516                 finSelectedSplinePoint(masklay, &spline, &point, TRUE);
517         }
518
519         ED_mask_select_toggle_all(mask, SEL_DESELECT);
520
521         if (!spline) {
522                 /* no selected splines in active masklay, create new spline */
523                 spline = BKE_mask_spline_add(masklay);
524         }
525
526         masklay->act_spline = spline;
527         new_point = spline->points;
528
529         masklay->act_point = new_point;
530
531         {
532                 ScrArea *sa = CTX_wm_area(C);
533                 ARegion *ar = CTX_wm_region(C);
534
535                 float zoom_x, zoom_y;
536                 /* calc view zoom in a simplistic way */
537                 ED_mask_zoom(sa, ar, &zoom_x, &zoom_y);
538
539                 view_zoom = zoom_x + zoom_y / 2.0f;
540                 view_zoom = 1.0f / view_zoom;
541
542                 /* arbitrary but gives good results */
543                 view_zoom /= 500.0f;
544         }
545
546         setup_vertex_point(mask, spline, new_point, co, NULL, 0.5f, ref_point, FALSE, view_zoom);
547
548         {
549                 int point_index = (((int)(new_point - spline->points) + 0) % spline->tot_point);
550                 BKE_mask_layer_shape_changed_add(masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index, TRUE, TRUE);
551         }
552
553         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
554
555         return TRUE;
556 }
557
558 static int add_vertex_exec(bContext *C, wmOperator *op)
559 {
560         Scene *scene = CTX_data_scene(C);
561         Mask *mask = CTX_data_edit_mask(C);
562         MaskLayer *masklay;
563
564         float co[2];
565
566         if (mask == NULL) {
567                 /* if there's no active mask, create one */
568                 mask = ED_mask_new(C, NULL);
569         }
570
571         masklay = BKE_mask_layer_active(mask);
572
573         if (masklay && masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
574                 masklay = NULL;
575         }
576
577         RNA_float_get_array(op->ptr, "location", co);
578
579         /* TODO, having an active point but no active spline is possible, why? */
580         if (masklay && masklay->act_spline && masklay->act_point && MASKPOINT_ISSEL_ANY(masklay->act_point)) {
581
582                 /* cheap trick - double click for cyclic */
583                 MaskSpline *spline = masklay->act_spline;
584                 MaskSplinePoint *point = masklay->act_point;
585
586                 int is_sta = (point == spline->points);
587                 int is_end = (point == &spline->points[spline->tot_point - 1]);
588
589                 /* then check are we overlapping the mouse */
590                 if ((is_sta || is_end) && equals_v2v2(co, point->bezt.vec[1])) {
591                         if (spline->flag & MASK_SPLINE_CYCLIC) {
592                                 /* nothing to do */
593                                 return OPERATOR_CANCELLED;
594                         }
595                         else {
596                                 /* recalc the connecting point as well to make a nice even curve */
597                                 MaskSplinePoint *point_other = is_end ? spline->points : &spline->points[spline->tot_point - 1];
598                                 spline->flag |= MASK_SPLINE_CYCLIC;
599
600                                 /* TODO, update keyframes in time */
601                                 BKE_mask_calc_handle_point_auto(spline, point, FALSE);
602                                 BKE_mask_calc_handle_point_auto(spline, point_other, FALSE);
603
604                                 /* TODO: only update this spline */
605                                 BKE_mask_update_display(mask, CFRA);
606
607                                 WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
608                                 return OPERATOR_FINISHED;
609                         }
610                 }
611
612                 if (!add_vertex_subdivide(C, mask, co)) {
613                         if (!add_vertex_extrude(C, mask, masklay, co)) {
614                                 return OPERATOR_CANCELLED;
615                         }
616                 }
617         }
618         else {
619                 if (!add_vertex_subdivide(C, mask, co)) {
620                         if (!add_vertex_new(C, mask, masklay, co)) {
621                                 return OPERATOR_CANCELLED;
622                         }
623                 }
624         }
625
626         /* TODO: only update this spline */
627         BKE_mask_update_display(mask, CFRA);
628
629         return OPERATOR_FINISHED;
630 }
631
632 static int add_vertex_invoke(bContext *C, wmOperator *op, wmEvent *event)
633 {
634         ScrArea *sa = CTX_wm_area(C);
635         ARegion *ar = CTX_wm_region(C);
636
637         float co[2];
638
639         ED_mask_mouse_pos(sa, ar, event->mval, co);
640
641         RNA_float_set_array(op->ptr, "location", co);
642
643         return add_vertex_exec(C, op);
644 }
645
646 void MASK_OT_add_vertex(wmOperatorType *ot)
647 {
648         /* identifiers */
649         ot->name = "Add Vertex";
650         ot->description = "Add vertex to active spline";
651         ot->idname = "MASK_OT_add_vertex";
652
653         /* api callbacks */
654         ot->exec = add_vertex_exec;
655         ot->invoke = add_vertex_invoke;
656         ot->poll = ED_operator_mask;
657
658         /* flags */
659         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
660
661         /* properties */
662         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
663                              "Location", "Location of vertex in normalized space", -1.0f, 1.0f);
664 }
665
666 /******************** add feather vertex *********************/
667
668 static int add_feather_vertex_exec(bContext *C, wmOperator *op)
669 {
670         Mask *mask = CTX_data_edit_mask(C);
671         MaskLayer *masklay;
672         MaskSpline *spline;
673         MaskSplinePoint *point = NULL;
674         const float threshold = 9;
675         float co[2], u;
676
677         RNA_float_get_array(op->ptr, "location", co);
678
679         point = ED_mask_point_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL);
680         if (point)
681                 return OPERATOR_FINISHED;
682
683         if (find_nearest_diff_point(C, mask, co, threshold, TRUE, &masklay, &spline, &point, &u, NULL, TRUE)) {
684                 Scene *scene = CTX_data_scene(C);
685                 float w = BKE_mask_point_weight(spline, point, u);
686                 float weight_scalar = BKE_mask_point_weight_scalar(spline, point, u);
687
688                 if (weight_scalar != 0.0f) {
689                         w = w / weight_scalar;
690                 }
691
692                 BKE_mask_point_add_uw(point, u, w);
693
694                 BKE_mask_update_display(mask, scene->r.cfra);
695
696                 WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
697
698                 DAG_id_tag_update(&mask->id, 0);
699
700                 return OPERATOR_FINISHED;
701         }
702
703         return OPERATOR_CANCELLED;
704 }
705
706 static int add_feather_vertex_invoke(bContext *C, wmOperator *op, wmEvent *event)
707 {
708         ScrArea *sa = CTX_wm_area(C);
709         ARegion *ar = CTX_wm_region(C);
710
711         float co[2];
712
713         ED_mask_mouse_pos(sa, ar, event->mval, co);
714
715         RNA_float_set_array(op->ptr, "location", co);
716
717         return add_feather_vertex_exec(C, op);
718 }
719
720 void MASK_OT_add_feather_vertex(wmOperatorType *ot)
721 {
722         /* identifiers */
723         ot->name = "Add feather Vertex";
724         ot->description = "Add vertex to feather";
725         ot->idname = "MASK_OT_add_feather_vertex";
726
727         /* api callbacks */
728         ot->exec = add_feather_vertex_exec;
729         ot->invoke = add_feather_vertex_invoke;
730         ot->poll = ED_maskedit_mask_poll;
731
732         /* flags */
733         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
734
735         /* properties */
736         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
737                              "Location", "Location of vertex in normalized space", -1.0f, 1.0f);
738 }