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, &masklay, &spline, &point, &u, tangent, true, true)) { 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, 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_new(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2]) { Scene *scene = CTX_data_scene(C); const float ctime = CFRA; 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; } ED_mask_select_toggle_all(mask, SEL_DESELECT); spline = BKE_mask_spline_add(masklay); masklay->act_spline = spline; new_point = spline->points; masklay->act_point = new_point; 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); } WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); return true; }
static int add_vertex_new(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2]) { MaskSpline *spline; MaskSplinePoint *point; MaskSplinePoint *new_point = NULL, *ref_point = NULL; float view_zoom; 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; spline = NULL; point = NULL; } else { finSelectedSplinePoint(masklay, &spline, &point, TRUE); } ED_mask_select_toggle_all(mask, SEL_DESELECT); if (!spline) { /* no selected splines in active masklay, create new spline */ spline = BKE_mask_spline_add(masklay); } masklay->act_spline = spline; new_point = spline->points; masklay->act_point = new_point; { ScrArea *sa = CTX_wm_area(C); ARegion *ar = CTX_wm_region(C); float zoom_x, zoom_y; /* calc view zoom in a simplistic way */ ED_mask_zoom(sa, ar, &zoom_x, &zoom_y); view_zoom = zoom_x + zoom_y / 2.0f; view_zoom = 1.0f / view_zoom; /* arbitrary but gives good results */ view_zoom /= 500.0f; } setup_vertex_point(mask, spline, new_point, co, NULL, 0.5f, ref_point, FALSE, view_zoom); { 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); return TRUE; }
static void rna_MaskSpline_points_add(ID *id, MaskSpline *spline, int count) { Mask *mask = (Mask *) id; MaskLayer *layer; int active_point_index = -1; int i, spline_shape_index; if (count <= 0) { return; } for (layer = mask->masklayers.first; layer; layer = layer->next) { if (BLI_findindex(&layer->splines, spline) != -1) { break; } } if (!layer) { /* Shall not happen actually */ BLI_assert(!"No layer found for the spline"); return; } if (layer->act_spline == spline) { active_point_index = layer->act_point - spline->points; } spline->points = MEM_recallocN(spline->points, sizeof(MaskSplinePoint) * (spline->tot_point + count)); spline->tot_point += count; if (active_point_index >= 0) { layer->act_point = spline->points + active_point_index; } spline_shape_index = BKE_mask_layer_shape_spline_to_index(layer, spline); for (i = 0; i < count; i++) { int point_index = spline->tot_point - count + i; MaskSplinePoint *new_point = spline->points + point_index; new_point->bezt.h1 = new_point->bezt.h2 = HD_ALIGN; BKE_mask_calc_handle_point_auto(spline, new_point, TRUE); BKE_mask_parent_init(&new_point->parent); /* Not efficient, but there's no other way for now */ BKE_mask_layer_shape_changed_add(layer, spline_shape_index + point_index, true, true); } WM_main_add_notifier(NC_MASK | ND_DATA, mask); DAG_id_tag_update(&mask->id, 0); }
static bool add_vertex_extrude(const bContext *C, Mask *mask, MaskLayer *masklay, const float co[2]) { 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, 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; }