diff options
author | Campbell Barton <ideasman42@gmail.com> | 2019-04-17 07:17:24 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2019-04-17 07:21:24 +0300 |
commit | e12c08e8d170b7ca40f204a5b0423c23a9fbc2c1 (patch) | |
tree | 8cf3453d12edb177a218ef8009357518ec6cab6a /source/blender/editors/mask | |
parent | b3dabc200a4b0399ec6b81f2ff2730d07b44fcaa (diff) |
ClangFormat: apply to source, most of intern
Apply clang format as proposed in T53211.
For details on usage and instructions for migrating branches
without conflicts, see:
https://wiki.blender.org/wiki/Tools/ClangFormat
Diffstat (limited to 'source/blender/editors/mask')
-rw-r--r-- | source/blender/editors/mask/CMakeLists.txt | 40 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_add.c | 1448 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_draw.c | 1311 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_edit.c | 849 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_editaction.c | 353 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_intern.h | 30 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_ops.c | 3814 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_relationships.c | 251 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_select.c | 1324 | ||||
-rw-r--r-- | source/blender/editors/mask/mask_shapekey.c | 663 |
10 files changed, 5095 insertions, 4988 deletions
diff --git a/source/blender/editors/mask/CMakeLists.txt b/source/blender/editors/mask/CMakeLists.txt index a8ed6812897..9dabcc0e8bb 100644 --- a/source/blender/editors/mask/CMakeLists.txt +++ b/source/blender/editors/mask/CMakeLists.txt @@ -18,33 +18,33 @@ # ***** END GPL LICENSE BLOCK ***** set(INC - ../include - ../../blenkernel - ../../blenlib - ../../depsgraph - ../../gpu - ../../makesdna - ../../makesrna - ../../windowmanager - ../../../../intern/guardedalloc - ../../../../intern/glew-mx + ../include + ../../blenkernel + ../../blenlib + ../../depsgraph + ../../gpu + ../../makesdna + ../../makesrna + ../../windowmanager + ../../../../intern/guardedalloc + ../../../../intern/glew-mx ) set(INC_SYS - ${GLEW_INCLUDE_PATH} + ${GLEW_INCLUDE_PATH} ) set(SRC - mask_add.c - mask_draw.c - mask_edit.c - mask_editaction.c - mask_ops.c - mask_relationships.c - mask_select.c - mask_shapekey.c + mask_add.c + mask_draw.c + mask_edit.c + mask_editaction.c + mask_ops.c + mask_relationships.c + mask_select.c + mask_shapekey.c - mask_intern.h + mask_intern.h ) set(LIB diff --git a/source/blender/editors/mask/mask_add.c b/source/blender/editors/mask/mask_add.c index 579f3765710..1c872e73226 100644 --- a/source/blender/editors/mask/mask_add.c +++ b/source/blender/editors/mask/mask_add.c @@ -38,19 +38,19 @@ #include "WM_types.h" #include "ED_select_utils.h" -#include "ED_mask.h" /* own include */ +#include "ED_mask.h" /* own include */ #include "ED_screen.h" #include "RNA_access.h" #include "RNA_define.h" -#include "mask_intern.h" /* own include */ - +#include "mask_intern.h" /* own include */ bool ED_mask_find_nearest_diff_point(const bContext *C, struct Mask *mask, const float normal_co[2], - int threshold, bool feather, + int threshold, + bool feather, float tangent[2], const bool use_deform, const bool use_project, @@ -60,840 +60,882 @@ bool ED_mask_find_nearest_diff_point(const bContext *C, float *u_r, float *score_r) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); - - MaskLayer *masklay, *point_masklay; - MaskSpline *point_spline; - MaskSplinePoint *point = NULL; - float dist_best_sq = FLT_MAX, co[2]; - int width, height; - float u = 0.0f; - float scalex, scaley; - - ED_mask_get_size(sa, &width, &height); - ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley); - - co[0] = normal_co[0] * scalex; - co[1] = normal_co[1] * scaley; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - int i; - MaskSplinePoint *cur_point; - - for (i = 0, cur_point = use_deform ? spline->points_deform : spline->points; - i < spline->tot_point; - i++, cur_point++) - { - float *diff_points; - unsigned int tot_diff_point; - - diff_points = BKE_mask_point_segment_diff(spline, cur_point, width, height, - &tot_diff_point); - - if (diff_points) { - int j, tot_point; - unsigned int tot_feather_point; - float *feather_points = NULL, *points; - - if (feather) { - feather_points = BKE_mask_point_segment_feather_diff(spline, cur_point, - width, height, - &tot_feather_point); - - points = feather_points; - tot_point = tot_feather_point; - } - else { - points = diff_points; - tot_point = tot_diff_point; - } - - for (j = 0; j < tot_point - 1; j++) { - float dist_sq, a[2], b[2]; - - a[0] = points[2 * j] * scalex; - a[1] = points[2 * j + 1] * scaley; - - b[0] = points[2 * j + 2] * scalex; - b[1] = points[2 * j + 3] * scaley; - - dist_sq = dist_squared_to_line_segment_v2(co, a, b); - - if (dist_sq < dist_best_sq) { - if (tangent) - sub_v2_v2v2(tangent, &diff_points[2 * j + 2], &diff_points[2 * j]); - - point_masklay = masklay; - point_spline = spline; - point = use_deform ? &spline->points[(cur_point - spline->points_deform)] : cur_point; - dist_best_sq = dist_sq; - u = (float)j / tot_point; - } - } - - if (feather_points) - MEM_freeN(feather_points); - - MEM_freeN(diff_points); - } - } - } - } - - if (point && dist_best_sq < threshold) { - if (masklay_r) - *masklay_r = point_masklay; - - if (spline_r) - *spline_r = point_spline; - - if (point_r) - *point_r = point; - - if (u_r) { - /* TODO(sergey): Projection fails in some weirdo cases.. */ - if (use_project) { - u = BKE_mask_spline_project_co(point_spline, point, u, normal_co, MASK_PROJ_ANY); - } - - *u_r = u; - } + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); + + MaskLayer *masklay, *point_masklay; + MaskSpline *point_spline; + MaskSplinePoint *point = NULL; + float dist_best_sq = FLT_MAX, co[2]; + int width, height; + float u = 0.0f; + float scalex, scaley; + + ED_mask_get_size(sa, &width, &height); + ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley); + + co[0] = normal_co[0] * scalex; + co[1] = normal_co[1] * scaley; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + int i; + MaskSplinePoint *cur_point; + + for (i = 0, cur_point = use_deform ? spline->points_deform : spline->points; + i < spline->tot_point; + i++, cur_point++) { + float *diff_points; + unsigned int tot_diff_point; + + diff_points = BKE_mask_point_segment_diff( + spline, cur_point, width, height, &tot_diff_point); + + if (diff_points) { + int j, tot_point; + unsigned int tot_feather_point; + float *feather_points = NULL, *points; + + if (feather) { + feather_points = BKE_mask_point_segment_feather_diff( + spline, cur_point, width, height, &tot_feather_point); + + points = feather_points; + tot_point = tot_feather_point; + } + else { + points = diff_points; + tot_point = tot_diff_point; + } + + for (j = 0; j < tot_point - 1; j++) { + float dist_sq, a[2], b[2]; + + a[0] = points[2 * j] * scalex; + a[1] = points[2 * j + 1] * scaley; + + b[0] = points[2 * j + 2] * scalex; + b[1] = points[2 * j + 3] * scaley; + + dist_sq = dist_squared_to_line_segment_v2(co, a, b); + + if (dist_sq < dist_best_sq) { + if (tangent) + sub_v2_v2v2(tangent, &diff_points[2 * j + 2], &diff_points[2 * j]); - if (score_r) { - *score_r = dist_best_sq; - } + point_masklay = masklay; + point_spline = spline; + point = use_deform ? &spline->points[(cur_point - spline->points_deform)] : + cur_point; + dist_best_sq = dist_sq; + u = (float)j / tot_point; + } + } - return true; - } + if (feather_points) + MEM_freeN(feather_points); - if (masklay_r) - *masklay_r = NULL; + MEM_freeN(diff_points); + } + } + } + } - if (spline_r) - *spline_r = NULL; + if (point && dist_best_sq < threshold) { + if (masklay_r) + *masklay_r = point_masklay; - if (point_r) - *point_r = NULL; + if (spline_r) + *spline_r = point_spline; - return false; + if (point_r) + *point_r = point; + + if (u_r) { + /* TODO(sergey): Projection fails in some weirdo cases.. */ + if (use_project) { + u = BKE_mask_spline_project_co(point_spline, point, u, normal_co, MASK_PROJ_ANY); + } + + *u_r = u; + } + + if (score_r) { + *score_r = dist_best_sq; + } + + return true; + } + + if (masklay_r) + *masklay_r = NULL; + + if (spline_r) + *spline_r = NULL; + + if (point_r) + *point_r = NULL; + + return false; } /******************** add vertex *********************/ -static void setup_vertex_point( - Mask *mask, MaskSpline *spline, MaskSplinePoint *new_point, - const float point_co[2], const float u, const float ctime, - const MaskSplinePoint *reference_point, const bool reference_adjacent) +static void setup_vertex_point(Mask *mask, + MaskSpline *spline, + MaskSplinePoint *new_point, + const float point_co[2], + const float u, + const float ctime, + const MaskSplinePoint *reference_point, + const bool reference_adjacent) { - const MaskSplinePoint *reference_parent_point = NULL; - BezTriple *bezt; - float co[3]; - - copy_v2_v2(co, point_co); - co[2] = 0.0f; - - /* point coordinate */ - bezt = &new_point->bezt; - - bezt->h1 = bezt->h2 = HD_ALIGN; - - if (reference_point) { - if (reference_point->bezt.h1 == HD_VECT && reference_point->bezt.h2 == HD_VECT) { - /* If the reference point is sharp try using some smooth point as reference - * for handles. - */ - int point_index = reference_point - spline->points; - int delta = new_point == spline->points ? 1 : -1; - int i = 0; - for (i = 0; i < spline->tot_point - 1; ++i) { - MaskSplinePoint *current_point; - - point_index += delta; - if (point_index == -1 || point_index >= spline->tot_point) { - if (spline->flag & MASK_SPLINE_CYCLIC) { - if (point_index == -1) { - point_index = spline->tot_point - 1; - } - else if (point_index >= spline->tot_point) { - point_index = 0; - } - } - else { - break; - } - } - - current_point = &spline->points[point_index]; - if (current_point->bezt.h1 != HD_VECT || current_point->bezt.h2 != HD_VECT) { - bezt->h1 = bezt->h2 = MAX2(current_point->bezt.h2, current_point->bezt.h1); - break; - } - } - } - else { - bezt->h1 = bezt->h2 = MAX2(reference_point->bezt.h2, reference_point->bezt.h1); - } - - reference_parent_point = reference_point; - } - else if (reference_adjacent) { - if (spline->tot_point != 1) { - MaskSplinePoint *prev_point, *next_point, *close_point; - - const int index = (int)(new_point - spline->points); - if (spline->flag & MASK_SPLINE_CYCLIC) { - prev_point = &spline->points[mod_i(index - 1, spline->tot_point)]; - next_point = &spline->points[mod_i(index + 1, spline->tot_point)]; - } - else { - prev_point = (index != 0) ? &spline->points[index - 1] : NULL; - next_point = (index != spline->tot_point - 1) ? &spline->points[index + 1] : NULL; - } - - if (prev_point && next_point) { - close_point = (len_squared_v2v2(new_point->bezt.vec[1], prev_point->bezt.vec[1]) < - len_squared_v2v2(new_point->bezt.vec[1], next_point->bezt.vec[1])) ? - prev_point : next_point; - } - else { - close_point = prev_point ? prev_point : next_point; - } - - /* handle type */ - char handle_type = 0; - if (prev_point) { - handle_type = prev_point->bezt.h2; - } - if (next_point) { - handle_type = MAX2(next_point->bezt.h2, handle_type); - } - bezt->h1 = bezt->h2 = handle_type; - - /* parent */ - reference_parent_point = close_point; - - /* note, we may want to copy other attributes later, radius? pressure? color? */ - } - } - - copy_v3_v3(bezt->vec[0], co); - copy_v3_v3(bezt->vec[1], co); - copy_v3_v3(bezt->vec[2], co); - - if (reference_parent_point) { - new_point->parent = reference_parent_point->parent; - - if (new_point->parent.id) { - float parent_matrix[3][3]; - BKE_mask_point_parent_matrix_get(new_point, ctime, parent_matrix); - invert_m3(parent_matrix); - mul_m3_v2(parent_matrix, new_point->bezt.vec[1]); - } - } - else { - BKE_mask_parent_init(&new_point->parent); - } - - if (spline->tot_point != 1) { - BKE_mask_calc_handle_adjacent_interp(spline, new_point, u); - } - - /* select new point */ - MASKPOINT_SEL_ALL(new_point); - ED_mask_select_flush_all(mask); + const MaskSplinePoint *reference_parent_point = NULL; + BezTriple *bezt; + float co[3]; + + copy_v2_v2(co, point_co); + co[2] = 0.0f; + + /* point coordinate */ + bezt = &new_point->bezt; + + bezt->h1 = bezt->h2 = HD_ALIGN; + + if (reference_point) { + if (reference_point->bezt.h1 == HD_VECT && reference_point->bezt.h2 == HD_VECT) { + /* If the reference point is sharp try using some smooth point as reference + * for handles. + */ + int point_index = reference_point - spline->points; + int delta = new_point == spline->points ? 1 : -1; + int i = 0; + for (i = 0; i < spline->tot_point - 1; ++i) { + MaskSplinePoint *current_point; + + point_index += delta; + if (point_index == -1 || point_index >= spline->tot_point) { + if (spline->flag & MASK_SPLINE_CYCLIC) { + if (point_index == -1) { + point_index = spline->tot_point - 1; + } + else if (point_index >= spline->tot_point) { + point_index = 0; + } + } + else { + break; + } + } + + current_point = &spline->points[point_index]; + if (current_point->bezt.h1 != HD_VECT || current_point->bezt.h2 != HD_VECT) { + bezt->h1 = bezt->h2 = MAX2(current_point->bezt.h2, current_point->bezt.h1); + break; + } + } + } + else { + bezt->h1 = bezt->h2 = MAX2(reference_point->bezt.h2, reference_point->bezt.h1); + } + + reference_parent_point = reference_point; + } + else if (reference_adjacent) { + if (spline->tot_point != 1) { + MaskSplinePoint *prev_point, *next_point, *close_point; + + const int index = (int)(new_point - spline->points); + if (spline->flag & MASK_SPLINE_CYCLIC) { + prev_point = &spline->points[mod_i(index - 1, spline->tot_point)]; + next_point = &spline->points[mod_i(index + 1, spline->tot_point)]; + } + else { + prev_point = (index != 0) ? &spline->points[index - 1] : NULL; + next_point = (index != spline->tot_point - 1) ? &spline->points[index + 1] : NULL; + } + + if (prev_point && next_point) { + close_point = (len_squared_v2v2(new_point->bezt.vec[1], prev_point->bezt.vec[1]) < + len_squared_v2v2(new_point->bezt.vec[1], next_point->bezt.vec[1])) ? + prev_point : + next_point; + } + else { + close_point = prev_point ? prev_point : next_point; + } + + /* handle type */ + char handle_type = 0; + if (prev_point) { + handle_type = prev_point->bezt.h2; + } + if (next_point) { + handle_type = MAX2(next_point->bezt.h2, handle_type); + } + bezt->h1 = bezt->h2 = handle_type; + + /* parent */ + reference_parent_point = close_point; + + /* note, we may want to copy other attributes later, radius? pressure? color? */ + } + } + + copy_v3_v3(bezt->vec[0], co); + copy_v3_v3(bezt->vec[1], co); + copy_v3_v3(bezt->vec[2], co); + + if (reference_parent_point) { + new_point->parent = reference_parent_point->parent; + + if (new_point->parent.id) { + float parent_matrix[3][3]; + BKE_mask_point_parent_matrix_get(new_point, ctime, parent_matrix); + invert_m3(parent_matrix); + mul_m3_v2(parent_matrix, new_point->bezt.vec[1]); + } + } + else { + BKE_mask_parent_init(&new_point->parent); + } + + if (spline->tot_point != 1) { + BKE_mask_calc_handle_adjacent_interp(spline, new_point, u); + } + + /* select new point */ + MASKPOINT_SEL_ALL(new_point); + ED_mask_select_flush_all(mask); } - /* **** add extrude vertex **** */ -static void finSelectedSplinePoint(MaskLayer *masklay, MaskSpline **spline, MaskSplinePoint **point, bool check_active) +static void finSelectedSplinePoint(MaskLayer *masklay, + MaskSpline **spline, + MaskSplinePoint **point, + bool check_active) { - MaskSpline *cur_spline = masklay->splines.first; - - *spline = NULL; - *point = NULL; - - if (check_active) { - /* TODO, having an active point but no active spline is possible, why? */ - if (masklay->act_spline && masklay->act_point && MASKPOINT_ISSEL_ANY(masklay->act_point)) { - *spline = masklay->act_spline; - *point = masklay->act_point; - return; - } - } - - while (cur_spline) { - int i; - - for (i = 0; i < cur_spline->tot_point; i++) { - MaskSplinePoint *cur_point = &cur_spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(cur_point)) { - if (*spline != NULL && *spline != cur_spline) { - *spline = NULL; - *point = NULL; - return; - } - else if (*point) { - *point = NULL; - } - else { - *spline = cur_spline; - *point = cur_point; - } - } - } - - cur_spline = cur_spline->next; - } + MaskSpline *cur_spline = masklay->splines.first; + + *spline = NULL; + *point = NULL; + + if (check_active) { + /* TODO, having an active point but no active spline is possible, why? */ + if (masklay->act_spline && masklay->act_point && MASKPOINT_ISSEL_ANY(masklay->act_point)) { + *spline = masklay->act_spline; + *point = masklay->act_point; + return; + } + } + + while (cur_spline) { + int i; + + for (i = 0; i < cur_spline->tot_point; i++) { + MaskSplinePoint *cur_point = &cur_spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(cur_point)) { + if (*spline != NULL && *spline != cur_spline) { + *spline = NULL; + *point = NULL; + return; + } + else if (*point) { + *point = NULL; + } + else { + *spline = cur_spline; + *point = cur_point; + } + } + } + + cur_spline = cur_spline->next; + } } /* **** add subdivide vertex **** */ static void mask_spline_add_point_at_index(MaskSpline *spline, int point_index) { - MaskSplinePoint *new_point_array; + MaskSplinePoint *new_point_array; - new_point_array = MEM_callocN(sizeof(MaskSplinePoint) * (spline->tot_point + 1), "add mask vert points"); + new_point_array = MEM_callocN(sizeof(MaskSplinePoint) * (spline->tot_point + 1), + "add mask vert points"); - memcpy(new_point_array, spline->points, sizeof(MaskSplinePoint) * (point_index + 1)); - memcpy(new_point_array + point_index + 2, spline->points + point_index + 1, - sizeof(MaskSplinePoint) * (spline->tot_point - point_index - 1)); + memcpy(new_point_array, spline->points, sizeof(MaskSplinePoint) * (point_index + 1)); + memcpy(new_point_array + point_index + 2, + spline->points + point_index + 1, + sizeof(MaskSplinePoint) * (spline->tot_point - point_index - 1)); - MEM_freeN(spline->points); - spline->points = new_point_array; - spline->tot_point++; + MEM_freeN(spline->points); + spline->points = new_point_array; + spline->tot_point++; } static bool add_vertex_subdivide(const bContext *C, Mask *mask, const float co[2]) { - MaskLayer *masklay; - MaskSpline *spline; - MaskSplinePoint *point = NULL; - const float threshold = 9; - float tangent[2]; - float u; - - if (ED_mask_find_nearest_diff_point(C, mask, co, threshold, false, tangent, true, true, - &masklay, &spline, &point, &u, NULL)) - { - Scene *scene = CTX_data_scene(C); - const float ctime = CFRA; - - MaskSplinePoint *new_point; - int point_index = point - spline->points; - - ED_mask_select_toggle_all(mask, SEL_DESELECT); - - mask_spline_add_point_at_index(spline, point_index); - - new_point = &spline->points[point_index + 1]; - - setup_vertex_point(mask, spline, new_point, co, u, ctime, NULL, true); - - /* TODO - we could pass the spline! */ - BKE_mask_layer_shape_changed_add(masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index + 1, true, true); - - masklay->act_spline = spline; - masklay->act_point = new_point; - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - - return true; - } - - return false; + MaskLayer *masklay; + MaskSpline *spline; + MaskSplinePoint *point = NULL; + const float threshold = 9; + float tangent[2]; + float u; + + if (ED_mask_find_nearest_diff_point(C, + mask, + co, + threshold, + false, + tangent, + true, + true, + &masklay, + &spline, + &point, + &u, + NULL)) { + Scene *scene = CTX_data_scene(C); + const float ctime = CFRA; + + MaskSplinePoint *new_point; + int point_index = point - spline->points; + + ED_mask_select_toggle_all(mask, SEL_DESELECT); + + mask_spline_add_point_at_index(spline, point_index); + + new_point = &spline->points[point_index + 1]; + + setup_vertex_point(mask, spline, new_point, co, u, ctime, NULL, true); + + /* TODO - we could pass the spline! */ + BKE_mask_layer_shape_changed_add(masklay, + BKE_mask_layer_shape_spline_to_index(masklay, spline) + + point_index + 1, + true, + true); + + masklay->act_spline = spline; + masklay->act_point = new_point; + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + + return true; + } + + return false; } -static bool add_vertex_extrude(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2]) +static bool add_vertex_extrude(const bContext *C, + Mask *mask, + MaskLayer *masklay, + const float co[2]) { - Scene *scene = CTX_data_scene(C); - const float ctime = CFRA; - - MaskSpline *spline; - MaskSplinePoint *point; - MaskSplinePoint *new_point = NULL, *ref_point = NULL; - - /* check on which side we want to add the point */ - int point_index; - float tangent_point[2]; - float tangent_co[2]; - bool do_cyclic_correct = false; - bool do_prev; /* use prev point rather then next?? */ - - if (!masklay) { - return false; - } - else { - finSelectedSplinePoint(masklay, &spline, &point, true); - } - - ED_mask_select_toggle_all(mask, SEL_DESELECT); - - point_index = (point - spline->points); - - MASKPOINT_DESEL_ALL(point); - - if ((spline->flag & MASK_SPLINE_CYCLIC) || - (point_index > 0 && point_index != spline->tot_point - 1)) - { - BKE_mask_calc_tangent_polyline(spline, point, tangent_point); - sub_v2_v2v2(tangent_co, co, point->bezt.vec[1]); - - if (dot_v2v2(tangent_point, tangent_co) < 0.0f) { - do_prev = true; - } - else { - do_prev = false; - } - } - else if (((spline->flag & MASK_SPLINE_CYCLIC) == 0) && (point_index == 0)) { - do_prev = true; - } - else if (((spline->flag & MASK_SPLINE_CYCLIC) == 0) && (point_index == spline->tot_point - 1)) { - do_prev = false; - } - else { - do_prev = false; /* quiet warning */ - /* should never get here */ - BLI_assert(0); - } - - /* use the point before the active one */ - if (do_prev) { - point_index--; - if (point_index < 0) { - point_index += spline->tot_point; /* wrap index */ - if ((spline->flag & MASK_SPLINE_CYCLIC) == 0) { - do_cyclic_correct = true; - point_index = 0; - } - } - } - -// print_v2("", tangent_point); -// printf("%d\n", point_index); - - mask_spline_add_point_at_index(spline, point_index); - - if (do_cyclic_correct) { - ref_point = &spline->points[point_index + 1]; - new_point = &spline->points[point_index]; - *ref_point = *new_point; - memset(new_point, 0, sizeof(*new_point)); - } - else { - ref_point = &spline->points[point_index]; - new_point = &spline->points[point_index + 1]; - } - - masklay->act_point = new_point; - - setup_vertex_point(mask, spline, new_point, co, 0.5f, ctime, ref_point, false); - - if (masklay->splines_shapes.first) { - point_index = (((int)(new_point - spline->points) + 0) % spline->tot_point); - BKE_mask_layer_shape_changed_add(masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index, true, true); - } - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - - return true; + Scene *scene = CTX_data_scene(C); + const float ctime = CFRA; + + MaskSpline *spline; + MaskSplinePoint *point; + MaskSplinePoint *new_point = NULL, *ref_point = NULL; + + /* check on which side we want to add the point */ + int point_index; + float tangent_point[2]; + float tangent_co[2]; + bool do_cyclic_correct = false; + bool do_prev; /* use prev point rather then next?? */ + + if (!masklay) { + return false; + } + else { + finSelectedSplinePoint(masklay, &spline, &point, true); + } + + ED_mask_select_toggle_all(mask, SEL_DESELECT); + + point_index = (point - spline->points); + + MASKPOINT_DESEL_ALL(point); + + if ((spline->flag & MASK_SPLINE_CYCLIC) || + (point_index > 0 && point_index != spline->tot_point - 1)) { + BKE_mask_calc_tangent_polyline(spline, point, tangent_point); + sub_v2_v2v2(tangent_co, co, point->bezt.vec[1]); + + if (dot_v2v2(tangent_point, tangent_co) < 0.0f) { + do_prev = true; + } + else { + do_prev = false; + } + } + else if (((spline->flag & MASK_SPLINE_CYCLIC) == 0) && (point_index == 0)) { + do_prev = true; + } + else if (((spline->flag & MASK_SPLINE_CYCLIC) == 0) && (point_index == spline->tot_point - 1)) { + do_prev = false; + } + else { + do_prev = false; /* quiet warning */ + /* should never get here */ + BLI_assert(0); + } + + /* use the point before the active one */ + if (do_prev) { + point_index--; + if (point_index < 0) { + point_index += spline->tot_point; /* wrap index */ + if ((spline->flag & MASK_SPLINE_CYCLIC) == 0) { + do_cyclic_correct = true; + point_index = 0; + } + } + } + + // print_v2("", tangent_point); + // printf("%d\n", point_index); + + mask_spline_add_point_at_index(spline, point_index); + + if (do_cyclic_correct) { + ref_point = &spline->points[point_index + 1]; + new_point = &spline->points[point_index]; + *ref_point = *new_point; + memset(new_point, 0, sizeof(*new_point)); + } + else { + ref_point = &spline->points[point_index]; + new_point = &spline->points[point_index + 1]; + } + + masklay->act_point = new_point; + + setup_vertex_point(mask, spline, new_point, co, 0.5f, ctime, ref_point, false); + + if (masklay->splines_shapes.first) { + point_index = (((int)(new_point - spline->points) + 0) % spline->tot_point); + BKE_mask_layer_shape_changed_add( + masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index, true, true); + } + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + + return true; } static bool add_vertex_new(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2]) { - Scene *scene = CTX_data_scene(C); - const float ctime = CFRA; + Scene *scene = CTX_data_scene(C); + const float ctime = CFRA; - MaskSpline *spline; - MaskSplinePoint *new_point = NULL, *ref_point = NULL; + MaskSpline *spline; + MaskSplinePoint *new_point = NULL, *ref_point = NULL; - if (!masklay) { - /* if there's no masklay currently operationg on, create new one */ - masklay = BKE_mask_layer_new(mask, ""); - mask->masklay_act = mask->masklay_tot - 1; - } + if (!masklay) { + /* if there's no masklay currently operationg on, create new one */ + masklay = BKE_mask_layer_new(mask, ""); + mask->masklay_act = mask->masklay_tot - 1; + } - ED_mask_select_toggle_all(mask, SEL_DESELECT); + ED_mask_select_toggle_all(mask, SEL_DESELECT); - spline = BKE_mask_spline_add(masklay); + spline = BKE_mask_spline_add(masklay); - masklay->act_spline = spline; - new_point = spline->points; + masklay->act_spline = spline; + new_point = spline->points; - masklay->act_point = new_point; + masklay->act_point = new_point; - setup_vertex_point(mask, spline, new_point, co, 0.5f, ctime, ref_point, false); + setup_vertex_point(mask, spline, new_point, co, 0.5f, ctime, ref_point, false); - { - int point_index = (((int)(new_point - spline->points) + 0) % spline->tot_point); - BKE_mask_layer_shape_changed_add(masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index, true, true); - } + { + int point_index = (((int)(new_point - spline->points) + 0) % spline->tot_point); + BKE_mask_layer_shape_changed_add( + masklay, BKE_mask_layer_shape_spline_to_index(masklay, spline) + point_index, true, true); + } - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return true; + return true; } static int add_vertex_exec(bContext *C, wmOperator *op) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - - float co[2]; - - if (mask == NULL) { - /* if there's no active mask, create one */ - mask = ED_mask_new(C, NULL); - } - - masklay = BKE_mask_layer_active(mask); - - if (masklay && masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - masklay = NULL; - } - - RNA_float_get_array(op->ptr, "location", co); - - /* TODO, having an active point but no active spline is possible, why? */ - if (masklay && masklay->act_spline && masklay->act_point && MASKPOINT_ISSEL_ANY(masklay->act_point)) { - - /* cheap trick - double click for cyclic */ - MaskSpline *spline = masklay->act_spline; - MaskSplinePoint *point = masklay->act_point; - - const bool is_sta = (point == spline->points); - const bool is_end = (point == &spline->points[spline->tot_point - 1]); - - /* then check are we overlapping the mouse */ - if ((is_sta || is_end) && equals_v2v2(co, point->bezt.vec[1])) { - if (spline->flag & MASK_SPLINE_CYCLIC) { - /* nothing to do */ - return OPERATOR_CANCELLED; - } - else { - /* recalc the connecting point as well to make a nice even curve */ - MaskSplinePoint *point_other = is_end ? spline->points : &spline->points[spline->tot_point - 1]; - spline->flag |= MASK_SPLINE_CYCLIC; - - /* TODO, update keyframes in time */ - BKE_mask_calc_handle_point_auto(spline, point, false); - BKE_mask_calc_handle_point_auto(spline, point_other, false); - - /* TODO: only update this spline */ - BKE_mask_update_display(mask, CFRA); - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return OPERATOR_FINISHED; - } - } - - if (!add_vertex_subdivide(C, mask, co)) { - if (!add_vertex_extrude(C, mask, masklay, co)) { - return OPERATOR_CANCELLED; - } - } - } - else { - if (!add_vertex_subdivide(C, mask, co)) { - if (!add_vertex_new(C, mask, masklay, co)) { - return OPERATOR_CANCELLED; - } - } - } - - /* TODO: only update this spline */ - BKE_mask_update_display(mask, CFRA); - - return OPERATOR_FINISHED; + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + + float co[2]; + + if (mask == NULL) { + /* if there's no active mask, create one */ + mask = ED_mask_new(C, NULL); + } + + masklay = BKE_mask_layer_active(mask); + + if (masklay && masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + masklay = NULL; + } + + RNA_float_get_array(op->ptr, "location", co); + + /* TODO, having an active point but no active spline is possible, why? */ + if (masklay && masklay->act_spline && masklay->act_point && + MASKPOINT_ISSEL_ANY(masklay->act_point)) { + + /* cheap trick - double click for cyclic */ + MaskSpline *spline = masklay->act_spline; + MaskSplinePoint *point = masklay->act_point; + + const bool is_sta = (point == spline->points); + const bool is_end = (point == &spline->points[spline->tot_point - 1]); + + /* then check are we overlapping the mouse */ + if ((is_sta || is_end) && equals_v2v2(co, point->bezt.vec[1])) { + if (spline->flag & MASK_SPLINE_CYCLIC) { + /* nothing to do */ + return OPERATOR_CANCELLED; + } + else { + /* recalc the connecting point as well to make a nice even curve */ + MaskSplinePoint *point_other = is_end ? spline->points : + &spline->points[spline->tot_point - 1]; + spline->flag |= MASK_SPLINE_CYCLIC; + + /* TODO, update keyframes in time */ + BKE_mask_calc_handle_point_auto(spline, point, false); + BKE_mask_calc_handle_point_auto(spline, point_other, false); + + /* TODO: only update this spline */ + BKE_mask_update_display(mask, CFRA); + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + return OPERATOR_FINISHED; + } + } + + if (!add_vertex_subdivide(C, mask, co)) { + if (!add_vertex_extrude(C, mask, masklay, co)) { + return OPERATOR_CANCELLED; + } + } + } + else { + if (!add_vertex_subdivide(C, mask, co)) { + if (!add_vertex_new(C, mask, masklay, co)) { + return OPERATOR_CANCELLED; + } + } + } + + /* TODO: only update this spline */ + BKE_mask_update_display(mask, CFRA); + + return OPERATOR_FINISHED; } static int add_vertex_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - float co[2]; + float co[2]; - ED_mask_mouse_pos(sa, ar, event->mval, co); + ED_mask_mouse_pos(sa, ar, event->mval, co); - RNA_float_set_array(op->ptr, "location", co); + RNA_float_set_array(op->ptr, "location", co); - return add_vertex_exec(C, op); + return add_vertex_exec(C, op); } void MASK_OT_add_vertex(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Add Vertex"; - ot->description = "Add vertex to active spline"; - ot->idname = "MASK_OT_add_vertex"; - - /* api callbacks */ - ot->exec = add_vertex_exec; - ot->invoke = add_vertex_invoke; - ot->poll = ED_operator_mask; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, - "Location", "Location of vertex in normalized space", -1.0f, 1.0f); + /* identifiers */ + ot->name = "Add Vertex"; + ot->description = "Add vertex to active spline"; + ot->idname = "MASK_OT_add_vertex"; + + /* api callbacks */ + ot->exec = add_vertex_exec; + ot->invoke = add_vertex_invoke; + ot->poll = ED_operator_mask; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_float_vector(ot->srna, + "location", + 2, + NULL, + -FLT_MAX, + FLT_MAX, + "Location", + "Location of vertex in normalized space", + -1.0f, + 1.0f); } /******************** add feather vertex *********************/ static int add_feather_vertex_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - MaskSpline *spline; - MaskSplinePoint *point = NULL; - const float threshold = 9; - float co[2], u; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + MaskSpline *spline; + MaskSplinePoint *point = NULL; + const float threshold = 9; + float co[2], u; - RNA_float_get_array(op->ptr, "location", co); + RNA_float_get_array(op->ptr, "location", co); - point = ED_mask_point_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL); - if (point) - return OPERATOR_FINISHED; + point = ED_mask_point_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL); + if (point) + return OPERATOR_FINISHED; - if (ED_mask_find_nearest_diff_point(C, mask, co, threshold, true, NULL, true, true, - &masklay, &spline, &point, &u, NULL)) - { - Scene *scene = CTX_data_scene(C); - float w = BKE_mask_point_weight(spline, point, u); - float weight_scalar = BKE_mask_point_weight_scalar(spline, point, u); + if (ED_mask_find_nearest_diff_point( + C, mask, co, threshold, true, NULL, true, true, &masklay, &spline, &point, &u, NULL)) { + Scene *scene = CTX_data_scene(C); + float w = BKE_mask_point_weight(spline, point, u); + float weight_scalar = BKE_mask_point_weight_scalar(spline, point, u); - if (weight_scalar != 0.0f) { - w = w / weight_scalar; - } + if (weight_scalar != 0.0f) { + w = w / weight_scalar; + } - BKE_mask_point_add_uw(point, u, w); + BKE_mask_point_add_uw(point, u, w); - BKE_mask_update_display(mask, scene->r.cfra); + BKE_mask_update_display(mask, scene->r.cfra); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - DEG_id_tag_update(&mask->id, 0); + DEG_id_tag_update(&mask->id, 0); - return OPERATOR_FINISHED; - } + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } static int add_feather_vertex_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - float co[2]; + float co[2]; - ED_mask_mouse_pos(sa, ar, event->mval, co); + ED_mask_mouse_pos(sa, ar, event->mval, co); - RNA_float_set_array(op->ptr, "location", co); + RNA_float_set_array(op->ptr, "location", co); - return add_feather_vertex_exec(C, op); + return add_feather_vertex_exec(C, op); } void MASK_OT_add_feather_vertex(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Add Feather Vertex"; - ot->description = "Add vertex to feather"; - ot->idname = "MASK_OT_add_feather_vertex"; - - /* api callbacks */ - ot->exec = add_feather_vertex_exec; - ot->invoke = add_feather_vertex_invoke; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, - "Location", "Location of vertex in normalized space", -1.0f, 1.0f); + /* identifiers */ + ot->name = "Add Feather Vertex"; + ot->description = "Add vertex to feather"; + ot->idname = "MASK_OT_add_feather_vertex"; + + /* api callbacks */ + ot->exec = add_feather_vertex_exec; + ot->invoke = add_feather_vertex_invoke; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_float_vector(ot->srna, + "location", + 2, + NULL, + -FLT_MAX, + FLT_MAX, + "Location", + "Location of vertex in normalized space", + -1.0f, + 1.0f); } /******************** common primitive functions *********************/ -static int create_primitive_from_points(bContext *C, wmOperator *op, const float (*points)[2], - int num_points, char handle_type) +static int create_primitive_from_points( + bContext *C, wmOperator *op, const float (*points)[2], int num_points, char handle_type) { - ScrArea *sa = CTX_wm_area(C); - Scene *scene = CTX_data_scene(C); - Mask *mask; - MaskLayer *mask_layer; - MaskSpline *new_spline; - float scale, location[2], frame_size[2]; - int i, width, height; - int size = RNA_float_get(op->ptr, "size"); - - ED_mask_get_size(sa, &width, &height); - scale = (float)size / max_ii(width, height); - - /* Get location in mask space. */ - frame_size[0] = width; - frame_size[1] = height; - RNA_float_get_array(op->ptr, "location", location); - location[0] /= width; - location[1] /= height; - BKE_mask_coord_from_frame(location, location, frame_size); - - /* Make it so new primitive is centered to mouse location. */ - location[0] -= 0.5f * scale; - location[1] -= 0.5f * scale; - - bool added_mask = false; - mask_layer = ED_mask_layer_ensure(C, &added_mask); - mask = CTX_data_edit_mask(C); - - ED_mask_select_toggle_all(mask, SEL_DESELECT); - - new_spline = BKE_mask_spline_add(mask_layer); - new_spline->flag = MASK_SPLINE_CYCLIC | SELECT; - new_spline->points = MEM_recallocN(new_spline->points, - sizeof(MaskSplinePoint) * num_points); - - mask_layer->act_spline = new_spline; - mask_layer->act_point = NULL; - - const int spline_index = BKE_mask_layer_shape_spline_to_index(mask_layer, new_spline); - - for (i = 0; i < num_points; i++) { - new_spline->tot_point = i + 1; - - MaskSplinePoint *new_point = &new_spline->points[i]; - BKE_mask_parent_init(&new_point->parent); - - copy_v2_v2(new_point->bezt.vec[1], points[i]); - mul_v2_fl(new_point->bezt.vec[1], scale); - add_v2_v2(new_point->bezt.vec[1], location); - - new_point->bezt.h1 = handle_type; - new_point->bezt.h2 = handle_type; - BKE_mask_point_select_set(new_point, true); - - if (mask_layer->splines_shapes.first) { - BKE_mask_layer_shape_changed_add(mask_layer, - spline_index + i, - true, true); - } - } - - if (added_mask) { - WM_event_add_notifier(C, NC_MASK | NA_ADDED, NULL); - } - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - - /* TODO: only update this spline */ - BKE_mask_update_display(mask, CFRA); - - return OPERATOR_FINISHED; + ScrArea *sa = CTX_wm_area(C); + Scene *scene = CTX_data_scene(C); + Mask *mask; + MaskLayer *mask_layer; + MaskSpline *new_spline; + float scale, location[2], frame_size[2]; + int i, width, height; + int size = RNA_float_get(op->ptr, "size"); + + ED_mask_get_size(sa, &width, &height); + scale = (float)size / max_ii(width, height); + + /* Get location in mask space. */ + frame_size[0] = width; + frame_size[1] = height; + RNA_float_get_array(op->ptr, "location", location); + location[0] /= width; + location[1] /= height; + BKE_mask_coord_from_frame(location, location, frame_size); + + /* Make it so new primitive is centered to mouse location. */ + location[0] -= 0.5f * scale; + location[1] -= 0.5f * scale; + + bool added_mask = false; + mask_layer = ED_mask_layer_ensure(C, &added_mask); + mask = CTX_data_edit_mask(C); + + ED_mask_select_toggle_all(mask, SEL_DESELECT); + + new_spline = BKE_mask_spline_add(mask_layer); + new_spline->flag = MASK_SPLINE_CYCLIC | SELECT; + new_spline->points = MEM_recallocN(new_spline->points, sizeof(MaskSplinePoint) * num_points); + + mask_layer->act_spline = new_spline; + mask_layer->act_point = NULL; + + const int spline_index = BKE_mask_layer_shape_spline_to_index(mask_layer, new_spline); + + for (i = 0; i < num_points; i++) { + new_spline->tot_point = i + 1; + + MaskSplinePoint *new_point = &new_spline->points[i]; + BKE_mask_parent_init(&new_point->parent); + + copy_v2_v2(new_point->bezt.vec[1], points[i]); + mul_v2_fl(new_point->bezt.vec[1], scale); + add_v2_v2(new_point->bezt.vec[1], location); + + new_point->bezt.h1 = handle_type; + new_point->bezt.h2 = handle_type; + BKE_mask_point_select_set(new_point, true); + + if (mask_layer->splines_shapes.first) { + BKE_mask_layer_shape_changed_add(mask_layer, spline_index + i, true, true); + } + } + + if (added_mask) { + WM_event_add_notifier(C, NC_MASK | NA_ADDED, NULL); + } + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + + /* TODO: only update this spline */ + BKE_mask_update_display(mask, CFRA); + + return OPERATOR_FINISHED; } static int primitive_add_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - ScrArea *sa = CTX_wm_area(C); - float cursor[2]; - int width, height; + ScrArea *sa = CTX_wm_area(C); + float cursor[2]; + int width, height; - ED_mask_get_size(sa, &width, &height); - ED_mask_cursor_location_get(sa, cursor); + ED_mask_get_size(sa, &width, &height); + ED_mask_cursor_location_get(sa, cursor); - cursor[0] *= width; - cursor[1] *= height; + cursor[0] *= width; + cursor[1] *= height; - RNA_float_set_array(op->ptr, "location", cursor); + RNA_float_set_array(op->ptr, "location", cursor); - return op->type->exec(C, op); + return op->type->exec(C, op); } static void define_prinitive_add_properties(wmOperatorType *ot) { - RNA_def_float(ot->srna, "size", 100, -FLT_MAX, FLT_MAX, - "Size", "Size of new circle", -FLT_MAX, FLT_MAX); - RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, - "Location", "Location of new circle", -FLT_MAX, FLT_MAX); + RNA_def_float( + ot->srna, "size", 100, -FLT_MAX, FLT_MAX, "Size", "Size of new circle", -FLT_MAX, FLT_MAX); + RNA_def_float_vector(ot->srna, + "location", + 2, + NULL, + -FLT_MAX, + FLT_MAX, + "Location", + "Location of new circle", + -FLT_MAX, + FLT_MAX); } /******************** primitive add circle *********************/ static int primitive_circle_add_exec(bContext *C, wmOperator *op) { - const float points[4][2] = {{0.0f, 0.5f}, - {0.5f, 1.0f}, - {1.0f, 0.5f}, - {0.5f, 0.0f}}; - int num_points = sizeof(points) / (2 * sizeof(float)); + const float points[4][2] = {{0.0f, 0.5f}, {0.5f, 1.0f}, {1.0f, 0.5f}, {0.5f, 0.0f}}; + int num_points = sizeof(points) / (2 * sizeof(float)); - create_primitive_from_points(C, op, points, num_points, HD_AUTO); + create_primitive_from_points(C, op, points, num_points, HD_AUTO); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_primitive_circle_add(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Add Circle"; - ot->description = "Add new circle-shaped spline"; - ot->idname = "MASK_OT_primitive_circle_add"; + /* identifiers */ + ot->name = "Add Circle"; + ot->description = "Add new circle-shaped spline"; + ot->idname = "MASK_OT_primitive_circle_add"; - /* api callbacks */ - ot->exec = primitive_circle_add_exec; - ot->invoke = primitive_add_invoke; - ot->poll = ED_operator_mask; + /* api callbacks */ + ot->exec = primitive_circle_add_exec; + ot->invoke = primitive_add_invoke; + ot->poll = ED_operator_mask; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - /* properties */ - define_prinitive_add_properties(ot); + /* properties */ + define_prinitive_add_properties(ot); } /******************** primitive add suqare *********************/ static int primitive_square_add_exec(bContext *C, wmOperator *op) { - const float points[4][2] = {{0.0f, 0.0f}, - {0.0f, 1.0f}, - {1.0f, 1.0f}, - {1.0f, 0.0f}}; - int num_points = sizeof(points) / (2 * sizeof(float)); + const float points[4][2] = {{0.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}}; + int num_points = sizeof(points) / (2 * sizeof(float)); - create_primitive_from_points(C, op, points, num_points, HD_VECT); + create_primitive_from_points(C, op, points, num_points, HD_VECT); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_primitive_square_add(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Add Square"; - ot->description = "Add new square-shaped spline"; - ot->idname = "MASK_OT_primitive_square_add"; + /* identifiers */ + ot->name = "Add Square"; + ot->description = "Add new square-shaped spline"; + ot->idname = "MASK_OT_primitive_square_add"; - /* api callbacks */ - ot->exec = primitive_square_add_exec; - ot->invoke = primitive_add_invoke; - ot->poll = ED_operator_mask; + /* api callbacks */ + ot->exec = primitive_square_add_exec; + ot->invoke = primitive_add_invoke; + ot->poll = ED_operator_mask; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - /* properties */ - define_prinitive_add_properties(ot); + /* properties */ + define_prinitive_add_properties(ot); } diff --git a/source/blender/editors/mask/mask_draw.c b/source/blender/editors/mask/mask_draw.c index 0164fe4cc03..479e4fd2d6c 100644 --- a/source/blender/editors/mask/mask_draw.c +++ b/source/blender/editors/mask/mask_draw.c @@ -33,11 +33,11 @@ #include "DNA_mask_types.h" #include "DNA_screen_types.h" -#include "DNA_object_types.h" /* SELECT */ +#include "DNA_object_types.h" /* SELECT */ #include "DNA_space_types.h" #include "ED_clip.h" -#include "ED_mask.h" /* own include */ +#include "ED_mask.h" /* own include */ #include "ED_space_api.h" #include "BIF_glutil.h" @@ -52,720 +52,761 @@ #include "UI_resources.h" #include "UI_view2d.h" -#include "mask_intern.h" /* own include */ +#include "mask_intern.h" /* own include */ -static void mask_spline_color_get(MaskLayer *masklay, MaskSpline *spline, const bool is_sel, +static void mask_spline_color_get(MaskLayer *masklay, + MaskSpline *spline, + const bool is_sel, unsigned char r_rgb[4]) { - if (is_sel) { - if (masklay->act_spline == spline) { - r_rgb[0] = r_rgb[1] = r_rgb[2] = 255; - } - else { - r_rgb[0] = 255; - r_rgb[1] = r_rgb[2] = 0; - } - } - else { - r_rgb[0] = 128; - r_rgb[1] = r_rgb[2] = 0; - } - - r_rgb[3] = 255; + if (is_sel) { + if (masklay->act_spline == spline) { + r_rgb[0] = r_rgb[1] = r_rgb[2] = 255; + } + else { + r_rgb[0] = 255; + r_rgb[1] = r_rgb[2] = 0; + } + } + else { + r_rgb[0] = 128; + r_rgb[1] = r_rgb[2] = 0; + } + + r_rgb[3] = 255; } -static void mask_spline_feather_color_get(MaskLayer *UNUSED(masklay), MaskSpline *UNUSED(spline), const bool is_sel, +static void mask_spline_feather_color_get(MaskLayer *UNUSED(masklay), + MaskSpline *UNUSED(spline), + const bool is_sel, unsigned char r_rgb[4]) { - if (is_sel) { - r_rgb[1] = 255; - r_rgb[0] = r_rgb[2] = 0; - } - else { - r_rgb[1] = 128; - r_rgb[0] = r_rgb[2] = 0; - } - - r_rgb[3] = 255; + if (is_sel) { + r_rgb[1] = 255; + r_rgb[0] = r_rgb[2] = 0; + } + else { + r_rgb[1] = 128; + r_rgb[0] = r_rgb[2] = 0; + } + + r_rgb[3] = 255; } static void mask_point_undistort_pos(SpaceClip *sc, float r_co[2], const float co[2]) { - BKE_mask_coord_to_movieclip(sc->clip, &sc->user, r_co, co); - ED_clip_point_undistorted_pos(sc, r_co, r_co); - BKE_mask_coord_from_movieclip(sc->clip, &sc->user, r_co, r_co); + BKE_mask_coord_to_movieclip(sc->clip, &sc->user, r_co, co); + ED_clip_point_undistorted_pos(sc, r_co, r_co); + BKE_mask_coord_from_movieclip(sc->clip, &sc->user, r_co, r_co); } -static void draw_single_handle(const MaskLayer *mask_layer, const MaskSplinePoint *point, - const eMaskWhichHandle which_handle, const int draw_type, +static void draw_single_handle(const MaskLayer *mask_layer, + const MaskSplinePoint *point, + const eMaskWhichHandle which_handle, + const int draw_type, const float handle_size, - const float point_pos[2], const float handle_pos[2]) + const float point_pos[2], + const float handle_pos[2]) { - const BezTriple *bezt = &point->bezt; - char handle_type; - - if (which_handle == MASK_WHICH_HANDLE_STICK || which_handle == MASK_WHICH_HANDLE_LEFT) { - handle_type = bezt->h1; - } - else { - handle_type = bezt->h2; - } - - if (handle_type == HD_VECT) { - return; - } - - GPUVertFormat *format = immVertexFormat(); - uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - const unsigned char rgb_gray[4] = {0x60, 0x60, 0x60, 0xff}; - - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - immUniformColor3ubv(rgb_gray); - - /* this could be split into its own loop */ - if (draw_type == MASK_DT_OUTLINE) { - GPU_line_width(3.0f); - immBegin(GPU_PRIM_LINES, 2); - immVertex2fv(pos, point_pos); - immVertex2fv(pos, handle_pos); - immEnd(); - } - - switch (handle_type) { - case HD_FREE: - immUniformThemeColor(TH_HANDLE_FREE); - break; - case HD_AUTO: - immUniformThemeColor(TH_HANDLE_AUTO); - break; - case HD_ALIGN: - case HD_ALIGN_DOUBLESIDE: - immUniformThemeColor(TH_HANDLE_ALIGN); - break; - } - - GPU_line_width(1.0f); - immBegin(GPU_PRIM_LINES, 2); - immVertex2fv(pos, point_pos); - immVertex2fv(pos, handle_pos); - immEnd(); - immUnbindProgram(); - - /* draw handle points */ - immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA); - immUniform1f("size", handle_size); - immUniform1f("outlineWidth", 1.5f); - - float point_color[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; /* active color by default */ - if (MASKPOINT_ISSEL_HANDLE(point, which_handle)) { - if (point != mask_layer->act_point) { - UI_GetThemeColor3fv(TH_HANDLE_VERTEX_SELECT, point_color); - } - } - else { - UI_GetThemeColor3fv(TH_HANDLE_VERTEX, point_color); - } - - immUniform4fv("outlineColor", point_color); - immUniformColor3fvAlpha(point_color, 0.25f); - - immBegin(GPU_PRIM_POINTS, 1); - immVertex2fv(pos, handle_pos); - immEnd(); - - immUnbindProgram(); + const BezTriple *bezt = &point->bezt; + char handle_type; + + if (which_handle == MASK_WHICH_HANDLE_STICK || which_handle == MASK_WHICH_HANDLE_LEFT) { + handle_type = bezt->h1; + } + else { + handle_type = bezt->h2; + } + + if (handle_type == HD_VECT) { + return; + } + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + const unsigned char rgb_gray[4] = {0x60, 0x60, 0x60, 0xff}; + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor3ubv(rgb_gray); + + /* this could be split into its own loop */ + if (draw_type == MASK_DT_OUTLINE) { + GPU_line_width(3.0f); + immBegin(GPU_PRIM_LINES, 2); + immVertex2fv(pos, point_pos); + immVertex2fv(pos, handle_pos); + immEnd(); + } + + switch (handle_type) { + case HD_FREE: + immUniformThemeColor(TH_HANDLE_FREE); + break; + case HD_AUTO: + immUniformThemeColor(TH_HANDLE_AUTO); + break; + case HD_ALIGN: + case HD_ALIGN_DOUBLESIDE: + immUniformThemeColor(TH_HANDLE_ALIGN); + break; + } + + GPU_line_width(1.0f); + immBegin(GPU_PRIM_LINES, 2); + immVertex2fv(pos, point_pos); + immVertex2fv(pos, handle_pos); + immEnd(); + immUnbindProgram(); + + /* draw handle points */ + immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA); + immUniform1f("size", handle_size); + immUniform1f("outlineWidth", 1.5f); + + float point_color[4] = {1.0f, 1.0f, 1.0f, 1.0f}; /* active color by default */ + if (MASKPOINT_ISSEL_HANDLE(point, which_handle)) { + if (point != mask_layer->act_point) { + UI_GetThemeColor3fv(TH_HANDLE_VERTEX_SELECT, point_color); + } + } + else { + UI_GetThemeColor3fv(TH_HANDLE_VERTEX, point_color); + } + + immUniform4fv("outlineColor", point_color); + immUniformColor3fvAlpha(point_color, 0.25f); + + immBegin(GPU_PRIM_POINTS, 1); + immVertex2fv(pos, handle_pos); + immEnd(); + + immUnbindProgram(); } /* return non-zero if spline is selected */ -static void draw_spline_points(const bContext *C, MaskLayer *masklay, MaskSpline *spline, - const char draw_flag, const char draw_type) +static void draw_spline_points(const bContext *C, + MaskLayer *masklay, + MaskSpline *spline, + const char draw_flag, + const char draw_type) { - const bool is_spline_sel = (spline->flag & SELECT) && (masklay->restrictflag & MASK_RESTRICT_SELECT) == 0; - const bool is_smooth = (draw_flag & MASK_DRAWFLAG_SMOOTH) != 0; - - unsigned char rgb_spline[4]; - MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - SpaceClip *sc = CTX_wm_space_clip(C); - bool undistort = false; - - int tot_feather_point; - float (*feather_points)[2], (*fp)[2]; - float min[2], max[2]; - - if (!spline->tot_point) - return; - - if (sc) - undistort = sc->clip && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT); - - /* TODO, add this to sequence editor */ - float handle_size = 2.0f * UI_GetThemeValuef(TH_HANDLE_VERTEX_SIZE) * U.pixelsize; - - mask_spline_color_get(masklay, spline, is_spline_sel, rgb_spline); - - GPUVertFormat *format = immVertexFormat(); - uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - - immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA); - immUniform1f("size", 0.7f * handle_size); - - /* feather points */ - feather_points = fp = BKE_mask_spline_feather_points(spline, &tot_feather_point); - for (int i = 0; i < spline->tot_point; i++) { - - /* watch it! this is intentionally not the deform array, only check for sel */ - MaskSplinePoint *point = &spline->points[i]; - - for (int j = 0; j <= point->tot_uw; j++) { - float feather_point[2]; - bool sel = false; - - copy_v2_v2(feather_point, *fp); - - if (undistort) - mask_point_undistort_pos(sc, feather_point, feather_point); - - if (j == 0) { - sel = MASKPOINT_ISSEL_ANY(point); - } - else { - sel = (point->uw[j - 1].flag & SELECT) != 0; - } - - if (sel) { - if (point == masklay->act_point) - immUniformColor3f(1.0f, 1.0f, 1.0f); - else - immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX_SELECT, 0, 255); - } - else { - immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX, 0, 255); - } - - immBegin(GPU_PRIM_POINTS, 1); - immVertex2fv(pos, feather_point); - immEnd(); - - fp++; - } - } - MEM_freeN(feather_points); - - immUnbindProgram(); - - if (is_smooth) { - GPU_line_smooth(true); - } - - /* control points */ - INIT_MINMAX2(min, max); - for (int i = 0; i < spline->tot_point; i++) { - - /* watch it! this is intentionally not the deform array, only check for sel */ - MaskSplinePoint *point = &spline->points[i]; - MaskSplinePoint *point_deform = &points_array[i]; - BezTriple *bezt = &point_deform->bezt; - - float vert[2]; - - copy_v2_v2(vert, bezt->vec[1]); - - if (undistort) { - mask_point_undistort_pos(sc, vert, vert); - } - - /* draw handle segment */ - if (BKE_mask_point_handles_mode_get(point) == MASK_HANDLE_MODE_STICK) { - float handle[2]; - BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_STICK, handle); - if (undistort) { - mask_point_undistort_pos(sc, handle, handle); - } - draw_single_handle(masklay, point, MASK_WHICH_HANDLE_STICK, - draw_type, handle_size, vert, handle); - } - else { - float handle_left[2], handle_right[2]; - BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_LEFT, handle_left); - BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_RIGHT, handle_right); - if (undistort) { - mask_point_undistort_pos(sc, handle_left, handle_left); - mask_point_undistort_pos(sc, handle_left, handle_left); - } - draw_single_handle(masklay, point, MASK_WHICH_HANDLE_LEFT, - draw_type, handle_size, vert, handle_left); - draw_single_handle(masklay, point, MASK_WHICH_HANDLE_RIGHT, - draw_type, handle_size, vert, handle_right); - } - - /* bind program in loop so it does not interfere with draw_single_handle */ - immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA); - - /* draw CV point */ - if (MASKPOINT_ISSEL_KNOT(point)) { - if (point == masklay->act_point) - immUniformColor3f(1.0f, 1.0f, 1.0f); - else - immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX_SELECT, 0, 255); - } - else - immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX, 0, 255); - - immBegin(GPU_PRIM_POINTS, 1); - immVertex2fv(pos, vert); - immEnd(); - - immUnbindProgram(); - - minmax_v2v2_v2(min, max, vert); - } - - if (is_smooth) { - GPU_line_smooth(false); - } - - if (is_spline_sel) { - float x = (min[0] + max[0]) * 0.5f; - float y = (min[1] + max[1]) * 0.5f; - - immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA); - immUniform1f("outlineWidth", 1.5f); - - if (masklay->act_spline == spline) { - immUniformColor3f(1.0f, 1.0f, 1.0f); - } - else { - immUniformColor3f(1.0f, 1.0f, 0.0f); - } - - immUniform4f("outlineColor", 0.0f, 0.0f, 0.0f, 1.0f); - immUniform1f("size", 12.0f); - - immBegin(GPU_PRIM_POINTS, 1); - immVertex2f(pos, x, y); - immEnd(); - - immUnbindProgram(); - } + const bool is_spline_sel = (spline->flag & SELECT) && + (masklay->restrictflag & MASK_RESTRICT_SELECT) == 0; + const bool is_smooth = (draw_flag & MASK_DRAWFLAG_SMOOTH) != 0; + + unsigned char rgb_spline[4]; + MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + SpaceClip *sc = CTX_wm_space_clip(C); + bool undistort = false; + + int tot_feather_point; + float(*feather_points)[2], (*fp)[2]; + float min[2], max[2]; + + if (!spline->tot_point) + return; + + if (sc) + undistort = sc->clip && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT); + + /* TODO, add this to sequence editor */ + float handle_size = 2.0f * UI_GetThemeValuef(TH_HANDLE_VERTEX_SIZE) * U.pixelsize; + + mask_spline_color_get(masklay, spline, is_spline_sel, rgb_spline); + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA); + immUniform1f("size", 0.7f * handle_size); + + /* feather points */ + feather_points = fp = BKE_mask_spline_feather_points(spline, &tot_feather_point); + for (int i = 0; i < spline->tot_point; i++) { + + /* watch it! this is intentionally not the deform array, only check for sel */ + MaskSplinePoint *point = &spline->points[i]; + + for (int j = 0; j <= point->tot_uw; j++) { + float feather_point[2]; + bool sel = false; + + copy_v2_v2(feather_point, *fp); + + if (undistort) + mask_point_undistort_pos(sc, feather_point, feather_point); + + if (j == 0) { + sel = MASKPOINT_ISSEL_ANY(point); + } + else { + sel = (point->uw[j - 1].flag & SELECT) != 0; + } + + if (sel) { + if (point == masklay->act_point) + immUniformColor3f(1.0f, 1.0f, 1.0f); + else + immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX_SELECT, 0, 255); + } + else { + immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX, 0, 255); + } + + immBegin(GPU_PRIM_POINTS, 1); + immVertex2fv(pos, feather_point); + immEnd(); + + fp++; + } + } + MEM_freeN(feather_points); + + immUnbindProgram(); + + if (is_smooth) { + GPU_line_smooth(true); + } + + /* control points */ + INIT_MINMAX2(min, max); + for (int i = 0; i < spline->tot_point; i++) { + + /* watch it! this is intentionally not the deform array, only check for sel */ + MaskSplinePoint *point = &spline->points[i]; + MaskSplinePoint *point_deform = &points_array[i]; + BezTriple *bezt = &point_deform->bezt; + + float vert[2]; + + copy_v2_v2(vert, bezt->vec[1]); + + if (undistort) { + mask_point_undistort_pos(sc, vert, vert); + } + + /* draw handle segment */ + if (BKE_mask_point_handles_mode_get(point) == MASK_HANDLE_MODE_STICK) { + float handle[2]; + BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_STICK, handle); + if (undistort) { + mask_point_undistort_pos(sc, handle, handle); + } + draw_single_handle( + masklay, point, MASK_WHICH_HANDLE_STICK, draw_type, handle_size, vert, handle); + } + else { + float handle_left[2], handle_right[2]; + BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_LEFT, handle_left); + BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_RIGHT, handle_right); + if (undistort) { + mask_point_undistort_pos(sc, handle_left, handle_left); + mask_point_undistort_pos(sc, handle_left, handle_left); + } + draw_single_handle( + masklay, point, MASK_WHICH_HANDLE_LEFT, draw_type, handle_size, vert, handle_left); + draw_single_handle( + masklay, point, MASK_WHICH_HANDLE_RIGHT, draw_type, handle_size, vert, handle_right); + } + + /* bind program in loop so it does not interfere with draw_single_handle */ + immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA); + + /* draw CV point */ + if (MASKPOINT_ISSEL_KNOT(point)) { + if (point == masklay->act_point) + immUniformColor3f(1.0f, 1.0f, 1.0f); + else + immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX_SELECT, 0, 255); + } + else + immUniformThemeColorShadeAlpha(TH_HANDLE_VERTEX, 0, 255); + + immBegin(GPU_PRIM_POINTS, 1); + immVertex2fv(pos, vert); + immEnd(); + + immUnbindProgram(); + + minmax_v2v2_v2(min, max, vert); + } + + if (is_smooth) { + GPU_line_smooth(false); + } + + if (is_spline_sel) { + float x = (min[0] + max[0]) * 0.5f; + float y = (min[1] + max[1]) * 0.5f; + + immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA); + immUniform1f("outlineWidth", 1.5f); + + if (masklay->act_spline == spline) { + immUniformColor3f(1.0f, 1.0f, 1.0f); + } + else { + immUniformColor3f(1.0f, 1.0f, 0.0f); + } + + immUniform4f("outlineColor", 0.0f, 0.0f, 0.0f, 1.0f); + immUniform1f("size", 12.0f); + + immBegin(GPU_PRIM_POINTS, 1); + immVertex2f(pos, x, y); + immEnd(); + + immUnbindProgram(); + } } -static void mask_color_active_tint(unsigned char r_rgb[4], const unsigned char rgb[4], const bool is_active) +static void mask_color_active_tint(unsigned char r_rgb[4], + const unsigned char rgb[4], + const bool is_active) { - if (!is_active) { - r_rgb[0] = (unsigned char)((((int)(rgb[0])) + 128) / 2); - r_rgb[1] = (unsigned char)((((int)(rgb[1])) + 128) / 2); - r_rgb[2] = (unsigned char)((((int)(rgb[2])) + 128) / 2); - r_rgb[3] = rgb[3]; - } - else { - *(unsigned int *)r_rgb = *(const unsigned int *)rgb; - } + if (!is_active) { + r_rgb[0] = (unsigned char)((((int)(rgb[0])) + 128) / 2); + r_rgb[1] = (unsigned char)((((int)(rgb[1])) + 128) / 2); + r_rgb[2] = (unsigned char)((((int)(rgb[2])) + 128) / 2); + r_rgb[3] = rgb[3]; + } + else { + *(unsigned int *)r_rgb = *(const unsigned int *)rgb; + } } -static void mask_draw_array(unsigned int pos, GPUPrimType prim_type, const float (*points)[2], unsigned int vertex_len) +static void mask_draw_array(unsigned int pos, + GPUPrimType prim_type, + const float (*points)[2], + unsigned int vertex_len) { - immBegin(prim_type, vertex_len); - for (unsigned int i = 0; i < vertex_len; ++i) { - immVertex2fv(pos, points[i]); - } - immEnd(); + immBegin(prim_type, vertex_len); + for (unsigned int i = 0; i < vertex_len; ++i) { + immVertex2fv(pos, points[i]); + } + immEnd(); } -static void mask_draw_curve_type(const bContext *C, MaskSpline *spline, float (*orig_points)[2], int tot_point, - const bool is_feather, const bool is_active, - const unsigned char rgb_spline[4], const char draw_type) +static void mask_draw_curve_type(const bContext *C, + MaskSpline *spline, + float (*orig_points)[2], + int tot_point, + const bool is_feather, + const bool is_active, + const unsigned char rgb_spline[4], + const char draw_type) { - const GPUPrimType draw_method = (spline->flag & MASK_SPLINE_CYCLIC) ? GPU_PRIM_LINE_LOOP : GPU_PRIM_LINE_STRIP; - const unsigned char rgb_black[4] = {0x00, 0x00, 0x00, 0xff}; - unsigned char rgb_tmp[4]; - SpaceClip *sc = CTX_wm_space_clip(C); - float (*points)[2] = orig_points; - - if (sc) { - const bool undistort = sc->clip && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT); - - if (undistort) { - points = MEM_callocN(2 * tot_point * sizeof(float), "undistorthed mask curve"); - - for (int i = 0; i < tot_point; i++) { - mask_point_undistort_pos(sc, points[i], orig_points[i]); - } - } - } - - GPUVertFormat *format = immVertexFormat(); - uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); - - switch (draw_type) { - - case MASK_DT_OUTLINE: - /* TODO(merwin): use fancy line shader here - * probably better with geometry shader (after core profile switch) - */ - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - - GPU_line_width(3.0f); - - mask_color_active_tint(rgb_tmp, rgb_black, is_active); - immUniformColor4ubv(rgb_tmp); - mask_draw_array(pos, draw_method, points, tot_point); - - GPU_line_width(1.0f); - - mask_color_active_tint(rgb_tmp, rgb_spline, is_active); - immUniformColor4ubv(rgb_tmp); - mask_draw_array(pos, draw_method, points, tot_point); - - immUnbindProgram(); - break; - - case MASK_DT_BLACK: - case MASK_DT_WHITE: - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - GPU_line_width(1.0f); - - if (draw_type == MASK_DT_BLACK) { rgb_tmp[0] = rgb_tmp[1] = rgb_tmp[2] = 0; } - else { rgb_tmp[0] = rgb_tmp[1] = rgb_tmp[2] = 255; } - /* alpha values seem too low but gl draws many points that compensate for it */ - if (is_feather) { rgb_tmp[3] = 64; } - else { rgb_tmp[3] = 128; } - - if (is_feather) { - rgb_tmp[0] = (unsigned char)(((short)rgb_tmp[0] + (short)rgb_spline[0]) / 2); - rgb_tmp[1] = (unsigned char)(((short)rgb_tmp[1] + (short)rgb_spline[1]) / 2); - rgb_tmp[2] = (unsigned char)(((short)rgb_tmp[2] + (short)rgb_spline[2]) / 2); - } - - mask_color_active_tint(rgb_tmp, rgb_tmp, is_active); - immUniformColor4ubv(rgb_tmp); - mask_draw_array(pos, draw_method, points, tot_point); - - immUnbindProgram(); - break; + const GPUPrimType draw_method = (spline->flag & MASK_SPLINE_CYCLIC) ? GPU_PRIM_LINE_LOOP : + GPU_PRIM_LINE_STRIP; + const unsigned char rgb_black[4] = {0x00, 0x00, 0x00, 0xff}; + unsigned char rgb_tmp[4]; + SpaceClip *sc = CTX_wm_space_clip(C); + float(*points)[2] = orig_points; + + if (sc) { + const bool undistort = sc->clip && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT); + + if (undistort) { + points = MEM_callocN(2 * tot_point * sizeof(float), "undistorthed mask curve"); + + for (int i = 0; i < tot_point; i++) { + mask_point_undistort_pos(sc, points[i], orig_points[i]); + } + } + } + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + switch (draw_type) { + + case MASK_DT_OUTLINE: + /* TODO(merwin): use fancy line shader here + * probably better with geometry shader (after core profile switch) + */ + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + + GPU_line_width(3.0f); + + mask_color_active_tint(rgb_tmp, rgb_black, is_active); + immUniformColor4ubv(rgb_tmp); + mask_draw_array(pos, draw_method, points, tot_point); + + GPU_line_width(1.0f); + + mask_color_active_tint(rgb_tmp, rgb_spline, is_active); + immUniformColor4ubv(rgb_tmp); + mask_draw_array(pos, draw_method, points, tot_point); + + immUnbindProgram(); + break; + + case MASK_DT_BLACK: + case MASK_DT_WHITE: + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + GPU_line_width(1.0f); + + if (draw_type == MASK_DT_BLACK) { + rgb_tmp[0] = rgb_tmp[1] = rgb_tmp[2] = 0; + } + else { + rgb_tmp[0] = rgb_tmp[1] = rgb_tmp[2] = 255; + } + /* alpha values seem too low but gl draws many points that compensate for it */ + if (is_feather) { + rgb_tmp[3] = 64; + } + else { + rgb_tmp[3] = 128; + } + + if (is_feather) { + rgb_tmp[0] = (unsigned char)(((short)rgb_tmp[0] + (short)rgb_spline[0]) / 2); + rgb_tmp[1] = (unsigned char)(((short)rgb_tmp[1] + (short)rgb_spline[1]) / 2); + rgb_tmp[2] = (unsigned char)(((short)rgb_tmp[2] + (short)rgb_spline[2]) / 2); + } + + mask_color_active_tint(rgb_tmp, rgb_tmp, is_active); + immUniformColor4ubv(rgb_tmp); + mask_draw_array(pos, draw_method, points, tot_point); + + immUnbindProgram(); + break; + + case MASK_DT_DASH: { + float colors[8]; + + mask_color_active_tint(rgb_tmp, rgb_spline, is_active); + rgba_uchar_to_float(colors, rgb_tmp); + mask_color_active_tint(rgb_tmp, rgb_black, is_active); + rgba_uchar_to_float(colors + 4, rgb_tmp); + + immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR); + + float viewport_size[4]; + GPU_viewport_size_get_f(viewport_size); + immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC); + + immUniform1i("colors_len", 2); /* "advanced" mode */ + immUniformArray4fv("colors", colors, 2); + immUniform1f("dash_width", 4.0f); + GPU_line_width(1.0f); + + mask_draw_array(pos, draw_method, points, tot_point); + + immUnbindProgram(); + break; + } + + default: + BLI_assert(false); + } - case MASK_DT_DASH: - { - float colors[8]; - - mask_color_active_tint(rgb_tmp, rgb_spline, is_active); - rgba_uchar_to_float(colors, rgb_tmp); - mask_color_active_tint(rgb_tmp, rgb_black, is_active); - rgba_uchar_to_float(colors + 4, rgb_tmp); - - immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR); - - float viewport_size[4]; - GPU_viewport_size_get_f(viewport_size); - immUniform2f("viewport_size", viewport_size[2] / UI_DPI_FAC, viewport_size[3] / UI_DPI_FAC); - - immUniform1i("colors_len", 2); /* "advanced" mode */ - immUniformArray4fv("colors", colors, 2); - immUniform1f("dash_width", 4.0f); - GPU_line_width(1.0f); - - mask_draw_array(pos, draw_method, points, tot_point); - - immUnbindProgram(); - break; - } - - default: - BLI_assert(false); - } - - if (points != orig_points) - MEM_freeN(points); + if (points != orig_points) + MEM_freeN(points); } -static void draw_spline_curve(const bContext *C, MaskLayer *masklay, MaskSpline *spline, - const char draw_flag, const char draw_type, +static void draw_spline_curve(const bContext *C, + MaskLayer *masklay, + MaskSpline *spline, + const char draw_flag, + const char draw_type, const bool is_active, - const int width, const int height) + const int width, + const int height) { - const unsigned int resol = max_ii(BKE_mask_spline_feather_resolution(spline, width, height), - BKE_mask_spline_resolution(spline, width, height)); + const unsigned int resol = max_ii(BKE_mask_spline_feather_resolution(spline, width, height), + BKE_mask_spline_resolution(spline, width, height)); - unsigned char rgb_tmp[4]; + unsigned char rgb_tmp[4]; - const bool is_spline_sel = (spline->flag & SELECT) && (masklay->restrictflag & MASK_RESTRICT_SELECT) == 0; - const bool is_smooth = (draw_flag & MASK_DRAWFLAG_SMOOTH) != 0; - const bool is_fill = (spline->flag & MASK_SPLINE_NOFILL) == 0; + const bool is_spline_sel = (spline->flag & SELECT) && + (masklay->restrictflag & MASK_RESTRICT_SELECT) == 0; + const bool is_smooth = (draw_flag & MASK_DRAWFLAG_SMOOTH) != 0; + const bool is_fill = (spline->flag & MASK_SPLINE_NOFILL) == 0; - unsigned int tot_diff_point; - float (*diff_points)[2]; + unsigned int tot_diff_point; + float(*diff_points)[2]; - unsigned int tot_feather_point; - float (*feather_points)[2]; + unsigned int tot_feather_point; + float(*feather_points)[2]; - diff_points = BKE_mask_spline_differentiate_with_resolution(spline, &tot_diff_point, resol); + diff_points = BKE_mask_spline_differentiate_with_resolution(spline, &tot_diff_point, resol); - if (!diff_points) - return; + if (!diff_points) + return; - if (is_smooth) { - GPU_line_smooth(true); - } + if (is_smooth) { + GPU_line_smooth(true); + } - feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution(spline, &tot_feather_point, resol, (is_fill != false)); + feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution( + spline, &tot_feather_point, resol, (is_fill != false)); - /* draw feather */ - mask_spline_feather_color_get(masklay, spline, is_spline_sel, rgb_tmp); - mask_draw_curve_type(C, spline, feather_points, tot_feather_point, - true, is_active, - rgb_tmp, draw_type); + /* draw feather */ + mask_spline_feather_color_get(masklay, spline, is_spline_sel, rgb_tmp); + mask_draw_curve_type( + C, spline, feather_points, tot_feather_point, true, is_active, rgb_tmp, draw_type); - if (!is_fill) { - const float *fp = &diff_points[0][0]; - float *fp_feather = &feather_points[0][0]; + if (!is_fill) { + const float *fp = &diff_points[0][0]; + float *fp_feather = &feather_points[0][0]; - BLI_assert(tot_diff_point == tot_feather_point); + BLI_assert(tot_diff_point == tot_feather_point); - for (int i = 0; i < tot_diff_point; i++, fp += 2, fp_feather += 2) { - float tvec[2]; - sub_v2_v2v2(tvec, fp, fp_feather); - add_v2_v2v2(fp_feather, fp, tvec); - } + for (int i = 0; i < tot_diff_point; i++, fp += 2, fp_feather += 2) { + float tvec[2]; + sub_v2_v2v2(tvec, fp, fp_feather); + add_v2_v2v2(fp_feather, fp, tvec); + } - /* same as above */ - mask_draw_curve_type(C, spline, feather_points, tot_feather_point, - true, is_active, - rgb_tmp, draw_type); - } + /* same as above */ + mask_draw_curve_type( + C, spline, feather_points, tot_feather_point, true, is_active, rgb_tmp, draw_type); + } - MEM_freeN(feather_points); + MEM_freeN(feather_points); - /* draw main curve */ - mask_spline_color_get(masklay, spline, is_spline_sel, rgb_tmp); - mask_draw_curve_type(C, spline, diff_points, tot_diff_point, - false, is_active, - rgb_tmp, draw_type); - MEM_freeN(diff_points); + /* draw main curve */ + mask_spline_color_get(masklay, spline, is_spline_sel, rgb_tmp); + mask_draw_curve_type( + C, spline, diff_points, tot_diff_point, false, is_active, rgb_tmp, draw_type); + MEM_freeN(diff_points); - if (is_smooth) { - GPU_line_smooth(false); - } + if (is_smooth) { + GPU_line_smooth(false); + } } -static void draw_masklays(const bContext *C, Mask *mask, const char draw_flag, const char draw_type, - const int width, const int height) +static void draw_masklays(const bContext *C, + Mask *mask, + const char draw_flag, + const char draw_type, + const int width, + const int height) { - GPU_blend(true); - GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA); - GPU_enable_program_point_size(); - - MaskLayer *masklay; - int i; - - for (masklay = mask->masklayers.first, i = 0; masklay; masklay = masklay->next, i++) { - MaskSpline *spline; - const bool is_active = (i == mask->masklay_act); - - if (masklay->restrictflag & MASK_RESTRICT_VIEW) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - - /* draw curve itself first... */ - draw_spline_curve(C, masklay, spline, draw_flag, draw_type, is_active, width, height); - - if (!(masklay->restrictflag & MASK_RESTRICT_SELECT)) { - /* ...and then handles over the curve so they're nicely visible */ - draw_spline_points(C, masklay, spline, draw_flag, draw_type); - } - - /* show undeform for testing */ - if (0) { - void *back = spline->points_deform; - - spline->points_deform = NULL; - draw_spline_curve(C, masklay, spline, draw_flag, draw_type, is_active, width, height); - draw_spline_points(C, masklay, spline, draw_flag, draw_type); - spline->points_deform = back; - } - } - } - - GPU_disable_program_point_size(); - GPU_blend(false); + GPU_blend(true); + GPU_blend_set_func_separate( + GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA); + GPU_enable_program_point_size(); + + MaskLayer *masklay; + int i; + + for (masklay = mask->masklayers.first, i = 0; masklay; masklay = masklay->next, i++) { + MaskSpline *spline; + const bool is_active = (i == mask->masklay_act); + + if (masklay->restrictflag & MASK_RESTRICT_VIEW) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + + /* draw curve itself first... */ + draw_spline_curve(C, masklay, spline, draw_flag, draw_type, is_active, width, height); + + if (!(masklay->restrictflag & MASK_RESTRICT_SELECT)) { + /* ...and then handles over the curve so they're nicely visible */ + draw_spline_points(C, masklay, spline, draw_flag, draw_type); + } + + /* show undeform for testing */ + if (0) { + void *back = spline->points_deform; + + spline->points_deform = NULL; + draw_spline_curve(C, masklay, spline, draw_flag, draw_type, is_active, width, height); + draw_spline_points(C, masklay, spline, draw_flag, draw_type); + spline->points_deform = back; + } + } + } + + GPU_disable_program_point_size(); + GPU_blend(false); } -void ED_mask_draw(const bContext *C, - const char draw_flag, const char draw_type) +void ED_mask_draw(const bContext *C, const char draw_flag, const char draw_type) { - ScrArea *sa = CTX_wm_area(C); - Mask *mask = CTX_data_edit_mask(C); - int width, height; + ScrArea *sa = CTX_wm_area(C); + Mask *mask = CTX_data_edit_mask(C); + int width, height; - if (!mask) - return; + if (!mask) + return; - ED_mask_get_size(sa, &width, &height); + ED_mask_get_size(sa, &width, &height); - draw_masklays(C, mask, draw_flag, draw_type, width, height); + draw_masklays(C, mask, draw_flag, draw_type, width, height); } static float *mask_rasterize(Mask *mask, const int width, const int height) { - MaskRasterHandle *handle; - float *buffer = MEM_mallocN(sizeof(float) * height * width, "rasterized mask buffer"); + MaskRasterHandle *handle; + float *buffer = MEM_mallocN(sizeof(float) * height * width, "rasterized mask buffer"); - /* Initialize rasterization handle. */ - handle = BKE_maskrasterize_handle_new(); - BKE_maskrasterize_handle_init(handle, mask, width, height, true, true, true); + /* Initialize rasterization handle. */ + handle = BKE_maskrasterize_handle_new(); + BKE_maskrasterize_handle_init(handle, mask, width, height, true, true, true); - BKE_maskrasterize_buffer(handle, width, height, buffer); + BKE_maskrasterize_buffer(handle, width, height, buffer); - /* Free memory. */ - BKE_maskrasterize_handle_free(handle); + /* Free memory. */ + BKE_maskrasterize_handle_free(handle); - return buffer; + return buffer; } /* sets up the opengl context. * width, height are to match the values from ED_mask_get_size() */ void ED_mask_draw_region( - Mask *mask, ARegion *ar, - const char draw_flag, const char draw_type, const char overlay_mode, - /* convert directly into aspect corrected vars */ - const int width_i, const int height_i, - const float aspx, const float aspy, - const bool do_scale_applied, const bool do_draw_cb, - /* optional - only used by clip */ - float stabmat[4][4], - /* optional - only used when do_post_draw is set or called from clip editor */ - const bContext *C) + Mask *mask, + ARegion *ar, + const char draw_flag, + const char draw_type, + const char overlay_mode, + /* convert directly into aspect corrected vars */ + const int width_i, + const int height_i, + const float aspx, + const float aspy, + const bool do_scale_applied, + const bool do_draw_cb, + /* optional - only used by clip */ + float stabmat[4][4], + /* optional - only used when do_post_draw is set or called from clip editor */ + const bContext *C) { - struct View2D *v2d = &ar->v2d; - - /* aspect always scales vertically in movie and image spaces */ - const float width = width_i, height = (float)height_i * (aspy / aspx); - - int x, y; - /* int w, h; */ - float zoomx, zoomy; - - /* frame image */ - float maxdim; - float xofs, yofs; - - /* find window pixel coordinates of origin */ - UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y); - - - /* w = BLI_rctf_size_x(&v2d->tot); */ - /* h = BLI_rctf_size_y(&v2d->tot); */ - - - zoomx = (float)(BLI_rcti_size_x(&ar->winrct) + 1) / BLI_rctf_size_x(&ar->v2d.cur); - zoomy = (float)(BLI_rcti_size_y(&ar->winrct) + 1) / BLI_rctf_size_y(&ar->v2d.cur); - - if (do_scale_applied) { - zoomx /= width; - zoomy /= height; - } - - x += v2d->tot.xmin * zoomx; - y += v2d->tot.ymin * zoomy; - - /* frame the image */ - maxdim = max_ff(width, height); - if (width == height) { - xofs = yofs = 0; - } - else if (width < height) { - xofs = ((height - width) / -2.0f) * zoomx; - yofs = 0.0f; - } - else { /* (width > height) */ - xofs = 0.0f; - yofs = ((width - height) / -2.0f) * zoomy; - } - - if (draw_flag & MASK_DRAWFLAG_OVERLAY) { - float red[4] = {1.0f, 0.0f, 0.0f, 0.0f}; - float *buffer = mask_rasterize(mask, width, height); - - if (overlay_mode != MASK_OVERLAY_ALPHACHANNEL) { - /* More blending types could be supported in the future. */ - GPU_blend(true); - GPU_blend_set_func(GPU_DST_COLOR, GPU_ZERO); - } - - GPU_matrix_push(); - GPU_matrix_translate_2f(x, y); - GPU_matrix_scale_2f(zoomx, zoomy); - if (stabmat) { - GPU_matrix_mul(stabmat); - } - IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR); - GPU_shader_uniform_vector(state.shader, GPU_shader_get_uniform_ensure(state.shader, "shuffle"), 4, 1, red); - immDrawPixelsTex(&state, 0.0f, 0.0f, width, height, GL_RED, GL_FLOAT, GL_NEAREST, buffer, 1.0f, 1.0f, NULL); - - GPU_matrix_pop(); - - if (overlay_mode != MASK_OVERLAY_ALPHACHANNEL) { - GPU_blend(false); - } - - MEM_freeN(buffer); - } - - /* apply transformation so mask editing tools will assume drawing from the - * origin in normalized space */ - GPU_matrix_push(); - GPU_matrix_translate_2f(x + xofs, y + yofs); - GPU_matrix_scale_2f(zoomx, zoomy); - if (stabmat) { - GPU_matrix_mul(stabmat); - } - GPU_matrix_scale_2f(maxdim, maxdim); - - if (do_draw_cb) { - ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW); - } - - /* draw! */ - draw_masklays(C, mask, draw_flag, draw_type, width, height); - - if (do_draw_cb) { - ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW); - } - - GPU_matrix_pop(); + struct View2D *v2d = &ar->v2d; + + /* aspect always scales vertically in movie and image spaces */ + const float width = width_i, height = (float)height_i * (aspy / aspx); + + int x, y; + /* int w, h; */ + float zoomx, zoomy; + + /* frame image */ + float maxdim; + float xofs, yofs; + + /* find window pixel coordinates of origin */ + UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y); + + /* w = BLI_rctf_size_x(&v2d->tot); */ + /* h = BLI_rctf_size_y(&v2d->tot); */ + + zoomx = (float)(BLI_rcti_size_x(&ar->winrct) + 1) / BLI_rctf_size_x(&ar->v2d.cur); + zoomy = (float)(BLI_rcti_size_y(&ar->winrct) + 1) / BLI_rctf_size_y(&ar->v2d.cur); + + if (do_scale_applied) { + zoomx /= width; + zoomy /= height; + } + + x += v2d->tot.xmin * zoomx; + y += v2d->tot.ymin * zoomy; + + /* frame the image */ + maxdim = max_ff(width, height); + if (width == height) { + xofs = yofs = 0; + } + else if (width < height) { + xofs = ((height - width) / -2.0f) * zoomx; + yofs = 0.0f; + } + else { /* (width > height) */ + xofs = 0.0f; + yofs = ((width - height) / -2.0f) * zoomy; + } + + if (draw_flag & MASK_DRAWFLAG_OVERLAY) { + float red[4] = {1.0f, 0.0f, 0.0f, 0.0f}; + float *buffer = mask_rasterize(mask, width, height); + + if (overlay_mode != MASK_OVERLAY_ALPHACHANNEL) { + /* More blending types could be supported in the future. */ + GPU_blend(true); + GPU_blend_set_func(GPU_DST_COLOR, GPU_ZERO); + } + + GPU_matrix_push(); + GPU_matrix_translate_2f(x, y); + GPU_matrix_scale_2f(zoomx, zoomy); + if (stabmat) { + GPU_matrix_mul(stabmat); + } + IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR); + GPU_shader_uniform_vector( + state.shader, GPU_shader_get_uniform_ensure(state.shader, "shuffle"), 4, 1, red); + immDrawPixelsTex( + &state, 0.0f, 0.0f, width, height, GL_RED, GL_FLOAT, GL_NEAREST, buffer, 1.0f, 1.0f, NULL); + + GPU_matrix_pop(); + + if (overlay_mode != MASK_OVERLAY_ALPHACHANNEL) { + GPU_blend(false); + } + + MEM_freeN(buffer); + } + + /* apply transformation so mask editing tools will assume drawing from the + * origin in normalized space */ + GPU_matrix_push(); + GPU_matrix_translate_2f(x + xofs, y + yofs); + GPU_matrix_scale_2f(zoomx, zoomy); + if (stabmat) { + GPU_matrix_mul(stabmat); + } + GPU_matrix_scale_2f(maxdim, maxdim); + + if (do_draw_cb) { + ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW); + } + + /* draw! */ + draw_masklays(C, mask, draw_flag, draw_type, width, height); + + if (do_draw_cb) { + ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW); + } + + GPU_matrix_pop(); } void ED_mask_draw_frames(Mask *mask, ARegion *ar, const int cfra, const int sfra, const int efra) { - const float framelen = ar->winx / (float)(efra - sfra + 1); + const float framelen = ar->winx / (float)(efra - sfra + 1); - MaskLayer *masklay = BKE_mask_layer_active(mask); + MaskLayer *masklay = BKE_mask_layer_active(mask); - if (masklay) { - unsigned int num_lines = BLI_listbase_count(&masklay->splines_shapes); + if (masklay) { + unsigned int num_lines = BLI_listbase_count(&masklay->splines_shapes); - if (num_lines > 0) { - uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); + if (num_lines > 0) { + uint pos = GPU_vertformat_attr_add( + immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT); - immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); - immUniformColor4ub(255, 175, 0, 255); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor4ub(255, 175, 0, 255); - immBegin(GPU_PRIM_LINES, 2 * num_lines); + immBegin(GPU_PRIM_LINES, 2 * num_lines); - for (MaskLayerShape *masklay_shape = masklay->splines_shapes.first; - masklay_shape; - masklay_shape = masklay_shape->next) - { - int frame = masklay_shape->frame; + for (MaskLayerShape *masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + int frame = masklay_shape->frame; - /* draw_keyframe(i, CFRA, sfra, framelen, 1); */ - int height = (frame == cfra) ? 22 : 10; - int x = (frame - sfra) * framelen; - immVertex2i(pos, x, 0); - immVertex2i(pos, x, height); - } - immEnd(); - immUnbindProgram(); - } - } + /* draw_keyframe(i, CFRA, sfra, framelen, 1); */ + int height = (frame == cfra) ? 22 : 10; + int x = (frame - sfra) * framelen; + immVertex2i(pos, x, 0); + immVertex2i(pos, x, height); + } + immEnd(); + immUnbindProgram(); + } + } } diff --git a/source/blender/editors/mask/mask_edit.c b/source/blender/editors/mask/mask_edit.c index a2deaac30ac..7325686d4f0 100644 --- a/source/blender/editors/mask/mask_edit.c +++ b/source/blender/editors/mask/mask_edit.c @@ -21,7 +21,6 @@ * \ingroup edmask */ - #include "BLI_math.h" #include "BKE_context.h" @@ -35,7 +34,7 @@ #include "ED_screen.h" #include "ED_select_utils.h" -#include "ED_mask.h" /* own include */ +#include "ED_mask.h" /* own include */ #include "ED_image.h" #include "ED_object.h" /* ED_keymap_proportional_maskmode only */ #include "ED_clip.h" @@ -46,40 +45,40 @@ #include "RNA_access.h" -#include "mask_intern.h" /* own include */ +#include "mask_intern.h" /* own include */ /********************** generic poll functions *********************/ bool ED_maskedit_poll(bContext *C) { - ScrArea *sa = CTX_wm_area(C); - if (sa) { - switch (sa->spacetype) { - case SPACE_CLIP: - return ED_space_clip_maskedit_poll(C); - case SPACE_SEQ: - return ED_space_sequencer_maskedit_poll(C); - case SPACE_IMAGE: - return ED_space_image_maskedit_poll(C); - } - } - return false; + ScrArea *sa = CTX_wm_area(C); + if (sa) { + switch (sa->spacetype) { + case SPACE_CLIP: + return ED_space_clip_maskedit_poll(C); + case SPACE_SEQ: + return ED_space_sequencer_maskedit_poll(C); + case SPACE_IMAGE: + return ED_space_image_maskedit_poll(C); + } + } + return false; } bool ED_maskedit_mask_poll(bContext *C) { - ScrArea *sa = CTX_wm_area(C); - if (sa) { - switch (sa->spacetype) { - case SPACE_CLIP: - return ED_space_clip_maskedit_mask_poll(C); - case SPACE_SEQ: - return ED_space_sequencer_maskedit_mask_poll(C); - case SPACE_IMAGE: - return ED_space_image_maskedit_mask_poll(C); - } - } - return false; + ScrArea *sa = CTX_wm_area(C); + if (sa) { + switch (sa->spacetype) { + case SPACE_CLIP: + return ED_space_clip_maskedit_mask_poll(C); + case SPACE_SEQ: + return ED_space_sequencer_maskedit_mask_poll(C); + case SPACE_IMAGE: + return ED_space_image_maskedit_mask_poll(C); + } + } + return false; } /********************** registration *********************/ @@ -87,461 +86,439 @@ bool ED_maskedit_mask_poll(bContext *C) /* takes event->mval */ void ED_mask_mouse_pos(ScrArea *sa, ARegion *ar, const int mval[2], float co[2]) { - if (sa) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - ED_clip_mouse_pos(sc, ar, mval, co); - BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co); - break; - } - case SPACE_SEQ: - { - UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &co[0], &co[1]); - break; - } - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - ED_image_mouse_pos(sima, ar, mval, co); - BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - zero_v2(co); - break; - } - } - else { - BLI_assert(0); - zero_v2(co); - } + if (sa) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_clip_mouse_pos(sc, ar, mval, co); + BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co); + break; + } + case SPACE_SEQ: { + UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &co[0], &co[1]); + break; + } + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_image_mouse_pos(sima, ar, mval, co); + BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + zero_v2(co); + break; + } + } + else { + BLI_assert(0); + zero_v2(co); + } } /* input: x/y - mval space * output: xr/yr - mask point space */ void ED_mask_point_pos(ScrArea *sa, ARegion *ar, float x, float y, float *xr, float *yr) { - float co[2]; - - if (sa) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - ED_clip_point_stable_pos(sc, ar, x, y, &co[0], &co[1]); - BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co); - break; - } - case SPACE_SEQ: - zero_v2(co); /* MASKTODO */ - break; - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - ED_image_point_pos(sima, ar, x, y, &co[0], &co[1]); - BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - zero_v2(co); - break; - } - } - else { - BLI_assert(0); - zero_v2(co); - } - - *xr = co[0]; - *yr = co[1]; + float co[2]; + + if (sa) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_clip_point_stable_pos(sc, ar, x, y, &co[0], &co[1]); + BKE_mask_coord_from_movieclip(sc->clip, &sc->user, co, co); + break; + } + case SPACE_SEQ: + zero_v2(co); /* MASKTODO */ + break; + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_image_point_pos(sima, ar, x, y, &co[0], &co[1]); + BKE_mask_coord_from_image(sima->image, &sima->iuser, co, co); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + zero_v2(co); + break; + } + } + else { + BLI_assert(0); + zero_v2(co); + } + + *xr = co[0]; + *yr = co[1]; } void ED_mask_point_pos__reverse(ScrArea *sa, ARegion *ar, float x, float y, float *xr, float *yr) { - float co[2]; - - if (sa) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - co[0] = x; - co[1] = y; - BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co); - ED_clip_point_stable_pos__reverse(sc, ar, co, co); - break; - } - case SPACE_SEQ: - zero_v2(co); /* MASKTODO */ - break; - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - co[0] = x; - co[1] = y; - BKE_mask_coord_to_image(sima->image, &sima->iuser, co, co); - ED_image_point_pos__reverse(sima, ar, co, co); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - zero_v2(co); - break; - } - } - else { - BLI_assert(0); - zero_v2(co); - } - - *xr = co[0]; - *yr = co[1]; + float co[2]; + + if (sa) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + co[0] = x; + co[1] = y; + BKE_mask_coord_to_movieclip(sc->clip, &sc->user, co, co); + ED_clip_point_stable_pos__reverse(sc, ar, co, co); + break; + } + case SPACE_SEQ: + zero_v2(co); /* MASKTODO */ + break; + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + co[0] = x; + co[1] = y; + BKE_mask_coord_to_image(sima->image, &sima->iuser, co, co); + ED_image_point_pos__reverse(sima, ar, co, co); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + zero_v2(co); + break; + } + } + else { + BLI_assert(0); + zero_v2(co); + } + + *xr = co[0]; + *yr = co[1]; } void ED_mask_get_size(ScrArea *sa, int *width, int *height) { - if (sa && sa->spacedata.first) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - ED_space_clip_get_size(sc, width, height); - break; - } - case SPACE_SEQ: - { -// Scene *scene = CTX_data_scene(C); -// *width = (scene->r.size * scene->r.xsch) / 100; -// *height = (scene->r.size * scene->r.ysch) / 100; - break; - } - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - ED_space_image_get_size(sima, width, height); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - *width = 0; - *height = 0; - break; - } - } - else { - BLI_assert(0); - *width = 0; - *height = 0; - } + if (sa && sa->spacedata.first) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_space_clip_get_size(sc, width, height); + break; + } + case SPACE_SEQ: { + // Scene *scene = CTX_data_scene(C); + // *width = (scene->r.size * scene->r.xsch) / 100; + // *height = (scene->r.size * scene->r.ysch) / 100; + break; + } + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_space_image_get_size(sima, width, height); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + *width = 0; + *height = 0; + break; + } + } + else { + BLI_assert(0); + *width = 0; + *height = 0; + } } void ED_mask_zoom(ScrArea *sa, ARegion *ar, float *zoomx, float *zoomy) { - if (sa && sa->spacedata.first) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - ED_space_clip_get_zoom(sc, ar, zoomx, zoomy); - break; - } - case SPACE_SEQ: - { - *zoomx = *zoomy = 1.0f; - break; - } - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - ED_space_image_get_zoom(sima, ar, zoomx, zoomy); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - *zoomx = *zoomy = 1.0f; - break; - } - } - else { - BLI_assert(0); - *zoomx = *zoomy = 1.0f; - } + if (sa && sa->spacedata.first) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_space_clip_get_zoom(sc, ar, zoomx, zoomy); + break; + } + case SPACE_SEQ: { + *zoomx = *zoomy = 1.0f; + break; + } + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_space_image_get_zoom(sima, ar, zoomx, zoomy); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + *zoomx = *zoomy = 1.0f; + break; + } + } + else { + BLI_assert(0); + *zoomx = *zoomy = 1.0f; + } } void ED_mask_get_aspect(ScrArea *sa, ARegion *UNUSED(ar), float *aspx, float *aspy) { - if (sa && sa->spacedata.first) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - ED_space_clip_get_aspect(sc, aspx, aspy); - break; - } - case SPACE_SEQ: - { - *aspx = *aspy = 1.0f; /* MASKTODO - render aspect? */ - break; - } - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - ED_space_image_get_aspect(sima, aspx, aspy); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - *aspx = *aspy = 1.0f; - break; - } - } - else { - BLI_assert(0); - *aspx = *aspy = 1.0f; - } + if (sa && sa->spacedata.first) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_space_clip_get_aspect(sc, aspx, aspy); + break; + } + case SPACE_SEQ: { + *aspx = *aspy = 1.0f; /* MASKTODO - render aspect? */ + break; + } + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_space_image_get_aspect(sima, aspx, aspy); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + *aspx = *aspy = 1.0f; + break; + } + } + else { + BLI_assert(0); + *aspx = *aspy = 1.0f; + } } void ED_mask_pixelspace_factor(ScrArea *sa, ARegion *ar, float *scalex, float *scaley) { - if (sa && sa->spacedata.first) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - float aspx, aspy; - - UI_view2d_scale_get(&ar->v2d, scalex, scaley); - ED_space_clip_get_aspect(sc, &aspx, &aspy); - - *scalex *= aspx; - *scaley *= aspy; - break; - } - case SPACE_SEQ: - { - *scalex = *scaley = 1.0f; /* MASKTODO? */ - break; - } - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - float aspx, aspy; - - UI_view2d_scale_get(&ar->v2d, scalex, scaley); - ED_space_image_get_aspect(sima, &aspx, &aspy); - - *scalex *= aspx; - *scaley *= aspy; - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - *scalex = *scaley = 1.0f; - break; - } - } - else { - BLI_assert(0); - *scalex = *scaley = 1.0f; - } + if (sa && sa->spacedata.first) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + float aspx, aspy; + + UI_view2d_scale_get(&ar->v2d, scalex, scaley); + ED_space_clip_get_aspect(sc, &aspx, &aspy); + + *scalex *= aspx; + *scaley *= aspy; + break; + } + case SPACE_SEQ: { + *scalex = *scaley = 1.0f; /* MASKTODO? */ + break; + } + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + float aspx, aspy; + + UI_view2d_scale_get(&ar->v2d, scalex, scaley); + ED_space_image_get_aspect(sima, &aspx, &aspy); + + *scalex *= aspx; + *scaley *= aspy; + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + *scalex = *scaley = 1.0f; + break; + } + } + else { + BLI_assert(0); + *scalex = *scaley = 1.0f; + } } void ED_mask_cursor_location_get(ScrArea *sa, float cursor[2]) { - if (sa) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *space_clip = sa->spacedata.first; - copy_v2_v2(cursor, space_clip->cursor); - break; - } - case SPACE_SEQ: - { - zero_v2(cursor); - break; - } - case SPACE_IMAGE: - { - SpaceImage *space_image = sa->spacedata.first; - copy_v2_v2(cursor, space_image->cursor); - break; - } - default: - /* possible other spaces from which mask editing is available */ - BLI_assert(0); - zero_v2(cursor); - break; - } - } - else { - BLI_assert(0); - zero_v2(cursor); - } + if (sa) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *space_clip = sa->spacedata.first; + copy_v2_v2(cursor, space_clip->cursor); + break; + } + case SPACE_SEQ: { + zero_v2(cursor); + break; + } + case SPACE_IMAGE: { + SpaceImage *space_image = sa->spacedata.first; + copy_v2_v2(cursor, space_image->cursor); + break; + } + default: + /* possible other spaces from which mask editing is available */ + BLI_assert(0); + zero_v2(cursor); + break; + } + } + else { + BLI_assert(0); + zero_v2(cursor); + } } bool ED_mask_selected_minmax(const bContext *C, float min[2], float max[2]) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *mask_layer; - bool ok = false; - - if (mask == NULL) - return ok; - - INIT_MINMAX2(min, max); - for (mask_layer = mask->masklayers.first; - mask_layer != NULL; - mask_layer = mask_layer->next) - { - MaskSpline *spline; - if (mask_layer->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - for (spline = mask_layer->splines.first; - spline != NULL; - spline = spline->next) - { - MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - int i; - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - MaskSplinePoint *deform_point = &points_array[i]; - BezTriple *bezt = &point->bezt; - float handle[2]; - if (!MASKPOINT_ISSEL_ANY(point)) { - continue; - } - if (bezt->f2 & SELECT) { - minmax_v2v2_v2(min, max, deform_point->bezt.vec[1]); - } - if (BKE_mask_point_handles_mode_get(point) == MASK_HANDLE_MODE_STICK) { - BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_STICK, handle); - minmax_v2v2_v2(min, max, handle); - } - else { - if ((bezt->f1 & SELECT) && (bezt->h1 != HD_VECT)) { - BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_LEFT, handle); - minmax_v2v2_v2(min, max, handle); - } - if ((bezt->f3 & SELECT) && (bezt->h2 != HD_VECT)) { - BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_RIGHT, handle); - minmax_v2v2_v2(min, max, handle); - } - } - ok = true; - } - } - } - return ok; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *mask_layer; + bool ok = false; + + if (mask == NULL) + return ok; + + INIT_MINMAX2(min, max); + for (mask_layer = mask->masklayers.first; mask_layer != NULL; mask_layer = mask_layer->next) { + MaskSpline *spline; + if (mask_layer->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + for (spline = mask_layer->splines.first; spline != NULL; spline = spline->next) { + MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + int i; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + MaskSplinePoint *deform_point = &points_array[i]; + BezTriple *bezt = &point->bezt; + float handle[2]; + if (!MASKPOINT_ISSEL_ANY(point)) { + continue; + } + if (bezt->f2 & SELECT) { + minmax_v2v2_v2(min, max, deform_point->bezt.vec[1]); + } + if (BKE_mask_point_handles_mode_get(point) == MASK_HANDLE_MODE_STICK) { + BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_STICK, handle); + minmax_v2v2_v2(min, max, handle); + } + else { + if ((bezt->f1 & SELECT) && (bezt->h1 != HD_VECT)) { + BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_LEFT, handle); + minmax_v2v2_v2(min, max, handle); + } + if ((bezt->f3 & SELECT) && (bezt->h2 != HD_VECT)) { + BKE_mask_point_handle(deform_point, MASK_WHICH_HANDLE_RIGHT, handle); + minmax_v2v2_v2(min, max, handle); + } + } + ok = true; + } + } + } + return ok; } /********************** registration *********************/ void ED_operatortypes_mask(void) { - WM_operatortype_append(MASK_OT_new); - - /* mask layers */ - WM_operatortype_append(MASK_OT_layer_new); - WM_operatortype_append(MASK_OT_layer_remove); - - /* add */ - WM_operatortype_append(MASK_OT_add_vertex); - WM_operatortype_append(MASK_OT_add_feather_vertex); - WM_operatortype_append(MASK_OT_primitive_circle_add); - WM_operatortype_append(MASK_OT_primitive_square_add); - - /* geometry */ - WM_operatortype_append(MASK_OT_switch_direction); - WM_operatortype_append(MASK_OT_normals_make_consistent); - WM_operatortype_append(MASK_OT_delete); - - /* select */ - WM_operatortype_append(MASK_OT_select); - WM_operatortype_append(MASK_OT_select_all); - WM_operatortype_append(MASK_OT_select_box); - WM_operatortype_append(MASK_OT_select_lasso); - WM_operatortype_append(MASK_OT_select_circle); - WM_operatortype_append(MASK_OT_select_linked_pick); - WM_operatortype_append(MASK_OT_select_linked); - WM_operatortype_append(MASK_OT_select_more); - WM_operatortype_append(MASK_OT_select_less); - - /* hide/reveal */ - WM_operatortype_append(MASK_OT_hide_view_clear); - WM_operatortype_append(MASK_OT_hide_view_set); - - /* feather */ - WM_operatortype_append(MASK_OT_feather_weight_clear); - - /* shape */ - WM_operatortype_append(MASK_OT_slide_point); - WM_operatortype_append(MASK_OT_slide_spline_curvature); - WM_operatortype_append(MASK_OT_cyclic_toggle); - WM_operatortype_append(MASK_OT_handle_type_set); - - /* relationships */ - WM_operatortype_append(MASK_OT_parent_set); - WM_operatortype_append(MASK_OT_parent_clear); - - /* shapekeys */ - WM_operatortype_append(MASK_OT_shape_key_insert); - WM_operatortype_append(MASK_OT_shape_key_clear); - WM_operatortype_append(MASK_OT_shape_key_feather_reset); - WM_operatortype_append(MASK_OT_shape_key_rekey); - - /* layers */ - WM_operatortype_append(MASK_OT_layer_move); - - /* duplicate */ - WM_operatortype_append(MASK_OT_duplicate); - - /* clipboard */ - WM_operatortype_append(MASK_OT_copy_splines); - WM_operatortype_append(MASK_OT_paste_splines); + WM_operatortype_append(MASK_OT_new); + + /* mask layers */ + WM_operatortype_append(MASK_OT_layer_new); + WM_operatortype_append(MASK_OT_layer_remove); + + /* add */ + WM_operatortype_append(MASK_OT_add_vertex); + WM_operatortype_append(MASK_OT_add_feather_vertex); + WM_operatortype_append(MASK_OT_primitive_circle_add); + WM_operatortype_append(MASK_OT_primitive_square_add); + + /* geometry */ + WM_operatortype_append(MASK_OT_switch_direction); + WM_operatortype_append(MASK_OT_normals_make_consistent); + WM_operatortype_append(MASK_OT_delete); + + /* select */ + WM_operatortype_append(MASK_OT_select); + WM_operatortype_append(MASK_OT_select_all); + WM_operatortype_append(MASK_OT_select_box); + WM_operatortype_append(MASK_OT_select_lasso); + WM_operatortype_append(MASK_OT_select_circle); + WM_operatortype_append(MASK_OT_select_linked_pick); + WM_operatortype_append(MASK_OT_select_linked); + WM_operatortype_append(MASK_OT_select_more); + WM_operatortype_append(MASK_OT_select_less); + + /* hide/reveal */ + WM_operatortype_append(MASK_OT_hide_view_clear); + WM_operatortype_append(MASK_OT_hide_view_set); + + /* feather */ + WM_operatortype_append(MASK_OT_feather_weight_clear); + + /* shape */ + WM_operatortype_append(MASK_OT_slide_point); + WM_operatortype_append(MASK_OT_slide_spline_curvature); + WM_operatortype_append(MASK_OT_cyclic_toggle); + WM_operatortype_append(MASK_OT_handle_type_set); + + /* relationships */ + WM_operatortype_append(MASK_OT_parent_set); + WM_operatortype_append(MASK_OT_parent_clear); + + /* shapekeys */ + WM_operatortype_append(MASK_OT_shape_key_insert); + WM_operatortype_append(MASK_OT_shape_key_clear); + WM_operatortype_append(MASK_OT_shape_key_feather_reset); + WM_operatortype_append(MASK_OT_shape_key_rekey); + + /* layers */ + WM_operatortype_append(MASK_OT_layer_move); + + /* duplicate */ + WM_operatortype_append(MASK_OT_duplicate); + + /* clipboard */ + WM_operatortype_append(MASK_OT_copy_splines); + WM_operatortype_append(MASK_OT_paste_splines); } void ED_keymap_mask(wmKeyConfig *keyconf) { - wmKeyMap *keymap = WM_keymap_ensure(keyconf, "Mask Editing", 0, 0); - keymap->poll = ED_maskedit_poll; + wmKeyMap *keymap = WM_keymap_ensure(keyconf, "Mask Editing", 0, 0); + keymap->poll = ED_maskedit_poll; } void ED_operatormacros_mask(void) { - wmOperatorType *ot; - wmOperatorTypeMacro *otmacro; - - ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", "Add Vertex and Slide", - "Add new vertex and slide it", OPTYPE_UNDO | OPTYPE_REGISTER); - ot->description = "Add new vertex and slide it"; - WM_operatortype_macro_define(ot, "MASK_OT_add_vertex"); - otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point"); - RNA_boolean_set(otmacro->ptr, "is_new_point", true); - - ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", "Add Feather Vertex and Slide", - "Add new vertex to feather and slide it", OPTYPE_UNDO | OPTYPE_REGISTER); - ot->description = "Add new feather vertex and slide it"; - WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex"); - otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point"); - RNA_boolean_set(otmacro->ptr, "slide_feather", true); - - ot = WM_operatortype_append_macro("MASK_OT_duplicate_move", "Add Duplicate", "Duplicate mask and move", - OPTYPE_UNDO | OPTYPE_REGISTER); - WM_operatortype_macro_define(ot, "MASK_OT_duplicate"); - otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate"); - RNA_enum_set(otmacro->ptr, "proportional", 0); - RNA_boolean_set(otmacro->ptr, "mirror", false); + wmOperatorType *ot; + wmOperatorTypeMacro *otmacro; + + ot = WM_operatortype_append_macro("MASK_OT_add_vertex_slide", + "Add Vertex and Slide", + "Add new vertex and slide it", + OPTYPE_UNDO | OPTYPE_REGISTER); + ot->description = "Add new vertex and slide it"; + WM_operatortype_macro_define(ot, "MASK_OT_add_vertex"); + otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point"); + RNA_boolean_set(otmacro->ptr, "is_new_point", true); + + ot = WM_operatortype_append_macro("MASK_OT_add_feather_vertex_slide", + "Add Feather Vertex and Slide", + "Add new vertex to feather and slide it", + OPTYPE_UNDO | OPTYPE_REGISTER); + ot->description = "Add new feather vertex and slide it"; + WM_operatortype_macro_define(ot, "MASK_OT_add_feather_vertex"); + otmacro = WM_operatortype_macro_define(ot, "MASK_OT_slide_point"); + RNA_boolean_set(otmacro->ptr, "slide_feather", true); + + ot = WM_operatortype_append_macro("MASK_OT_duplicate_move", + "Add Duplicate", + "Duplicate mask and move", + OPTYPE_UNDO | OPTYPE_REGISTER); + WM_operatortype_macro_define(ot, "MASK_OT_duplicate"); + otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate"); + RNA_enum_set(otmacro->ptr, "proportional", 0); + RNA_boolean_set(otmacro->ptr, "mirror", false); } diff --git a/source/blender/editors/mask/mask_editaction.c b/source/blender/editors/mask/mask_editaction.c index 9c7eed6ba51..d30e62d89f5 100644 --- a/source/blender/editors/mask/mask_editaction.c +++ b/source/blender/editors/mask/mask_editaction.c @@ -40,7 +40,7 @@ #include "ED_anim_api.h" #include "ED_keyframes_edit.h" -#include "ED_mask.h" /* own include */ +#include "ED_mask.h" /* own include */ #include "ED_markers.h" /* ***************************************** */ @@ -53,23 +53,26 @@ /* Generics - Loopers */ /* Loops over the mask-frames for a mask-layer, and applies the given callback */ -bool ED_masklayer_frames_looper(MaskLayer *masklay, Scene *scene, short (*masklay_shape_cb)(MaskLayerShape *, Scene *)) +bool ED_masklayer_frames_looper(MaskLayer *masklay, + Scene *scene, + short (*masklay_shape_cb)(MaskLayerShape *, Scene *)) { - MaskLayerShape *masklay_shape; - - /* error checker */ - if (masklay == NULL) - return false; - - /* do loop */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) { - /* execute callback */ - if (masklay_shape_cb(masklay_shape, scene)) - return true; - } - - /* nothing to return */ - return false; + MaskLayerShape *masklay_shape; + + /* error checker */ + if (masklay == NULL) + return false; + + /* do loop */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + /* execute callback */ + if (masklay_shape_cb(masklay_shape, scene)) + return true; + } + + /* nothing to return */ + return false; } /* ****************************************** */ @@ -78,24 +81,25 @@ bool ED_masklayer_frames_looper(MaskLayer *masklay, Scene *scene, short (*maskla /* make a listing all the mask-frames in a layer as cfraelems */ void ED_masklayer_make_cfra_list(MaskLayer *masklay, ListBase *elems, bool onlysel) { - MaskLayerShape *masklay_shape; - CfraElem *ce; + MaskLayerShape *masklay_shape; + CfraElem *ce; - /* error checking */ - if (ELEM(NULL, masklay, elems)) - return; + /* error checking */ + if (ELEM(NULL, masklay, elems)) + return; - /* loop through mask-frames, adding */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) { - if ((onlysel == false) || (masklay_shape->flag & MASK_SHAPE_SELECT)) { - ce = MEM_callocN(sizeof(CfraElem), "CfraElem"); + /* loop through mask-frames, adding */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + if ((onlysel == false) || (masklay_shape->flag & MASK_SHAPE_SELECT)) { + ce = MEM_callocN(sizeof(CfraElem), "CfraElem"); - ce->cfra = (float)masklay_shape->frame; - ce->sel = (masklay_shape->flag & MASK_SHAPE_SELECT) ? 1 : 0; + ce->cfra = (float)masklay_shape->frame; + ce->sel = (masklay_shape->flag & MASK_SHAPE_SELECT) ? 1 : 0; - BLI_addtail(elems, ce); - } - } + BLI_addtail(elems, ce); + } + } } /* ***************************************** */ @@ -104,125 +108,132 @@ void ED_masklayer_make_cfra_list(MaskLayer *masklay, ListBase *elems, bool onlys /* check if one of the frames in this layer is selected */ bool ED_masklayer_frame_select_check(MaskLayer *masklay) { - MaskLayerShape *masklay_shape; + MaskLayerShape *masklay_shape; - /* error checking */ - if (masklay == NULL) - return 0; + /* error checking */ + if (masklay == NULL) + return 0; - /* stop at the first one found */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) { - if (masklay_shape->flag & MASK_SHAPE_SELECT) - return 1; - } + /* stop at the first one found */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + if (masklay_shape->flag & MASK_SHAPE_SELECT) + return 1; + } - /* not found */ - return 0; + /* not found */ + return 0; } /* helper function - select mask-frame based on SELECT_* mode */ static void masklayshape_select(MaskLayerShape *masklay_shape, short select_mode) { - if (masklay_shape == NULL) - return; - - switch (select_mode) { - case SELECT_ADD: - masklay_shape->flag |= MASK_SHAPE_SELECT; - break; - case SELECT_SUBTRACT: - masklay_shape->flag &= ~MASK_SHAPE_SELECT; - break; - case SELECT_INVERT: - masklay_shape->flag ^= MASK_SHAPE_SELECT; - break; - } + if (masklay_shape == NULL) + return; + + switch (select_mode) { + case SELECT_ADD: + masklay_shape->flag |= MASK_SHAPE_SELECT; + break; + case SELECT_SUBTRACT: + masklay_shape->flag &= ~MASK_SHAPE_SELECT; + break; + case SELECT_INVERT: + masklay_shape->flag ^= MASK_SHAPE_SELECT; + break; + } } /* set all/none/invert select (like above, but with SELECT_* modes) */ void ED_mask_select_frames(MaskLayer *masklay, short select_mode) { - MaskLayerShape *masklay_shape; + MaskLayerShape *masklay_shape; - /* error checking */ - if (masklay == NULL) - return; + /* error checking */ + if (masklay == NULL) + return; - /* handle according to mode */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) { - masklayshape_select(masklay_shape, select_mode); - } + /* handle according to mode */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + masklayshape_select(masklay_shape, select_mode); + } } /* set all/none/invert select */ void ED_masklayer_frame_select_set(MaskLayer *masklay, short mode) { - /* error checking */ - if (masklay == NULL) - return; + /* error checking */ + if (masklay == NULL) + return; - /* now call the standard function */ - ED_mask_select_frames(masklay, mode); + /* now call the standard function */ + ED_mask_select_frames(masklay, mode); } /* select the frame in this layer that occurs on this frame (there should only be one at most) */ void ED_mask_select_frame(MaskLayer *masklay, int selx, short select_mode) { - MaskLayerShape *masklay_shape; + MaskLayerShape *masklay_shape; - if (masklay == NULL) - return; + if (masklay == NULL) + return; - masklay_shape = BKE_mask_layer_shape_find_frame(masklay, selx); + masklay_shape = BKE_mask_layer_shape_find_frame(masklay, selx); - if (masklay_shape) { - masklayshape_select(masklay_shape, select_mode); - } + if (masklay_shape) { + masklayshape_select(masklay_shape, select_mode); + } } /* select the frames in this layer that occur within the bounds specified */ void ED_masklayer_frames_select_box(MaskLayer *masklay, float min, float max, short select_mode) { - MaskLayerShape *masklay_shape; + MaskLayerShape *masklay_shape; - if (masklay == NULL) - return; + if (masklay == NULL) + return; - /* only select those frames which are in bounds */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) { - if (IN_RANGE(masklay_shape->frame, min, max)) - masklayshape_select(masklay_shape, select_mode); - } + /* only select those frames which are in bounds */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + if (IN_RANGE(masklay_shape->frame, min, max)) + masklayshape_select(masklay_shape, select_mode); + } } /* select the frames in this layer that occur within the lasso/circle region specified */ -void ED_masklayer_frames_select_region(KeyframeEditData *ked, MaskLayer *masklay, short tool, short select_mode) +void ED_masklayer_frames_select_region(KeyframeEditData *ked, + MaskLayer *masklay, + short tool, + short select_mode) { - MaskLayerShape *masklay_shape; - - if (masklay == NULL) - return; - - /* only select frames which are within the region */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape->next) { - /* construct a dummy point coordinate to do this testing with */ - float pt[2] = {0}; - - pt[0] = masklay_shape->frame; - pt[1] = ked->channel_y; - - /* check the necessary regions */ - if (tool == BEZT_OK_CHANNEL_LASSO) { - /* Lasso */ - if (keyframe_region_lasso_test(ked->data, pt)) - masklayshape_select(masklay_shape, select_mode); - } - else if (tool == BEZT_OK_CHANNEL_CIRCLE) { - /* Circle */ - if (keyframe_region_circle_test(ked->data, pt)) - masklayshape_select(masklay_shape, select_mode); - } - } + MaskLayerShape *masklay_shape; + + if (masklay == NULL) + return; + + /* only select frames which are within the region */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + /* construct a dummy point coordinate to do this testing with */ + float pt[2] = {0}; + + pt[0] = masklay_shape->frame; + pt[1] = ked->channel_y; + + /* check the necessary regions */ + if (tool == BEZT_OK_CHANNEL_LASSO) { + /* Lasso */ + if (keyframe_region_lasso_test(ked->data, pt)) + masklayshape_select(masklay_shape, select_mode); + } + else if (tool == BEZT_OK_CHANNEL_CIRCLE) { + /* Circle */ + if (keyframe_region_circle_test(ked->data, pt)) + masklayshape_select(masklay_shape, select_mode); + } + } } /* ***************************************** */ @@ -231,51 +242,52 @@ void ED_masklayer_frames_select_region(KeyframeEditData *ked, MaskLayer *masklay /* Delete selected frames */ bool ED_masklayer_frames_delete(MaskLayer *masklay) { - MaskLayerShape *masklay_shape, *masklay_shape_next; - bool changed = false; + MaskLayerShape *masklay_shape, *masklay_shape_next; + bool changed = false; - /* error checking */ - if (masklay == NULL) - return false; + /* error checking */ + if (masklay == NULL) + return false; - /* check for frames to delete */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = masklay_shape_next) { - masklay_shape_next = masklay_shape->next; + /* check for frames to delete */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape_next) { + masklay_shape_next = masklay_shape->next; - if (masklay_shape->flag & MASK_SHAPE_SELECT) { - BKE_mask_layer_shape_unlink(masklay, masklay_shape); - changed = true; - } - } + if (masklay_shape->flag & MASK_SHAPE_SELECT) { + BKE_mask_layer_shape_unlink(masklay, masklay_shape); + changed = true; + } + } - return changed; + return changed; } /* Duplicate selected frames from given mask-layer */ void ED_masklayer_frames_duplicate(MaskLayer *masklay) { - MaskLayerShape *masklay_shape, *gpfn; + MaskLayerShape *masklay_shape, *gpfn; - /* error checking */ - if (masklay == NULL) - return; + /* error checking */ + if (masklay == NULL) + return; - /* duplicate selected frames */ - for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = gpfn) { - gpfn = masklay_shape->next; + /* duplicate selected frames */ + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; masklay_shape = gpfn) { + gpfn = masklay_shape->next; - /* duplicate this frame */ - if (masklay_shape->flag & MASK_SHAPE_SELECT) { - MaskLayerShape *mask_shape_dupe; + /* duplicate this frame */ + if (masklay_shape->flag & MASK_SHAPE_SELECT) { + MaskLayerShape *mask_shape_dupe; - /* duplicate frame, and deselect self */ - mask_shape_dupe = BKE_mask_layer_shape_duplicate(masklay_shape); - masklay_shape->flag &= ~MASK_SHAPE_SELECT; + /* duplicate frame, and deselect self */ + mask_shape_dupe = BKE_mask_layer_shape_duplicate(masklay_shape); + masklay_shape->flag &= ~MASK_SHAPE_SELECT; - /* XXX - how to handle duplicate frames? */ - BLI_insertlinkafter(&masklay->splines_shapes, masklay_shape, mask_shape_dupe); - } - } + /* XXX - how to handle duplicate frames? */ + BLI_insertlinkafter(&masklay->splines_shapes, masklay_shape, mask_shape_dupe); + } + } } /* -------------------------------------- */ @@ -283,50 +295,51 @@ void ED_masklayer_frames_duplicate(MaskLayer *masklay) static short snap_masklayer_nearest(MaskLayerShape *masklay_shape, Scene *UNUSED(scene)) { - if (masklay_shape->flag & MASK_SHAPE_SELECT) - masklay_shape->frame = (int)(floor(masklay_shape->frame + 0.5)); - return 0; + if (masklay_shape->flag & MASK_SHAPE_SELECT) + masklay_shape->frame = (int)(floor(masklay_shape->frame + 0.5)); + return 0; } static short snap_masklayer_nearestsec(MaskLayerShape *masklay_shape, Scene *scene) { - float secf = (float)FPS; - if (masklay_shape->flag & MASK_SHAPE_SELECT) - masklay_shape->frame = (int)(floorf(masklay_shape->frame / secf + 0.5f) * secf); - return 0; + float secf = (float)FPS; + if (masklay_shape->flag & MASK_SHAPE_SELECT) + masklay_shape->frame = (int)(floorf(masklay_shape->frame / secf + 0.5f) * secf); + return 0; } static short snap_masklayer_cframe(MaskLayerShape *masklay_shape, Scene *scene) { - if (masklay_shape->flag & MASK_SHAPE_SELECT) - masklay_shape->frame = (int)CFRA; - return 0; + if (masklay_shape->flag & MASK_SHAPE_SELECT) + masklay_shape->frame = (int)CFRA; + return 0; } static short snap_masklayer_nearmarker(MaskLayerShape *masklay_shape, Scene *scene) { - if (masklay_shape->flag & MASK_SHAPE_SELECT) - masklay_shape->frame = (int)ED_markers_find_nearest_marker_time(&scene->markers, (float)masklay_shape->frame); - return 0; + if (masklay_shape->flag & MASK_SHAPE_SELECT) + masklay_shape->frame = (int)ED_markers_find_nearest_marker_time(&scene->markers, + (float)masklay_shape->frame); + return 0; } /* snap selected frames to ... */ void ED_masklayer_snap_frames(MaskLayer *masklay, Scene *scene, short mode) { - switch (mode) { - case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */ - ED_masklayer_frames_looper(masklay, scene, snap_masklayer_nearest); - break; - case SNAP_KEYS_CURFRAME: /* snap to current frame */ - ED_masklayer_frames_looper(masklay, scene, snap_masklayer_cframe); - break; - case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */ - ED_masklayer_frames_looper(masklay, scene, snap_masklayer_nearmarker); - break; - case SNAP_KEYS_NEARSEC: /* snap to nearest second */ - ED_masklayer_frames_looper(masklay, scene, snap_masklayer_nearestsec); - break; - default: /* just in case */ - break; - } + switch (mode) { + case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */ + ED_masklayer_frames_looper(masklay, scene, snap_masklayer_nearest); + break; + case SNAP_KEYS_CURFRAME: /* snap to current frame */ + ED_masklayer_frames_looper(masklay, scene, snap_masklayer_cframe); + break; + case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */ + ED_masklayer_frames_looper(masklay, scene, snap_masklayer_nearmarker); + break; + case SNAP_KEYS_NEARSEC: /* snap to nearest second */ + ED_masklayer_frames_looper(masklay, scene, snap_masklayer_nearestsec); + break; + default: /* just in case */ + break; + } } diff --git a/source/blender/editors/mask/mask_intern.h b/source/blender/editors/mask/mask_intern.h index 77f537e2e4a..67785f1ae46 100644 --- a/source/blender/editors/mask/mask_intern.h +++ b/source/blender/editors/mask/mask_intern.h @@ -34,7 +34,8 @@ struct wmOperatorType; bool ED_mask_find_nearest_diff_point(const struct bContext *C, struct Mask *mask, const float normal_co[2], - int threshold, bool feather, + int threshold, + bool feather, float tangent[2], const bool use_deform, const bool use_project, @@ -71,15 +72,24 @@ void MASK_OT_normals_make_consistent(struct wmOperatorType *ot); void MASK_OT_handle_type_set(struct wmOperatorType *ot); -bool ED_mask_feather_find_nearest( - const struct bContext *C, struct Mask *mask, const float normal_co[2], const float threshold, - struct MaskLayer **masklay_r, struct MaskSpline **spline_r, struct MaskSplinePoint **point_r, - struct MaskSplinePointUW **uw_r, float *score); - -struct MaskSplinePoint *ED_mask_point_find_nearest( - const struct bContext *C, struct Mask *mask, const float normal_co[2], const float threshold, - struct MaskLayer **masklay_r, struct MaskSpline **spline_r, eMaskWhichHandle *which_handle_r, - float *score); +bool ED_mask_feather_find_nearest(const struct bContext *C, + struct Mask *mask, + const float normal_co[2], + const float threshold, + struct MaskLayer **masklay_r, + struct MaskSpline **spline_r, + struct MaskSplinePoint **point_r, + struct MaskSplinePointUW **uw_r, + float *score); + +struct MaskSplinePoint *ED_mask_point_find_nearest(const struct bContext *C, + struct Mask *mask, + const float normal_co[2], + const float threshold, + struct MaskLayer **masklay_r, + struct MaskSpline **spline_r, + eMaskWhichHandle *which_handle_r, + float *score); void MASK_OT_layer_move(struct wmOperatorType *ot); diff --git a/source/blender/editors/mask/mask_ops.c b/source/blender/editors/mask/mask_ops.c index 1bb76045c72..2b925b9095e 100644 --- a/source/blender/editors/mask/mask_ops.c +++ b/source/blender/editors/mask/mask_ops.c @@ -34,7 +34,7 @@ #include "DNA_scene_types.h" #include "DNA_mask_types.h" -#include "DNA_object_types.h" /* SELECT */ +#include "DNA_object_types.h" /* SELECT */ #include "WM_api.h" #include "WM_types.h" @@ -49,1255 +49,1292 @@ #include "RNA_access.h" #include "RNA_define.h" -#include "mask_intern.h" /* own include */ +#include "mask_intern.h" /* own include */ /******************** utility functions *********************/ -static void mask_point_scaled_handle(/*const*/ MaskSplinePoint *point, /*const*/ eMaskWhichHandle which_handle, - const float scalex, const float scaley, float handle[2]) +static void mask_point_scaled_handle(/*const*/ MaskSplinePoint *point, + /*const*/ eMaskWhichHandle which_handle, + const float scalex, + const float scaley, + float handle[2]) { - BKE_mask_point_handle(point, which_handle, handle); - handle[0] *= scalex; - handle[1] *= scaley; + BKE_mask_point_handle(point, which_handle, handle); + handle[0] *= scalex; + handle[1] *= scaley; } -MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, Mask *mask, const float normal_co[2], const float threshold, - MaskLayer **masklay_r, MaskSpline **spline_r, - eMaskWhichHandle *which_handle_r, float *score) +MaskSplinePoint *ED_mask_point_find_nearest(const bContext *C, + Mask *mask, + const float normal_co[2], + const float threshold, + MaskLayer **masklay_r, + MaskSpline **spline_r, + eMaskWhichHandle *which_handle_r, + float *score) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); - - MaskLayer *masklay; - MaskLayer *point_masklay = NULL; - MaskSpline *point_spline = NULL; - MaskSplinePoint *point = NULL; - float co[2]; - const float threshold_sq = threshold * threshold; - float len_sq = FLT_MAX, scalex, scaley; - eMaskWhichHandle which_handle = MASK_WHICH_HANDLE_NONE; - int width, height; - - ED_mask_get_size(sa, &width, &height); - ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley); - - co[0] = normal_co[0] * scalex; - co[1] = normal_co[1] * scaley; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - - int i; - - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *cur_point = &spline->points[i]; - MaskSplinePoint *cur_point_deform = &points_array[i]; - eMaskWhichHandle cur_which_handle = MASK_WHICH_HANDLE_NONE; - BezTriple *bezt = &cur_point_deform->bezt; - float cur_len_sq, vec[2]; - - vec[0] = bezt->vec[1][0] * scalex; - vec[1] = bezt->vec[1][1] * scaley; - - cur_len_sq = len_squared_v2v2(co, vec); - - if (cur_len_sq < len_sq) { - point_spline = spline; - point_masklay = masklay; - point = cur_point; - len_sq = cur_len_sq; - which_handle = MASK_WHICH_HANDLE_NONE; - } - - if (BKE_mask_point_handles_mode_get(cur_point_deform) == MASK_HANDLE_MODE_STICK) { - float handle[2]; - mask_point_scaled_handle(cur_point_deform, MASK_WHICH_HANDLE_STICK, scalex, scaley, handle); - cur_len_sq = len_squared_v2v2(co, handle); - cur_which_handle = MASK_WHICH_HANDLE_STICK; - } - else { - float handle_left[2], handle_right[2]; - float len_left_sq, len_right_sq; - mask_point_scaled_handle(cur_point_deform, MASK_WHICH_HANDLE_LEFT, scalex, scaley, handle_left); - mask_point_scaled_handle(cur_point_deform, MASK_WHICH_HANDLE_RIGHT, scalex, scaley, handle_right); - - len_left_sq = len_squared_v2v2(co, handle_left); - len_right_sq = len_squared_v2v2(co, handle_right); - if (i == 0) { - if (len_left_sq <= len_right_sq) { - if (bezt->h1 != HD_VECT) { - cur_which_handle = MASK_WHICH_HANDLE_LEFT; - cur_len_sq = len_left_sq; - } - } - else if (bezt->h2 != HD_VECT) { - cur_which_handle = MASK_WHICH_HANDLE_RIGHT; - cur_len_sq = len_right_sq; - } - } - else { - if (len_right_sq <= len_left_sq) { - if (bezt->h2 != HD_VECT) { - cur_which_handle = MASK_WHICH_HANDLE_RIGHT; - cur_len_sq = len_right_sq; - } - } - else if (bezt->h1 != HD_VECT) { - cur_which_handle = MASK_WHICH_HANDLE_LEFT; - cur_len_sq = len_left_sq; - } - } - } - - if (cur_len_sq <= len_sq && cur_which_handle != MASK_WHICH_HANDLE_NONE) { - point_masklay = masklay; - point_spline = spline; - point = cur_point; - len_sq = cur_len_sq; - which_handle = cur_which_handle; - } - } - } - } - - if (len_sq < threshold_sq) { - if (masklay_r) - *masklay_r = point_masklay; - - if (spline_r) - *spline_r = point_spline; - - if (which_handle_r) - *which_handle_r = which_handle; - - if (score) - *score = sqrtf(len_sq); - - return point; - } - - if (masklay_r) - *masklay_r = NULL; - - if (spline_r) - *spline_r = NULL; - - if (which_handle_r) - *which_handle_r = MASK_WHICH_HANDLE_NONE; - - return NULL; + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); + + MaskLayer *masklay; + MaskLayer *point_masklay = NULL; + MaskSpline *point_spline = NULL; + MaskSplinePoint *point = NULL; + float co[2]; + const float threshold_sq = threshold * threshold; + float len_sq = FLT_MAX, scalex, scaley; + eMaskWhichHandle which_handle = MASK_WHICH_HANDLE_NONE; + int width, height; + + ED_mask_get_size(sa, &width, &height); + ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley); + + co[0] = normal_co[0] * scalex; + co[1] = normal_co[1] * scaley; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + + int i; + + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *cur_point = &spline->points[i]; + MaskSplinePoint *cur_point_deform = &points_array[i]; + eMaskWhichHandle cur_which_handle = MASK_WHICH_HANDLE_NONE; + BezTriple *bezt = &cur_point_deform->bezt; + float cur_len_sq, vec[2]; + + vec[0] = bezt->vec[1][0] * scalex; + vec[1] = bezt->vec[1][1] * scaley; + + cur_len_sq = len_squared_v2v2(co, vec); + + if (cur_len_sq < len_sq) { + point_spline = spline; + point_masklay = masklay; + point = cur_point; + len_sq = cur_len_sq; + which_handle = MASK_WHICH_HANDLE_NONE; + } + + if (BKE_mask_point_handles_mode_get(cur_point_deform) == MASK_HANDLE_MODE_STICK) { + float handle[2]; + mask_point_scaled_handle( + cur_point_deform, MASK_WHICH_HANDLE_STICK, scalex, scaley, handle); + cur_len_sq = len_squared_v2v2(co, handle); + cur_which_handle = MASK_WHICH_HANDLE_STICK; + } + else { + float handle_left[2], handle_right[2]; + float len_left_sq, len_right_sq; + mask_point_scaled_handle( + cur_point_deform, MASK_WHICH_HANDLE_LEFT, scalex, scaley, handle_left); + mask_point_scaled_handle( + cur_point_deform, MASK_WHICH_HANDLE_RIGHT, scalex, scaley, handle_right); + + len_left_sq = len_squared_v2v2(co, handle_left); + len_right_sq = len_squared_v2v2(co, handle_right); + if (i == 0) { + if (len_left_sq <= len_right_sq) { + if (bezt->h1 != HD_VECT) { + cur_which_handle = MASK_WHICH_HANDLE_LEFT; + cur_len_sq = len_left_sq; + } + } + else if (bezt->h2 != HD_VECT) { + cur_which_handle = MASK_WHICH_HANDLE_RIGHT; + cur_len_sq = len_right_sq; + } + } + else { + if (len_right_sq <= len_left_sq) { + if (bezt->h2 != HD_VECT) { + cur_which_handle = MASK_WHICH_HANDLE_RIGHT; + cur_len_sq = len_right_sq; + } + } + else if (bezt->h1 != HD_VECT) { + cur_which_handle = MASK_WHICH_HANDLE_LEFT; + cur_len_sq = len_left_sq; + } + } + } + + if (cur_len_sq <= len_sq && cur_which_handle != MASK_WHICH_HANDLE_NONE) { + point_masklay = masklay; + point_spline = spline; + point = cur_point; + len_sq = cur_len_sq; + which_handle = cur_which_handle; + } + } + } + } + + if (len_sq < threshold_sq) { + if (masklay_r) + *masklay_r = point_masklay; + + if (spline_r) + *spline_r = point_spline; + + if (which_handle_r) + *which_handle_r = which_handle; + + if (score) + *score = sqrtf(len_sq); + + return point; + } + + if (masklay_r) + *masklay_r = NULL; + + if (spline_r) + *spline_r = NULL; + + if (which_handle_r) + *which_handle_r = MASK_WHICH_HANDLE_NONE; + + return NULL; } -bool ED_mask_feather_find_nearest(const bContext *C, Mask *mask, const float normal_co[2], const float threshold, - MaskLayer **masklay_r, MaskSpline **spline_r, MaskSplinePoint **point_r, - MaskSplinePointUW **uw_r, float *score) +bool ED_mask_feather_find_nearest(const bContext *C, + Mask *mask, + const float normal_co[2], + const float threshold, + MaskLayer **masklay_r, + MaskSpline **spline_r, + MaskSplinePoint **point_r, + MaskSplinePointUW **uw_r, + float *score) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - MaskLayer *masklay, *point_masklay = NULL; - MaskSpline *point_spline = NULL; - MaskSplinePoint *point = NULL; - MaskSplinePointUW *uw = NULL; - const float threshold_sq = threshold * threshold; - float len = FLT_MAX, co[2]; - float scalex, scaley; - int width, height; + MaskLayer *masklay, *point_masklay = NULL; + MaskSpline *point_spline = NULL; + MaskSplinePoint *point = NULL; + MaskSplinePointUW *uw = NULL; + const float threshold_sq = threshold * threshold; + float len = FLT_MAX, co[2]; + float scalex, scaley; + int width, height; - ED_mask_get_size(sa, &width, &height); - ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley); + ED_mask_get_size(sa, &width, &height); + ED_mask_pixelspace_factor(sa, ar, &scalex, &scaley); - co[0] = normal_co[0] * scalex; - co[1] = normal_co[1] * scaley; + co[0] = normal_co[0] * scalex; + co[1] = normal_co[1] * scaley; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; - for (spline = masklay->splines.first; spline; spline = spline->next) { - //MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + for (spline = masklay->splines.first; spline; spline = spline->next) { + //MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - int i, tot_feather_point; - float (*feather_points)[2], (*fp)[2]; + int i, tot_feather_point; + float(*feather_points)[2], (*fp)[2]; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - feather_points = fp = BKE_mask_spline_feather_points(spline, &tot_feather_point); + feather_points = fp = BKE_mask_spline_feather_points(spline, &tot_feather_point); - for (i = 0; i < spline->tot_point; i++) { - int j; - MaskSplinePoint *cur_point = &spline->points[i]; + for (i = 0; i < spline->tot_point; i++) { + int j; + MaskSplinePoint *cur_point = &spline->points[i]; - for (j = 0; j <= cur_point->tot_uw; j++) { - float cur_len_sq, vec[2]; + for (j = 0; j <= cur_point->tot_uw; j++) { + float cur_len_sq, vec[2]; - vec[0] = (*fp)[0] * scalex; - vec[1] = (*fp)[1] * scaley; + vec[0] = (*fp)[0] * scalex; + vec[1] = (*fp)[1] * scaley; - cur_len_sq = len_squared_v2v2(vec, co); + cur_len_sq = len_squared_v2v2(vec, co); - if (point == NULL || cur_len_sq < len) { - if (j == 0) - uw = NULL; - else - uw = &cur_point->uw[j - 1]; + if (point == NULL || cur_len_sq < len) { + if (j == 0) + uw = NULL; + else + uw = &cur_point->uw[j - 1]; - point_masklay = masklay; - point_spline = spline; - point = cur_point; - len = cur_len_sq; - } + point_masklay = masklay; + point_spline = spline; + point = cur_point; + len = cur_len_sq; + } - fp++; - } - } + fp++; + } + } - MEM_freeN(feather_points); - } - } + MEM_freeN(feather_points); + } + } - if (len < threshold_sq) { - if (masklay_r) - *masklay_r = point_masklay; + if (len < threshold_sq) { + if (masklay_r) + *masklay_r = point_masklay; - if (spline_r) - *spline_r = point_spline; + if (spline_r) + *spline_r = point_spline; - if (point_r) - *point_r = point; + if (point_r) + *point_r = point; - if (uw_r) - *uw_r = uw; + if (uw_r) + *uw_r = uw; - if (score) - *score = sqrtf(len); + if (score) + *score = sqrtf(len); - return true; - } + return true; + } - if (masklay_r) - *masklay_r = NULL; + if (masklay_r) + *masklay_r = NULL; - if (spline_r) - *spline_r = NULL; + if (spline_r) + *spline_r = NULL; - if (point_r) - *point_r = NULL; + if (point_r) + *point_r = NULL; - return false; + return false; } - /******************** create new mask *********************/ Mask *ED_mask_new(bContext *C, const char *name) { - ScrArea *sa = CTX_wm_area(C); - Main *bmain = CTX_data_main(C); - Mask *mask; - - mask = BKE_mask_new(bmain, name); - - if (sa && sa->spacedata.first) { - switch (sa->spacetype) { - case SPACE_CLIP: - { - SpaceClip *sc = sa->spacedata.first; - ED_space_clip_set_mask(C, sc, mask); - break; - } - case SPACE_SEQ: - { - /* do nothing */ - break; - } - case SPACE_IMAGE: - { - SpaceImage *sima = sa->spacedata.first; - ED_space_image_set_mask(C, sima, mask); - break; - } - } - } - - return mask; + ScrArea *sa = CTX_wm_area(C); + Main *bmain = CTX_data_main(C); + Mask *mask; + + mask = BKE_mask_new(bmain, name); + + if (sa && sa->spacedata.first) { + switch (sa->spacetype) { + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_space_clip_set_mask(C, sc, mask); + break; + } + case SPACE_SEQ: { + /* do nothing */ + break; + } + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_space_image_set_mask(C, sima, mask); + break; + } + } + } + + return mask; } /* Get ative layer. Will create mask/layer to be sure there's an active layer. */ MaskLayer *ED_mask_layer_ensure(bContext *C, bool *r_added_mask) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *mask_layer; - - if (mask == NULL) { - /* If there's no active mask, create one. */ - mask = ED_mask_new(C, NULL); - *r_added_mask = true; - } - - mask_layer = BKE_mask_layer_active(mask); - if (mask_layer == NULL) { - /* If there's no active mask layer, create one. */ - mask_layer = BKE_mask_layer_new(mask, ""); - } - - return mask_layer; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *mask_layer; + + if (mask == NULL) { + /* If there's no active mask, create one. */ + mask = ED_mask_new(C, NULL); + *r_added_mask = true; + } + + mask_layer = BKE_mask_layer_active(mask); + if (mask_layer == NULL) { + /* If there's no active mask layer, create one. */ + mask_layer = BKE_mask_layer_new(mask, ""); + } + + return mask_layer; } static int mask_new_exec(bContext *C, wmOperator *op) { - char name[MAX_ID_NAME - 2]; + char name[MAX_ID_NAME - 2]; - RNA_string_get(op->ptr, "name", name); + RNA_string_get(op->ptr, "name", name); - ED_mask_new(C, name); + ED_mask_new(C, name); - WM_event_add_notifier(C, NC_MASK | NA_ADDED, NULL); + WM_event_add_notifier(C, NC_MASK | NA_ADDED, NULL); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_new(wmOperatorType *ot) { - /* identifiers */ - ot->name = "New Mask"; - ot->description = "Create new mask"; - ot->idname = "MASK_OT_new"; + /* identifiers */ + ot->name = "New Mask"; + ot->description = "Create new mask"; + ot->idname = "MASK_OT_new"; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - /* api callbacks */ - ot->exec = mask_new_exec; - ot->poll = ED_operator_mask; + /* api callbacks */ + ot->exec = mask_new_exec; + ot->poll = ED_operator_mask; - /* properties */ - RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Name of new mask"); + /* properties */ + RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Name of new mask"); } /******************** create new masklay *********************/ static int masklay_new_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - char name[MAX_ID_NAME - 2]; + Mask *mask = CTX_data_edit_mask(C); + char name[MAX_ID_NAME - 2]; - RNA_string_get(op->ptr, "name", name); + RNA_string_get(op->ptr, "name", name); - BKE_mask_layer_new(mask, name); - mask->masklay_act = mask->masklay_tot - 1; + BKE_mask_layer_new(mask, name); + mask->masklay_act = mask->masklay_tot - 1; - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_layer_new(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Add Mask Layer"; - ot->description = "Add new mask layer for masking"; - ot->idname = "MASK_OT_layer_new"; + /* identifiers */ + ot->name = "Add Mask Layer"; + ot->description = "Add new mask layer for masking"; + ot->idname = "MASK_OT_layer_new"; - /* api callbacks */ - ot->exec = masklay_new_exec; - ot->poll = ED_maskedit_poll; + /* api callbacks */ + ot->exec = masklay_new_exec; + ot->poll = ED_maskedit_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - /* properties */ - RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Name of new mask layer"); + /* properties */ + RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Name of new mask layer"); } /******************** remove mask layer *********************/ static int masklay_remove_exec(bContext *C, wmOperator *UNUSED(op)) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay = BKE_mask_layer_active(mask); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay = BKE_mask_layer_active(mask); - if (masklay) { - BKE_mask_layer_remove(mask, masklay); + if (masklay) { + BKE_mask_layer_remove(mask, masklay); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - } + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + } - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_layer_remove(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Remove Mask Layer"; - ot->description = "Remove mask layer"; - ot->idname = "MASK_OT_layer_remove"; + /* identifiers */ + ot->name = "Remove Mask Layer"; + ot->description = "Remove mask layer"; + ot->idname = "MASK_OT_layer_remove"; - /* api callbacks */ - ot->exec = masklay_remove_exec; - ot->poll = ED_maskedit_poll; + /* api callbacks */ + ot->exec = masklay_remove_exec; + ot->poll = ED_maskedit_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /******************** slide *********************/ enum { - SLIDE_ACTION_NONE = 0, - SLIDE_ACTION_POINT = 1, - SLIDE_ACTION_HANDLE = 2, - SLIDE_ACTION_FEATHER = 3, - SLIDE_ACTION_SPLINE = 4, + SLIDE_ACTION_NONE = 0, + SLIDE_ACTION_POINT = 1, + SLIDE_ACTION_HANDLE = 2, + SLIDE_ACTION_FEATHER = 3, + SLIDE_ACTION_SPLINE = 4, }; typedef struct SlidePointData { - /* Generic fields. */ - short event_invoke_type; - int action; - Mask *mask; - MaskLayer *masklay; - MaskSpline *spline, *orig_spline; - MaskSplinePoint *point; - MaskSplinePointUW *uw; - eMaskWhichHandle which_handle; - int width, height; - - float prev_mouse_coord[2]; - float no[2]; - - bool is_curvature_only, - is_accurate, - is_initial_feather, - is_overall_feather; - - bool is_sliding_new_point; - - /* Data needed to restre the state. */ - float vec[3][3]; - char old_h1, old_h2; - - /* Point sliding. */ - - /* Handle sliding. */ - float orig_handle_coord[2], prev_handle_coord[2]; - - /* Feather sliding. */ - float prev_feather_coord[2]; - float weight, weight_scalar; + /* Generic fields. */ + short event_invoke_type; + int action; + Mask *mask; + MaskLayer *masklay; + MaskSpline *spline, *orig_spline; + MaskSplinePoint *point; + MaskSplinePointUW *uw; + eMaskWhichHandle which_handle; + int width, height; + + float prev_mouse_coord[2]; + float no[2]; + + bool is_curvature_only, is_accurate, is_initial_feather, is_overall_feather; + + bool is_sliding_new_point; + + /* Data needed to restre the state. */ + float vec[3][3]; + char old_h1, old_h2; + + /* Point sliding. */ + + /* Handle sliding. */ + float orig_handle_coord[2], prev_handle_coord[2]; + + /* Feather sliding. */ + float prev_feather_coord[2]; + float weight, weight_scalar; } SlidePointData; static void mask_point_undistort_pos(SpaceClip *sc, float r_co[2], const float co[2]) { - BKE_mask_coord_to_movieclip(sc->clip, &sc->user, r_co, co); - ED_clip_point_undistorted_pos(sc, r_co, r_co); - BKE_mask_coord_from_movieclip(sc->clip, &sc->user, r_co, r_co); + BKE_mask_coord_to_movieclip(sc->clip, &sc->user, r_co, co); + ED_clip_point_undistorted_pos(sc, r_co, r_co); + BKE_mask_coord_from_movieclip(sc->clip, &sc->user, r_co, r_co); } static bool spline_under_mouse_get(const bContext *C, - Mask *mask, const float co[2], + Mask *mask, + const float co[2], MaskLayer **mask_layer_r, MaskSpline **mask_spline_r) { - const float threshold = 19.0f; - ScrArea *sa = CTX_wm_area(C); - SpaceClip *sc = CTX_wm_space_clip(C); - MaskLayer *mask_layer; - int width, height; - float pixel_co[2]; - float closest_dist_squared = 0.0f; - MaskLayer *closest_layer = NULL; - MaskSpline *closest_spline = NULL; - bool undistort = false; - *mask_layer_r = NULL; - *mask_spline_r = NULL; - ED_mask_get_size(sa, &width, &height); - pixel_co[0] = co[0] * width; - pixel_co[1] = co[1] * height; - if (sc != NULL) { - undistort = (sc->clip != NULL) && - (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT) != 0; - } - for (mask_layer = mask->masklayers.first; - mask_layer != NULL; - mask_layer = mask_layer->next) - { - MaskSpline *spline; - if (mask_layer->restrictflag & MASK_RESTRICT_SELECT) { - continue; - } - - for (spline = mask_layer->splines.first; - spline != NULL; - spline = spline->next) - { - MaskSplinePoint *points_array; - float min[2], max[2], center[2]; - float dist_squared; - int i; - float max_bb_side; - if ((spline->flag & SELECT) == 0) { - continue; - } - - points_array = BKE_mask_spline_point_array(spline); - INIT_MINMAX2(min, max); - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point_deform = &points_array[i]; - BezTriple *bezt = &point_deform->bezt; - - float vert[2]; - - copy_v2_v2(vert, bezt->vec[1]); - - if (undistort) { - mask_point_undistort_pos(sc, vert, vert); - } - - minmax_v2v2_v2(min, max, vert); - } - - center[0] = (min[0] + max[0]) / 2.0f * width; - center[1] = (min[1] + max[1]) / 2.0f * height; - dist_squared = len_squared_v2v2(pixel_co, center); - max_bb_side = min_ff((max[0] - min[0]) * width, (max[1] - min[1]) * height); - if (dist_squared <= max_bb_side * max_bb_side * 0.5f && - (closest_spline == NULL || dist_squared < closest_dist_squared)) - { - closest_layer = mask_layer; - closest_spline = spline; - closest_dist_squared = dist_squared; - } - } - } - if (closest_dist_squared < SQUARE(threshold) && closest_spline != NULL) { - float diff_score; - if (ED_mask_find_nearest_diff_point(C, mask, co, threshold, - false, NULL, true, false, - NULL, NULL, NULL, NULL, - &diff_score)) - { - if (SQUARE(diff_score) < closest_dist_squared) { - return false; - } - } - - *mask_layer_r = closest_layer; - *mask_spline_r = closest_spline; - return true; - } - return false; + const float threshold = 19.0f; + ScrArea *sa = CTX_wm_area(C); + SpaceClip *sc = CTX_wm_space_clip(C); + MaskLayer *mask_layer; + int width, height; + float pixel_co[2]; + float closest_dist_squared = 0.0f; + MaskLayer *closest_layer = NULL; + MaskSpline *closest_spline = NULL; + bool undistort = false; + *mask_layer_r = NULL; + *mask_spline_r = NULL; + ED_mask_get_size(sa, &width, &height); + pixel_co[0] = co[0] * width; + pixel_co[1] = co[1] * height; + if (sc != NULL) { + undistort = (sc->clip != NULL) && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT) != 0; + } + for (mask_layer = mask->masklayers.first; mask_layer != NULL; mask_layer = mask_layer->next) { + MaskSpline *spline; + if (mask_layer->restrictflag & MASK_RESTRICT_SELECT) { + continue; + } + + for (spline = mask_layer->splines.first; spline != NULL; spline = spline->next) { + MaskSplinePoint *points_array; + float min[2], max[2], center[2]; + float dist_squared; + int i; + float max_bb_side; + if ((spline->flag & SELECT) == 0) { + continue; + } + + points_array = BKE_mask_spline_point_array(spline); + INIT_MINMAX2(min, max); + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point_deform = &points_array[i]; + BezTriple *bezt = &point_deform->bezt; + + float vert[2]; + + copy_v2_v2(vert, bezt->vec[1]); + + if (undistort) { + mask_point_undistort_pos(sc, vert, vert); + } + + minmax_v2v2_v2(min, max, vert); + } + + center[0] = (min[0] + max[0]) / 2.0f * width; + center[1] = (min[1] + max[1]) / 2.0f * height; + dist_squared = len_squared_v2v2(pixel_co, center); + max_bb_side = min_ff((max[0] - min[0]) * width, (max[1] - min[1]) * height); + if (dist_squared <= max_bb_side * max_bb_side * 0.5f && + (closest_spline == NULL || dist_squared < closest_dist_squared)) { + closest_layer = mask_layer; + closest_spline = spline; + closest_dist_squared = dist_squared; + } + } + } + if (closest_dist_squared < SQUARE(threshold) && closest_spline != NULL) { + float diff_score; + if (ED_mask_find_nearest_diff_point(C, + mask, + co, + threshold, + false, + NULL, + true, + false, + NULL, + NULL, + NULL, + NULL, + &diff_score)) { + if (SQUARE(diff_score) < closest_dist_squared) { + return false; + } + } + + *mask_layer_r = closest_layer; + *mask_spline_r = closest_spline; + return true; + } + return false; } static bool slide_point_check_initial_feather(MaskSpline *spline) { - int i; + int i; - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; - if (point->bezt.weight != 0.0f) { - return false; - } - } + if (point->bezt.weight != 0.0f) { + return false; + } + } - return true; + return true; } -static void select_sliding_point(Mask *mask, MaskLayer *mask_layer, MaskSpline *spline, - MaskSplinePoint *point, eMaskWhichHandle which_handle) +static void select_sliding_point(Mask *mask, + MaskLayer *mask_layer, + MaskSpline *spline, + MaskSplinePoint *point, + eMaskWhichHandle which_handle) { - ED_mask_select_toggle_all(mask, SEL_DESELECT); - - switch (which_handle) { - case MASK_WHICH_HANDLE_NONE: - BKE_mask_point_select_set(point, true); - break; - case MASK_WHICH_HANDLE_LEFT: - point->bezt.f1 |= SELECT; - break; - case MASK_WHICH_HANDLE_RIGHT: - point->bezt.f3 |= SELECT; - break; - case MASK_WHICH_HANDLE_STICK: - point->bezt.f1 |= SELECT; - point->bezt.f3 |= SELECT; - break; - default: - BLI_assert(!"Unexpected situation in select_sliding_point()"); - } - - mask_layer->act_spline = spline; - mask_layer->act_point = point; - ED_mask_select_flush_all(mask); + ED_mask_select_toggle_all(mask, SEL_DESELECT); + + switch (which_handle) { + case MASK_WHICH_HANDLE_NONE: + BKE_mask_point_select_set(point, true); + break; + case MASK_WHICH_HANDLE_LEFT: + point->bezt.f1 |= SELECT; + break; + case MASK_WHICH_HANDLE_RIGHT: + point->bezt.f3 |= SELECT; + break; + case MASK_WHICH_HANDLE_STICK: + point->bezt.f1 |= SELECT; + point->bezt.f3 |= SELECT; + break; + default: + BLI_assert(!"Unexpected situation in select_sliding_point()"); + } + + mask_layer->act_spline = spline; + mask_layer->act_point = point; + ED_mask_select_flush_all(mask); } static void check_sliding_handle_type(MaskSplinePoint *point, eMaskWhichHandle which_handle) { - BezTriple *bezt = &point->bezt; - - if (which_handle == MASK_WHICH_HANDLE_LEFT) { - if (bezt->h1 == HD_VECT) { - bezt->h1 = HD_FREE; - } - else if (bezt->h1 == HD_AUTO) { - bezt->h1 = HD_ALIGN_DOUBLESIDE; - bezt->h2 = HD_ALIGN_DOUBLESIDE; - } - } - else if (which_handle == MASK_WHICH_HANDLE_RIGHT) { - if (bezt->h2 == HD_VECT) { - bezt->h2 = HD_FREE; - } - else if (bezt->h2 == HD_AUTO) { - bezt->h1 = HD_ALIGN_DOUBLESIDE; - bezt->h2 = HD_ALIGN_DOUBLESIDE; - } - } + BezTriple *bezt = &point->bezt; + + if (which_handle == MASK_WHICH_HANDLE_LEFT) { + if (bezt->h1 == HD_VECT) { + bezt->h1 = HD_FREE; + } + else if (bezt->h1 == HD_AUTO) { + bezt->h1 = HD_ALIGN_DOUBLESIDE; + bezt->h2 = HD_ALIGN_DOUBLESIDE; + } + } + else if (which_handle == MASK_WHICH_HANDLE_RIGHT) { + if (bezt->h2 == HD_VECT) { + bezt->h2 = HD_FREE; + } + else if (bezt->h2 == HD_AUTO) { + bezt->h1 = HD_ALIGN_DOUBLESIDE; + bezt->h2 = HD_ALIGN_DOUBLESIDE; + } + } } static void *slide_point_customdata(bContext *C, wmOperator *op, const wmEvent *event) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); - - Mask *mask = CTX_data_edit_mask(C); - SlidePointData *customdata = NULL; - MaskLayer *masklay, *cv_masklay, *feather_masklay; - MaskSpline *spline, *cv_spline, *feather_spline; - MaskSplinePoint *point, *cv_point, *feather_point; - MaskSplinePointUW *uw = NULL; - int width, height, action = SLIDE_ACTION_NONE; - const bool slide_feather = RNA_boolean_get(op->ptr, "slide_feather"); - float co[2], cv_score, feather_score; - const float threshold = 19; - eMaskWhichHandle which_handle; - - ED_mask_mouse_pos(sa, ar, event->mval, co); - ED_mask_get_size(sa, &width, &height); - - cv_point = ED_mask_point_find_nearest(C, mask, co, threshold, &cv_masklay, &cv_spline, &which_handle, &cv_score); - - if (ED_mask_feather_find_nearest(C, mask, co, threshold, &feather_masklay, &feather_spline, &feather_point, &uw, &feather_score)) { - if (slide_feather || !cv_point || feather_score < cv_score) { - action = SLIDE_ACTION_FEATHER; - - masklay = feather_masklay; - spline = feather_spline; - point = feather_point; - } - } - - if (cv_point && action == SLIDE_ACTION_NONE) { - if (which_handle != MASK_WHICH_HANDLE_NONE) - action = SLIDE_ACTION_HANDLE; - else - action = SLIDE_ACTION_POINT; - - masklay = cv_masklay; - spline = cv_spline; - point = cv_point; - } - - if (action == SLIDE_ACTION_NONE) { - if (spline_under_mouse_get(C, mask, co, &masklay, &spline)) { - action = SLIDE_ACTION_SPLINE; - point = NULL; - } - } - - if (action != SLIDE_ACTION_NONE) { - customdata = MEM_callocN(sizeof(SlidePointData), "mask slide point data"); - customdata->event_invoke_type = event->type; - customdata->mask = mask; - customdata->masklay = masklay; - customdata->spline = spline; - customdata->point = point; - customdata->width = width; - customdata->height = height; - customdata->action = action; - customdata->uw = uw; - - customdata->is_sliding_new_point = RNA_boolean_get(op->ptr, "is_new_point"); - - if (customdata->action != SLIDE_ACTION_SPLINE) { - customdata->old_h1 = point->bezt.h1; - customdata->old_h2 = point->bezt.h2; - select_sliding_point(mask, masklay, spline, point, which_handle); - check_sliding_handle_type(point, which_handle); - } - - if (uw) { - float co_uw[2]; - float weight_scalar = BKE_mask_point_weight_scalar(spline, point, uw->u); - - customdata->weight = uw->w; - customdata->weight_scalar = weight_scalar; - BKE_mask_point_segment_co(spline, point, uw->u, co_uw); - BKE_mask_point_normal(spline, point, uw->u, customdata->no); - - madd_v2_v2v2fl(customdata->prev_feather_coord, co_uw, customdata->no, uw->w * weight_scalar); - } - else if (customdata->action != SLIDE_ACTION_SPLINE) { - BezTriple *bezt = &point->bezt; - - customdata->weight = bezt->weight; - customdata->weight_scalar = 1.0f; - BKE_mask_point_normal(spline, point, 0.0f, customdata->no); - - madd_v2_v2v2fl(customdata->prev_feather_coord, bezt->vec[1], customdata->no, bezt->weight); - } - - if (customdata->action == SLIDE_ACTION_FEATHER) { - customdata->is_initial_feather = slide_point_check_initial_feather(spline); - } - - if (customdata->action != SLIDE_ACTION_SPLINE) { - copy_m3_m3(customdata->vec, point->bezt.vec); - if (which_handle != MASK_WHICH_HANDLE_NONE) { - BKE_mask_point_handle(point, which_handle, customdata->orig_handle_coord); - copy_v2_v2(customdata->prev_handle_coord, customdata->orig_handle_coord); - } - } - customdata->which_handle = which_handle; - - ED_mask_mouse_pos(sa, ar, event->mval, customdata->prev_mouse_coord); - } - - return customdata; + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); + + Mask *mask = CTX_data_edit_mask(C); + SlidePointData *customdata = NULL; + MaskLayer *masklay, *cv_masklay, *feather_masklay; + MaskSpline *spline, *cv_spline, *feather_spline; + MaskSplinePoint *point, *cv_point, *feather_point; + MaskSplinePointUW *uw = NULL; + int width, height, action = SLIDE_ACTION_NONE; + const bool slide_feather = RNA_boolean_get(op->ptr, "slide_feather"); + float co[2], cv_score, feather_score; + const float threshold = 19; + eMaskWhichHandle which_handle; + + ED_mask_mouse_pos(sa, ar, event->mval, co); + ED_mask_get_size(sa, &width, &height); + + cv_point = ED_mask_point_find_nearest( + C, mask, co, threshold, &cv_masklay, &cv_spline, &which_handle, &cv_score); + + if (ED_mask_feather_find_nearest(C, + mask, + co, + threshold, + &feather_masklay, + &feather_spline, + &feather_point, + &uw, + &feather_score)) { + if (slide_feather || !cv_point || feather_score < cv_score) { + action = SLIDE_ACTION_FEATHER; + + masklay = feather_masklay; + spline = feather_spline; + point = feather_point; + } + } + + if (cv_point && action == SLIDE_ACTION_NONE) { + if (which_handle != MASK_WHICH_HANDLE_NONE) + action = SLIDE_ACTION_HANDLE; + else + action = SLIDE_ACTION_POINT; + + masklay = cv_masklay; + spline = cv_spline; + point = cv_point; + } + + if (action == SLIDE_ACTION_NONE) { + if (spline_under_mouse_get(C, mask, co, &masklay, &spline)) { + action = SLIDE_ACTION_SPLINE; + point = NULL; + } + } + + if (action != SLIDE_ACTION_NONE) { + customdata = MEM_callocN(sizeof(SlidePointData), "mask slide point data"); + customdata->event_invoke_type = event->type; + customdata->mask = mask; + customdata->masklay = masklay; + customdata->spline = spline; + customdata->point = point; + customdata->width = width; + customdata->height = height; + customdata->action = action; + customdata->uw = uw; + + customdata->is_sliding_new_point = RNA_boolean_get(op->ptr, "is_new_point"); + + if (customdata->action != SLIDE_ACTION_SPLINE) { + customdata->old_h1 = point->bezt.h1; + customdata->old_h2 = point->bezt.h2; + select_sliding_point(mask, masklay, spline, point, which_handle); + check_sliding_handle_type(point, which_handle); + } + + if (uw) { + float co_uw[2]; + float weight_scalar = BKE_mask_point_weight_scalar(spline, point, uw->u); + + customdata->weight = uw->w; + customdata->weight_scalar = weight_scalar; + BKE_mask_point_segment_co(spline, point, uw->u, co_uw); + BKE_mask_point_normal(spline, point, uw->u, customdata->no); + + madd_v2_v2v2fl(customdata->prev_feather_coord, co_uw, customdata->no, uw->w * weight_scalar); + } + else if (customdata->action != SLIDE_ACTION_SPLINE) { + BezTriple *bezt = &point->bezt; + + customdata->weight = bezt->weight; + customdata->weight_scalar = 1.0f; + BKE_mask_point_normal(spline, point, 0.0f, customdata->no); + + madd_v2_v2v2fl(customdata->prev_feather_coord, bezt->vec[1], customdata->no, bezt->weight); + } + + if (customdata->action == SLIDE_ACTION_FEATHER) { + customdata->is_initial_feather = slide_point_check_initial_feather(spline); + } + + if (customdata->action != SLIDE_ACTION_SPLINE) { + copy_m3_m3(customdata->vec, point->bezt.vec); + if (which_handle != MASK_WHICH_HANDLE_NONE) { + BKE_mask_point_handle(point, which_handle, customdata->orig_handle_coord); + copy_v2_v2(customdata->prev_handle_coord, customdata->orig_handle_coord); + } + } + customdata->which_handle = which_handle; + + ED_mask_mouse_pos(sa, ar, event->mval, customdata->prev_mouse_coord); + } + + return customdata; } static int slide_point_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - Mask *mask = CTX_data_edit_mask(C); - SlidePointData *slidedata; + Mask *mask = CTX_data_edit_mask(C); + SlidePointData *slidedata; - if (mask == NULL) { - return OPERATOR_PASS_THROUGH; - } + if (mask == NULL) { + return OPERATOR_PASS_THROUGH; + } - slidedata = slide_point_customdata(C, op, event); + slidedata = slide_point_customdata(C, op, event); - if (slidedata) { - op->customdata = slidedata; + if (slidedata) { + op->customdata = slidedata; - WM_event_add_modal_handler(C, op); + WM_event_add_modal_handler(C, op); - slidedata->masklay->act_spline = slidedata->spline; - slidedata->masklay->act_point = slidedata->point; + slidedata->masklay->act_spline = slidedata->spline; + slidedata->masklay->act_point = slidedata->point; - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_RUNNING_MODAL; - } + return OPERATOR_RUNNING_MODAL; + } - return OPERATOR_PASS_THROUGH; + return OPERATOR_PASS_THROUGH; } static void slide_point_delta_all_feather(SlidePointData *data, float delta) { - int i; + int i; - for (i = 0; i < data->spline->tot_point; i++) { - MaskSplinePoint *point = &data->spline->points[i]; - MaskSplinePoint *orig_point = &data->orig_spline->points[i]; + for (i = 0; i < data->spline->tot_point; i++) { + MaskSplinePoint *point = &data->spline->points[i]; + MaskSplinePoint *orig_point = &data->orig_spline->points[i]; - point->bezt.weight = orig_point->bezt.weight + delta; - if (point->bezt.weight < 0.0f) { - point->bezt.weight = 0.0f; - } - } + point->bezt.weight = orig_point->bezt.weight + delta; + if (point->bezt.weight < 0.0f) { + point->bezt.weight = 0.0f; + } + } } static void slide_point_restore_spline(SlidePointData *data) { - int i; + int i; - for (i = 0; i < data->spline->tot_point; i++) { - MaskSplinePoint *point = &data->spline->points[i]; - MaskSplinePoint *orig_point = &data->orig_spline->points[i]; - int j; + for (i = 0; i < data->spline->tot_point; i++) { + MaskSplinePoint *point = &data->spline->points[i]; + MaskSplinePoint *orig_point = &data->orig_spline->points[i]; + int j; - point->bezt = orig_point->bezt; + point->bezt = orig_point->bezt; - for (j = 0; j < point->tot_uw; j++) - point->uw[j] = orig_point->uw[j]; - } + for (j = 0; j < point->tot_uw; j++) + point->uw[j] = orig_point->uw[j]; + } } static void cancel_slide_point(SlidePointData *data) { - /* cancel sliding */ - - if (data->orig_spline) { - slide_point_restore_spline(data); - } - else { - if (data->action == SLIDE_ACTION_FEATHER) { - if (data->uw) - data->uw->w = data->weight; - else - data->point->bezt.weight = data->weight; - } - else if (data->action != SLIDE_ACTION_SPLINE) { - copy_m3_m3(data->point->bezt.vec, data->vec); - data->point->bezt.h1 = data->old_h1; - data->point->bezt.h2 = data->old_h2; - } - } + /* cancel sliding */ + + if (data->orig_spline) { + slide_point_restore_spline(data); + } + else { + if (data->action == SLIDE_ACTION_FEATHER) { + if (data->uw) + data->uw->w = data->weight; + else + data->point->bezt.weight = data->weight; + } + else if (data->action != SLIDE_ACTION_SPLINE) { + copy_m3_m3(data->point->bezt.vec, data->vec); + data->point->bezt.h1 = data->old_h1; + data->point->bezt.h2 = data->old_h2; + } + } } static void free_slide_point_data(SlidePointData *data) { - if (data->orig_spline) - BKE_mask_spline_free(data->orig_spline); + if (data->orig_spline) + BKE_mask_spline_free(data->orig_spline); - MEM_freeN(data); + MEM_freeN(data); } static int slide_point_modal(bContext *C, wmOperator *op, const wmEvent *event) { - SlidePointData *data = (SlidePointData *)op->customdata; - BezTriple *bezt = &data->point->bezt; - float co[2]; - - switch (event->type) { - case LEFTALTKEY: - case RIGHTALTKEY: - case LEFTSHIFTKEY: - case RIGHTSHIFTKEY: - if (ELEM(event->type, LEFTALTKEY, RIGHTALTKEY)) { - if (data->action == SLIDE_ACTION_FEATHER) { - data->is_overall_feather = (event->val == KM_PRESS); - } - else { - data->is_curvature_only = (event->val == KM_PRESS); - } - } - - if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) - data->is_accurate = (event->val == KM_PRESS); - - ATTR_FALLTHROUGH; /* update CV position */ - case MOUSEMOVE: - { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); - float delta[2]; - - ED_mask_mouse_pos(sa, ar, event->mval, co); - sub_v2_v2v2(delta, co, data->prev_mouse_coord); - if (data->is_accurate) { - mul_v2_fl(delta, 0.2f); - } - copy_v2_v2(data->prev_mouse_coord, co); - - if (data->action == SLIDE_ACTION_HANDLE) { - float new_handle[2]; - - if (data->is_sliding_new_point && data->which_handle == MASK_WHICH_HANDLE_STICK) { - if (ELEM(data->point, &data->spline->points[0], - &data->spline->points[data->spline->tot_point - 1])) - { - SWAP(float, delta[0], delta[1]); - delta[1] *= -1; - - /* flip last point */ - if (data->point != &data->spline->points[0]) { - negate_v2(delta); - } - } - } - - add_v2_v2v2(new_handle, data->prev_handle_coord, delta); - - BKE_mask_point_set_handle(data->point, data->which_handle, - new_handle, data->is_curvature_only, - data->orig_handle_coord, data->vec); - BKE_mask_point_handle(data->point, data->which_handle, data->prev_handle_coord); - - if (data->is_sliding_new_point) { - if (ELEM(data->which_handle, MASK_WHICH_HANDLE_LEFT, MASK_WHICH_HANDLE_RIGHT)) { - float vec[2]; - short self_handle = (data->which_handle == MASK_WHICH_HANDLE_LEFT) ? 0 : 2; - short other_handle = (data->which_handle == MASK_WHICH_HANDLE_LEFT) ? 2 : 0; - - sub_v2_v2v2(vec, bezt->vec[1], bezt->vec[self_handle]); - add_v2_v2v2(bezt->vec[other_handle], bezt->vec[1], vec); - } - } - } - else if (data->action == SLIDE_ACTION_POINT) { - add_v2_v2(bezt->vec[0], delta); - add_v2_v2(bezt->vec[1], delta); - add_v2_v2(bezt->vec[2], delta); - } - else if (data->action == SLIDE_ACTION_FEATHER) { - float vec[2], no[2], p[2], c[2], w, offco[2]; - float *weight = NULL; - float weight_scalar = 1.0f; - bool is_overall_feather = data->is_overall_feather || data->is_initial_feather; - - add_v2_v2v2(offco, data->prev_feather_coord, delta); - - if (data->uw) { - /* project on both sides and find the closest one, - * prevents flickering when projecting onto both sides can happen */ - const float u_pos = BKE_mask_spline_project_co(data->spline, data->point, - data->uw->u, offco, MASK_PROJ_NEG); - const float u_neg = BKE_mask_spline_project_co(data->spline, data->point, - data->uw->u, offco, MASK_PROJ_POS); - float dist_pos = FLT_MAX; - float dist_neg = FLT_MAX; - float co_pos[2]; - float co_neg[2]; - float u; - - if (u_pos > 0.0f && u_pos < 1.0f) { - BKE_mask_point_segment_co(data->spline, data->point, u_pos, co_pos); - dist_pos = len_squared_v2v2(offco, co_pos); - } - - if (u_neg > 0.0f && u_neg < 1.0f) { - BKE_mask_point_segment_co(data->spline, data->point, u_neg, co_neg); - dist_neg = len_squared_v2v2(offco, co_neg); - } - - u = dist_pos < dist_neg ? u_pos : u_neg; - - if (u > 0.0f && u < 1.0f) { - data->uw->u = u; - - data->uw = BKE_mask_point_sort_uw(data->point, data->uw); - weight = &data->uw->w; - weight_scalar = BKE_mask_point_weight_scalar(data->spline, data->point, u); - if (weight_scalar != 0.0f) { - weight_scalar = 1.0f / weight_scalar; - } - - BKE_mask_point_normal(data->spline, data->point, data->uw->u, no); - BKE_mask_point_segment_co(data->spline, data->point, data->uw->u, p); - } - } - else { - weight = &bezt->weight; - /* weight_scalar = 1.0f; keep as is */ - copy_v2_v2(no, data->no); - copy_v2_v2(p, bezt->vec[1]); - } - - if (weight) { - sub_v2_v2v2(c, offco, p); - project_v2_v2v2_normalized(vec, c, no); - - w = len_v2(vec); - - if (is_overall_feather) { - float w_delta; - - if (dot_v2v2(no, vec) <= 0.0f) - w = -w; - - w_delta = w - data->weight * data->weight_scalar; - - if (data->orig_spline == NULL) { - /* restore weight for currently sliding point, so orig_spline would be created - * with original weights used - */ - *weight = data->weight; - - data->orig_spline = BKE_mask_spline_copy(data->spline); - } - - if (data->is_initial_feather) { - *weight = w * weight_scalar; - } - - slide_point_delta_all_feather(data, w_delta); - } - else { - if (dot_v2v2(no, vec) <= 0.0f) - w = 0.0f; - - if (data->orig_spline) { - /* restore possible overall feather changes */ - slide_point_restore_spline(data); - - BKE_mask_spline_free(data->orig_spline); - data->orig_spline = NULL; - } - - if (weight_scalar != 0.0f) { - *weight = w * weight_scalar; - } - } - - copy_v2_v2(data->prev_feather_coord, offco); - } - } - else if (data->action == SLIDE_ACTION_SPLINE) { - int i; - - if (data->orig_spline == NULL) { - data->orig_spline = BKE_mask_spline_copy(data->spline); - } - - for (i = 0; i < data->spline->tot_point; i++) { - MaskSplinePoint *point = &data->spline->points[i]; - add_v2_v2(point->bezt.vec[0], delta); - add_v2_v2(point->bezt.vec[1], delta); - add_v2_v2(point->bezt.vec[2], delta); - } - } - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask); - DEG_id_tag_update(&data->mask->id, 0); - - break; - } - - case LEFTMOUSE: - case RIGHTMOUSE: - if (event->type == data->event_invoke_type && event->val == KM_RELEASE) { - Scene *scene = CTX_data_scene(C); - - /* dont key sliding feather uw's */ - if ((data->action == SLIDE_ACTION_FEATHER && data->uw) == false) { - if (IS_AUTOKEY_ON(scene)) { - ED_mask_layer_shape_auto_key(data->masklay, CFRA); - } - } - - if (data->is_sliding_new_point) { - if (len_squared_v2v2(bezt->vec[0], bezt->vec[1]) < FLT_EPSILON) { - bezt->h1 = HD_VECT; - } - if (len_squared_v2v2(bezt->vec[2], bezt->vec[1]) < FLT_EPSILON) { - bezt->h2 = HD_VECT; - } - } - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask); - DEG_id_tag_update(&data->mask->id, 0); - - free_slide_point_data(op->customdata); /* keep this last! */ - return OPERATOR_FINISHED; - } - else if (event->type != data->event_invoke_type && event->val == KM_PRESS) { - /* pass to ESCKEY */ - } - else { - break; - } - - case ESCKEY: - cancel_slide_point(op->customdata); - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask); - DEG_id_tag_update(&data->mask->id, 0); - - free_slide_point_data(op->customdata); /* keep this last! */ - return OPERATOR_CANCELLED; - } - - return OPERATOR_RUNNING_MODAL; + SlidePointData *data = (SlidePointData *)op->customdata; + BezTriple *bezt = &data->point->bezt; + float co[2]; + + switch (event->type) { + case LEFTALTKEY: + case RIGHTALTKEY: + case LEFTSHIFTKEY: + case RIGHTSHIFTKEY: + if (ELEM(event->type, LEFTALTKEY, RIGHTALTKEY)) { + if (data->action == SLIDE_ACTION_FEATHER) { + data->is_overall_feather = (event->val == KM_PRESS); + } + else { + data->is_curvature_only = (event->val == KM_PRESS); + } + } + + if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) + data->is_accurate = (event->val == KM_PRESS); + + ATTR_FALLTHROUGH; /* update CV position */ + case MOUSEMOVE: { + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); + float delta[2]; + + ED_mask_mouse_pos(sa, ar, event->mval, co); + sub_v2_v2v2(delta, co, data->prev_mouse_coord); + if (data->is_accurate) { + mul_v2_fl(delta, 0.2f); + } + copy_v2_v2(data->prev_mouse_coord, co); + + if (data->action == SLIDE_ACTION_HANDLE) { + float new_handle[2]; + + if (data->is_sliding_new_point && data->which_handle == MASK_WHICH_HANDLE_STICK) { + if (ELEM(data->point, + &data->spline->points[0], + &data->spline->points[data->spline->tot_point - 1])) { + SWAP(float, delta[0], delta[1]); + delta[1] *= -1; + + /* flip last point */ + if (data->point != &data->spline->points[0]) { + negate_v2(delta); + } + } + } + + add_v2_v2v2(new_handle, data->prev_handle_coord, delta); + + BKE_mask_point_set_handle(data->point, + data->which_handle, + new_handle, + data->is_curvature_only, + data->orig_handle_coord, + data->vec); + BKE_mask_point_handle(data->point, data->which_handle, data->prev_handle_coord); + + if (data->is_sliding_new_point) { + if (ELEM(data->which_handle, MASK_WHICH_HANDLE_LEFT, MASK_WHICH_HANDLE_RIGHT)) { + float vec[2]; + short self_handle = (data->which_handle == MASK_WHICH_HANDLE_LEFT) ? 0 : 2; + short other_handle = (data->which_handle == MASK_WHICH_HANDLE_LEFT) ? 2 : 0; + + sub_v2_v2v2(vec, bezt->vec[1], bezt->vec[self_handle]); + add_v2_v2v2(bezt->vec[other_handle], bezt->vec[1], vec); + } + } + } + else if (data->action == SLIDE_ACTION_POINT) { + add_v2_v2(bezt->vec[0], delta); + add_v2_v2(bezt->vec[1], delta); + add_v2_v2(bezt->vec[2], delta); + } + else if (data->action == SLIDE_ACTION_FEATHER) { + float vec[2], no[2], p[2], c[2], w, offco[2]; + float *weight = NULL; + float weight_scalar = 1.0f; + bool is_overall_feather = data->is_overall_feather || data->is_initial_feather; + + add_v2_v2v2(offco, data->prev_feather_coord, delta); + + if (data->uw) { + /* project on both sides and find the closest one, + * prevents flickering when projecting onto both sides can happen */ + const float u_pos = BKE_mask_spline_project_co( + data->spline, data->point, data->uw->u, offco, MASK_PROJ_NEG); + const float u_neg = BKE_mask_spline_project_co( + data->spline, data->point, data->uw->u, offco, MASK_PROJ_POS); + float dist_pos = FLT_MAX; + float dist_neg = FLT_MAX; + float co_pos[2]; + float co_neg[2]; + float u; + + if (u_pos > 0.0f && u_pos < 1.0f) { + BKE_mask_point_segment_co(data->spline, data->point, u_pos, co_pos); + dist_pos = len_squared_v2v2(offco, co_pos); + } + + if (u_neg > 0.0f && u_neg < 1.0f) { + BKE_mask_point_segment_co(data->spline, data->point, u_neg, co_neg); + dist_neg = len_squared_v2v2(offco, co_neg); + } + + u = dist_pos < dist_neg ? u_pos : u_neg; + + if (u > 0.0f && u < 1.0f) { + data->uw->u = u; + + data->uw = BKE_mask_point_sort_uw(data->point, data->uw); + weight = &data->uw->w; + weight_scalar = BKE_mask_point_weight_scalar(data->spline, data->point, u); + if (weight_scalar != 0.0f) { + weight_scalar = 1.0f / weight_scalar; + } + + BKE_mask_point_normal(data->spline, data->point, data->uw->u, no); + BKE_mask_point_segment_co(data->spline, data->point, data->uw->u, p); + } + } + else { + weight = &bezt->weight; + /* weight_scalar = 1.0f; keep as is */ + copy_v2_v2(no, data->no); + copy_v2_v2(p, bezt->vec[1]); + } + + if (weight) { + sub_v2_v2v2(c, offco, p); + project_v2_v2v2_normalized(vec, c, no); + + w = len_v2(vec); + + if (is_overall_feather) { + float w_delta; + + if (dot_v2v2(no, vec) <= 0.0f) + w = -w; + + w_delta = w - data->weight * data->weight_scalar; + + if (data->orig_spline == NULL) { + /* restore weight for currently sliding point, so orig_spline would be created + * with original weights used + */ + *weight = data->weight; + + data->orig_spline = BKE_mask_spline_copy(data->spline); + } + + if (data->is_initial_feather) { + *weight = w * weight_scalar; + } + + slide_point_delta_all_feather(data, w_delta); + } + else { + if (dot_v2v2(no, vec) <= 0.0f) + w = 0.0f; + + if (data->orig_spline) { + /* restore possible overall feather changes */ + slide_point_restore_spline(data); + + BKE_mask_spline_free(data->orig_spline); + data->orig_spline = NULL; + } + + if (weight_scalar != 0.0f) { + *weight = w * weight_scalar; + } + } + + copy_v2_v2(data->prev_feather_coord, offco); + } + } + else if (data->action == SLIDE_ACTION_SPLINE) { + int i; + + if (data->orig_spline == NULL) { + data->orig_spline = BKE_mask_spline_copy(data->spline); + } + + for (i = 0; i < data->spline->tot_point; i++) { + MaskSplinePoint *point = &data->spline->points[i]; + add_v2_v2(point->bezt.vec[0], delta); + add_v2_v2(point->bezt.vec[1], delta); + add_v2_v2(point->bezt.vec[2], delta); + } + } + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask); + DEG_id_tag_update(&data->mask->id, 0); + + break; + } + + case LEFTMOUSE: + case RIGHTMOUSE: + if (event->type == data->event_invoke_type && event->val == KM_RELEASE) { + Scene *scene = CTX_data_scene(C); + + /* dont key sliding feather uw's */ + if ((data->action == SLIDE_ACTION_FEATHER && data->uw) == false) { + if (IS_AUTOKEY_ON(scene)) { + ED_mask_layer_shape_auto_key(data->masklay, CFRA); + } + } + + if (data->is_sliding_new_point) { + if (len_squared_v2v2(bezt->vec[0], bezt->vec[1]) < FLT_EPSILON) { + bezt->h1 = HD_VECT; + } + if (len_squared_v2v2(bezt->vec[2], bezt->vec[1]) < FLT_EPSILON) { + bezt->h2 = HD_VECT; + } + } + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask); + DEG_id_tag_update(&data->mask->id, 0); + + free_slide_point_data(op->customdata); /* keep this last! */ + return OPERATOR_FINISHED; + } + else if (event->type != data->event_invoke_type && event->val == KM_PRESS) { + /* pass to ESCKEY */ + } + else { + break; + } + + case ESCKEY: + cancel_slide_point(op->customdata); + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, data->mask); + DEG_id_tag_update(&data->mask->id, 0); + + free_slide_point_data(op->customdata); /* keep this last! */ + return OPERATOR_CANCELLED; + } + + return OPERATOR_RUNNING_MODAL; } void MASK_OT_slide_point(wmOperatorType *ot) { - PropertyRNA *prop; - - /* identifiers */ - ot->name = "Slide Point"; - ot->description = "Slide control points"; - ot->idname = "MASK_OT_slide_point"; - - /* api callbacks */ - ot->invoke = slide_point_invoke; - ot->modal = slide_point_modal; - ot->poll = ED_operator_mask; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - RNA_def_boolean(ot->srna, "slide_feather", 0, "Slide Feather", "First try to slide feather instead of vertex"); - - prop = RNA_def_boolean(ot->srna, "is_new_point", 0, "Slide New Point", "Newly created vertex is being slid"); - RNA_def_property_flag(prop, PROP_SKIP_SAVE); + PropertyRNA *prop; + + /* identifiers */ + ot->name = "Slide Point"; + ot->description = "Slide control points"; + ot->idname = "MASK_OT_slide_point"; + + /* api callbacks */ + ot->invoke = slide_point_invoke; + ot->modal = slide_point_modal; + ot->poll = ED_operator_mask; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + RNA_def_boolean(ot->srna, + "slide_feather", + 0, + "Slide Feather", + "First try to slide feather instead of vertex"); + + prop = RNA_def_boolean( + ot->srna, "is_new_point", 0, "Slide New Point", "Newly created vertex is being slid"); + RNA_def_property_flag(prop, PROP_SKIP_SAVE); } /******************** slide spline curvature *********************/ typedef struct SlideSplineCurvatureData { - short event_invoke_type; + short event_invoke_type; - Mask *mask; - MaskLayer *mask_layer; - MaskSpline *spline; - MaskSplinePoint *point; - float u; - bool accurate; + Mask *mask; + MaskLayer *mask_layer; + MaskSpline *spline; + MaskSplinePoint *point; + float u; + bool accurate; - BezTriple *adjust_bezt, *other_bezt; - BezTriple bezt_backup, other_bezt_backup; + BezTriple *adjust_bezt, *other_bezt; + BezTriple bezt_backup, other_bezt_backup; - float prev_mouse_coord[2]; - float prev_spline_coord[2]; + float prev_mouse_coord[2]; + float prev_spline_coord[2]; - float P0[2], P1[2], P2[2], P3[3]; + float P0[2], P1[2], P2[2], P3[3]; } SlideSplineCurvatureData; static void cancel_slide_spline_curvature(SlideSplineCurvatureData *slide_data) { - *slide_data->adjust_bezt = slide_data->bezt_backup; - *slide_data->other_bezt = slide_data->other_bezt_backup; + *slide_data->adjust_bezt = slide_data->bezt_backup; + *slide_data->other_bezt = slide_data->other_bezt_backup; } - static void free_slide_spline_curvature_data(SlideSplineCurvatureData *slide_data) { - MEM_freeN(slide_data); + MEM_freeN(slide_data); } static bool slide_spline_curvature_check(bContext *C, const wmEvent *event) { - Mask *mask = CTX_data_edit_mask(C); - float co[2]; - const float threshold = 19.0f; + Mask *mask = CTX_data_edit_mask(C); + float co[2]; + const float threshold = 19.0f; - ED_mask_mouse_pos(CTX_wm_area(C), CTX_wm_region(C), event->mval, co); + ED_mask_mouse_pos(CTX_wm_area(C), CTX_wm_region(C), event->mval, co); - if (ED_mask_point_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL)) { - return false; - } + if (ED_mask_point_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL)) { + return false; + } - if (ED_mask_feather_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL, NULL)) { - return false; - } + if (ED_mask_feather_find_nearest(C, mask, co, threshold, NULL, NULL, NULL, NULL, NULL)) { + return false; + } - return true; + return true; } -static SlideSplineCurvatureData *slide_spline_curvature_customdata( - bContext *C, const wmEvent *event) +static SlideSplineCurvatureData *slide_spline_curvature_customdata(bContext *C, + const wmEvent *event) { - const float threshold = 19.0f; - - Mask *mask = CTX_data_edit_mask(C); - SlideSplineCurvatureData *slide_data; - MaskLayer *mask_layer; - MaskSpline *spline; - MaskSplinePoint *point; - float u, co[2]; - BezTriple *next_bezt; - - ED_mask_mouse_pos(CTX_wm_area(C), CTX_wm_region(C), event->mval, co); - - if (!ED_mask_find_nearest_diff_point(C, mask, co, threshold, false, - NULL, true, false, - &mask_layer, &spline, &point, &u, - NULL)) - { - return NULL; - } - - next_bezt = BKE_mask_spline_point_next_bezt(spline, spline->points, point); - if (next_bezt == NULL) { - return NULL; - } - - slide_data = MEM_callocN(sizeof(SlideSplineCurvatureData), "slide curvature slide"); - slide_data->event_invoke_type = event->type; - slide_data->mask = mask; - slide_data->mask_layer = mask_layer; - slide_data->spline = spline; - slide_data->point = point; - slide_data->u = u; - - copy_v2_v2(slide_data->prev_mouse_coord, co); - BKE_mask_point_segment_co(spline, point, u, slide_data->prev_spline_coord); - - copy_v2_v2(slide_data->P0, point->bezt.vec[1]); - copy_v2_v2(slide_data->P1, point->bezt.vec[2]); - copy_v2_v2(slide_data->P2, next_bezt->vec[0]); - copy_v2_v2(slide_data->P3, next_bezt->vec[1]); - - /* Depending to which end we're closer to adjust either left or right side of the spline. */ - if (u <= 0.5f) { - slide_data->adjust_bezt = &point->bezt; - slide_data->other_bezt = next_bezt; - } - else { - slide_data->adjust_bezt = next_bezt; - slide_data->other_bezt = &point->bezt; - } - - /* Data needed for restoring state. */ - slide_data->bezt_backup = *slide_data->adjust_bezt; - slide_data->other_bezt_backup = *slide_data->other_bezt; - - /* Let's dont touch other side of the point for now, so set handle to FREE. */ - if (u < 0.5f) { - if (slide_data->adjust_bezt->h2 <= HD_VECT) { - slide_data->adjust_bezt->h2 = HD_FREE; - } - } - else { - if (slide_data->adjust_bezt->h1 <= HD_VECT) { - slide_data->adjust_bezt->h1 = HD_FREE; - } - } - - /* Change selection */ - ED_mask_select_toggle_all(mask, SEL_DESELECT); - slide_data->adjust_bezt->f2 |= SELECT; - slide_data->other_bezt->f2 |= SELECT; - if (u < 0.5f) { - slide_data->adjust_bezt->f3 |= SELECT; - slide_data->other_bezt->f1 |= SELECT; - } - else { - slide_data->adjust_bezt->f1 |= SELECT; - slide_data->other_bezt->f3 |= SELECT; - } - mask_layer->act_spline = spline; - mask_layer->act_point = point; - ED_mask_select_flush_all(mask); - - return slide_data; + const float threshold = 19.0f; + + Mask *mask = CTX_data_edit_mask(C); + SlideSplineCurvatureData *slide_data; + MaskLayer *mask_layer; + MaskSpline *spline; + MaskSplinePoint *point; + float u, co[2]; + BezTriple *next_bezt; + + ED_mask_mouse_pos(CTX_wm_area(C), CTX_wm_region(C), event->mval, co); + + if (!ED_mask_find_nearest_diff_point(C, + mask, + co, + threshold, + false, + NULL, + true, + false, + &mask_layer, + &spline, + &point, + &u, + NULL)) { + return NULL; + } + + next_bezt = BKE_mask_spline_point_next_bezt(spline, spline->points, point); + if (next_bezt == NULL) { + return NULL; + } + + slide_data = MEM_callocN(sizeof(SlideSplineCurvatureData), "slide curvature slide"); + slide_data->event_invoke_type = event->type; + slide_data->mask = mask; + slide_data->mask_layer = mask_layer; + slide_data->spline = spline; + slide_data->point = point; + slide_data->u = u; + + copy_v2_v2(slide_data->prev_mouse_coord, co); + BKE_mask_point_segment_co(spline, point, u, slide_data->prev_spline_coord); + + copy_v2_v2(slide_data->P0, point->bezt.vec[1]); + copy_v2_v2(slide_data->P1, point->bezt.vec[2]); + copy_v2_v2(slide_data->P2, next_bezt->vec[0]); + copy_v2_v2(slide_data->P3, next_bezt->vec[1]); + + /* Depending to which end we're closer to adjust either left or right side of the spline. */ + if (u <= 0.5f) { + slide_data->adjust_bezt = &point->bezt; + slide_data->other_bezt = next_bezt; + } + else { + slide_data->adjust_bezt = next_bezt; + slide_data->other_bezt = &point->bezt; + } + + /* Data needed for restoring state. */ + slide_data->bezt_backup = *slide_data->adjust_bezt; + slide_data->other_bezt_backup = *slide_data->other_bezt; + + /* Let's dont touch other side of the point for now, so set handle to FREE. */ + if (u < 0.5f) { + if (slide_data->adjust_bezt->h2 <= HD_VECT) { + slide_data->adjust_bezt->h2 = HD_FREE; + } + } + else { + if (slide_data->adjust_bezt->h1 <= HD_VECT) { + slide_data->adjust_bezt->h1 = HD_FREE; + } + } + + /* Change selection */ + ED_mask_select_toggle_all(mask, SEL_DESELECT); + slide_data->adjust_bezt->f2 |= SELECT; + slide_data->other_bezt->f2 |= SELECT; + if (u < 0.5f) { + slide_data->adjust_bezt->f3 |= SELECT; + slide_data->other_bezt->f1 |= SELECT; + } + else { + slide_data->adjust_bezt->f1 |= SELECT; + slide_data->other_bezt->f3 |= SELECT; + } + mask_layer->act_spline = spline; + mask_layer->act_point = point; + ED_mask_select_flush_all(mask); + + return slide_data; } static int slide_spline_curvature_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - Mask *mask = CTX_data_edit_mask(C); - SlideSplineCurvatureData *slide_data; - - if (mask == NULL) { - return OPERATOR_PASS_THROUGH; - } - - /* Be sure we don't conflict with point slide here. */ - if (!slide_spline_curvature_check(C, event)) { - return OPERATOR_PASS_THROUGH; - } - - slide_data = slide_spline_curvature_customdata(C, event); - if (slide_data != NULL) { - op->customdata = slide_data; - WM_event_add_modal_handler(C, op); - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_RUNNING_MODAL; - } - - return OPERATOR_PASS_THROUGH; + Mask *mask = CTX_data_edit_mask(C); + SlideSplineCurvatureData *slide_data; + + if (mask == NULL) { + return OPERATOR_PASS_THROUGH; + } + + /* Be sure we don't conflict with point slide here. */ + if (!slide_spline_curvature_check(C, event)) { + return OPERATOR_PASS_THROUGH; + } + + slide_data = slide_spline_curvature_customdata(C, event); + if (slide_data != NULL) { + op->customdata = slide_data; + WM_event_add_modal_handler(C, op); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + return OPERATOR_RUNNING_MODAL; + } + + return OPERATOR_PASS_THROUGH; } static void slide_spline_solve_P1(const float u, @@ -1307,13 +1344,13 @@ static void slide_spline_solve_P1(const float u, const float P3[2], float solution[2]) { - const float u2 = u * u, u3 = u * u * u; - const float v = 1.0f - u; - const float v2 = v * v, v3 = v * v * v; - const float inv_divider = 1.0f / (3.0f * v2 * u); - const float t = 3.0f * v * u2; - solution[0] = -(v3 * P0[0] + t * P2[0] + u3 * P3[0] - B[0]) * inv_divider; - solution[1] = -(v3 * P0[1] + t * P2[1] + u3 * P3[1] - B[1]) * inv_divider; + const float u2 = u * u, u3 = u * u * u; + const float v = 1.0f - u; + const float v2 = v * v, v3 = v * v * v; + const float inv_divider = 1.0f / (3.0f * v2 * u); + const float t = 3.0f * v * u2; + solution[0] = -(v3 * P0[0] + t * P2[0] + u3 * P3[0] - B[0]) * inv_divider; + solution[1] = -(v3 * P0[1] + t * P2[1] + u3 * P3[1] - B[1]) * inv_divider; } static void slide_spline_solve_P2(const float u, @@ -1323,1055 +1360,1032 @@ static void slide_spline_solve_P2(const float u, const float P3[2], float solution[2]) { - const float u2 = u * u, u3 = u * u * u; - const float v = 1.0f - u; - const float v2 = v * v, v3 = v * v * v; - const float inv_divider = 1.0f / (3.0f * v * u2); - const float t = 3.0f * v2 * u; - solution[0] = -(v3 * P0[0] + t * P1[0] + u3 * P3[0] - B[0]) * inv_divider; - solution[1] = -(v3 * P0[1] + t * P1[1] + u3 * P3[1] - B[1]) * inv_divider; + const float u2 = u * u, u3 = u * u * u; + const float v = 1.0f - u; + const float v2 = v * v, v3 = v * v * v; + const float inv_divider = 1.0f / (3.0f * v * u2); + const float t = 3.0f * v2 * u; + solution[0] = -(v3 * P0[0] + t * P1[0] + u3 * P3[0] - B[0]) * inv_divider; + solution[1] = -(v3 * P0[1] + t * P1[1] + u3 * P3[1] - B[1]) * inv_divider; } static int slide_spline_curvature_modal(bContext *C, wmOperator *op, const wmEvent *event) { - Scene *scene = CTX_data_scene(C); - const float margin = 0.2f; - SlideSplineCurvatureData *slide_data = (SlideSplineCurvatureData *) op->customdata; - float u = slide_data->u; - - switch (event->type) { - case LEFTSHIFTKEY: - case RIGHTSHIFTKEY: - case LEFTCTRLKEY: - case RIGHTCTRLKEY: - if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) { - slide_data->accurate = (event->val == KM_PRESS); - } - - if (ELEM(event->type, LEFTCTRLKEY, RIGHTCTRLKEY)) { - if (event->val == KM_PRESS) { - slide_data->adjust_bezt->h1 = slide_data->adjust_bezt->h2 = HD_FREE; - if ((u > margin && u < 0.5f) || (u >= 0.5f && u < 1.0f - margin)) { - slide_data->other_bezt->h1 = slide_data->other_bezt->h2 = HD_FREE; - } - } - else if (event->val == KM_RELEASE) { - slide_data->adjust_bezt->h1 = slide_data->bezt_backup.h1; - slide_data->adjust_bezt->h2 = slide_data->bezt_backup.h2; - slide_data->other_bezt->h1 = slide_data->other_bezt_backup.h1; - slide_data->other_bezt->h2 = slide_data->other_bezt_backup.h2; - } - - if (u < 0.5f) { - copy_v2_v2(slide_data->adjust_bezt->vec[0], slide_data->bezt_backup.vec[0]); - copy_v2_v2(slide_data->other_bezt->vec[2], slide_data->other_bezt_backup.vec[2]); - } - else { - copy_v2_v2(slide_data->adjust_bezt->vec[2], slide_data->bezt_backup.vec[2]); - copy_v2_v2(slide_data->other_bezt->vec[0], slide_data->other_bezt_backup.vec[0]); - } - - } - - ATTR_FALLTHROUGH; /* update CV position */ - case MOUSEMOVE: - { - float B[2], mouse_coord[2], delta[2]; - - /* Get coordinate spline is expected to go through. */ - ED_mask_mouse_pos(CTX_wm_area(C), CTX_wm_region(C), event->mval, mouse_coord); - sub_v2_v2v2(delta, mouse_coord, slide_data->prev_mouse_coord); - if (slide_data->accurate) { - mul_v2_fl(delta, 0.2f); - } - add_v2_v2v2(B, slide_data->prev_spline_coord, delta); - copy_v2_v2(slide_data->prev_spline_coord, B); - copy_v2_v2(slide_data->prev_mouse_coord, mouse_coord); - - if (u < 0.5f) { - float oldP2[2]; - bool need_restore_P2 = false; - - if (u > margin) { - float solution[2]; - float x = (u - margin) * 0.5f / (0.5f - margin); - float weight = (3 * x * x - 2 * x * x * x); - - slide_spline_solve_P2(u, B, - slide_data->P0, - slide_data->P1, - slide_data->P3, - solution); - - copy_v2_v2(oldP2, slide_data->P2); - interp_v2_v2v2(slide_data->P2, slide_data->P2, solution, weight); - copy_v2_v2(slide_data->other_bezt->vec[0], slide_data->P2); - need_restore_P2 = true; - - /* Tweak handle type in order to be able to apply the delta. */ - if (weight > 0.0f) { - if (slide_data->other_bezt->h1 <= HD_VECT) { - slide_data->other_bezt->h1 = HD_FREE; - } - } - } - - slide_spline_solve_P1(u, B, - slide_data->P0, - slide_data->P2, - slide_data->P3, - slide_data->adjust_bezt->vec[2]); - - if (need_restore_P2) { - copy_v2_v2(slide_data->P2, oldP2); - } - } - else { - float oldP1[2]; - bool need_restore_P1 = false; - - if (u < 1.0f - margin) { - float solution[2]; - float x = ((1.0f - u) - margin) * 0.5f / (0.5f - margin); - float weight = 3 * x * x - 2 * x * x * x; - - slide_spline_solve_P1(u, B, - slide_data->P0, - slide_data->P2, - slide_data->P3, - solution); - - copy_v2_v2(oldP1, slide_data->P1); - interp_v2_v2v2(slide_data->P1, slide_data->P1, solution, weight); - copy_v2_v2(slide_data->other_bezt->vec[2], slide_data->P1); - need_restore_P1 = true; - - /* Tweak handle type in order to be able to apply the delta. */ - if (weight > 0.0f) { - if (slide_data->other_bezt->h2 <= HD_VECT) { - slide_data->other_bezt->h2 = HD_FREE; - } - } - } - - slide_spline_solve_P2(u, B, - slide_data->P0, - slide_data->P1, - slide_data->P3, - slide_data->adjust_bezt->vec[0]); - - if (need_restore_P1) { - copy_v2_v2(slide_data->P1, oldP1); - } - } - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, slide_data->mask); - DEG_id_tag_update(&slide_data->mask->id, 0); - - break; - } - - case LEFTMOUSE: - case RIGHTMOUSE: - if (event->type == slide_data->event_invoke_type && event->val == KM_RELEASE) { - /* dont key sliding feather uw's */ - if (IS_AUTOKEY_ON(scene)) { - ED_mask_layer_shape_auto_key(slide_data->mask_layer, CFRA); - } - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, slide_data->mask); - DEG_id_tag_update(&slide_data->mask->id, 0); - - free_slide_spline_curvature_data(slide_data); /* keep this last! */ - return OPERATOR_FINISHED; - } - - break; - - case ESCKEY: - cancel_slide_spline_curvature(slide_data); - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, slide_data->mask); - DEG_id_tag_update(&slide_data->mask->id, 0); - - free_slide_spline_curvature_data(op->customdata); /* keep this last! */ - return OPERATOR_CANCELLED; - } - - return OPERATOR_RUNNING_MODAL; + Scene *scene = CTX_data_scene(C); + const float margin = 0.2f; + SlideSplineCurvatureData *slide_data = (SlideSplineCurvatureData *)op->customdata; + float u = slide_data->u; + + switch (event->type) { + case LEFTSHIFTKEY: + case RIGHTSHIFTKEY: + case LEFTCTRLKEY: + case RIGHTCTRLKEY: + if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) { + slide_data->accurate = (event->val == KM_PRESS); + } + + if (ELEM(event->type, LEFTCTRLKEY, RIGHTCTRLKEY)) { + if (event->val == KM_PRESS) { + slide_data->adjust_bezt->h1 = slide_data->adjust_bezt->h2 = HD_FREE; + if ((u > margin && u < 0.5f) || (u >= 0.5f && u < 1.0f - margin)) { + slide_data->other_bezt->h1 = slide_data->other_bezt->h2 = HD_FREE; + } + } + else if (event->val == KM_RELEASE) { + slide_data->adjust_bezt->h1 = slide_data->bezt_backup.h1; + slide_data->adjust_bezt->h2 = slide_data->bezt_backup.h2; + slide_data->other_bezt->h1 = slide_data->other_bezt_backup.h1; + slide_data->other_bezt->h2 = slide_data->other_bezt_backup.h2; + } + + if (u < 0.5f) { + copy_v2_v2(slide_data->adjust_bezt->vec[0], slide_data->bezt_backup.vec[0]); + copy_v2_v2(slide_data->other_bezt->vec[2], slide_data->other_bezt_backup.vec[2]); + } + else { + copy_v2_v2(slide_data->adjust_bezt->vec[2], slide_data->bezt_backup.vec[2]); + copy_v2_v2(slide_data->other_bezt->vec[0], slide_data->other_bezt_backup.vec[0]); + } + } + + ATTR_FALLTHROUGH; /* update CV position */ + case MOUSEMOVE: { + float B[2], mouse_coord[2], delta[2]; + + /* Get coordinate spline is expected to go through. */ + ED_mask_mouse_pos(CTX_wm_area(C), CTX_wm_region(C), event->mval, mouse_coord); + sub_v2_v2v2(delta, mouse_coord, slide_data->prev_mouse_coord); + if (slide_data->accurate) { + mul_v2_fl(delta, 0.2f); + } + add_v2_v2v2(B, slide_data->prev_spline_coord, delta); + copy_v2_v2(slide_data->prev_spline_coord, B); + copy_v2_v2(slide_data->prev_mouse_coord, mouse_coord); + + if (u < 0.5f) { + float oldP2[2]; + bool need_restore_P2 = false; + + if (u > margin) { + float solution[2]; + float x = (u - margin) * 0.5f / (0.5f - margin); + float weight = (3 * x * x - 2 * x * x * x); + + slide_spline_solve_P2(u, B, slide_data->P0, slide_data->P1, slide_data->P3, solution); + + copy_v2_v2(oldP2, slide_data->P2); + interp_v2_v2v2(slide_data->P2, slide_data->P2, solution, weight); + copy_v2_v2(slide_data->other_bezt->vec[0], slide_data->P2); + need_restore_P2 = true; + + /* Tweak handle type in order to be able to apply the delta. */ + if (weight > 0.0f) { + if (slide_data->other_bezt->h1 <= HD_VECT) { + slide_data->other_bezt->h1 = HD_FREE; + } + } + } + + slide_spline_solve_P1( + u, B, slide_data->P0, slide_data->P2, slide_data->P3, slide_data->adjust_bezt->vec[2]); + + if (need_restore_P2) { + copy_v2_v2(slide_data->P2, oldP2); + } + } + else { + float oldP1[2]; + bool need_restore_P1 = false; + + if (u < 1.0f - margin) { + float solution[2]; + float x = ((1.0f - u) - margin) * 0.5f / (0.5f - margin); + float weight = 3 * x * x - 2 * x * x * x; + + slide_spline_solve_P1(u, B, slide_data->P0, slide_data->P2, slide_data->P3, solution); + + copy_v2_v2(oldP1, slide_data->P1); + interp_v2_v2v2(slide_data->P1, slide_data->P1, solution, weight); + copy_v2_v2(slide_data->other_bezt->vec[2], slide_data->P1); + need_restore_P1 = true; + + /* Tweak handle type in order to be able to apply the delta. */ + if (weight > 0.0f) { + if (slide_data->other_bezt->h2 <= HD_VECT) { + slide_data->other_bezt->h2 = HD_FREE; + } + } + } + + slide_spline_solve_P2( + u, B, slide_data->P0, slide_data->P1, slide_data->P3, slide_data->adjust_bezt->vec[0]); + + if (need_restore_P1) { + copy_v2_v2(slide_data->P1, oldP1); + } + } + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, slide_data->mask); + DEG_id_tag_update(&slide_data->mask->id, 0); + + break; + } + + case LEFTMOUSE: + case RIGHTMOUSE: + if (event->type == slide_data->event_invoke_type && event->val == KM_RELEASE) { + /* dont key sliding feather uw's */ + if (IS_AUTOKEY_ON(scene)) { + ED_mask_layer_shape_auto_key(slide_data->mask_layer, CFRA); + } + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, slide_data->mask); + DEG_id_tag_update(&slide_data->mask->id, 0); + + free_slide_spline_curvature_data(slide_data); /* keep this last! */ + return OPERATOR_FINISHED; + } + + break; + + case ESCKEY: + cancel_slide_spline_curvature(slide_data); + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, slide_data->mask); + DEG_id_tag_update(&slide_data->mask->id, 0); + + free_slide_spline_curvature_data(op->customdata); /* keep this last! */ + return OPERATOR_CANCELLED; + } + + return OPERATOR_RUNNING_MODAL; } void MASK_OT_slide_spline_curvature(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Slide Spline Curvature"; - ot->description = "Slide a point on the spline to define it's curvature"; - ot->idname = "MASK_OT_slide_spline_curvature"; - - /* api callbacks */ - ot->invoke = slide_spline_curvature_invoke; - ot->modal = slide_spline_curvature_modal; - ot->poll = ED_operator_mask; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* identifiers */ + ot->name = "Slide Spline Curvature"; + ot->description = "Slide a point on the spline to define it's curvature"; + ot->idname = "MASK_OT_slide_spline_curvature"; + + /* api callbacks */ + ot->invoke = slide_spline_curvature_invoke; + ot->modal = slide_spline_curvature_modal; + ot->poll = ED_operator_mask; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /******************** toggle cyclic *********************/ static int cyclic_toggle_exec(bContext *C, wmOperator *UNUSED(op)) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - if (ED_mask_spline_select_check(spline)) { - spline->flag ^= MASK_SPLINE_CYCLIC; - } - } - } + for (spline = masklay->splines.first; spline; spline = spline->next) { + if (ED_mask_spline_select_check(spline)) { + spline->flag ^= MASK_SPLINE_CYCLIC; + } + } + } - DEG_id_tag_update(&mask->id, 0); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + DEG_id_tag_update(&mask->id, 0); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_cyclic_toggle(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Toggle Cyclic"; - ot->description = "Toggle cyclic for selected splines"; - ot->idname = "MASK_OT_cyclic_toggle"; + /* identifiers */ + ot->name = "Toggle Cyclic"; + ot->description = "Toggle cyclic for selected splines"; + ot->idname = "MASK_OT_cyclic_toggle"; - /* api callbacks */ - ot->exec = cyclic_toggle_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = cyclic_toggle_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /******************** delete *********************/ static void delete_feather_points(MaskSplinePoint *point) { - int i, count = 0; - - if (!point->tot_uw) - return; - - for (i = 0; i < point->tot_uw; i++) { - if ((point->uw[i].flag & SELECT) == 0) - count++; - } - - if (count == 0) { - MEM_freeN(point->uw); - point->uw = NULL; - point->tot_uw = 0; - } - else { - MaskSplinePointUW *new_uw; - int j = 0; - - new_uw = MEM_callocN(count * sizeof(MaskSplinePointUW), "new mask uw points"); - - for (i = 0; i < point->tot_uw; i++) { - if ((point->uw[i].flag & SELECT) == 0) { - new_uw[j++] = point->uw[i]; - } - } - - MEM_freeN(point->uw); - - point->uw = new_uw; - point->tot_uw = count; - } + int i, count = 0; + + if (!point->tot_uw) + return; + + for (i = 0; i < point->tot_uw; i++) { + if ((point->uw[i].flag & SELECT) == 0) + count++; + } + + if (count == 0) { + MEM_freeN(point->uw); + point->uw = NULL; + point->tot_uw = 0; + } + else { + MaskSplinePointUW *new_uw; + int j = 0; + + new_uw = MEM_callocN(count * sizeof(MaskSplinePointUW), "new mask uw points"); + + for (i = 0; i < point->tot_uw; i++) { + if ((point->uw[i].flag & SELECT) == 0) { + new_uw[j++] = point->uw[i]; + } + } + + MEM_freeN(point->uw); + + point->uw = new_uw; + point->tot_uw = count; + } } static int delete_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - int mask_layer_shape_ofs = 0; + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + int mask_layer_shape_ofs = 0; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - spline = masklay->splines.first; + spline = masklay->splines.first; - while (spline) { - const int tot_point_orig = spline->tot_point; - int i, count = 0; - MaskSpline *next_spline = spline->next; + while (spline) { + const int tot_point_orig = spline->tot_point; + int i, count = 0; + MaskSpline *next_spline = spline->next; - /* count unselected points */ - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; + /* count unselected points */ + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; - if (!MASKPOINT_ISSEL_ANY(point)) - count++; - } + if (!MASKPOINT_ISSEL_ANY(point)) + count++; + } - if (count == 0) { - /* delete the whole spline */ - BLI_remlink(&masklay->splines, spline); - BKE_mask_spline_free(spline); + if (count == 0) { + /* delete the whole spline */ + BLI_remlink(&masklay->splines, spline); + BKE_mask_spline_free(spline); - if (spline == masklay->act_spline) { - masklay->act_spline = NULL; - masklay->act_point = NULL; - } + if (spline == masklay->act_spline) { + masklay->act_spline = NULL; + masklay->act_point = NULL; + } - BKE_mask_layer_shape_changed_remove(masklay, mask_layer_shape_ofs, tot_point_orig); - } - else { - MaskSplinePoint *new_points; - int j; + BKE_mask_layer_shape_changed_remove(masklay, mask_layer_shape_ofs, tot_point_orig); + } + else { + MaskSplinePoint *new_points; + int j; - new_points = MEM_callocN(count * sizeof(MaskSplinePoint), "deleteMaskPoints"); + new_points = MEM_callocN(count * sizeof(MaskSplinePoint), "deleteMaskPoints"); - for (i = 0, j = 0; i < tot_point_orig; i++) { - MaskSplinePoint *point = &spline->points[i]; + for (i = 0, j = 0; i < tot_point_orig; i++) { + MaskSplinePoint *point = &spline->points[i]; - if (!MASKPOINT_ISSEL_ANY(point)) { - if (point == masklay->act_point) - masklay->act_point = &new_points[j]; + if (!MASKPOINT_ISSEL_ANY(point)) { + if (point == masklay->act_point) + masklay->act_point = &new_points[j]; - delete_feather_points(point); + delete_feather_points(point); - new_points[j] = *point; - j++; - } - else { - if (point == masklay->act_point) - masklay->act_point = NULL; + new_points[j] = *point; + j++; + } + else { + if (point == masklay->act_point) + masklay->act_point = NULL; - BKE_mask_point_free(point); - spline->tot_point--; + BKE_mask_point_free(point); + spline->tot_point--; - BKE_mask_layer_shape_changed_remove(masklay, mask_layer_shape_ofs + j, 1); - } - } + BKE_mask_layer_shape_changed_remove(masklay, mask_layer_shape_ofs + j, 1); + } + } - mask_layer_shape_ofs += spline->tot_point; + mask_layer_shape_ofs += spline->tot_point; - MEM_freeN(spline->points); - spline->points = new_points; + MEM_freeN(spline->points); + spline->points = new_points; - ED_mask_select_flush_all(mask); - } + ED_mask_select_flush_all(mask); + } - changed = true; - spline = next_spline; - } + changed = true; + spline = next_spline; + } - /* not essential but confuses users when there are keys with no data! - * assume if they delete all data from the layer they also dont care about keys */ - if (BLI_listbase_is_empty(&masklay->splines)) { - BKE_mask_layer_free_shapes(masklay); - } - } + /* not essential but confuses users when there are keys with no data! + * assume if they delete all data from the layer they also dont care about keys */ + if (BLI_listbase_is_empty(&masklay->splines)) { + BKE_mask_layer_free_shapes(masklay); + } + } - if (!changed) { - return OPERATOR_CANCELLED; - } + if (!changed) { + return OPERATOR_CANCELLED; + } - /* TODO: only update edited splines */ - BKE_mask_update_display(mask, CFRA); + /* TODO: only update edited splines */ + BKE_mask_update_display(mask, CFRA); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_delete(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Delete"; - ot->description = "Delete selected control points or splines"; - ot->idname = "MASK_OT_delete"; - - /* api callbacks */ - ot->invoke = WM_operator_confirm; - ot->exec = delete_exec; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* identifiers */ + ot->name = "Delete"; + ot->description = "Delete selected control points or splines"; + ot->idname = "MASK_OT_delete"; + + /* api callbacks */ + ot->invoke = WM_operator_confirm; + ot->exec = delete_exec; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /* *** switch direction *** */ static int mask_switch_direction_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - - bool changed = false; - - /* do actual selection */ - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - bool changed_layer = false; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - if (ED_mask_spline_select_check(spline)) { - BKE_mask_spline_direction_switch(masklay, spline); - changed = true; - changed_layer = true; - } - } - - if (changed_layer) { - if (IS_AUTOKEY_ON(scene)) { - ED_mask_layer_shape_auto_key(masklay, CFRA); - } - } - } - - if (changed) { - /* TODO: only update this spline */ - BKE_mask_update_display(mask, CFRA); - - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - - return OPERATOR_FINISHED; - } - - return OPERATOR_CANCELLED; + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + + bool changed = false; + + /* do actual selection */ + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + bool changed_layer = false; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + if (ED_mask_spline_select_check(spline)) { + BKE_mask_spline_direction_switch(masklay, spline); + changed = true; + changed_layer = true; + } + } + + if (changed_layer) { + if (IS_AUTOKEY_ON(scene)) { + ED_mask_layer_shape_auto_key(masklay, CFRA); + } + } + } + + if (changed) { + /* TODO: only update this spline */ + BKE_mask_update_display(mask, CFRA); + + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + + return OPERATOR_FINISHED; + } + + return OPERATOR_CANCELLED; } void MASK_OT_switch_direction(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Switch Direction"; - ot->description = "Switch direction of selected splines"; - ot->idname = "MASK_OT_switch_direction"; + /* identifiers */ + ot->name = "Switch Direction"; + ot->description = "Switch direction of selected splines"; + ot->idname = "MASK_OT_switch_direction"; - /* api callbacks */ - ot->exec = mask_switch_direction_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_switch_direction_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } - /* *** recalc normals *** */ static int mask_normals_make_consistent_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - int i; - - bool changed = false; - - /* do actual selection */ - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - bool changed_layer = false; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(point)) { - BKE_mask_calc_handle_point_auto(spline, point, false); - changed = true; - changed_layer = true; - } - } - } - - if (changed_layer) { - if (IS_AUTOKEY_ON(scene)) { - ED_mask_layer_shape_auto_key(masklay, CFRA); - } - } - } - - if (changed) { - /* TODO: only update this spline */ - BKE_mask_update_display(mask, CFRA); - - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - - return OPERATOR_FINISHED; - } - - return OPERATOR_CANCELLED; + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + int i; + + bool changed = false; + + /* do actual selection */ + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + bool changed_layer = false; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(point)) { + BKE_mask_calc_handle_point_auto(spline, point, false); + changed = true; + changed_layer = true; + } + } + } + + if (changed_layer) { + if (IS_AUTOKEY_ON(scene)) { + ED_mask_layer_shape_auto_key(masklay, CFRA); + } + } + } + + if (changed) { + /* TODO: only update this spline */ + BKE_mask_update_display(mask, CFRA); + + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + + return OPERATOR_FINISHED; + } + + return OPERATOR_CANCELLED; } /* named to match mesh recalc normals */ void MASK_OT_normals_make_consistent(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Recalc Normals"; - ot->description = "Re-calculate the direction of selected handles"; - ot->idname = "MASK_OT_normals_make_consistent"; + /* identifiers */ + ot->name = "Recalc Normals"; + ot->description = "Re-calculate the direction of selected handles"; + ot->idname = "MASK_OT_normals_make_consistent"; - /* api callbacks */ - ot->exec = mask_normals_make_consistent_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_normals_make_consistent_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } - /******************** set handle type *********************/ static int set_handle_type_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - int handle_type = RNA_enum_get(op->ptr, "type"); - - bool changed = false; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - int i; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(point)) { - BezTriple *bezt = &point->bezt; - - if (bezt->f2 & SELECT) { - bezt->h1 = handle_type; - bezt->h2 = handle_type; - } - else { - if (bezt->f1 & SELECT) { - bezt->h1 = handle_type; - } - if (bezt->f3 & SELECT) { - bezt->h2 = handle_type; - } - } - - if (handle_type == HD_ALIGN) { - float vec[3]; - sub_v3_v3v3(vec, bezt->vec[0], bezt->vec[1]); - add_v3_v3v3(bezt->vec[2], bezt->vec[1], vec); - } - - changed = true; - } - } - } - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - return OPERATOR_CANCELLED; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + int handle_type = RNA_enum_get(op->ptr, "type"); + + bool changed = false; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + int i; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(point)) { + BezTriple *bezt = &point->bezt; + + if (bezt->f2 & SELECT) { + bezt->h1 = handle_type; + bezt->h2 = handle_type; + } + else { + if (bezt->f1 & SELECT) { + bezt->h1 = handle_type; + } + if (bezt->f3 & SELECT) { + bezt->h2 = handle_type; + } + } + + if (handle_type == HD_ALIGN) { + float vec[3]; + sub_v3_v3v3(vec, bezt->vec[0], bezt->vec[1]); + add_v3_v3v3(bezt->vec[2], bezt->vec[1], vec); + } + + changed = true; + } + } + } + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + return OPERATOR_CANCELLED; } void MASK_OT_handle_type_set(wmOperatorType *ot) { - static const EnumPropertyItem editcurve_handle_type_items[] = { - {HD_AUTO, "AUTO", 0, "Auto", ""}, - {HD_VECT, "VECTOR", 0, "Vector", ""}, - {HD_ALIGN, "ALIGNED", 0, "Aligned Single", ""}, - {HD_ALIGN_DOUBLESIDE, "ALIGNED_DOUBLESIDE", 0, "Aligned", ""}, - {HD_FREE, "FREE", 0, "Free", ""}, - {0, NULL, 0, NULL, NULL}, - }; - - /* identifiers */ - ot->name = "Set Handle Type"; - ot->description = "Set type of handles for selected control points"; - ot->idname = "MASK_OT_handle_type_set"; - - /* api callbacks */ - ot->invoke = WM_menu_invoke; - ot->exec = set_handle_type_exec; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - ot->prop = RNA_def_enum(ot->srna, "type", editcurve_handle_type_items, 1, "Type", "Spline type"); + static const EnumPropertyItem editcurve_handle_type_items[] = { + {HD_AUTO, "AUTO", 0, "Auto", ""}, + {HD_VECT, "VECTOR", 0, "Vector", ""}, + {HD_ALIGN, "ALIGNED", 0, "Aligned Single", ""}, + {HD_ALIGN_DOUBLESIDE, "ALIGNED_DOUBLESIDE", 0, "Aligned", ""}, + {HD_FREE, "FREE", 0, "Free", ""}, + {0, NULL, 0, NULL, NULL}, + }; + + /* identifiers */ + ot->name = "Set Handle Type"; + ot->description = "Set type of handles for selected control points"; + ot->idname = "MASK_OT_handle_type_set"; + + /* api callbacks */ + ot->invoke = WM_menu_invoke; + ot->exec = set_handle_type_exec; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + ot->prop = RNA_def_enum(ot->srna, "type", editcurve_handle_type_items, 1, "Type", "Spline type"); } - /* ********* clear/set restrict view *********/ static int mask_hide_view_clear_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; - const bool select = RNA_boolean_get(op->ptr, "select"); - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - - if (masklay->restrictflag & OB_RESTRICT_VIEW) { - ED_mask_layer_select_set(masklay, select); - masklay->restrictflag &= ~OB_RESTRICT_VIEW; - changed = true; - } - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; + const bool select = RNA_boolean_get(op->ptr, "select"); + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + + if (masklay->restrictflag & OB_RESTRICT_VIEW) { + ED_mask_layer_select_set(masklay, select); + masklay->restrictflag &= ~OB_RESTRICT_VIEW; + changed = true; + } + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_hide_view_clear(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Clear Restrict View"; - ot->description = "Reveal the layer by setting the hide flag"; - ot->idname = "MASK_OT_hide_view_clear"; + /* identifiers */ + ot->name = "Clear Restrict View"; + ot->description = "Reveal the layer by setting the hide flag"; + ot->idname = "MASK_OT_hide_view_clear"; - /* api callbacks */ - ot->exec = mask_hide_view_clear_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_hide_view_clear_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - RNA_def_boolean(ot->srna, "select", true, "Select", ""); + RNA_def_boolean(ot->srna, "select", true, "Select", ""); } static int mask_hide_view_set_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - const bool unselected = RNA_boolean_get(op->ptr, "unselected"); - bool changed = false; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - - if (masklay->restrictflag & MASK_RESTRICT_SELECT) { - continue; - } - - if (!unselected) { - if (ED_mask_layer_select_check(masklay)) { - ED_mask_layer_select_set(masklay, false); - - masklay->restrictflag |= OB_RESTRICT_VIEW; - changed = true; - if (masklay == BKE_mask_layer_active(mask)) { - BKE_mask_layer_active_set(mask, NULL); - } - } - } - else { - if (!ED_mask_layer_select_check(masklay)) { - masklay->restrictflag |= OB_RESTRICT_VIEW; - changed = true; - if (masklay == BKE_mask_layer_active(mask)) { - BKE_mask_layer_active_set(mask, NULL); - } - } - } - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + const bool unselected = RNA_boolean_get(op->ptr, "unselected"); + bool changed = false; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + + if (masklay->restrictflag & MASK_RESTRICT_SELECT) { + continue; + } + + if (!unselected) { + if (ED_mask_layer_select_check(masklay)) { + ED_mask_layer_select_set(masklay, false); + + masklay->restrictflag |= OB_RESTRICT_VIEW; + changed = true; + if (masklay == BKE_mask_layer_active(mask)) { + BKE_mask_layer_active_set(mask, NULL); + } + } + } + else { + if (!ED_mask_layer_select_check(masklay)) { + masklay->restrictflag |= OB_RESTRICT_VIEW; + changed = true; + if (masklay == BKE_mask_layer_active(mask)) { + BKE_mask_layer_active_set(mask, NULL); + } + } + } + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_hide_view_set(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Set Restrict View"; - ot->description = "Hide the layer by setting the hide flag"; - ot->idname = "MASK_OT_hide_view_set"; + /* identifiers */ + ot->name = "Set Restrict View"; + ot->description = "Hide the layer by setting the hide flag"; + ot->idname = "MASK_OT_hide_view_set"; - /* api callbacks */ - ot->exec = mask_hide_view_set_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_hide_view_set_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected layers"); + RNA_def_boolean( + ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected layers"); } - static int mask_feather_weight_clear_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; - int i; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - - if (masklay->restrictflag & (MASK_RESTRICT_SELECT | MASK_RESTRICT_VIEW)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(point)) { - BezTriple *bezt = &point->bezt; - bezt->weight = 0.0f; - changed = true; - } - } - } - } - - if (changed) { - /* TODO: only update edited splines */ - BKE_mask_update_display(mask, CFRA); - - WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; + int i; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + + if (masklay->restrictflag & (MASK_RESTRICT_SELECT | MASK_RESTRICT_VIEW)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(point)) { + BezTriple *bezt = &point->bezt; + bezt->weight = 0.0f; + changed = true; + } + } + } + } + + if (changed) { + /* TODO: only update edited splines */ + BKE_mask_update_display(mask, CFRA); + + WM_event_add_notifier(C, NC_MASK | ND_DRAW, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_feather_weight_clear(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Clear Feather Weight"; - ot->description = "Reset the feather weight to zero"; - ot->idname = "MASK_OT_feather_weight_clear"; + /* identifiers */ + ot->name = "Clear Feather Weight"; + ot->description = "Reset the feather weight to zero"; + ot->idname = "MASK_OT_feather_weight_clear"; - /* api callbacks */ - ot->exec = mask_feather_weight_clear_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_feather_weight_clear_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /******************** move mask layer operator *********************/ static bool mask_layer_move_poll(bContext *C) { - if (ED_maskedit_mask_poll(C)) { - Mask *mask = CTX_data_edit_mask(C); + if (ED_maskedit_mask_poll(C)) { + Mask *mask = CTX_data_edit_mask(C); - return mask->masklay_tot > 0; - } + return mask->masklay_tot > 0; + } - return false; + return false; } static int mask_layer_move_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *mask_layer = BLI_findlink(&mask->masklayers, mask->masklay_act); - MaskLayer *mask_layer_other; - int direction = RNA_enum_get(op->ptr, "direction"); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *mask_layer = BLI_findlink(&mask->masklayers, mask->masklay_act); + MaskLayer *mask_layer_other; + int direction = RNA_enum_get(op->ptr, "direction"); - if (!mask_layer) - return OPERATOR_CANCELLED; + if (!mask_layer) + return OPERATOR_CANCELLED; - if (direction == -1) { - mask_layer_other = mask_layer->prev; + if (direction == -1) { + mask_layer_other = mask_layer->prev; - if (!mask_layer_other) - return OPERATOR_CANCELLED; + if (!mask_layer_other) + return OPERATOR_CANCELLED; - BLI_remlink(&mask->masklayers, mask_layer); - BLI_insertlinkbefore(&mask->masklayers, mask_layer_other, mask_layer); - mask->masklay_act--; - } - else if (direction == 1) { - mask_layer_other = mask_layer->next; + BLI_remlink(&mask->masklayers, mask_layer); + BLI_insertlinkbefore(&mask->masklayers, mask_layer_other, mask_layer); + mask->masklay_act--; + } + else if (direction == 1) { + mask_layer_other = mask_layer->next; - if (!mask_layer_other) - return OPERATOR_CANCELLED; + if (!mask_layer_other) + return OPERATOR_CANCELLED; - BLI_remlink(&mask->masklayers, mask_layer); - BLI_insertlinkafter(&mask->masklayers, mask_layer_other, mask_layer); - mask->masklay_act++; - } + BLI_remlink(&mask->masklayers, mask_layer); + BLI_insertlinkafter(&mask->masklayers, mask_layer_other, mask_layer); + mask->masklay_act++; + } - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_layer_move(wmOperatorType *ot) { - static const EnumPropertyItem direction_items[] = { - {-1, "UP", 0, "Up", ""}, - {1, "DOWN", 0, "Down", ""}, - {0, NULL, 0, NULL, NULL}, - }; - - /* identifiers */ - ot->name = "Move Layer"; - ot->description = "Move the active layer up/down in the list"; - ot->idname = "MASK_OT_layer_move"; - - /* api callbacks */ - ot->exec = mask_layer_move_exec; - ot->poll = mask_layer_move_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - RNA_def_enum(ot->srna, "direction", direction_items, 0, "Direction", "Direction to move the active layer"); + static const EnumPropertyItem direction_items[] = { + {-1, "UP", 0, "Up", ""}, + {1, "DOWN", 0, "Down", ""}, + {0, NULL, 0, NULL, NULL}, + }; + + /* identifiers */ + ot->name = "Move Layer"; + ot->description = "Move the active layer up/down in the list"; + ot->idname = "MASK_OT_layer_move"; + + /* api callbacks */ + ot->exec = mask_layer_move_exec; + ot->poll = mask_layer_move_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_enum(ot->srna, + "direction", + direction_items, + 0, + "Direction", + "Direction to move the active layer"); } /******************** duplicate *********************/ static int mask_duplicate_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *mask_layer; - - for (mask_layer = mask->masklayers.first; - mask_layer; - mask_layer = mask_layer->next) - { - MaskSpline *spline; - - for (spline = mask_layer->splines.last; - spline; - spline = spline->prev) - { - MaskSplinePoint *point = spline->points; - int i = 0; - while (i < spline->tot_point) { - int start = i, end = -1; - /* Find next selected segment. */ - while (MASKPOINT_ISSEL_ANY(point)) { - BKE_mask_point_select_set(point, false); - end = i; - if (i >= spline->tot_point - 1) { - break; - } - i++; - point++; - } - if (end >= start) { - int tot_point; - int tot_point_shape_start = 0; - MaskSpline *new_spline = BKE_mask_spline_add(mask_layer); - MaskSplinePoint *new_point; - int b; - - /* BKE_mask_spline_add might allocate the points, - * need to free them in this case. */ - if (new_spline->points) { - MEM_freeN(new_spline->points); - } - - /* Copy options from old spline. */ - new_spline->flag = spline->flag; - new_spline->offset_mode = spline->offset_mode; - new_spline->weight_interp = spline->weight_interp; - new_spline->parent = spline->parent; - - /* Allocate new points and copy them from old spline. */ - new_spline->tot_point = end - start + 1; - new_spline->points = MEM_mallocN(sizeof(MaskSplinePoint) * new_spline->tot_point, - "duplicated mask points"); - - memcpy(new_spline->points, spline->points + start, - new_spline->tot_point * sizeof(MaskSplinePoint)); - - tot_point = new_spline->tot_point; - - /* animation requires points added one by one */ - if (mask_layer->splines_shapes.first) { - new_spline->tot_point = 0; - tot_point_shape_start = BKE_mask_layer_shape_spline_to_index(mask_layer, new_spline); - } - - /* Select points and duplicate their UWs (if needed). */ - for (b = 0, new_point = new_spline->points; - b < tot_point; - b++, new_point++) - { - if (new_point->uw) { - new_point->uw = MEM_dupallocN(new_point->uw); - } - BKE_mask_point_select_set(new_point, true); - - - if (mask_layer->splines_shapes.first) { - new_spline->tot_point++; - BKE_mask_layer_shape_changed_add(mask_layer, tot_point_shape_start + b, true, false); - } - } - - /* Clear cyclic flag if we didn't copy the whole spline. */ - if (new_spline->flag & MASK_SPLINE_CYCLIC) { - if (start != 0 || end != spline->tot_point - 1) { - new_spline->flag &= ~MASK_SPLINE_CYCLIC; - } - } - - /* Flush selection to splines. */ - new_spline->flag |= SELECT; - spline->flag &= ~SELECT; - - mask_layer->act_spline = new_spline; - } - i++; - point++; - } - } - } - - /* TODO: only update edited splines */ - BKE_mask_update_display(mask, CFRA); - - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - - return OPERATOR_FINISHED; + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *mask_layer; + + for (mask_layer = mask->masklayers.first; mask_layer; mask_layer = mask_layer->next) { + MaskSpline *spline; + + for (spline = mask_layer->splines.last; spline; spline = spline->prev) { + MaskSplinePoint *point = spline->points; + int i = 0; + while (i < spline->tot_point) { + int start = i, end = -1; + /* Find next selected segment. */ + while (MASKPOINT_ISSEL_ANY(point)) { + BKE_mask_point_select_set(point, false); + end = i; + if (i >= spline->tot_point - 1) { + break; + } + i++; + point++; + } + if (end >= start) { + int tot_point; + int tot_point_shape_start = 0; + MaskSpline *new_spline = BKE_mask_spline_add(mask_layer); + MaskSplinePoint *new_point; + int b; + + /* BKE_mask_spline_add might allocate the points, + * need to free them in this case. */ + if (new_spline->points) { + MEM_freeN(new_spline->points); + } + + /* Copy options from old spline. */ + new_spline->flag = spline->flag; + new_spline->offset_mode = spline->offset_mode; + new_spline->weight_interp = spline->weight_interp; + new_spline->parent = spline->parent; + + /* Allocate new points and copy them from old spline. */ + new_spline->tot_point = end - start + 1; + new_spline->points = MEM_mallocN(sizeof(MaskSplinePoint) * new_spline->tot_point, + "duplicated mask points"); + + memcpy(new_spline->points, + spline->points + start, + new_spline->tot_point * sizeof(MaskSplinePoint)); + + tot_point = new_spline->tot_point; + + /* animation requires points added one by one */ + if (mask_layer->splines_shapes.first) { + new_spline->tot_point = 0; + tot_point_shape_start = BKE_mask_layer_shape_spline_to_index(mask_layer, new_spline); + } + + /* Select points and duplicate their UWs (if needed). */ + for (b = 0, new_point = new_spline->points; b < tot_point; b++, new_point++) { + if (new_point->uw) { + new_point->uw = MEM_dupallocN(new_point->uw); + } + BKE_mask_point_select_set(new_point, true); + + if (mask_layer->splines_shapes.first) { + new_spline->tot_point++; + BKE_mask_layer_shape_changed_add(mask_layer, tot_point_shape_start + b, true, false); + } + } + + /* Clear cyclic flag if we didn't copy the whole spline. */ + if (new_spline->flag & MASK_SPLINE_CYCLIC) { + if (start != 0 || end != spline->tot_point - 1) { + new_spline->flag &= ~MASK_SPLINE_CYCLIC; + } + } + + /* Flush selection to splines. */ + new_spline->flag |= SELECT; + spline->flag &= ~SELECT; + + mask_layer->act_spline = new_spline; + } + i++; + point++; + } + } + } + + /* TODO: only update edited splines */ + BKE_mask_update_display(mask, CFRA); + + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + + return OPERATOR_FINISHED; } void MASK_OT_duplicate(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Duplicate Mask"; - ot->description = "Duplicate selected control points and segments between them"; - ot->idname = "MASK_OT_duplicate"; + /* identifiers */ + ot->name = "Duplicate Mask"; + ot->description = "Duplicate selected control points and segments between them"; + ot->idname = "MASK_OT_duplicate"; - /* api callbacks */ - ot->exec = mask_duplicate_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_duplicate_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /********************** copy splines to clipboard operator *********************/ static int copy_splines_exec(bContext *C, wmOperator *UNUSED(op)) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *mask_layer = BKE_mask_layer_active(mask); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *mask_layer = BKE_mask_layer_active(mask); - if (mask_layer == NULL) { - return OPERATOR_CANCELLED; - } + if (mask_layer == NULL) { + return OPERATOR_CANCELLED; + } - BKE_mask_clipboard_copy_from_layer(mask_layer); + BKE_mask_clipboard_copy_from_layer(mask_layer); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_copy_splines(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Copy Splines"; - ot->description = "Copy selected splines to clipboard"; - ot->idname = "MASK_OT_copy_splines"; + /* identifiers */ + ot->name = "Copy Splines"; + ot->description = "Copy selected splines to clipboard"; + ot->idname = "MASK_OT_copy_splines"; - /* api callbacks */ - ot->exec = copy_splines_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = copy_splines_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER; + /* flags */ + ot->flag = OPTYPE_REGISTER; } /********************** paste tracks from clipboard operator *********************/ static bool paste_splines_poll(bContext *C) { - if (ED_maskedit_mask_poll(C)) { - return BKE_mask_clipboard_is_empty() == false; - } + if (ED_maskedit_mask_poll(C)) { + return BKE_mask_clipboard_is_empty() == false; + } - return 0; + return 0; } static int paste_splines_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *mask_layer = BKE_mask_layer_active(mask); + Scene *scene = CTX_data_scene(C); + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *mask_layer = BKE_mask_layer_active(mask); - if (mask_layer == NULL) { - mask_layer = BKE_mask_layer_new(mask, ""); - } + if (mask_layer == NULL) { + mask_layer = BKE_mask_layer_new(mask, ""); + } - BKE_mask_clipboard_paste_to_layer(CTX_data_main(C), mask_layer); + BKE_mask_clipboard_paste_to_layer(CTX_data_main(C), mask_layer); - /* TODO: only update edited splines */ - BKE_mask_update_display(mask, CFRA); + /* TODO: only update edited splines */ + BKE_mask_update_display(mask, CFRA); - WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); + WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_paste_splines(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Paste Splines"; - ot->description = "Paste splines from clipboard"; - ot->idname = "MASK_OT_paste_splines"; + /* identifiers */ + ot->name = "Paste Splines"; + ot->description = "Paste splines from clipboard"; + ot->idname = "MASK_OT_paste_splines"; - /* api callbacks */ - ot->exec = paste_splines_exec; - ot->poll = paste_splines_poll; + /* api callbacks */ + ot->exec = paste_splines_exec; + ot->poll = paste_splines_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } diff --git a/source/blender/editors/mask/mask_relationships.c b/source/blender/editors/mask/mask_relationships.c index d7dfcc64ab0..bb9359bad71 100644 --- a/source/blender/editors/mask/mask_relationships.c +++ b/source/blender/editors/mask/mask_relationships.c @@ -21,7 +21,6 @@ * \ingroup edmask */ - #include "BLI_math.h" #include "BLI_string.h" @@ -37,157 +36,159 @@ #include "WM_types.h" #include "ED_screen.h" -#include "ED_clip.h" /* frame remapping functions */ +#include "ED_clip.h" /* frame remapping functions */ -#include "mask_intern.h" /* own include */ +#include "mask_intern.h" /* own include */ static int mask_parent_clear_exec(bContext *C, wmOperator *UNUSED(op)) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - int i; + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + int i; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; - if (MASKPOINT_ISSEL_ANY(point)) { - point->parent.id = NULL; - } - } - } - } + if (MASKPOINT_ISSEL_ANY(point)) { + point->parent.id = NULL; + } + } + } + } - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, 0); + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, 0); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_parent_clear(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Clear Parent"; - ot->description = "Clear the mask's parenting"; - ot->idname = "MASK_OT_parent_clear"; + /* identifiers */ + ot->name = "Clear Parent"; + ot->description = "Clear the mask's parenting"; + ot->idname = "MASK_OT_parent_clear"; - /* api callbacks */ - ot->exec = mask_parent_clear_exec; + /* api callbacks */ + ot->exec = mask_parent_clear_exec; - ot->poll = ED_maskedit_mask_poll; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } static int mask_parent_set_exec(bContext *C, wmOperator *UNUSED(op)) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - - /* parent info */ - SpaceClip *sc = CTX_wm_space_clip(C); - MovieClip *clip = ED_space_clip_get_clip(sc); - MovieTracking *tracking; - MovieTrackingTrack *track; - MovieTrackingPlaneTrack *plane_track; - MovieTrackingObject *tracking_object; - /* done */ - - int framenr, parent_type; - float parmask_pos[2], orig_corners[4][2]; - const char *sub_parent_name; - - if (ELEM(NULL, sc, clip)) { - return OPERATOR_CANCELLED; - } - - framenr = ED_space_clip_get_clip_frame_number(sc); - - tracking = &clip->tracking; - tracking_object = BKE_tracking_object_get_active(&clip->tracking); - - if (tracking_object == NULL) { - return OPERATOR_CANCELLED; - } - - if ((track = BKE_tracking_track_get_active(tracking)) != NULL) { - MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr); - float marker_pos_ofs[2]; - - add_v2_v2v2(marker_pos_ofs, marker->pos, track->offset); - - BKE_mask_coord_from_movieclip(clip, &sc->user, parmask_pos, marker_pos_ofs); - - sub_parent_name = track->name; - parent_type = MASK_PARENT_POINT_TRACK; - memset(orig_corners, 0, sizeof(orig_corners)); - } - else if ((plane_track = BKE_tracking_plane_track_get_active(tracking)) != NULL) { - MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track, framenr); - - zero_v2(parmask_pos); - sub_parent_name = plane_track->name; - parent_type = MASK_PARENT_PLANE_TRACK; - memcpy(orig_corners, plane_marker->corners, sizeof(orig_corners)); - } - else { - return OPERATOR_CANCELLED; - } - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - int i; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(point)) { - point->parent.id_type = ID_MC; - point->parent.id = &clip->id; - point->parent.type = parent_type; - BLI_strncpy(point->parent.parent, tracking_object->name, sizeof(point->parent.parent)); - BLI_strncpy(point->parent.sub_parent, sub_parent_name, sizeof(point->parent.sub_parent)); - - copy_v2_v2(point->parent.parent_orig, parmask_pos); - memcpy(point->parent.parent_corners_orig, orig_corners, sizeof(point->parent.parent_corners_orig)); - } - } - } - } - - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + + /* parent info */ + SpaceClip *sc = CTX_wm_space_clip(C); + MovieClip *clip = ED_space_clip_get_clip(sc); + MovieTracking *tracking; + MovieTrackingTrack *track; + MovieTrackingPlaneTrack *plane_track; + MovieTrackingObject *tracking_object; + /* done */ + + int framenr, parent_type; + float parmask_pos[2], orig_corners[4][2]; + const char *sub_parent_name; + + if (ELEM(NULL, sc, clip)) { + return OPERATOR_CANCELLED; + } + + framenr = ED_space_clip_get_clip_frame_number(sc); + + tracking = &clip->tracking; + tracking_object = BKE_tracking_object_get_active(&clip->tracking); + + if (tracking_object == NULL) { + return OPERATOR_CANCELLED; + } + + if ((track = BKE_tracking_track_get_active(tracking)) != NULL) { + MovieTrackingMarker *marker = BKE_tracking_marker_get(track, framenr); + float marker_pos_ofs[2]; + + add_v2_v2v2(marker_pos_ofs, marker->pos, track->offset); + + BKE_mask_coord_from_movieclip(clip, &sc->user, parmask_pos, marker_pos_ofs); + + sub_parent_name = track->name; + parent_type = MASK_PARENT_POINT_TRACK; + memset(orig_corners, 0, sizeof(orig_corners)); + } + else if ((plane_track = BKE_tracking_plane_track_get_active(tracking)) != NULL) { + MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track, framenr); + + zero_v2(parmask_pos); + sub_parent_name = plane_track->name; + parent_type = MASK_PARENT_PLANE_TRACK; + memcpy(orig_corners, plane_marker->corners, sizeof(orig_corners)); + } + else { + return OPERATOR_CANCELLED; + } + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + int i; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(point)) { + point->parent.id_type = ID_MC; + point->parent.id = &clip->id; + point->parent.type = parent_type; + BLI_strncpy(point->parent.parent, tracking_object->name, sizeof(point->parent.parent)); + BLI_strncpy(point->parent.sub_parent, sub_parent_name, sizeof(point->parent.sub_parent)); + + copy_v2_v2(point->parent.parent_orig, parmask_pos); + memcpy(point->parent.parent_corners_orig, + orig_corners, + sizeof(point->parent.parent_corners_orig)); + } + } + } + } + + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; } /** based on #OBJECT_OT_parent_set */ void MASK_OT_parent_set(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Make Parent"; - ot->description = "Set the mask's parenting"; - ot->idname = "MASK_OT_parent_set"; + /* identifiers */ + ot->name = "Make Parent"; + ot->description = "Set the mask's parenting"; + ot->idname = "MASK_OT_parent_set"; - /* api callbacks */ - //ot->invoke = mask_parent_set_invoke; - ot->exec = mask_parent_set_exec; + /* api callbacks */ + //ot->invoke = mask_parent_set_invoke; + ot->exec = mask_parent_set_exec; - ot->poll = ED_space_clip_maskedit_mask_poll; + ot->poll = ED_space_clip_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } diff --git a/source/blender/editors/mask/mask_select.c b/source/blender/editors/mask/mask_select.c index 89a15ec316d..076f8d067b9 100644 --- a/source/blender/editors/mask/mask_select.c +++ b/source/blender/editors/mask/mask_select.c @@ -38,12 +38,12 @@ #include "ED_screen.h" #include "ED_select_utils.h" -#include "ED_mask.h" /* own include */ +#include "ED_mask.h" /* own include */ #include "RNA_access.h" #include "RNA_define.h" -#include "mask_intern.h" /* own include */ +#include "mask_intern.h" /* own include */ /* -------------------------------------------------------------------- */ /** \name Public Mask Selection API @@ -52,157 +52,156 @@ /* 'check' select */ bool ED_mask_spline_select_check(MaskSpline *spline) { - int i; + int i; - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; - if (MASKPOINT_ISSEL_ANY(point)) - return true; - } + if (MASKPOINT_ISSEL_ANY(point)) + return true; + } - return false; + return false; } bool ED_mask_layer_select_check(MaskLayer *masklay) { - MaskSpline *spline; + MaskSpline *spline; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - return false; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + return false; + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - if (ED_mask_spline_select_check(spline)) { - return true; - } - } + for (spline = masklay->splines.first; spline; spline = spline->next) { + if (ED_mask_spline_select_check(spline)) { + return true; + } + } - return false; + return false; } bool ED_mask_select_check(Mask *mask) { - MaskLayer *masklay; + MaskLayer *masklay; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - if (ED_mask_layer_select_check(masklay)) { - return true; - } - } + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + if (ED_mask_layer_select_check(masklay)) { + return true; + } + } - return false; + return false; } /* 'sel' select */ void ED_mask_spline_select_set(MaskSpline *spline, const bool do_select) { - int i; + int i; - if (do_select) - spline->flag |= SELECT; - else - spline->flag &= ~SELECT; + if (do_select) + spline->flag |= SELECT; + else + spline->flag &= ~SELECT; - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; - BKE_mask_point_select_set(point, do_select); - } + BKE_mask_point_select_set(point, do_select); + } } void ED_mask_layer_select_set(MaskLayer *masklay, const bool do_select) { - MaskSpline *spline; + MaskSpline *spline; - if (masklay->restrictflag & MASK_RESTRICT_SELECT) { - if (do_select == true) { - return; - } - } + if (masklay->restrictflag & MASK_RESTRICT_SELECT) { + if (do_select == true) { + return; + } + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - ED_mask_spline_select_set(spline, do_select); - } + for (spline = masklay->splines.first; spline; spline = spline->next) { + ED_mask_spline_select_set(spline, do_select); + } } void ED_mask_select_toggle_all(Mask *mask, int action) { - MaskLayer *masklay; - - if (action == SEL_TOGGLE) { - if (ED_mask_select_check(mask)) - action = SEL_DESELECT; - else - action = SEL_SELECT; - } - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - - if (masklay->restrictflag & MASK_RESTRICT_VIEW) { - continue; - } - - if (action == SEL_INVERT) { - /* we don't have generic functions for this, its restricted to this operator - * if one day we need to re-use such functionality, they can be split out */ - - MaskSpline *spline; - if (masklay->restrictflag & MASK_RESTRICT_SELECT) { - continue; - } - for (spline = masklay->splines.first; spline; spline = spline->next) { - int i; - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - BKE_mask_point_select_set(point, !MASKPOINT_ISSEL_ANY(point)); - } - } - - } - else { - ED_mask_layer_select_set(masklay, (action == SEL_SELECT) ? true : false); - } - } + MaskLayer *masklay; + + if (action == SEL_TOGGLE) { + if (ED_mask_select_check(mask)) + action = SEL_DESELECT; + else + action = SEL_SELECT; + } + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + + if (masklay->restrictflag & MASK_RESTRICT_VIEW) { + continue; + } + + if (action == SEL_INVERT) { + /* we don't have generic functions for this, its restricted to this operator + * if one day we need to re-use such functionality, they can be split out */ + + MaskSpline *spline; + if (masklay->restrictflag & MASK_RESTRICT_SELECT) { + continue; + } + for (spline = masklay->splines.first; spline; spline = spline->next) { + int i; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + BKE_mask_point_select_set(point, !MASKPOINT_ISSEL_ANY(point)); + } + } + } + else { + ED_mask_layer_select_set(masklay, (action == SEL_SELECT) ? true : false); + } + } } void ED_mask_select_flush_all(Mask *mask) { - MaskLayer *masklay; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - - for (spline = masklay->splines.first; spline; spline = spline->next) { - int i; - - spline->flag &= ~SELECT; - - /* intentionally _dont_ do this in the masklay loop - * so we clear flags on all splines */ - if (masklay->restrictflag & MASK_RESTRICT_VIEW) { - continue; - } - - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *cur_point = &spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(cur_point)) { - spline->flag |= SELECT; - } - else { - int j; - - for (j = 0; j < cur_point->tot_uw; j++) { - if (cur_point->uw[j].flag & SELECT) { - spline->flag |= SELECT; - break; - } - } - } - } - } - } + MaskLayer *masklay; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + + for (spline = masklay->splines.first; spline; spline = spline->next) { + int i; + + spline->flag &= ~SELECT; + + /* intentionally _dont_ do this in the masklay loop + * so we clear flags on all splines */ + if (masklay->restrictflag & MASK_RESTRICT_VIEW) { + continue; + } + + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *cur_point = &spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(cur_point)) { + spline->flag |= SELECT; + } + else { + int j; + + for (j = 0; j < cur_point->tot_uw; j++) { + if (cur_point->uw[j].flag & SELECT) { + spline->flag |= SELECT; + break; + } + } + } + } + } + } } /** \} */ @@ -213,33 +212,33 @@ void ED_mask_select_flush_all(Mask *mask) static int select_all_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - int action = RNA_enum_get(op->ptr, "action"); + Mask *mask = CTX_data_edit_mask(C); + int action = RNA_enum_get(op->ptr, "action"); - ED_mask_select_toggle_all(mask, action); - ED_mask_select_flush_all(mask); + ED_mask_select_toggle_all(mask, action); + ED_mask_select_flush_all(mask); - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_FINISHED; + return OPERATOR_FINISHED; } void MASK_OT_select_all(wmOperatorType *ot) { - /* identifiers */ - ot->name = "(De)select All"; - ot->description = "Change selection of all curve points"; - ot->idname = "MASK_OT_select_all"; + /* identifiers */ + ot->name = "(De)select All"; + ot->description = "Change selection of all curve points"; + ot->idname = "MASK_OT_select_all"; - /* api callbacks */ - ot->exec = select_all_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = select_all_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - /* properties */ - WM_operator_properties_select_all(ot); + /* properties */ + WM_operator_properties_select_all(ot); } /** \} */ @@ -250,152 +249,164 @@ void MASK_OT_select_all(wmOperatorType *ot) static int select_exec(bContext *C, wmOperator *op) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - MaskSpline *spline; - MaskSplinePoint *point = NULL; - float co[2]; - bool extend = RNA_boolean_get(op->ptr, "extend"); - bool deselect = RNA_boolean_get(op->ptr, "deselect"); - bool toggle = RNA_boolean_get(op->ptr, "toggle"); - eMaskWhichHandle which_handle; - const float threshold = 19; - - RNA_float_get_array(op->ptr, "location", co); - - point = ED_mask_point_find_nearest(C, mask, co, threshold, &masklay, &spline, &which_handle, NULL); - - if (extend == false && deselect == false && toggle == false) - ED_mask_select_toggle_all(mask, SEL_DESELECT); - - if (point) { - if (which_handle != MASK_WHICH_HANDLE_NONE) { - if (extend) { - masklay->act_spline = spline; - masklay->act_point = point; - - BKE_mask_point_select_set_handle(point, which_handle, true); - } - else if (deselect) { - BKE_mask_point_select_set_handle(point, which_handle, false); - } - else { - masklay->act_spline = spline; - masklay->act_point = point; - - if (!MASKPOINT_ISSEL_HANDLE(point, which_handle)) { - BKE_mask_point_select_set_handle(point, which_handle, true); - } - else if (toggle) { - BKE_mask_point_select_set_handle(point, which_handle, false); - } - } - } - else { - if (extend) { - masklay->act_spline = spline; - masklay->act_point = point; - - BKE_mask_point_select_set(point, true); - } - else if (deselect) { - BKE_mask_point_select_set(point, false); - } - else { - masklay->act_spline = spline; - masklay->act_point = point; - - if (!MASKPOINT_ISSEL_ANY(point)) { - BKE_mask_point_select_set(point, true); - } - else if (toggle) { - BKE_mask_point_select_set(point, false); - } - } - } - - masklay->act_spline = spline; - masklay->act_point = point; - - ED_mask_select_flush_all(mask); - - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - - return OPERATOR_FINISHED; - } - else { - MaskSplinePointUW *uw; - - if (ED_mask_feather_find_nearest(C, mask, co, threshold, &masklay, &spline, &point, &uw, NULL)) { - - if (extend) { - masklay->act_spline = spline; - masklay->act_point = point; - - if (uw) uw->flag |= SELECT; - } - else if (deselect) { - if (uw) uw->flag &= ~SELECT; - } - else { - masklay->act_spline = spline; - masklay->act_point = point; - - if (uw) { - if (!(uw->flag & SELECT)) { - uw->flag |= SELECT; - } - else if (toggle) { - uw->flag &= ~SELECT; - } - } - } - - ED_mask_select_flush_all(mask); - - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - - return OPERATOR_FINISHED; - } - } - - return OPERATOR_PASS_THROUGH; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + MaskSpline *spline; + MaskSplinePoint *point = NULL; + float co[2]; + bool extend = RNA_boolean_get(op->ptr, "extend"); + bool deselect = RNA_boolean_get(op->ptr, "deselect"); + bool toggle = RNA_boolean_get(op->ptr, "toggle"); + eMaskWhichHandle which_handle; + const float threshold = 19; + + RNA_float_get_array(op->ptr, "location", co); + + point = ED_mask_point_find_nearest( + C, mask, co, threshold, &masklay, &spline, &which_handle, NULL); + + if (extend == false && deselect == false && toggle == false) + ED_mask_select_toggle_all(mask, SEL_DESELECT); + + if (point) { + if (which_handle != MASK_WHICH_HANDLE_NONE) { + if (extend) { + masklay->act_spline = spline; + masklay->act_point = point; + + BKE_mask_point_select_set_handle(point, which_handle, true); + } + else if (deselect) { + BKE_mask_point_select_set_handle(point, which_handle, false); + } + else { + masklay->act_spline = spline; + masklay->act_point = point; + + if (!MASKPOINT_ISSEL_HANDLE(point, which_handle)) { + BKE_mask_point_select_set_handle(point, which_handle, true); + } + else if (toggle) { + BKE_mask_point_select_set_handle(point, which_handle, false); + } + } + } + else { + if (extend) { + masklay->act_spline = spline; + masklay->act_point = point; + + BKE_mask_point_select_set(point, true); + } + else if (deselect) { + BKE_mask_point_select_set(point, false); + } + else { + masklay->act_spline = spline; + masklay->act_point = point; + + if (!MASKPOINT_ISSEL_ANY(point)) { + BKE_mask_point_select_set(point, true); + } + else if (toggle) { + BKE_mask_point_select_set(point, false); + } + } + } + + masklay->act_spline = spline; + masklay->act_point = point; + + ED_mask_select_flush_all(mask); + + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + + return OPERATOR_FINISHED; + } + else { + MaskSplinePointUW *uw; + + if (ED_mask_feather_find_nearest( + C, mask, co, threshold, &masklay, &spline, &point, &uw, NULL)) { + + if (extend) { + masklay->act_spline = spline; + masklay->act_point = point; + + if (uw) + uw->flag |= SELECT; + } + else if (deselect) { + if (uw) + uw->flag &= ~SELECT; + } + else { + masklay->act_spline = spline; + masklay->act_point = point; + + if (uw) { + if (!(uw->flag & SELECT)) { + uw->flag |= SELECT; + } + else if (toggle) { + uw->flag &= ~SELECT; + } + } + } + + ED_mask_select_flush_all(mask); + + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + + return OPERATOR_FINISHED; + } + } + + return OPERATOR_PASS_THROUGH; } static int select_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - float co[2]; + float co[2]; - ED_mask_mouse_pos(sa, ar, event->mval, co); + ED_mask_mouse_pos(sa, ar, event->mval, co); - RNA_float_set_array(op->ptr, "location", co); + RNA_float_set_array(op->ptr, "location", co); - return select_exec(C, op); + return select_exec(C, op); } void MASK_OT_select(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select"; - ot->description = "Select spline points"; - ot->idname = "MASK_OT_select"; - - /* api callbacks */ - ot->exec = select_exec; - ot->invoke = select_invoke; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_UNDO; - - /* properties */ - WM_operator_properties_mouse_select(ot); - - RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX, - "Location", "Location of vertex in normalized space", -1.0f, 1.0f); + /* identifiers */ + ot->name = "Select"; + ot->description = "Select spline points"; + ot->idname = "MASK_OT_select"; + + /* api callbacks */ + ot->exec = select_exec; + ot->invoke = select_invoke; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_UNDO; + + /* properties */ + WM_operator_properties_mouse_select(ot); + + RNA_def_float_vector(ot->srna, + "location", + 2, + NULL, + -FLT_MAX, + FLT_MAX, + "Location", + "Location of vertex in normalized space", + -1.0f, + 1.0f); } /** \} */ @@ -406,86 +417,86 @@ void MASK_OT_select(wmOperatorType *ot) static int box_select_exec(bContext *C, wmOperator *op) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - int i; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + int i; - rcti rect; - rctf rectf; - bool changed = false; + rcti rect; + rctf rectf; + bool changed = false; - const eSelectOp sel_op = RNA_enum_get(op->ptr, "mode"); - const bool select = (sel_op != SEL_OP_SUB); - if (SEL_OP_USE_PRE_DESELECT(sel_op)) { - ED_mask_select_toggle_all(mask, SEL_DESELECT); - changed = true; - } + const eSelectOp sel_op = RNA_enum_get(op->ptr, "mode"); + const bool select = (sel_op != SEL_OP_SUB); + if (SEL_OP_USE_PRE_DESELECT(sel_op)) { + ED_mask_select_toggle_all(mask, SEL_DESELECT); + changed = true; + } - /* get rectangle from operator */ - WM_operator_properties_border_to_rcti(op, &rect); + /* get rectangle from operator */ + WM_operator_properties_border_to_rcti(op, &rect); - ED_mask_point_pos(sa, ar, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin); - ED_mask_point_pos(sa, ar, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax); + ED_mask_point_pos(sa, ar, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin); + ED_mask_point_pos(sa, ar, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax); - /* do actual selection */ - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; + /* do actual selection */ + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + for (spline = masklay->splines.first; spline; spline = spline->next) { + MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - MaskSplinePoint *point_deform = &points_array[i]; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + MaskSplinePoint *point_deform = &points_array[i]; - /* TODO: handles? */ - /* TODO: uw? */ - if (BLI_rctf_isect_pt_v(&rectf, point_deform->bezt.vec[1])) { - BKE_mask_point_select_set(point, select); - BKE_mask_point_select_set_handle(point, MASK_WHICH_HANDLE_BOTH, select); - changed = true; - } - } - } - } + /* TODO: handles? */ + /* TODO: uw? */ + if (BLI_rctf_isect_pt_v(&rectf, point_deform->bezt.vec[1])) { + BKE_mask_point_select_set(point, select); + BKE_mask_point_select_set_handle(point, MASK_WHICH_HANDLE_BOTH, select); + changed = true; + } + } + } + } - if (changed) { - ED_mask_select_flush_all(mask); + if (changed) { + ED_mask_select_flush_all(mask); - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_FINISHED; - } + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } void MASK_OT_select_box(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Box Select"; - ot->description = "Select curve points using box selection"; - ot->idname = "MASK_OT_select_box"; - - /* api callbacks */ - ot->invoke = WM_gesture_box_invoke; - ot->exec = box_select_exec; - ot->modal = WM_gesture_box_modal; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_UNDO; - - /* properties */ - WM_operator_properties_gesture_box(ot); - WM_operator_properties_select_operation_simple(ot); + /* identifiers */ + ot->name = "Box Select"; + ot->description = "Select curve points using box selection"; + ot->idname = "MASK_OT_select_box"; + + /* api callbacks */ + ot->invoke = WM_gesture_box_invoke; + ot->exec = box_select_exec; + ot->modal = WM_gesture_box_modal; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_UNDO; + + /* properties */ + WM_operator_properties_gesture_box(ot); + WM_operator_properties_select_operation_simple(ot); } /** \} */ @@ -494,112 +505,117 @@ void MASK_OT_select_box(wmOperatorType *ot) /** \name Lasso Select Operator * \{ */ -static bool do_lasso_select_mask(bContext *C, const int mcords[][2], short moves, const eSelectOp sel_op) +static bool do_lasso_select_mask(bContext *C, + const int mcords[][2], + short moves, + const eSelectOp sel_op) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); - - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - int i; - - rcti rect; - bool changed = false; - - const bool select = (sel_op != SEL_OP_SUB); - if (SEL_OP_USE_PRE_DESELECT(sel_op)) { - ED_mask_select_toggle_all(mask, SEL_DESELECT); - changed = true; - } - - /* get rectangle from operator */ - BLI_lasso_boundbox(&rect, mcords, moves); - - /* do actual selection */ - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - MaskSplinePoint *point_deform = &points_array[i]; - - /* TODO: handles? */ - /* TODO: uw? */ - - if (MASKPOINT_ISSEL_ANY(point) && select) { - continue; - } - - float screen_co[2]; - - /* point in screen coords */ - ED_mask_point_pos__reverse(sa, ar, - point_deform->bezt.vec[1][0], point_deform->bezt.vec[1][1], - &screen_co[0], &screen_co[1]); - - if (BLI_rcti_isect_pt(&rect, screen_co[0], screen_co[1]) && - BLI_lasso_is_point_inside(mcords, moves, screen_co[0], screen_co[1], INT_MAX)) - { - BKE_mask_point_select_set(point, select); - BKE_mask_point_select_set_handle(point, MASK_WHICH_HANDLE_BOTH, select); - changed = true; - } - } - } - } - - if (changed) { - ED_mask_select_flush_all(mask); - - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - } - - return changed; + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); + + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + int i; + + rcti rect; + bool changed = false; + + const bool select = (sel_op != SEL_OP_SUB); + if (SEL_OP_USE_PRE_DESELECT(sel_op)) { + ED_mask_select_toggle_all(mask, SEL_DESELECT); + changed = true; + } + + /* get rectangle from operator */ + BLI_lasso_boundbox(&rect, mcords, moves); + + /* do actual selection */ + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + MaskSplinePoint *point_deform = &points_array[i]; + + /* TODO: handles? */ + /* TODO: uw? */ + + if (MASKPOINT_ISSEL_ANY(point) && select) { + continue; + } + + float screen_co[2]; + + /* point in screen coords */ + ED_mask_point_pos__reverse(sa, + ar, + point_deform->bezt.vec[1][0], + point_deform->bezt.vec[1][1], + &screen_co[0], + &screen_co[1]); + + if (BLI_rcti_isect_pt(&rect, screen_co[0], screen_co[1]) && + BLI_lasso_is_point_inside(mcords, moves, screen_co[0], screen_co[1], INT_MAX)) { + BKE_mask_point_select_set(point, select); + BKE_mask_point_select_set_handle(point, MASK_WHICH_HANDLE_BOTH, select); + changed = true; + } + } + } + } + + if (changed) { + ED_mask_select_flush_all(mask); + + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + } + + return changed; } static int clip_lasso_select_exec(bContext *C, wmOperator *op) { - int mcords_tot; - const int (*mcords)[2] = WM_gesture_lasso_path_to_array(C, op, &mcords_tot); + int mcords_tot; + const int(*mcords)[2] = WM_gesture_lasso_path_to_array(C, op, &mcords_tot); - if (mcords) { - const eSelectOp sel_op = RNA_enum_get(op->ptr, "mode"); - do_lasso_select_mask(C, mcords, mcords_tot, sel_op); + if (mcords) { + const eSelectOp sel_op = RNA_enum_get(op->ptr, "mode"); + do_lasso_select_mask(C, mcords, mcords_tot, sel_op); - MEM_freeN((void *)mcords); + MEM_freeN((void *)mcords); - return OPERATOR_FINISHED; - } - return OPERATOR_PASS_THROUGH; + return OPERATOR_FINISHED; + } + return OPERATOR_PASS_THROUGH; } void MASK_OT_select_lasso(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Lasso Select"; - ot->description = "Select curve points using lasso selection"; - ot->idname = "MASK_OT_select_lasso"; - - /* api callbacks */ - ot->invoke = WM_gesture_lasso_invoke; - ot->modal = WM_gesture_lasso_modal; - ot->exec = clip_lasso_select_exec; - ot->poll = ED_maskedit_mask_poll; - ot->cancel = WM_gesture_lasso_cancel; - - /* flags */ - ot->flag = OPTYPE_UNDO; - - /* properties */ - WM_operator_properties_gesture_lasso(ot); - WM_operator_properties_select_operation_simple(ot); + /* identifiers */ + ot->name = "Lasso Select"; + ot->description = "Select curve points using lasso selection"; + ot->idname = "MASK_OT_select_lasso"; + + /* api callbacks */ + ot->invoke = WM_gesture_lasso_invoke; + ot->modal = WM_gesture_lasso_modal; + ot->exec = clip_lasso_select_exec; + ot->poll = ED_maskedit_mask_poll; + ot->cancel = WM_gesture_lasso_cancel; + + /* flags */ + ot->flag = OPTYPE_UNDO; + + /* properties */ + WM_operator_properties_gesture_lasso(ot); + WM_operator_properties_select_operation_simple(ot); } /** \} */ @@ -608,108 +624,110 @@ void MASK_OT_select_lasso(wmOperatorType *ot) /** \name Circle Select Operator * \{ */ -static int mask_spline_point_inside_ellipse(BezTriple *bezt, const float offset[2], const float ellipse[2]) +static int mask_spline_point_inside_ellipse(BezTriple *bezt, + const float offset[2], + const float ellipse[2]) { - /* normalized ellipse: ell[0] = scaleX, ell[1] = scaleY */ - float x, y; + /* normalized ellipse: ell[0] = scaleX, ell[1] = scaleY */ + float x, y; - x = (bezt->vec[1][0] - offset[0]) * ellipse[0]; - y = (bezt->vec[1][1] - offset[1]) * ellipse[1]; + x = (bezt->vec[1][0] - offset[0]) * ellipse[0]; + y = (bezt->vec[1][1] - offset[1]) * ellipse[1]; - return x * x + y * y < 1.0f; + return x * x + y * y < 1.0f; } static int circle_select_exec(bContext *C, wmOperator *op) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - int i; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + int i; - float zoomx, zoomy, offset[2], ellipse[2]; - int width, height; - bool changed = false; + float zoomx, zoomy, offset[2], ellipse[2]; + int width, height; + bool changed = false; - /* get operator properties */ - const int x = RNA_int_get(op->ptr, "x"); - const int y = RNA_int_get(op->ptr, "y"); - const int radius = RNA_int_get(op->ptr, "radius"); + /* get operator properties */ + const int x = RNA_int_get(op->ptr, "x"); + const int y = RNA_int_get(op->ptr, "y"); + const int radius = RNA_int_get(op->ptr, "radius"); - /* compute ellipse and position in unified coordinates */ - ED_mask_get_size(sa, &width, &height); - ED_mask_zoom(sa, ar, &zoomx, &zoomy); - width = height = max_ii(width, height); + /* compute ellipse and position in unified coordinates */ + ED_mask_get_size(sa, &width, &height); + ED_mask_zoom(sa, ar, &zoomx, &zoomy); + width = height = max_ii(width, height); - ellipse[0] = width * zoomx / radius; - ellipse[1] = height * zoomy / radius; + ellipse[0] = width * zoomx / radius; + ellipse[1] = height * zoomy / radius; - ED_mask_point_pos(sa, ar, x, y, &offset[0], &offset[1]); + ED_mask_point_pos(sa, ar, x, y, &offset[0], &offset[1]); - const eSelectOp sel_op = ED_select_op_modal( - RNA_enum_get(op->ptr, "mode"), WM_gesture_is_modal_first(op->customdata)); - const bool select = (sel_op != SEL_OP_SUB); - if (SEL_OP_USE_PRE_DESELECT(sel_op)) { - ED_mask_select_toggle_all(mask, SEL_DESELECT); - changed = true; - } + const eSelectOp sel_op = ED_select_op_modal(RNA_enum_get(op->ptr, "mode"), + WM_gesture_is_modal_first(op->customdata)); + const bool select = (sel_op != SEL_OP_SUB); + if (SEL_OP_USE_PRE_DESELECT(sel_op)) { + ED_mask_select_toggle_all(mask, SEL_DESELECT); + changed = true; + } - /* do actual selection */ - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; + /* do actual selection */ + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); + for (spline = masklay->splines.first; spline; spline = spline->next) { + MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline); - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - MaskSplinePoint *point_deform = &points_array[i]; + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + MaskSplinePoint *point_deform = &points_array[i]; - if (mask_spline_point_inside_ellipse(&point_deform->bezt, offset, ellipse)) { - BKE_mask_point_select_set(point, select); - BKE_mask_point_select_set_handle(point, MASK_WHICH_HANDLE_BOTH, select); + if (mask_spline_point_inside_ellipse(&point_deform->bezt, offset, ellipse)) { + BKE_mask_point_select_set(point, select); + BKE_mask_point_select_set_handle(point, MASK_WHICH_HANDLE_BOTH, select); - changed = true; - } - } - } - } + changed = true; + } + } + } + } - if (changed) { - ED_mask_select_flush_all(mask); + if (changed) { + ED_mask_select_flush_all(mask); - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_FINISHED; - } + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } void MASK_OT_select_circle(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Circle Select"; - ot->description = "Select curve points using circle selection"; - ot->idname = "MASK_OT_select_circle"; - - /* api callbacks */ - ot->invoke = WM_gesture_circle_invoke; - ot->modal = WM_gesture_circle_modal; - ot->exec = circle_select_exec; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - WM_operator_properties_gesture_circle(ot); - WM_operator_properties_select_operation_simple(ot); + /* identifiers */ + ot->name = "Circle Select"; + ot->description = "Select curve points using circle selection"; + ot->idname = "MASK_OT_select_circle"; + + /* api callbacks */ + ot->invoke = WM_gesture_circle_invoke; + ot->modal = WM_gesture_circle_modal; + ot->exec = circle_select_exec; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + WM_operator_properties_gesture_circle(ot); + WM_operator_properties_select_operation_simple(ot); } /** \} */ @@ -720,56 +738,56 @@ void MASK_OT_select_circle(wmOperatorType *ot) static int mask_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); + ScrArea *sa = CTX_wm_area(C); + ARegion *ar = CTX_wm_region(C); - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - MaskSpline *spline; - MaskSplinePoint *point = NULL; - float co[2]; - bool do_select = !RNA_boolean_get(op->ptr, "deselect"); - const float threshold = 19; - bool changed = false; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + MaskSpline *spline; + MaskSplinePoint *point = NULL; + float co[2]; + bool do_select = !RNA_boolean_get(op->ptr, "deselect"); + const float threshold = 19; + bool changed = false; - ED_mask_mouse_pos(sa, ar, event->mval, co); + ED_mask_mouse_pos(sa, ar, event->mval, co); - point = ED_mask_point_find_nearest(C, mask, co, threshold, &masklay, &spline, NULL, NULL); + point = ED_mask_point_find_nearest(C, mask, co, threshold, &masklay, &spline, NULL, NULL); - if (point) { - ED_mask_spline_select_set(spline, do_select); - masklay->act_spline = spline; - masklay->act_point = point; + if (point) { + ED_mask_spline_select_set(spline, do_select); + masklay->act_spline = spline; + masklay->act_point = point; - changed = true; - } + changed = true; + } - if (changed) { - ED_mask_select_flush_all(mask); + if (changed) { + ED_mask_select_flush_all(mask); - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_FINISHED; - } + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } void MASK_OT_select_linked_pick(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select Linked"; - ot->idname = "MASK_OT_select_linked_pick"; - ot->description = "(De)select all points linked to the curve under the mouse cursor"; + /* identifiers */ + ot->name = "Select Linked"; + ot->idname = "MASK_OT_select_linked_pick"; + ot->description = "(De)select all points linked to the curve under the mouse cursor"; - /* api callbacks */ - ot->invoke = mask_select_linked_pick_invoke; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->invoke = mask_select_linked_pick_invoke; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", ""); + RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", ""); } /** \} */ @@ -780,51 +798,51 @@ void MASK_OT_select_linked_pick(wmOperatorType *ot) static int mask_select_linked_exec(bContext *C, wmOperator *UNUSED(op)) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; - bool changed = false; + bool changed = false; - /* do actual selection */ - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; + /* do actual selection */ + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } - for (spline = masklay->splines.first; spline; spline = spline->next) { - if (ED_mask_spline_select_check(spline)) { - ED_mask_spline_select_set(spline, true); - changed = true; - } - } - } + for (spline = masklay->splines.first; spline; spline = spline->next) { + if (ED_mask_spline_select_check(spline)) { + ED_mask_spline_select_set(spline, true); + changed = true; + } + } + } - if (changed) { - ED_mask_select_flush_all(mask); + if (changed) { + ED_mask_select_flush_all(mask); - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - return OPERATOR_FINISHED; - } + return OPERATOR_FINISHED; + } - return OPERATOR_CANCELLED; + return OPERATOR_CANCELLED; } void MASK_OT_select_linked(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select Linked All"; - ot->idname = "MASK_OT_select_linked"; - ot->description = "Select all curve points linked to already selected ones"; + /* identifiers */ + ot->name = "Select Linked All"; + ot->idname = "MASK_OT_select_linked"; + ot->description = "Select all curve points linked to already selected ones"; - /* api callbacks */ - ot->exec = mask_select_linked_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_select_linked_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /** \} */ @@ -835,118 +853,118 @@ void MASK_OT_select_linked(wmOperatorType *ot) static int mask_select_more_less(bContext *C, bool more) { - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskSpline *spline; - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - for (spline = masklay->splines.first; spline; spline = spline->next) { - const bool cyclic = (spline->flag & MASK_SPLINE_CYCLIC) != 0; - bool start_sel, end_sel, prev_sel, cur_sel; - int i; - - /* reselect point if any handle is selected to make the result more predictable */ - for (i = 0; i < spline->tot_point; i++) { - BKE_mask_point_select_set(spline->points + i, MASKPOINT_ISSEL_ANY(spline->points + i)); - } - - /* select more/less does not affect empty/single point splines */ - if (spline->tot_point < 2) { - continue; - } - - if (cyclic) { - start_sel = !!MASKPOINT_ISSEL_KNOT(spline->points); - end_sel = !!MASKPOINT_ISSEL_KNOT(&spline->points[spline->tot_point - 1]); - } - else { - start_sel = false; - end_sel = false; - } - - for (i = 0; i < spline->tot_point; i++) { - if (i == 0 && !cyclic) { - continue; - } - - prev_sel = (i > 0) ? !!MASKPOINT_ISSEL_KNOT(&spline->points[i - 1]) : end_sel; - cur_sel = !!MASKPOINT_ISSEL_KNOT(&spline->points[i]); - - if (cur_sel != more) { - if (prev_sel == more) { - BKE_mask_point_select_set(&spline->points[i], more); - } - i++; - } - } - - for (i = spline->tot_point - 1; i >= 0; i--) { - if (i == spline->tot_point - 1 && !cyclic) { - continue; - } - - prev_sel = (i < spline->tot_point - 1) ? !!MASKPOINT_ISSEL_KNOT(&spline->points[i + 1]) : start_sel; - cur_sel = !!MASKPOINT_ISSEL_KNOT(&spline->points[i]); - - if (cur_sel != more) { - if (prev_sel == more) { - BKE_mask_point_select_set(&spline->points[i], more); - } - i--; - } - } - } - } - - WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); - - return OPERATOR_FINISHED; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskSpline *spline; + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + for (spline = masklay->splines.first; spline; spline = spline->next) { + const bool cyclic = (spline->flag & MASK_SPLINE_CYCLIC) != 0; + bool start_sel, end_sel, prev_sel, cur_sel; + int i; + + /* reselect point if any handle is selected to make the result more predictable */ + for (i = 0; i < spline->tot_point; i++) { + BKE_mask_point_select_set(spline->points + i, MASKPOINT_ISSEL_ANY(spline->points + i)); + } + + /* select more/less does not affect empty/single point splines */ + if (spline->tot_point < 2) { + continue; + } + + if (cyclic) { + start_sel = !!MASKPOINT_ISSEL_KNOT(spline->points); + end_sel = !!MASKPOINT_ISSEL_KNOT(&spline->points[spline->tot_point - 1]); + } + else { + start_sel = false; + end_sel = false; + } + + for (i = 0; i < spline->tot_point; i++) { + if (i == 0 && !cyclic) { + continue; + } + + prev_sel = (i > 0) ? !!MASKPOINT_ISSEL_KNOT(&spline->points[i - 1]) : end_sel; + cur_sel = !!MASKPOINT_ISSEL_KNOT(&spline->points[i]); + + if (cur_sel != more) { + if (prev_sel == more) { + BKE_mask_point_select_set(&spline->points[i], more); + } + i++; + } + } + + for (i = spline->tot_point - 1; i >= 0; i--) { + if (i == spline->tot_point - 1 && !cyclic) { + continue; + } + + prev_sel = (i < spline->tot_point - 1) ? !!MASKPOINT_ISSEL_KNOT(&spline->points[i + 1]) : + start_sel; + cur_sel = !!MASKPOINT_ISSEL_KNOT(&spline->points[i]); + + if (cur_sel != more) { + if (prev_sel == more) { + BKE_mask_point_select_set(&spline->points[i], more); + } + i--; + } + } + } + } + + WM_event_add_notifier(C, NC_MASK | ND_SELECT, mask); + + return OPERATOR_FINISHED; } static int mask_select_more_exec(bContext *C, wmOperator *UNUSED(op)) { - return mask_select_more_less(C, true); + return mask_select_more_less(C, true); } void MASK_OT_select_more(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select More"; - ot->idname = "MASK_OT_select_more"; - ot->description = "Select more spline points connected to initial selection"; + /* identifiers */ + ot->name = "Select More"; + ot->idname = "MASK_OT_select_more"; + ot->description = "Select more spline points connected to initial selection"; - /* api callbacks */ - ot->exec = mask_select_more_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_select_more_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } static int mask_select_less_exec(bContext *C, wmOperator *UNUSED(op)) { - return mask_select_more_less(C, false); + return mask_select_more_less(C, false); } void MASK_OT_select_less(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Select Less"; - ot->idname = "MASK_OT_select_less"; - ot->description = "Deselect spline points at the boundary of each selection region"; + /* identifiers */ + ot->name = "Select Less"; + ot->idname = "MASK_OT_select_less"; + ot->description = "Deselect spline points at the boundary of each selection region"; - /* api callbacks */ - ot->exec = mask_select_less_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_select_less_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } - /** \} */ diff --git a/source/blender/editors/mask/mask_shapekey.c b/source/blender/editors/mask/mask_shapekey.c index 7d33d13cbd3..edc22943124 100644 --- a/source/blender/editors/mask/mask_shapekey.c +++ b/source/blender/editors/mask/mask_shapekey.c @@ -42,193 +42,190 @@ #include "WM_api.h" #include "WM_types.h" -#include "ED_mask.h" /* own include */ +#include "ED_mask.h" /* own include */ -#include "mask_intern.h" /* own include */ +#include "mask_intern.h" /* own include */ static int mask_shape_key_insert_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - const int frame = CFRA; - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskLayerShape *masklay_shape; - - if (!ED_mask_layer_select_check(masklay)) { - continue; - } - - masklay_shape = BKE_mask_layer_shape_verify_frame(masklay, frame); - BKE_mask_layer_shape_from_mask(masklay, masklay_shape); - changed = true; - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Scene *scene = CTX_data_scene(C); + const int frame = CFRA; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskLayerShape *masklay_shape; + + if (!ED_mask_layer_select_check(masklay)) { + continue; + } + + masklay_shape = BKE_mask_layer_shape_verify_frame(masklay, frame); + BKE_mask_layer_shape_from_mask(masklay, masklay_shape); + changed = true; + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_shape_key_insert(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Insert Shape Key"; - ot->idname = "MASK_OT_shape_key_insert"; + /* identifiers */ + ot->name = "Insert Shape Key"; + ot->idname = "MASK_OT_shape_key_insert"; - /* api callbacks */ - ot->exec = mask_shape_key_insert_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_shape_key_insert_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } static int mask_shape_key_clear_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - const int frame = CFRA; - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - MaskLayerShape *masklay_shape; - - if (!ED_mask_layer_select_check(masklay)) { - continue; - } - - masklay_shape = BKE_mask_layer_shape_find_frame(masklay, frame); - - if (masklay_shape) { - BKE_mask_layer_shape_unlink(masklay, masklay_shape); - changed = true; - } - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, ID_RECALC_GEOMETRY); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Scene *scene = CTX_data_scene(C); + const int frame = CFRA; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + MaskLayerShape *masklay_shape; + + if (!ED_mask_layer_select_check(masklay)) { + continue; + } + + masklay_shape = BKE_mask_layer_shape_find_frame(masklay, frame); + + if (masklay_shape) { + BKE_mask_layer_shape_unlink(masklay, masklay_shape); + changed = true; + } + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, ID_RECALC_GEOMETRY); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_shape_key_clear(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Clear Shape Key"; - ot->idname = "MASK_OT_shape_key_clear"; + /* identifiers */ + ot->name = "Clear Shape Key"; + ot->idname = "MASK_OT_shape_key_clear"; - /* api callbacks */ - ot->exec = mask_shape_key_clear_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_shape_key_clear_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } static int mask_shape_key_feather_reset_exec(bContext *C, wmOperator *UNUSED(op)) { - Scene *scene = CTX_data_scene(C); - const int frame = CFRA; - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - if (masklay->splines_shapes.first) { - MaskLayerShape *masklay_shape_reset; - MaskLayerShape *masklay_shape; - - /* get the shapekey of the current state */ - masklay_shape_reset = BKE_mask_layer_shape_alloc(masklay, frame); - /* initialize from mask - as if inseting a keyframe */ - BKE_mask_layer_shape_from_mask(masklay, masklay_shape_reset); - - for (masklay_shape = masklay->splines_shapes.first; - masklay_shape; - masklay_shape = masklay_shape->next) - { - - if (masklay_shape_reset->tot_vert == masklay_shape->tot_vert) { - int i_abs = 0; - int i; - MaskSpline *spline; - MaskLayerShapeElem *shape_ele_src; - MaskLayerShapeElem *shape_ele_dst; - - shape_ele_src = (MaskLayerShapeElem *)masklay_shape_reset->data; - shape_ele_dst = (MaskLayerShapeElem *)masklay_shape->data; - - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - if (MASKPOINT_ISSEL_ANY(point)) { - /* TODO - nicer access here */ - shape_ele_dst->value[6] = shape_ele_src->value[6]; - } - - shape_ele_src++; - shape_ele_dst++; - - i_abs++; - } - } - - } - else { - // printf("%s: skipping\n", __func__); - } - - changed = true; - } - - BKE_mask_layer_shape_free(masklay_shape_reset); - } - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Scene *scene = CTX_data_scene(C); + const int frame = CFRA; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + if (masklay->splines_shapes.first) { + MaskLayerShape *masklay_shape_reset; + MaskLayerShape *masklay_shape; + + /* get the shapekey of the current state */ + masklay_shape_reset = BKE_mask_layer_shape_alloc(masklay, frame); + /* initialize from mask - as if inseting a keyframe */ + BKE_mask_layer_shape_from_mask(masklay, masklay_shape_reset); + + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape->next) { + + if (masklay_shape_reset->tot_vert == masklay_shape->tot_vert) { + int i_abs = 0; + int i; + MaskSpline *spline; + MaskLayerShapeElem *shape_ele_src; + MaskLayerShapeElem *shape_ele_dst; + + shape_ele_src = (MaskLayerShapeElem *)masklay_shape_reset->data; + shape_ele_dst = (MaskLayerShapeElem *)masklay_shape->data; + + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + if (MASKPOINT_ISSEL_ANY(point)) { + /* TODO - nicer access here */ + shape_ele_dst->value[6] = shape_ele_src->value[6]; + } + + shape_ele_src++; + shape_ele_dst++; + + i_abs++; + } + } + } + else { + // printf("%s: skipping\n", __func__); + } + + changed = true; + } + + BKE_mask_layer_shape_free(masklay_shape_reset); + } + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_shape_key_feather_reset(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Feather Reset Animation"; - ot->description = "Reset feather weights on all selected points animation values"; - ot->idname = "MASK_OT_shape_key_feather_reset"; + /* identifiers */ + ot->name = "Feather Reset Animation"; + ot->description = "Reset feather weights on all selected points animation values"; + ot->idname = "MASK_OT_shape_key_feather_reset"; - /* api callbacks */ - ot->exec = mask_shape_key_feather_reset_exec; - ot->poll = ED_maskedit_mask_poll; + /* api callbacks */ + ot->exec = mask_shape_key_feather_reset_exec; + ot->poll = ED_maskedit_mask_poll; - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } /* @@ -241,208 +238,202 @@ void MASK_OT_shape_key_feather_reset(wmOperatorType *ot) */ static int mask_shape_key_rekey_exec(bContext *C, wmOperator *op) { - Scene *scene = CTX_data_scene(C); - const int frame = CFRA; - Mask *mask = CTX_data_edit_mask(C); - MaskLayer *masklay; - bool changed = false; - - const bool do_feather = RNA_boolean_get(op->ptr, "feather"); - const bool do_location = RNA_boolean_get(op->ptr, "location"); - - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - - if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { - continue; - } - - /* we need at least one point selected here to bother re-interpolating */ - if (!ED_mask_layer_select_check(masklay)) { - continue; - } - - if (masklay->splines_shapes.first) { - MaskLayerShape *masklay_shape, *masklay_shape_next; - MaskLayerShape *masklay_shape_lastsel = NULL; - - for (masklay_shape = masklay->splines_shapes.first; - masklay_shape; - masklay_shape = masklay_shape_next) - { - MaskLayerShape *masklay_shape_a = NULL; - MaskLayerShape *masklay_shape_b = NULL; - - masklay_shape_next = masklay_shape->next; - - /* find contiguous selections */ - if (masklay_shape->flag & MASK_SHAPE_SELECT) { - if (masklay_shape_lastsel == NULL) { - masklay_shape_lastsel = masklay_shape; - } - if ((masklay_shape->next == NULL) || - (((MaskLayerShape *)masklay_shape->next)->flag & MASK_SHAPE_SELECT) == 0) - { - masklay_shape_a = masklay_shape_lastsel; - masklay_shape_b = masklay_shape; - masklay_shape_lastsel = NULL; - - /* this will be freed below, step over selection */ - masklay_shape_next = masklay_shape->next; - } - } - - /* we have a from<>to? - re-interpolate! */ - if (masklay_shape_a && masklay_shape_b) { - ListBase shapes_tmp = {NULL, NULL}; - MaskLayerShape *masklay_shape_tmp; - MaskLayerShape *masklay_shape_tmp_next; - MaskLayerShape *masklay_shape_tmp_last = masklay_shape_b->next; - MaskLayerShape *masklay_shape_tmp_rekey; - - /* move keys */ - for (masklay_shape_tmp = masklay_shape_a; - masklay_shape_tmp && (masklay_shape_tmp != masklay_shape_tmp_last); - masklay_shape_tmp = masklay_shape_tmp_next) - { - masklay_shape_tmp_next = masklay_shape_tmp->next; - BLI_remlink(&masklay->splines_shapes, masklay_shape_tmp); - BLI_addtail(&shapes_tmp, masklay_shape_tmp); - } - - /* re-key, note: cant modify the keys here since it messes uop */ - for (masklay_shape_tmp = shapes_tmp.first; - masklay_shape_tmp; - masklay_shape_tmp = masklay_shape_tmp->next) - { - BKE_mask_layer_evaluate(masklay, masklay_shape_tmp->frame, true); - masklay_shape_tmp_rekey = BKE_mask_layer_shape_verify_frame(masklay, masklay_shape_tmp->frame); - BKE_mask_layer_shape_from_mask(masklay, masklay_shape_tmp_rekey); - masklay_shape_tmp_rekey->flag = masklay_shape_tmp->flag & MASK_SHAPE_SELECT; - } - - /* restore unselected points and free copies */ - for (masklay_shape_tmp = shapes_tmp.first; - masklay_shape_tmp; - masklay_shape_tmp = masklay_shape_tmp_next) - { - /* restore */ - int i_abs = 0; - int i; - MaskSpline *spline; - MaskLayerShapeElem *shape_ele_src; - MaskLayerShapeElem *shape_ele_dst; - - masklay_shape_tmp_next = masklay_shape_tmp->next; - - /* we know this exists, added above */ - masklay_shape_tmp_rekey = BKE_mask_layer_shape_find_frame(masklay, masklay_shape_tmp->frame); - - shape_ele_src = (MaskLayerShapeElem *)masklay_shape_tmp->data; - shape_ele_dst = (MaskLayerShapeElem *)masklay_shape_tmp_rekey->data; - - for (spline = masklay->splines.first; spline; spline = spline->next) { - for (i = 0; i < spline->tot_point; i++) { - MaskSplinePoint *point = &spline->points[i]; - - /* not especially efficient but makes this easier to follow */ - SWAP(MaskLayerShapeElem, *shape_ele_src, *shape_ele_dst); - - if (MASKPOINT_ISSEL_ANY(point)) { - if (do_location) { - memcpy(shape_ele_dst->value, shape_ele_src->value, sizeof(float) * 6); - } - if (do_feather) { - shape_ele_dst->value[6] = shape_ele_src->value[6]; - } - } - - shape_ele_src++; - shape_ele_dst++; - - i_abs++; - } - } - - BKE_mask_layer_shape_free(masklay_shape_tmp); - } - - changed = true; - } - } - - /* re-evaluate */ - BKE_mask_layer_evaluate(masklay, frame, true); - } - } - - if (changed) { - WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); - DEG_id_tag_update(&mask->id, 0); - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } + Scene *scene = CTX_data_scene(C); + const int frame = CFRA; + Mask *mask = CTX_data_edit_mask(C); + MaskLayer *masklay; + bool changed = false; + + const bool do_feather = RNA_boolean_get(op->ptr, "feather"); + const bool do_location = RNA_boolean_get(op->ptr, "location"); + + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + + if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) { + continue; + } + + /* we need at least one point selected here to bother re-interpolating */ + if (!ED_mask_layer_select_check(masklay)) { + continue; + } + + if (masklay->splines_shapes.first) { + MaskLayerShape *masklay_shape, *masklay_shape_next; + MaskLayerShape *masklay_shape_lastsel = NULL; + + for (masklay_shape = masklay->splines_shapes.first; masklay_shape; + masklay_shape = masklay_shape_next) { + MaskLayerShape *masklay_shape_a = NULL; + MaskLayerShape *masklay_shape_b = NULL; + + masklay_shape_next = masklay_shape->next; + + /* find contiguous selections */ + if (masklay_shape->flag & MASK_SHAPE_SELECT) { + if (masklay_shape_lastsel == NULL) { + masklay_shape_lastsel = masklay_shape; + } + if ((masklay_shape->next == NULL) || + (((MaskLayerShape *)masklay_shape->next)->flag & MASK_SHAPE_SELECT) == 0) { + masklay_shape_a = masklay_shape_lastsel; + masklay_shape_b = masklay_shape; + masklay_shape_lastsel = NULL; + + /* this will be freed below, step over selection */ + masklay_shape_next = masklay_shape->next; + } + } + + /* we have a from<>to? - re-interpolate! */ + if (masklay_shape_a && masklay_shape_b) { + ListBase shapes_tmp = {NULL, NULL}; + MaskLayerShape *masklay_shape_tmp; + MaskLayerShape *masklay_shape_tmp_next; + MaskLayerShape *masklay_shape_tmp_last = masklay_shape_b->next; + MaskLayerShape *masklay_shape_tmp_rekey; + + /* move keys */ + for (masklay_shape_tmp = masklay_shape_a; + masklay_shape_tmp && (masklay_shape_tmp != masklay_shape_tmp_last); + masklay_shape_tmp = masklay_shape_tmp_next) { + masklay_shape_tmp_next = masklay_shape_tmp->next; + BLI_remlink(&masklay->splines_shapes, masklay_shape_tmp); + BLI_addtail(&shapes_tmp, masklay_shape_tmp); + } + + /* re-key, note: cant modify the keys here since it messes uop */ + for (masklay_shape_tmp = shapes_tmp.first; masklay_shape_tmp; + masklay_shape_tmp = masklay_shape_tmp->next) { + BKE_mask_layer_evaluate(masklay, masklay_shape_tmp->frame, true); + masklay_shape_tmp_rekey = BKE_mask_layer_shape_verify_frame(masklay, + masklay_shape_tmp->frame); + BKE_mask_layer_shape_from_mask(masklay, masklay_shape_tmp_rekey); + masklay_shape_tmp_rekey->flag = masklay_shape_tmp->flag & MASK_SHAPE_SELECT; + } + + /* restore unselected points and free copies */ + for (masklay_shape_tmp = shapes_tmp.first; masklay_shape_tmp; + masklay_shape_tmp = masklay_shape_tmp_next) { + /* restore */ + int i_abs = 0; + int i; + MaskSpline *spline; + MaskLayerShapeElem *shape_ele_src; + MaskLayerShapeElem *shape_ele_dst; + + masklay_shape_tmp_next = masklay_shape_tmp->next; + + /* we know this exists, added above */ + masklay_shape_tmp_rekey = BKE_mask_layer_shape_find_frame(masklay, + masklay_shape_tmp->frame); + + shape_ele_src = (MaskLayerShapeElem *)masklay_shape_tmp->data; + shape_ele_dst = (MaskLayerShapeElem *)masklay_shape_tmp_rekey->data; + + for (spline = masklay->splines.first; spline; spline = spline->next) { + for (i = 0; i < spline->tot_point; i++) { + MaskSplinePoint *point = &spline->points[i]; + + /* not especially efficient but makes this easier to follow */ + SWAP(MaskLayerShapeElem, *shape_ele_src, *shape_ele_dst); + + if (MASKPOINT_ISSEL_ANY(point)) { + if (do_location) { + memcpy(shape_ele_dst->value, shape_ele_src->value, sizeof(float) * 6); + } + if (do_feather) { + shape_ele_dst->value[6] = shape_ele_src->value[6]; + } + } + + shape_ele_src++; + shape_ele_dst++; + + i_abs++; + } + } + + BKE_mask_layer_shape_free(masklay_shape_tmp); + } + + changed = true; + } + } + + /* re-evaluate */ + BKE_mask_layer_evaluate(masklay, frame, true); + } + } + + if (changed) { + WM_event_add_notifier(C, NC_MASK | ND_DATA, mask); + DEG_id_tag_update(&mask->id, 0); + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } } void MASK_OT_shape_key_rekey(wmOperatorType *ot) { - /* identifiers */ - ot->name = "Re-Key Points of Selected Shapes"; - ot->description = "Recalculate animation data on selected points for frames selected in the dopesheet"; - ot->idname = "MASK_OT_shape_key_rekey"; - - /* api callbacks */ - ot->exec = mask_shape_key_rekey_exec; - ot->poll = ED_maskedit_mask_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - RNA_def_boolean(ot->srna, "location", true, "Location", ""); - RNA_def_boolean(ot->srna, "feather", true, "Feather", ""); + /* identifiers */ + ot->name = "Re-Key Points of Selected Shapes"; + ot->description = + "Recalculate animation data on selected points for frames selected in the dopesheet"; + ot->idname = "MASK_OT_shape_key_rekey"; + + /* api callbacks */ + ot->exec = mask_shape_key_rekey_exec; + ot->poll = ED_maskedit_mask_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + RNA_def_boolean(ot->srna, "location", true, "Location", ""); + RNA_def_boolean(ot->srna, "feather", true, "Feather", ""); } - /* *** Shape Key Utils *** */ void ED_mask_layer_shape_auto_key(MaskLayer *masklay, const int frame) { - MaskLayerShape *masklay_shape; + MaskLayerShape *masklay_shape; - masklay_shape = BKE_mask_layer_shape_verify_frame(masklay, frame); - BKE_mask_layer_shape_from_mask(masklay, masklay_shape); + masklay_shape = BKE_mask_layer_shape_verify_frame(masklay, frame); + BKE_mask_layer_shape_from_mask(masklay, masklay_shape); } bool ED_mask_layer_shape_auto_key_all(Mask *mask, const int frame) { - MaskLayer *masklay; - bool changed = false; + MaskLayer *masklay; + bool changed = false; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - ED_mask_layer_shape_auto_key(masklay, frame); - changed = true; - } + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + ED_mask_layer_shape_auto_key(masklay, frame); + changed = true; + } - return changed; + return changed; } bool ED_mask_layer_shape_auto_key_select(Mask *mask, const int frame) { - MaskLayer *masklay; - bool changed = false; + MaskLayer *masklay; + bool changed = false; - for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { + for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) { - if (!ED_mask_layer_select_check(masklay)) { - continue; - } + if (!ED_mask_layer_select_check(masklay)) { + continue; + } - ED_mask_layer_shape_auto_key(masklay, frame); - changed = true; - } + ED_mask_layer_shape_auto_key(masklay, frame); + changed = true; + } - return changed; + return changed; } |