diff --git a/demos/gtk-demo/path_maze.c b/demos/gtk-demo/path_maze.c index 0fe896ff35..f81eb81e62 100644 --- a/demos/gtk-demo/path_maze.c +++ b/demos/gtk-demo/path_maze.c @@ -138,16 +138,20 @@ pointer_motion (GtkEventControllerMotion *controller, GtkMaze *self) { GskPathPoint point; - graphene_point_t pos; + float distance; if (!self->active) return; - gsk_path_get_closest_point (self->path, &GRAPHENE_POINT_INIT (x, y), INFINITY, &point); - - gsk_path_point_get_position (&point, self->path, &pos); - if (graphene_point_distance (&GRAPHENE_POINT_INIT (x, y), &pos, NULL, NULL) <= MAZE_STROKE_SIZE_ACTIVE / 2.0f) - return; + if (gsk_path_get_closest_point (self->path, + &GRAPHENE_POINT_INIT (x, y), + INFINITY, + &point, + &distance)) + { + if (distance < MAZE_STROKE_SIZE_ACTIVE / 2.f) + return; + } self->active = FALSE; gtk_widget_queue_draw (GTK_WIDGET (self)); diff --git a/demos/gtk-demo/path_spinner.c b/demos/gtk-demo/path_spinner.c index 8279b77b36..914a608198 100644 --- a/demos/gtk-demo/path_spinner.c +++ b/demos/gtk-demo/path_spinner.c @@ -203,8 +203,8 @@ update_path (GtkSpinnerPaintable *self) g_clear_pointer (&self->path, gsk_path_unref); - gsk_path_get_closest_point (self->circle, &p0, INFINITY, &start); - gsk_path_get_closest_point (self->circle, &p1, INFINITY, &end); + gsk_path_get_closest_point (self->circle, &p0, INFINITY, &start, NULL); + gsk_path_get_closest_point (self->circle, &p1, INFINITY, &end, NULL); builder = gsk_path_builder_new (); gsk_path_builder_add_segment (builder, self->circle, &start, &end); diff --git a/demos/gtk-demo/path_text.c b/demos/gtk-demo/path_text.c index e6ddd70552..16ebb6daa7 100644 --- a/demos/gtk-demo/path_text.c +++ b/demos/gtk-demo/path_text.c @@ -506,10 +506,9 @@ pointer_motion (GtkEventControllerMotion *controller, if (gsk_path_get_closest_point (self->line_path, &GRAPHENE_POINT_INIT (x, y), INFINITY, - &point)) + &point, + NULL)) { - gsk_path_point_get_position (&point, self->line_path, &pos); - gtk_widget_queue_draw (GTK_WIDGET (self)); } } diff --git a/gsk/gskpath.c b/gsk/gskpath.c index b62d84d42e..0c5d6aa026 100644 --- a/gsk/gskpath.c +++ b/gsk/gskpath.c @@ -565,6 +565,7 @@ gsk_path_get_end_point (GskPath *self, * @point: the point * @threshold: maximum allowed distance * @result: (out caller-allocates): return location for the closest point + * @distance: (out) (optional): return location for the distance * * Computes the closest point on the path to the given point * and sets the @result to it. @@ -581,7 +582,8 @@ gboolean gsk_path_get_closest_point (GskPath *self, const graphene_point_t *point, float threshold, - GskPathPoint *result) + GskPathPoint *result, + float *distance) { gboolean found; @@ -594,14 +596,17 @@ gsk_path_get_closest_point (GskPath *self, for (int i = 0; i < self->n_contours; i++) { - float distance; + float dist; - if (gsk_contour_get_closest_point (self->contours[i], point, threshold, result, &distance)) + if (gsk_contour_get_closest_point (self->contours[i], point, threshold, result, &dist)) { found = TRUE; g_assert (0 <= result->t && result->t <= 1); result->contour = i; - threshold = distance; + threshold = dist; + + if (distance) + *distance = dist; } } diff --git a/gsk/gskpath.h b/gsk/gskpath.h index 7010700f93..246440264e 100644 --- a/gsk/gskpath.h +++ b/gsk/gskpath.h @@ -135,7 +135,8 @@ GDK_AVAILABLE_IN_4_14 gboolean gsk_path_get_closest_point (GskPath *self, const graphene_point_t *point, float threshold, - GskPathPoint *result); + GskPathPoint *result, + float *distance); GDK_AVAILABLE_IN_4_14 gboolean gsk_path_foreach (GskPath *self, diff --git a/testsuite/gsk/path-special-cases.c b/testsuite/gsk/path-special-cases.c index 38aa2b2bf8..b28e090a0a 100644 --- a/testsuite/gsk/path-special-cases.c +++ b/testsuite/gsk/path-special-cases.c @@ -301,7 +301,7 @@ test_empty_path (void) g_assert_false (gsk_path_in_fill (path, &GRAPHENE_POINT_INIT (0, 0), GSK_FILL_RULE_WINDING)); - g_assert_false (gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (0, 0), INFINITY, &point)); + g_assert_false (gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (0, 0), INFINITY, &point, NULL)); gsk_path_unref (path); } @@ -314,6 +314,7 @@ test_rect_path (void) char *s; graphene_rect_t bounds; GskPathPoint point; + float distance; builder = gsk_path_builder_new (); gsk_path_builder_add_rect (builder, &GRAPHENE_RECT_INIT (0, 0, 200, 100)); @@ -332,7 +333,9 @@ test_rect_path (void) g_assert_true (gsk_path_in_fill (path, &GRAPHENE_POINT_INIT (50, 50), GSK_FILL_RULE_WINDING)); g_assert_false (gsk_path_in_fill (path, &GRAPHENE_POINT_INIT (200, 200), GSK_FILL_RULE_WINDING)); - g_assert_true (gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (200, 200), INFINITY, &point)); + g_assert_true (gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (200, 200), INFINITY, &point, &distance)); + + g_assert_true (distance == 100); gsk_path_unref (path); } @@ -455,7 +458,7 @@ test_path_point (void) g_assert_true (point.idx == 4); g_assert_true (point.t == 1); - ret = gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (200, 200), INFINITY, &point); + ret = gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (200, 200), INFINITY, &point, NULL); g_assert_true (ret); g_assert_true (point.contour == 0); @@ -473,7 +476,7 @@ test_path_point (void) g_assert_true (graphene_vec2_equal (&t2, &mx)); g_assert_true (curvature == 0); - ret = gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (100, 50), INFINITY, &point); + ret = gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (100, 50), INFINITY, &point, NULL); g_assert_true (ret); g_assert_true (point.contour == 0); @@ -534,8 +537,8 @@ test_path_segments (void) char *str; path = gsk_path_parse (tests[i].path); - g_assert_true (gsk_path_get_closest_point (path, &tests[i].p1, INFINITY, &p1)); - g_assert_true (gsk_path_get_closest_point (path, &tests[i].p2, INFINITY, &p2)); + g_assert_true (gsk_path_get_closest_point (path, &tests[i].p1, INFINITY, &p1, NULL)); + g_assert_true (gsk_path_get_closest_point (path, &tests[i].p2, INFINITY, &p2, NULL)); builder = gsk_path_builder_new (); gsk_path_builder_add_segment (builder, path, &p1, &p2); @@ -602,7 +605,7 @@ test_path_builder_add (void) path = gsk_path_parse ("M 10 10 L 100 100"); - gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (50, 50), INFINITY, &point1); + gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (50, 50), INFINITY, &point1, NULL); gsk_path_get_end_point (path, &point2); surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100, 100); @@ -872,8 +875,8 @@ test_circle (void) gsk_path_point_get_position (&point0, path, &p); g_assert_true (graphene_point_equal (&p, &GRAPHENE_POINT_INIT (1, 0))); - gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (1, 1), INFINITY, &point0); - gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (-1, 1), INFINITY, &point1); + gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (1, 1), INFINITY, &point0, NULL); + gsk_path_get_closest_point (path, &GRAPHENE_POINT_INIT (-1, 1), INFINITY, &point1, NULL); builder = gsk_path_builder_new (); gsk_path_builder_add_segment (builder, path, &point0, &point1);