gimp r27869 - in trunk: . app/core
- From: neo svn gnome org
- To: svn-commits-list gnome org
- Subject: gimp r27869 - in trunk: . app/core
- Date: Thu, 1 Jan 2009 14:34:21 +0000 (UTC)
Author: neo
Date: Thu Jan 1 14:34:21 2009
New Revision: 27869
URL: http://svn.gnome.org/viewvc/gimp?rev=27869&view=rev
Log:
2009-01-01 Sven Neumann <sven gimp org>
Bug 565112 â code duplication in app/core/gimpimage-snap.c
* app/core/gimpimage-snap.c: based on a patch from Daniel
Hornung,
add the utility function gimp_image_snap_distance().
Modified:
trunk/ChangeLog
trunk/app/core/gimpimage-snap.c
Modified: trunk/app/core/gimpimage-snap.c
==============================================================================
--- trunk/app/core/gimpimage-snap.c (original)
+++ trunk/app/core/gimpimage-snap.c Thu Jan 1 14:34:21 2009
@@ -37,9 +37,16 @@
#include "gimp-intl.h"
-/* public functions */
+static gboolean gimp_image_snap_distance (const gdouble unsnapped,
+ const gdouble nearest,
+ const gdouble epsilon,
+ gdouble *mindist,
+ gdouble *target);
+
+/* public functions */
+
gboolean
gimp_image_snap_x (GimpImage *image,
gdouble x,
@@ -49,9 +56,8 @@
gboolean snap_to_grid,
gboolean snap_to_canvas)
{
- gdouble mindist = G_MAXDOUBLE;
- gdouble dist;
- gboolean snapped = FALSE;
+ gdouble mindist = G_MAXDOUBLE;
+ gboolean snapped = FALSE;
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
g_return_val_if_fail (tx != NULL, FALSE);
@@ -81,14 +87,9 @@
if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_VERTICAL)
{
- dist = ABS (position - x);
-
- if (dist < MIN (epsilon_x, mindist))
- {
- mindist = dist;
- *tx = position;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, position,
+ epsilon_x,
+ &mindist, tx);
}
}
}
@@ -105,6 +106,7 @@
"xoffset", &xoffset,
NULL);
+ /* the snap-to-grid part could probably be rewritten */
while (xoffset > xspacing)
xoffset -= xspacing;
@@ -113,36 +115,20 @@
if (i < 0)
continue;
- dist = ABS (i - x);
-
- if (dist < MIN (epsilon_x, mindist))
- {
- mindist = dist;
- *tx = i;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, i,
+ epsilon_x,
+ &mindist, tx);
}
}
if (snap_to_canvas)
{
- dist = ABS (x);
-
- if (dist < MIN (epsilon_x, mindist))
- {
- mindist = dist;
- *tx = 0;
- snapped = TRUE;
- }
-
- dist = ABS (gimp_image_get_width (image) - x);
-
- if (dist < MIN (epsilon_x, mindist))
- {
- mindist = dist;
- *tx = gimp_image_get_width (image);
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, 0,
+ epsilon_x,
+ &mindist, tx);
+ snapped |= gimp_image_snap_distance (x, gimp_image_get_width (image),
+ epsilon_x,
+ &mindist, tx);
}
return snapped;
@@ -158,7 +144,6 @@
gboolean snap_to_canvas)
{
gdouble mindist = G_MAXDOUBLE;
- gdouble dist;
gboolean snapped = FALSE;
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
@@ -189,14 +174,9 @@
if (gimp_guide_get_orientation (guide) == GIMP_ORIENTATION_HORIZONTAL)
{
- dist = ABS (position - y);
-
- if (dist < MIN (epsilon_y, mindist))
- {
- mindist = dist;
- *ty = position;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y, position,
+ epsilon_y,
+ &mindist, ty);
}
}
}
@@ -221,36 +201,20 @@
if (i < 0)
continue;
- dist = ABS (i - y);
-
- if (dist < MIN (epsilon_y, mindist))
- {
- mindist = dist;
- *ty = i;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y, i,
+ epsilon_y,
+ &mindist, ty);
}
}
if (snap_to_canvas)
{
- dist = ABS (y);
-
- if (dist < MIN (epsilon_y, mindist))
- {
- mindist = dist;
- *ty = 0;
- snapped = TRUE;
- }
-
- dist = ABS (gimp_image_get_height (image) - y);
-
- if (dist < MIN (epsilon_y, mindist))
- {
- mindist = dist;
- *ty = gimp_image_get_height (image);
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y, 0,
+ epsilon_y,
+ &mindist, ty);
+ snapped |= gimp_image_snap_distance (y, gimp_image_get_height (image),
+ epsilon_y,
+ &mindist, ty);
}
return snapped;
@@ -271,7 +235,6 @@
{
gdouble mindist_x = G_MAXDOUBLE;
gdouble mindist_y = G_MAXDOUBLE;
- gdouble dist;
gboolean snapped = FALSE;
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
@@ -309,25 +272,15 @@
switch (gimp_guide_get_orientation (guide))
{
case GIMP_ORIENTATION_HORIZONTAL:
- dist = ABS (position - y);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty = position;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y, position,
+ epsilon_y,
+ &mindist_y, ty);
break;
case GIMP_ORIENTATION_VERTICAL:
- dist = ABS (position - x);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx = position;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, position,
+ epsilon_x,
+ &mindist_x, tx);
break;
default:
@@ -361,14 +314,9 @@
if (i < 0)
continue;
- dist = ABS (i - x);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx = i;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, i,
+ epsilon_x,
+ &mindist_x, tx);
}
for (i = yoffset; i <= gimp_image_get_height (image); i += yspacing)
@@ -376,54 +324,27 @@
if (i < 0)
continue;
- dist = ABS (i - y);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty = i;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y, i,
+ epsilon_y,
+ &mindist_y, ty);
}
}
if (snap_to_canvas)
{
- dist = ABS (x);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx = 0;
- snapped = TRUE;
- }
-
- dist = ABS (gimp_image_get_width (image) - x);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx = gimp_image_get_width (image);
- snapped = TRUE;
- }
-
- dist = ABS (y);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty = 0;
- snapped = TRUE;
- }
-
- dist = ABS (gimp_image_get_height (image) - y);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty = gimp_image_get_height (image);
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, 0,
+ epsilon_x,
+ &mindist_x, tx);
+ snapped |= gimp_image_snap_distance (x, gimp_image_get_width (image),
+ epsilon_x,
+ &mindist_x, tx);
+
+ snapped |= gimp_image_snap_distance (y, 0,
+ epsilon_y,
+ &mindist_y, ty);
+ snapped |= gimp_image_snap_distance (y, gimp_image_get_height (image),
+ epsilon_y,
+ &mindist_y, ty);
}
if (snap_to_vectors)
@@ -443,23 +364,12 @@
&nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.x - x);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx = nearest.x;
- snapped = TRUE;
- }
-
- dist = ABS (nearest.y - y);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty = nearest.y;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x, nearest.x,
+ epsilon_x,
+ &mindist_x, tx);
+ snapped |= gimp_image_snap_distance (y, nearest.y,
+ epsilon_y,
+ &mindist_y, ty);
}
}
}
@@ -598,28 +508,18 @@
1.0, &nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.y - y1);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty1 = nearest.y;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y1, nearest.y,
+ epsilon_y,
+ &mindist_y, ty1);
}
if (gimp_stroke_nearest_intersection_get (stroke, &coords1, &coords2,
1.0, &nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.x - x1);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx1 = nearest.x;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x1, nearest.x,
+ epsilon_x,
+ &mindist_x, tx1);
}
if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
@@ -661,14 +561,9 @@
1.0, &nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.x - x1);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx1 = nearest.x;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x1, nearest.x,
+ epsilon_x,
+ &mindist_x, tx1);
}
if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
@@ -696,28 +591,18 @@
1.0, &nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.x - x1);
-
- if (dist < MIN (epsilon_x, mindist_x))
- {
- mindist_x = dist;
- *tx1 = nearest.x;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (x1, nearest.x,
+ epsilon_x,
+ &mindist_x, tx1);
}
if (gimp_stroke_nearest_intersection_get (stroke, &coords1, &coords2,
1.0, &nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.y - y1);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty1 = nearest.y;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y1, nearest.y,
+ epsilon_y,
+ &mindist_y, ty1);
}
if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
@@ -759,14 +644,9 @@
1.0, &nearest,
NULL, NULL, NULL) >= 0)
{
- dist = ABS (nearest.y - y1);
-
- if (dist < MIN (epsilon_y, mindist_y))
- {
- mindist_y = dist;
- *ty1 = nearest.y;
- snapped = TRUE;
- }
+ snapped |= gimp_image_snap_distance (y1, nearest.y,
+ epsilon_y,
+ &mindist_y, ty1);
}
if (gimp_stroke_nearest_intersection_get (stroke, &coords2, &coords1,
@@ -787,3 +667,38 @@
return snapped;
}
+
+/* private functions */
+
+/**
+ * gimp_image_snap_distance:
+ * @unsnapped: One coordinate of the unsnapped position
+ * @nearest: One coordinate of a snapping position candidate
+ * @epsilon: The snapping threshold
+ * @mindist: The distance to the currently closest snapping target
+ * @target: The currently closest snapping target
+ *
+ * Finds out if snapping occurs from position to a snapping candidate
+ * and sets the target accordingly.
+ *
+ * Return value: %TRUE if snapping occured, %FALSE otherwise
+ */
+static gboolean
+gimp_image_snap_distance (const gdouble unsnapped,
+ const gdouble nearest,
+ const gdouble epsilon,
+ gdouble *mindist,
+ gdouble *target)
+{
+ const gdouble dist = ABS (nearest - unsnapped);
+
+ if (dist < MIN (epsilon, *mindist))
+ {
+ *mindist = dist;
+ *target = nearest;
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]