Merging r50522 through r50572 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / mask / mask_ops.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_ops.c
29  *  \ingroup edmask
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_listbase.h"
35 #include "BLI_math.h"
36
37 #include "BKE_context.h"
38 #include "BKE_depsgraph.h"
39 #include "BKE_mask.h"
40
41 #include "DNA_scene_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_clip.h"
49 #include "ED_image.h"
50 #include "ED_keyframing.h"
51 #include "ED_mask.h"
52 #include "ED_screen.h"
53
54 #include "RNA_access.h"
55 #include "RNA_define.h"
56
57 #include "mask_intern.h"  /* own include */
58
59 /******************** utility functions *********************/
60
61 MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, float normal_co[2], int threshold,
62                                             MaskLayer **masklay_r, MaskSpline **spline_r, int *is_handle_r,
63                                             float *score)
64 {
65         ScrArea *sa = CTX_wm_area(C);
66         ARegion *ar = CTX_wm_region(C);
67
68         MaskLayer *masklay;
69         MaskLayer *point_masklay = NULL;
70         MaskSpline *point_spline = NULL;
71         MaskSplinePoint *point = NULL;
72         float co[2];
73         float len = FLT_MAX, scalex, scaley;
74         int is_handle = FALSE, width, height;
75
76         ED_mask_get_size(sa, &width, &height);
77         ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley);
78
79         co[0] = normal_co[0] * scalex;
80         co[1] = normal_co[1] * scaley;
81
82         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
83                 MaskSpline *spline;
84
85                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
86                         continue;
87                 }
88
89                 for (spline = masklay->splines.first; spline; spline = spline->next) {
90                         MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
91
92                         int i;
93
94                         for (i = 0; i < spline->tot_point; i++) {
95                                 MaskSplinePoint *cur_point = &spline->points[i];
96                                 MaskSplinePoint *cur_point_deform = &points_array[i];
97                                 float cur_len, vec[2], handle[2];
98
99                                 vec[0] = cur_point_deform->bezt.vec[1][0] * scalex;
100                                 vec[1] = cur_point_deform->bezt.vec[1][1] * scaley;
101
102                                 if (BKE_mask_point_has_handle(cur_point)) {
103                                         BKE_mask_point_handle(cur_point_deform, handle);
104                                         handle[0] *= scalex;
105                                         handle[1] *= scaley;
106
107                                         cur_len = len_v2v2(co, handle);
108
109                                         if (cur_len < len) {
110                                                 point_masklay = masklay;
111                                                 point_spline = spline;
112                                                 point = cur_point;
113                                                 len = cur_len;
114                                                 is_handle = TRUE;
115                                         }
116                                 }
117
118                                 cur_len = len_v2v2(co, vec);
119
120                                 if (cur_len < len) {
121                                         point_spline = spline;
122                                         point_masklay = masklay;
123                                         point = cur_point;
124                                         len = cur_len;
125                                         is_handle = FALSE;
126                                 }
127                         }
128                 }
129         }
130
131         if (len < threshold) {
132                 if (masklay_r)
133                         *masklay_r = point_masklay;
134
135                 if (spline_r)
136                         *spline_r = point_spline;
137
138                 if (is_handle_r)
139                         *is_handle_r = is_handle;
140
141                 if (score)
142                         *score = len;
143
144                 return point;
145         }
146
147         if (masklay_r)
148                 *masklay_r = NULL;
149
150         if (spline_r)
151                 *spline_r = NULL;
152
153         if (is_handle_r)
154                 *is_handle_r = FALSE;
155
156         return NULL;
157 }
158
159 int ED_mask_feather_find_nearest(const bContext *C, Mask *mask, float normal_co[2], int threshold,
160                                  MaskLayer **masklay_r, MaskSpline **spline_r, MaskSplinePoint **point_r,
161                                  MaskSplinePointUW **uw_r, float *score)
162 {
163         ScrArea *sa = CTX_wm_area(C);
164         ARegion *ar = CTX_wm_region(C);
165
166         MaskLayer *masklay, *point_masklay = NULL;
167         MaskSpline *point_spline = NULL;
168         MaskSplinePoint *point = NULL;
169         MaskSplinePointUW *uw = NULL;
170         float len = FLT_MAX, co[2];
171         float scalex, scaley;
172         int width, height;
173
174         ED_mask_get_size(sa, &width, &height);
175         ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley);
176
177         co[0] = normal_co[0] * scalex;
178         co[1] = normal_co[1] * scaley;
179
180         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
181                 MaskSpline *spline;
182
183                 for (spline = masklay->splines.first; spline; spline = spline->next) {
184                         //MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
185
186                         int i, tot_feather_point;
187                         float (*feather_points)[2], (*fp)[2];
188
189                         if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
190                                 continue;
191                         }
192
193                         feather_points = fp = BKE_mask_spline_feather_points(spline, &tot_feather_point);
194
195                         for (i = 0; i < spline->tot_point; i++) {
196                                 int j;
197                                 MaskSplinePoint *cur_point = &spline->points[i];
198
199                                 for (j = 0; j < cur_point->tot_uw + 1; j++) {
200                                         float cur_len, vec[2];
201
202                                         vec[0] = (*fp)[0] * scalex;
203                                         vec[1] = (*fp)[1] * scaley;
204
205                                         cur_len = len_v2v2(vec, co);
206
207                                         if (point == NULL || cur_len < len) {
208                                                 if (j == 0)
209                                                         uw = NULL;
210                                                 else
211                                                         uw = &cur_point->uw[j - 1];
212
213                                                 point_masklay = masklay;
214                                                 point_spline = spline;
215                                                 point = cur_point;
216                                                 len = cur_len;
217                                         }
218
219                                         fp++;
220                                 }
221                         }
222
223                         MEM_freeN(feather_points);
224                 }
225         }
226
227         if (len < threshold) {
228                 if (masklay_r)
229                         *masklay_r = point_masklay;
230
231                 if (spline_r)
232                         *spline_r = point_spline;
233
234                 if (point_r)
235                         *point_r = point;
236
237                 if (uw_r)
238                         *uw_r = uw;
239
240                 if (score)
241                         *score = len;
242
243                 return TRUE;
244         }
245
246         if (masklay_r)
247                 *masklay_r = NULL;
248
249         if (spline_r)
250                 *spline_r = NULL;
251
252         if (point_r)
253                 *point_r = NULL;
254
255         return FALSE;
256 }
257
258
259 /******************** create new mask *********************/
260
261 static int mask_new_exec(bContext *C, wmOperator *op)
262 {
263         ScrArea *sa = CTX_wm_area(C);
264         Mask *mask;
265         char name[MAX_ID_NAME - 2];
266
267         RNA_string_get(op->ptr, "name", name);
268
269         mask = BKE_mask_new(name);
270
271         if (sa && sa->spacedata.first) {
272                 switch (sa->spacetype) {
273                         case SPACE_CLIP:
274                         {
275                                 SpaceClip *sc = sa->spacedata.first;
276                                 ED_space_clip_set_mask(C, sc, mask);
277                                 break;
278                         }
279                         case SPACE_SEQ:
280                         {
281                                 /* do nothing */
282                                 break;
283                         }
284                         case SPACE_IMAGE:
285                         {
286                                 SpaceImage *sima = sa->spacedata.first;
287                                 ED_space_image_set_mask(C, sima, mask);
288                                 break;
289                         }
290                 }
291         }
292
293         return OPERATOR_FINISHED;
294 }
295
296 void MASK_OT_new(wmOperatorType *ot)
297 {
298         /* identifiers */
299         ot->name = "New Mask";
300         ot->description = "Create new mask";
301         ot->idname = "MASK_OT_new";
302
303         /* flags */
304         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
305
306         /* api callbacks */
307         ot->exec = mask_new_exec;
308         ot->poll = ED_operator_mask;
309
310         /* properties */
311         RNA_def_string(ot->srna, "name", "", MAX_ID_NAME - 2, "Name", "Name of new mask");
312 }
313
314 /******************** create new masklay *********************/
315
316 static int masklay_new_exec(bContext *C, wmOperator *op)
317 {
318         Mask *mask = CTX_data_edit_mask(C);
319         char name[MAX_ID_NAME - 2];
320
321         RNA_string_get(op->ptr, "name", name);
322
323         BKE_mask_layer_new(mask, name);
324         mask->masklay_act = mask->masklay_tot - 1;
325
326         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
327
328         return OPERATOR_FINISHED;
329 }
330
331 void MASK_OT_layer_new(wmOperatorType *ot)
332 {
333         /* identifiers */
334         ot->name = "Add Mask Layer";
335         ot->description = "Add new mask layer for masking";
336         ot->idname = "MASK_OT_layer_new";
337
338         /* api callbacks */
339         ot->exec = masklay_new_exec;
340         ot->poll = ED_maskedit_poll;
341
342         /* flags */
343         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
344
345         /* properties */
346         RNA_def_string(ot->srna, "name", "", MAX_ID_NAME - 2, "Name", "Name of new mask layer");
347 }
348
349 /******************** remove mask layer *********************/
350
351 static int masklay_remove_exec(bContext *C, wmOperator *UNUSED(op))
352 {
353         Mask *mask = CTX_data_edit_mask(C);
354         MaskLayer *masklay = BKE_mask_layer_active(mask);
355
356         if (masklay) {
357                 BKE_mask_layer_remove(mask, masklay);
358
359                 WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
360         }
361
362         return OPERATOR_FINISHED;
363 }
364
365 void MASK_OT_layer_remove(wmOperatorType *ot)
366 {
367         /* identifiers */
368         ot->name = "Remove Mask Layer";
369         ot->description = "Remove mask layer";
370         ot->idname = "MASK_OT_layer_remove";
371
372         /* api callbacks */
373         ot->exec = masklay_remove_exec;
374         ot->poll = ED_maskedit_poll;
375
376         /* flags */
377         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
378 }
379
380 /******************** slide *********************/
381
382 enum {
383         SLIDE_ACTION_NONE    = 0,
384         SLIDE_ACTION_POINT   = 1,
385         SLIDE_ACTION_HANDLE  = 2,
386         SLIDE_ACTION_FEATHER = 3
387 };
388
389 typedef struct SlidePointData {
390         int action;
391
392         float co[2];
393         float vec[3][3];
394
395         Mask *mask;
396         MaskLayer *masklay;
397         MaskSpline *spline, *orig_spline;
398         MaskSplinePoint *point;
399         MaskSplinePointUW *uw;
400         float handle[2], no[2], feather[2];
401         int width, height;
402         float weight, weight_scalar;
403
404         short curvature_only, accurate;
405         short initial_feather, overall_feather;
406 } SlidePointData;
407
408 static int slide_point_check_initial_feather(MaskSpline *spline)
409 {
410         int i;
411
412         for (i = 0; i < spline->tot_point; i++) {
413                 MaskSplinePoint *point = &spline->points[i];
414
415                 if (point->bezt.weight != 0.0f)
416                         return FALSE;
417
418                 /* comment for now. if all bezt weights are zero - this is as good-as initial */
419 #if 0
420                 int j;
421                 for (j = 0; j < point->tot_uw; j++) {
422                         if (point->uw[j].w != 0.0f)
423                                 return FALSE;
424                 }
425 #endif
426         }
427
428         return TRUE;
429 }
430
431 static void *slide_point_customdata(bContext *C, wmOperator *op, wmEvent *event)
432 {
433         ScrArea *sa = CTX_wm_area(C);
434         ARegion *ar = CTX_wm_region(C);
435
436         Mask *mask = CTX_data_edit_mask(C);
437         SlidePointData *customdata = NULL;
438         MaskLayer *masklay, *cv_masklay, *feather_masklay;
439         MaskSpline *spline, *cv_spline, *feather_spline;
440         MaskSplinePoint *point, *cv_point, *feather_point;
441         MaskSplinePointUW *uw = NULL;
442         int is_handle = FALSE, width, height, action = SLIDE_ACTION_NONE;
443         int slide_feather = RNA_boolean_get(op->ptr, "slide_feather");
444         float co[2], cv_score, feather_score;
445         const float threshold = 19;
446
447         ED_mask_mouse_pos(sa, ar, event->mval, co);
448         ED_mask_get_size(sa, &width, &height);
449
450         cv_point = ED_mask_point_find_nearest(C, mask, co, threshold, &cv_masklay, &cv_spline, &is_handle, &cv_score);
451
452         if (ED_mask_feather_find_nearest(C, mask, co, threshold, &feather_masklay, &feather_spline, &feather_point, &uw, &feather_score)) {
453                 if (slide_feather || !cv_point || feather_score < cv_score) {
454                         action = SLIDE_ACTION_FEATHER;
455
456                         masklay = feather_masklay;
457                         spline = feather_spline;
458                         point = feather_point;
459                 }
460         }
461
462         if (cv_point && action == SLIDE_ACTION_NONE) {
463                 if (is_handle)
464                         action = SLIDE_ACTION_HANDLE;
465                 else
466                         action = SLIDE_ACTION_POINT;
467
468                 masklay = cv_masklay;
469                 spline = cv_spline;
470                 point = cv_point;
471         }
472
473         if (action != SLIDE_ACTION_NONE) {
474                 customdata = MEM_callocN(sizeof(SlidePointData), "mask slide point data");
475
476                 customdata->mask = mask;
477                 customdata->masklay = masklay;
478                 customdata->spline = spline;
479                 customdata->point = point;
480                 customdata->width = width;
481                 customdata->height = height;
482                 customdata->action = action;
483                 customdata->uw = uw;
484
485                 if (uw) {
486                         float co[2];
487                         float weight_scalar = BKE_mask_point_weight_scalar(spline, point, uw->u);
488
489                         customdata->weight = uw->w;
490                         customdata->weight_scalar = weight_scalar;
491                         BKE_mask_point_segment_co(spline, point, uw->u, co);
492                         BKE_mask_point_normal(spline, point, uw->u, customdata->no);
493
494                         madd_v2_v2v2fl(customdata->feather, co, customdata->no, uw->w * weight_scalar);
495                 }
496                 else {
497                         BezTriple *bezt = &point->bezt;
498
499                         customdata->weight = bezt->weight;
500                         customdata->weight_scalar = 1.0f;
501                         BKE_mask_point_normal(spline, point, 0.0f, customdata->no);
502
503                         madd_v2_v2v2fl(customdata->feather, bezt->vec[1], customdata->no, bezt->weight);
504                 }
505
506                 if (customdata->action == SLIDE_ACTION_FEATHER)
507                         customdata->initial_feather = slide_point_check_initial_feather(spline);
508
509                 copy_m3_m3(customdata->vec, point->bezt.vec);
510                 if (BKE_mask_point_has_handle(point))
511                         BKE_mask_point_handle(point, customdata->handle);
512                 ED_mask_mouse_pos(sa, ar, event->mval, customdata->co);
513         }
514
515         return customdata;
516 }
517
518 static int slide_point_invoke(bContext *C, wmOperator *op, wmEvent *event)
519 {
520         SlidePointData *slidedata = slide_point_customdata(C, op, event);
521
522         if (slidedata) {
523                 Mask *mask = CTX_data_edit_mask(C);
524
525                 op->customdata = slidedata;
526
527                 WM_event_add_modal_handler(C, op);
528
529 #if 0
530                 if (slidedata->uw) {
531                         if ((slidedata->uw->flag & SELECT) == 0) {
532                                 ED_mask_select_toggle_all(mask, SEL_DESELECT);
533
534                                 slidedata->uw->flag |= SELECT;
535
536                                 ED_mask_select_flush_all(mask);
537                         }
538                 }
539                 else if (!MASKPOINT_ISSEL_ANY(slidedata->point)) {
540                         ED_mask_select_toggle_all(mask, SEL_DESELECT);
541
542                         BKE_mask_point_select_set(slidedata->point, TRUE);
543
544                         ED_mask_select_flush_all(mask);
545                 }
546 #endif
547
548                 slidedata->masklay->act_spline = slidedata->spline;
549                 slidedata->masklay->act_point = slidedata->point;
550
551                 WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask);
552
553                 return OPERATOR_RUNNING_MODAL;
554         }
555
556         return OPERATOR_PASS_THROUGH;
557 }
558
559 static void slide_point_delta_all_feather(SlidePointData *data, float delta)
560 {
561         int i;
562
563         for (i = 0; i < data->spline->tot_point; i++) {
564                 MaskSplinePoint *point = &data->spline->points[i];
565                 MaskSplinePoint *orig_point = &data->orig_spline->points[i];
566
567                 point->bezt.weight = orig_point->bezt.weight + delta;
568                 if (point->bezt.weight < 0.0f)
569                         point->bezt.weight = 0.0f;
570
571                 /* not needed anymore */
572 #if 0
573                 int j;
574                 for (j = 0; j < point->tot_uw; j++) {
575                         point->uw[j].w = orig_point->uw[j].w + delta;
576                         if (point->uw[j].w < 0.0f)
577                                 point->uw[j].w = 0.0f;
578                 }
579 #endif
580         }
581 }
582
583 static void slide_point_restore_spline(SlidePointData *data)
584 {
585         int i;
586
587         for (i = 0; i < data->spline->tot_point; i++) {
588                 MaskSplinePoint *point = &data->spline->points[i];
589                 MaskSplinePoint *orig_point = &data->orig_spline->points[i];
590                 int j;
591
592                 point->bezt = orig_point->bezt;
593
594                 for (j = 0; j < point->tot_uw; j++)
595                         point->uw[j] = orig_point->uw[j];
596         }
597 }
598
599 static void cancel_slide_point(SlidePointData *data)
600 {
601         /* cancel sliding */
602
603         if (data->orig_spline) {
604                 slide_point_restore_spline(data);
605         }
606         else {
607                 if (data->action == SLIDE_ACTION_FEATHER) {
608                         if (data->uw)
609                                 data->uw->w = data->weight;
610                         else
611                                 data->point->bezt.weight = data->weight;
612                 }
613                 else {
614                         copy_m3_m3(data->point->bezt.vec, data->vec);
615                 }
616         }
617 }
618
619 static void free_slide_point_data(SlidePointData *data)
620 {
621         if (data->orig_spline)
622                 BKE_mask_spline_free(data->orig_spline);
623
624         MEM_freeN(data);
625 }
626
627 static int slide_point_modal(bContext *C, wmOperator *op, wmEvent *event)
628 {
629         SlidePointData *data = (SlidePointData *)op->customdata;
630         BezTriple *bezt = &data->point->bezt;
631         float co[2], dco[2];
632
633         switch (event->type) {
634                 case LEFTALTKEY:
635                 case RIGHTALTKEY:
636                 case LEFTSHIFTKEY:
637                 case RIGHTSHIFTKEY:
638                         if (ELEM(event->type, LEFTALTKEY, RIGHTALTKEY)) {
639                                 if (data->action == SLIDE_ACTION_FEATHER)
640                                         data->overall_feather = (event->val == KM_PRESS);
641                                 else
642                                         data->curvature_only = (event->val == KM_PRESS);
643                         }
644
645                         if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY))
646                                 data->accurate = (event->val == KM_PRESS);
647
648                 /* no break! update CV position */
649
650                 case MOUSEMOVE:
651                 {
652                         ScrArea *sa = CTX_wm_area(C);
653                         ARegion *ar = CTX_wm_region(C);
654
655                         ED_mask_mouse_pos(sa, ar, event->mval, co);
656                         sub_v2_v2v2(dco, co, data->co);
657
658                         if (data->action == SLIDE_ACTION_HANDLE) {
659                                 float delta[2], offco[2];
660
661                                 sub_v2_v2v2(delta, data->handle, data->co);
662
663                                 sub_v2_v2v2(offco, co, data->co);
664                                 if (data->accurate)
665                                         mul_v2_fl(offco, 0.2f);
666                                 add_v2_v2(offco, data->co);
667                                 add_v2_v2(offco, delta);
668
669                                 BKE_mask_point_set_handle(data->point, offco, data->curvature_only, data->handle, data->vec);
670                         }
671                         else if (data->action == SLIDE_ACTION_POINT) {
672                                 float delta[2];
673
674                                 copy_v2_v2(delta, dco);
675                                 if (data->accurate)
676                                         mul_v2_fl(delta, 0.2f);
677
678                                 add_v2_v2v2(bezt->vec[0], data->vec[0], delta);
679                                 add_v2_v2v2(bezt->vec[1], data->vec[1], delta);
680                                 add_v2_v2v2(bezt->vec[2], data->vec[2], delta);
681                         }
682                         else if (data->action == SLIDE_ACTION_FEATHER) {
683                                 float vec[2], no[2], p[2], c[2], w, offco[2];
684                                 float *weight = NULL;
685                                 float weight_scalar = 1.0f;
686                                 int overall_feather = data->overall_feather || data->initial_feather;
687
688                                 add_v2_v2v2(offco, data->feather, dco);
689
690                                 if (data->uw) {
691                                         /* project on both sides and find the closest one,
692                                          * prevents flickering when projecting onto both sides can happen */
693                                         const float u_pos = BKE_mask_spline_project_co(data->spline, data->point,
694                                                                                        data->uw->u, offco, MASK_PROJ_NEG);
695                                         const float u_neg = BKE_mask_spline_project_co(data->spline, data->point,
696                                                                                        data->uw->u, offco, MASK_PROJ_POS);
697                                         float dist_pos = FLT_MAX;
698                                         float dist_neg = FLT_MAX;
699                                         float co_pos[2];
700                                         float co_neg[2];
701                                         float u;
702
703                                         if (u_pos > 0.0f && u_pos < 1.0f) {
704                                                 BKE_mask_point_segment_co(data->spline, data->point, u_pos, co_pos);
705                                                 dist_pos = len_squared_v2v2(offco, co_pos);
706                                         }
707
708                                         if (u_neg > 0.0f && u_neg < 1.0f) {
709                                                 BKE_mask_point_segment_co(data->spline, data->point, u_neg, co_neg);
710                                                 dist_neg = len_squared_v2v2(offco, co_neg);
711                                         }
712
713                                         u = dist_pos < dist_neg ? u_pos : u_neg;
714
715                                         if (u > 0.0f && u < 1.0f) {
716                                                 data->uw->u = u;
717
718                                                 data->uw = BKE_mask_point_sort_uw(data->point, data->uw);
719                                                 weight = &data->uw->w;
720                                                 weight_scalar = BKE_mask_point_weight_scalar(data->spline, data->point, u);
721                                                 if (weight_scalar != 0.0f) {
722                                                         weight_scalar = 1.0f / weight_scalar;
723                                                 }
724
725                                                 BKE_mask_point_normal(data->spline, data->point, data->uw->u, no);
726                                                 BKE_mask_point_segment_co(data->spline, data->point, data->uw->u, p);
727                                         }
728                                 }
729                                 else {
730                                         weight = &bezt->weight;
731                                         /* weight_scalar = 1.0f; keep as is */
732                                         copy_v2_v2(no, data->no);
733                                         copy_v2_v2(p, bezt->vec[1]);
734                                 }
735
736                                 if (weight) {
737                                         sub_v2_v2v2(c, offco, p);
738                                         project_v2_v2v2(vec, c, no);
739
740                                         w = len_v2(vec);
741
742                                         if (overall_feather) {
743                                                 float delta;
744
745                                                 if (dot_v2v2(no, vec) <= 0.0f)
746                                                         w = -w;
747
748                                                 delta = w - data->weight * data->weight_scalar;
749
750                                                 if (data->orig_spline == NULL) {
751                                                         /* restore weight for currently sliding point, so orig_spline would be created
752                                                          * with original weights used
753                                                          */
754                                                         *weight = data->weight;
755
756                                                         data->orig_spline = BKE_mask_spline_copy(data->spline);
757                                                 }
758
759                                                 slide_point_delta_all_feather(data, delta);
760                                         }
761                                         else {
762                                                 if (dot_v2v2(no, vec) <= 0.0f)
763                                                         w = 0.0f;
764
765                                                 if (data->orig_spline) {
766                                                         /* restore possible overall feather changes */
767                                                         slide_point_restore_spline(data);
768
769                                                         BKE_mask_spline_free(data->orig_spline);
770                                                         data->orig_spline = NULL;
771                                                 }
772
773                                                 if (weight_scalar != 0.0f) {
774                                                         *weight = w * weight_scalar;
775                                                 }
776                                         }
777                                 }
778                         }
779
780                         WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask);
781                         DAG_id_tag_update(&data->mask->id, 0);
782
783                         break;
784                 }
785
786                 case LEFTMOUSE:
787                         if (event->val == KM_RELEASE) {
788                                 Scene *scene = CTX_data_scene(C);
789
790                                 /* dont key sliding feather uw's */
791                                 if ((data->action == SLIDE_ACTION_FEATHER && data->uw) == FALSE) {
792                                         if (IS_AUTOKEY_ON(scene)) {
793                                                 ED_mask_layer_shape_auto_key(data->masklay, CFRA);
794                                         }
795                                 }
796
797                                 WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask);
798                                 DAG_id_tag_update(&data->mask->id, 0);
799
800                                 free_slide_point_data(op->customdata); /* keep this last! */
801                                 return OPERATOR_FINISHED;
802                         }
803
804                         break;
805
806                 case ESCKEY:
807                         cancel_slide_point(op->customdata);
808
809                         WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask);
810                         DAG_id_tag_update(&data->mask->id, 0);
811
812                         free_slide_point_data(op->customdata); /* keep this last! */
813                         return OPERATOR_CANCELLED;
814         }
815
816         return OPERATOR_RUNNING_MODAL;
817 }
818
819 void MASK_OT_slide_point(wmOperatorType *ot)
820 {
821         /* identifiers */
822         ot->name = "Slide Point";
823         ot->description = "Slide control points";
824         ot->idname = "MASK_OT_slide_point";
825
826         /* api callbacks */
827         ot->invoke = slide_point_invoke;
828         ot->modal = slide_point_modal;
829         ot->poll = ED_maskedit_mask_poll;
830
831         /* flags */
832         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
833
834         RNA_def_boolean(ot->srna, "slide_feather", 0, "Slide Feather", "First try to slide feather instead of vertex");
835 }
836
837 /******************** toggle cyclic *********************/
838
839 static int cyclic_toggle_exec(bContext *C, wmOperator *UNUSED(op))
840 {
841         Mask *mask = CTX_data_edit_mask(C);
842         MaskLayer *masklay;
843
844         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
845                 MaskSpline *spline;
846
847                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
848                         continue;
849                 }
850
851                 for (spline = masklay->splines.first; spline; spline = spline->next) {
852                         if (ED_mask_spline_select_check(spline)) {
853                                 spline->flag ^= MASK_SPLINE_CYCLIC;
854                         }
855                 }
856         }
857
858         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
859
860         return OPERATOR_FINISHED;
861 }
862
863 void MASK_OT_cyclic_toggle(wmOperatorType *ot)
864 {
865         /* identifiers */
866         ot->name = "Toggle Cyclic";
867         ot->description = "Toggle cyclic for selected splines";
868         ot->idname = "MASK_OT_cyclic_toggle";
869
870         /* api callbacks */
871         ot->exec = cyclic_toggle_exec;
872         ot->poll = ED_maskedit_mask_poll;
873
874         /* flags */
875         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
876 }
877
878 /******************** delete *********************/
879
880 static void delete_feather_points(MaskSplinePoint *point)
881 {
882         int i, count = 0;
883
884         if (!point->tot_uw)
885                 return;
886
887         for (i = 0; i < point->tot_uw; i++) {
888                 if ((point->uw[i].flag & SELECT) == 0)
889                         count++;
890         }
891
892         if (count == 0) {
893                 MEM_freeN(point->uw);
894                 point->uw = NULL;
895                 point->tot_uw = 0;
896         }
897         else {
898                 MaskSplinePointUW *new_uw;
899                 int j = 0;
900
901                 new_uw = MEM_callocN(count * sizeof(MaskSplinePointUW), "new mask uw points");
902
903                 for (i = 0; i < point->tot_uw; i++) {
904                         if ((point->uw[i].flag & SELECT) == 0) {
905                                 new_uw[j++] = point->uw[i];
906                         }
907                 }
908
909                 MEM_freeN(point->uw);
910
911                 point->uw = new_uw;
912                 point->tot_uw = count;
913         }
914 }
915
916 static int delete_exec(bContext *C, wmOperator *UNUSED(op))
917 {
918         Scene *scene = CTX_data_scene(C);
919         Mask *mask = CTX_data_edit_mask(C);
920         MaskLayer *masklay;
921
922         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
923                 MaskSpline *spline;
924                 int mask_layer_shape_ofs = 0;
925
926                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
927                         continue;
928                 }
929
930                 spline = masklay->splines.first;
931
932                 while (spline) {
933                         const int tot_point_orig = spline->tot_point;
934                         int i, count = 0;
935                         MaskSpline *next_spline = spline->next;
936
937                         /* count unselected points */
938                         for (i = 0; i < spline->tot_point; i++) {
939                                 MaskSplinePoint *point = &spline->points[i];
940
941                                 if (!MASKPOINT_ISSEL_ANY(point))
942                                         count++;
943                         }
944
945                         if (count == 0) {
946
947                                 /* delete the whole spline */
948                                 BLI_remlink(&masklay->splines, spline);
949                                 BKE_mask_spline_free(spline);
950
951                                 if (spline == masklay->act_spline) {
952                                         masklay->act_spline = NULL;
953                                         masklay->act_point = NULL;
954                                 }
955
956                                 BKE_mask_layer_shape_changed_remove(masklay, mask_layer_shape_ofs, tot_point_orig);
957                         }
958                         else {
959                                 MaskSplinePoint *new_points;
960                                 int j;
961
962                                 new_points = MEM_callocN(count * sizeof(MaskSplinePoint), "deleteMaskPoints");
963
964                                 for (i = 0, j = 0; i < tot_point_orig; i++) {
965                                         MaskSplinePoint *point = &spline->points[i];
966
967                                         if (!MASKPOINT_ISSEL_ANY(point)) {
968                                                 if (point == masklay->act_point)
969                                                         masklay->act_point = &new_points[j];
970
971                                                 delete_feather_points(point);
972
973                                                 new_points[j] = *point;
974                                                 j++;
975                                         }
976                                         else {
977                                                 if (point == masklay->act_point)
978                                                         masklay->act_point = NULL;
979
980                                                 BKE_mask_point_free(point);
981                                                 spline->tot_point--;
982
983                                                 BKE_mask_layer_shape_changed_remove(masklay, mask_layer_shape_ofs + j, 1);
984                                         }
985                                 }
986
987                                 mask_layer_shape_ofs += spline->tot_point;
988
989                                 MEM_freeN(spline->points);
990                                 spline->points = new_points;
991
992                                 ED_mask_select_flush_all(mask);
993                         }
994
995                         spline = next_spline;
996                 }
997
998                 /* not essential but confuses users when there are keys with no data!
999                  * assume if they delete all data from the layer they also dont care about keys */
1000                 if (masklay->splines.first == NULL) {
1001                         BKE_mask_layer_free_shapes(masklay);
1002                 }
1003         }
1004
1005         /* TODO: only update edited splines */
1006         BKE_mask_update_display(mask, CFRA);
1007
1008         WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
1009
1010         return OPERATOR_FINISHED;
1011 }
1012
1013 void MASK_OT_delete(wmOperatorType *ot)
1014 {
1015         /* identifiers */
1016         ot->name = "Delete";
1017         ot->description = "Delete selected control points or splines";
1018         ot->idname = "MASK_OT_delete";
1019
1020         /* api callbacks */
1021         ot->invoke = WM_operator_confirm;
1022         ot->exec = delete_exec;
1023         ot->poll = ED_maskedit_mask_poll;
1024
1025         /* flags */
1026         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1027 }
1028
1029 /* *** switch direction *** */
1030 static int mask_switch_direction_exec(bContext *C, wmOperator *UNUSED(op))
1031 {
1032         Scene *scene = CTX_data_scene(C);
1033         Mask *mask = CTX_data_edit_mask(C);
1034         MaskLayer *masklay;
1035
1036         int change = FALSE;
1037
1038         /* do actual selection */
1039         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1040                 MaskSpline *spline;
1041                 int change_layer = FALSE;
1042
1043                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
1044                         continue;
1045                 }
1046
1047                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1048                         if (ED_mask_spline_select_check(spline)) {
1049                                 BKE_mask_spline_direction_switch(masklay, spline);
1050                                 change = TRUE;
1051                                 change_layer = TRUE;
1052                         }
1053                 }
1054
1055                 if (change_layer) {
1056                         if (IS_AUTOKEY_ON(scene)) {
1057                                 ED_mask_layer_shape_auto_key(masklay, CFRA);
1058                         }
1059                 }
1060         }
1061
1062         if (change) {
1063                 /* TODO: only update this spline */
1064                 BKE_mask_update_display(mask, CFRA);
1065
1066                 WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask);
1067
1068                 return OPERATOR_FINISHED;
1069         }
1070
1071         return OPERATOR_CANCELLED;
1072 }
1073
1074 void MASK_OT_switch_direction(wmOperatorType *ot)
1075 {
1076         /* identifiers */
1077         ot->name = "Switch Direction";
1078         ot->description = "Switch direction of selected splines";
1079         ot->idname = "MASK_OT_switch_direction";
1080
1081         /* api callbacks */
1082         ot->exec = mask_switch_direction_exec;
1083         ot->poll = ED_maskedit_mask_poll;
1084
1085         /* flags */
1086         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1087 }
1088
1089
1090 /* *** recalc normals *** */
1091 static int mask_normals_make_consistent_exec(bContext *C, wmOperator *UNUSED(op))
1092 {
1093         Scene *scene = CTX_data_scene(C);
1094         Mask *mask = CTX_data_edit_mask(C);
1095         MaskLayer *masklay;
1096         int i;
1097
1098         int change = FALSE;
1099
1100         /* do actual selection */
1101         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1102                 MaskSpline *spline;
1103                 int change_layer = FALSE;
1104
1105                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
1106                         continue;
1107                 }
1108
1109                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1110                         for (i = 0; i < spline->tot_point; i++) {
1111                                 MaskSplinePoint *point = &spline->points[i];
1112
1113                                 if (MASKPOINT_ISSEL_ANY(point)) {
1114                                         BKE_mask_calc_handle_point_auto(spline, point, FALSE);
1115                                         change = TRUE;
1116                                         change_layer = TRUE;
1117                                 }
1118                         }
1119                 }
1120
1121                 if (change_layer) {
1122                         if (IS_AUTOKEY_ON(scene)) {
1123                                 ED_mask_layer_shape_auto_key(masklay, CFRA);
1124                         }
1125                 }
1126         }
1127
1128         if (change) {
1129                 /* TODO: only update this spline */
1130                 BKE_mask_update_display(mask, CFRA);
1131
1132                 WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask);
1133
1134                 return OPERATOR_FINISHED;
1135         }
1136
1137         return OPERATOR_CANCELLED;
1138 }
1139
1140 /* named to match mesh recalc normals */
1141 void MASK_OT_normals_make_consistent(wmOperatorType *ot)
1142 {
1143         /* identifiers */
1144         ot->name = "Recalc Normals";
1145         ot->description = "Re-calculate the direction of selected handles";
1146         ot->idname = "MASK_OT_normals_make_consistent";
1147
1148         /* api callbacks */
1149         ot->exec = mask_normals_make_consistent_exec;
1150         ot->poll = ED_maskedit_mask_poll;
1151
1152         /* flags */
1153         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1154 }
1155
1156
1157 /******************** set handle type *********************/
1158
1159 static int set_handle_type_exec(bContext *C, wmOperator *op)
1160 {
1161         Mask *mask = CTX_data_edit_mask(C);
1162         MaskLayer *masklay;
1163         int handle_type = RNA_enum_get(op->ptr, "type");
1164
1165         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1166                 MaskSpline *spline;
1167                 int i;
1168
1169                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
1170                         continue;
1171                 }
1172
1173                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1174                         for (i = 0; i < spline->tot_point; i++) {
1175                                 MaskSplinePoint *point = &spline->points[i];
1176
1177                                 if (MASKPOINT_ISSEL_ANY(point)) {
1178                                         BezTriple *bezt = &point->bezt;
1179
1180                                         bezt->h1 = bezt->h2 = handle_type;
1181                                 }
1182                         }
1183                 }
1184         }
1185
1186         WM_event_add_notifier(C, NC_MASK | ND_DATA, mask);
1187         DAG_id_tag_update(&mask->id, 0);
1188
1189         return OPERATOR_FINISHED;
1190 }
1191
1192 void MASK_OT_handle_type_set(wmOperatorType *ot)
1193 {
1194         static EnumPropertyItem editcurve_handle_type_items[] = {
1195                 {HD_AUTO, "AUTO", 0, "Auto", ""},
1196                 {HD_VECT, "VECTOR", 0, "Vector", ""},
1197                 {HD_ALIGN, "ALIGNED", 0, "Aligned", ""},
1198                 {0, NULL, 0, NULL, NULL}
1199         };
1200
1201         /* identifiers */
1202         ot->name = "Set Handle Type";
1203         ot->description = "Set type of handles for selected control points";
1204         ot->idname = "MASK_OT_handle_type_set";
1205
1206         /* api callbacks */
1207         ot->invoke = WM_menu_invoke;
1208         ot->exec = set_handle_type_exec;
1209         ot->poll = ED_maskedit_mask_poll;
1210
1211         /* flags */
1212         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1213
1214         /* properties */
1215         ot->prop = RNA_def_enum(ot->srna, "type", editcurve_handle_type_items, 1, "Type", "Spline type");
1216 }
1217
1218
1219 /* ********* clear/set restrict view *********/
1220 static int mask_hide_view_clear_exec(bContext *C, wmOperator *UNUSED(op))
1221 {
1222         Mask *mask = CTX_data_edit_mask(C);
1223         MaskLayer *masklay;
1224         int changed = FALSE;
1225
1226         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1227
1228                 if (masklay->restrictflag & OB_RESTRICT_VIEW) {
1229                         ED_mask_layer_select_set(masklay, TRUE);
1230                         masklay->restrictflag &= ~OB_RESTRICT_VIEW;
1231                         changed = 1;
1232                 }
1233         }
1234
1235         if (changed) {
1236                 WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask);
1237                 DAG_id_tag_update(&mask->id, 0);
1238
1239                 return OPERATOR_FINISHED;
1240         }
1241         else {
1242                 return OPERATOR_CANCELLED;
1243         }
1244 }
1245
1246 void MASK_OT_hide_view_clear(wmOperatorType *ot)
1247 {
1248
1249         /* identifiers */
1250         ot->name = "Clear Restrict View";
1251         ot->description = "Reveal the layer by setting the hide flag";
1252         ot->idname = "MASK_OT_hide_view_clear";
1253
1254         /* api callbacks */
1255         ot->exec = mask_hide_view_clear_exec;
1256         ot->poll = ED_maskedit_mask_poll;
1257
1258         /* flags */
1259         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1260 }
1261
1262 static int mask_hide_view_set_exec(bContext *C, wmOperator *op)
1263 {
1264         Mask *mask = CTX_data_edit_mask(C);
1265         MaskLayer *masklay;
1266         const int unselected = RNA_boolean_get(op->ptr, "unselected");
1267         int changed = FALSE;
1268
1269         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1270
1271                 if (masklay->restrictflag & MASK_RESTRICT_SELECT) {
1272                         continue;
1273                 }
1274
1275                 if (!unselected) {
1276                         if (ED_mask_layer_select_check(masklay)) {
1277                                 ED_mask_layer_select_set(masklay, FALSE);
1278
1279                                 masklay->restrictflag |= OB_RESTRICT_VIEW;
1280                                 changed = 1;
1281                                 if (masklay == BKE_mask_layer_active(mask)) {
1282                                         BKE_mask_layer_active_set(mask, NULL);
1283                                 }
1284                         }
1285                 }
1286                 else {
1287                         if (!ED_mask_layer_select_check(masklay)) {
1288                                 masklay->restrictflag |= OB_RESTRICT_VIEW;
1289                                 changed = 1;
1290                                 if (masklay == BKE_mask_layer_active(mask)) {
1291                                         BKE_mask_layer_active_set(mask, NULL);
1292                                 }
1293                         }
1294                 }
1295         }
1296
1297         if (changed) {
1298                 WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask);
1299                 DAG_id_tag_update(&mask->id, 0);
1300
1301                 return OPERATOR_FINISHED;
1302         }
1303         else {
1304                 return OPERATOR_CANCELLED;
1305         }
1306 }
1307
1308 void MASK_OT_hide_view_set(wmOperatorType *ot)
1309 {
1310         /* identifiers */
1311         ot->name = "Set Restrict View";
1312         ot->description = "Hide the layer by setting the hide flag";
1313         ot->idname = "MASK_OT_hide_view_set";
1314
1315         /* api callbacks */
1316         ot->exec = mask_hide_view_set_exec;
1317         ot->poll = ED_maskedit_mask_poll;
1318
1319         /* flags */
1320         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1321
1322         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected layers");
1323 }
1324
1325
1326 static int mask_feather_weight_clear_exec(bContext *C, wmOperator *UNUSED(op))
1327 {
1328         Scene *scene = CTX_data_scene(C);
1329         Mask *mask = CTX_data_edit_mask(C);
1330         MaskLayer *masklay;
1331         int changed = FALSE;
1332         int i;
1333
1334         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1335                 MaskSpline *spline;
1336
1337                 if (masklay->restrictflag & (MASK_RESTRICT_SELECT | MASK_RESTRICT_VIEW)) {
1338                         continue;
1339                 }
1340
1341                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1342                         for (i = 0; i < spline->tot_point; i++) {
1343                                 MaskSplinePoint *point = &spline->points[i];
1344
1345                                 if (MASKPOINT_ISSEL_ANY(point)) {
1346                                         BezTriple *bezt = &point->bezt;
1347                                         bezt->weight = 0.0f;
1348                                         changed = TRUE;
1349                                 }
1350                         }
1351                 }
1352         }
1353
1354         if (changed) {
1355                 /* TODO: only update edited splines */
1356                 BKE_mask_update_display(mask, CFRA);
1357
1358                 WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask);
1359                 DAG_id_tag_update(&mask->id, 0);
1360
1361                 return OPERATOR_FINISHED;
1362         }
1363         else {
1364                 return OPERATOR_CANCELLED;
1365         }
1366 }
1367
1368 void MASK_OT_feather_weight_clear(wmOperatorType *ot)
1369 {
1370         /* identifiers */
1371         ot->name = "Clear Feather Weight";
1372         ot->description = "Reset the feather weight to zero";
1373         ot->idname = "MASK_OT_feather_weight_clear";
1374
1375         /* api callbacks */
1376         ot->exec = mask_feather_weight_clear_exec;
1377         ot->poll = ED_maskedit_mask_poll;
1378
1379         /* flags */
1380         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1381 }
1382
1383 /******************** move mask layer operator *********************/
1384
1385 static int mask_layer_move_poll(bContext *C)
1386 {
1387         if (ED_maskedit_mask_poll(C)) {
1388                 Mask *mask = CTX_data_edit_mask(C);
1389
1390                 return mask->masklay_tot > 0;
1391         }
1392
1393         return FALSE;
1394 }
1395
1396 static int mask_layer_move_exec(bContext *C, wmOperator *op)
1397 {
1398         Mask *mask = CTX_data_edit_mask(C);
1399         MaskLayer *mask_layer = BLI_findlink(&mask->masklayers, mask->masklay_act);
1400         MaskLayer *mask_layer_other;
1401         int direction = RNA_enum_get(op->ptr, "direction");
1402
1403         if (!mask_layer)
1404                 return OPERATOR_CANCELLED;
1405
1406         if (direction == -1) {
1407                 mask_layer_other = mask_layer->prev;
1408
1409                 if (!mask_layer_other)
1410                         return OPERATOR_CANCELLED;
1411
1412                 BLI_remlink(&mask->masklayers, mask_layer);
1413                 BLI_insertlinkbefore(&mask->masklayers, mask_layer_other, mask_layer);
1414                 mask->masklay_act--;
1415         }
1416         else if (direction == 1) {
1417                 mask_layer_other = mask_layer->next;
1418
1419                 if (!mask_layer_other)
1420                         return OPERATOR_CANCELLED;
1421
1422                 BLI_remlink(&mask->masklayers, mask_layer);
1423                 BLI_insertlinkafter(&mask->masklayers, mask_layer_other, mask_layer);
1424                 mask->masklay_act++;
1425         }
1426
1427         return OPERATOR_FINISHED;
1428 }
1429
1430 void MASK_OT_layer_move(wmOperatorType *ot)
1431 {
1432         static EnumPropertyItem direction_items[] = {
1433                 {-1, "UP", 0, "Up", ""},
1434                 {1, "DOWN", 0, "Down", ""},
1435                 {0, NULL, 0, NULL, NULL}
1436         };
1437
1438         /* identifiers */
1439         ot->name = "Move Layer";
1440         ot->description = "Move the active layer up/down in the list";
1441         ot->idname = "MASK_OT_layer_move";
1442
1443         /* api callbacks */
1444         ot->exec = mask_layer_move_exec;
1445         ot->poll = mask_layer_move_poll;
1446
1447         /* flags */
1448         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1449
1450         /* properties */
1451         RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move the active layer");
1452 }