[gimp/soc-2010-cage-2] Fix point selection detection



commit e133035440e79c9b209d8623c3bb9d1b37540be5
Author: Alexia Death <alexiadeath gmail com>
Date:   Sun Sep 5 13:33:26 2010 +0300

    Fix point selection detection

 app/gegl/gimpcageconfig.c             |   36 -----------
 app/gegl/gimpcageconfig.h             |   18 +------
 app/gegl/gimpoperationcagetransform.c |    6 +-
 app/tools/gimpcagetool.c              |  103 ++++++++++++++++++++++++++-------
 4 files changed, 86 insertions(+), 77 deletions(-)
---
diff --git a/app/gegl/gimpcageconfig.c b/app/gegl/gimpcageconfig.c
index 99417f8..e9c3408 100644
--- a/app/gegl/gimpcageconfig.c
+++ b/app/gegl/gimpcageconfig.c
@@ -202,43 +202,7 @@ gimp_cage_config_remove_last_cage_point (GimpCageConfig  *gcc)
   gimp_cage_config_compute_edge_normal (gcc);
 }
 
-gint
-gimp_cage_config_is_on_handle (GimpCageConfig  *gcc,
-                               GimpCageMode     mode,
-                               gdouble          x,
-                               gdouble          y,
-                               gint             handle_size)
-{
-  gint i;
-  gdouble vert_x, vert_y;
-
-  g_return_val_if_fail (GIMP_IS_CAGE_CONFIG (gcc), -1);
-
-  if (gcc->cage_vertice_number == 0)
-    return -1;
-
-  for (i = 0; i < gcc->cage_vertice_number; i++)
-  {
-    if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
-    {
-      vert_x = gcc->cage_vertices[i].x;
-      vert_y = gcc->cage_vertices[i].y;
-    }
-    else
-    {
-      vert_x = gcc->cage_vertices_d[i].x;
-      vert_y = gcc->cage_vertices_d[i].y;
-    }
-
-    if (x < vert_x + handle_size / 2 && x > vert_x -handle_size / 2 &&
-        y < vert_y + handle_size / 2 && y > vert_y -handle_size / 2)
-    {
-      return i;
-    }
-  }
 
-  return -1;
-}
                                               
 void
 gimp_cage_config_move_cage_point  (GimpCageConfig  *gcc,
diff --git a/app/gegl/gimpcageconfig.h b/app/gegl/gimpcageconfig.h
index 1e21778..2127796 100644
--- a/app/gegl/gimpcageconfig.h
+++ b/app/gegl/gimpcageconfig.h
@@ -76,23 +76,7 @@ void          gimp_cage_config_add_cage_point         (GimpCageConfig  *gcc,
  */
 void          gimp_cage_config_remove_last_cage_point (GimpCageConfig  *gcc);
 
-/**
- * gimp_cage_config_is_on_handle:
- * @gcc: the cage config
- * @mode: the actual mode of the cage, GIMP_CAGE_MODE_CAGE_CHANGE or GIMP_CAGE_MODE_DEFORM
- * @x: x value to check
- * @y: y value to check
- * @handle_size: size of the handle, in pixels
- * 
- * Check if a given point is on a handle of the cage, and return his number if yes.
- * 
- * Returns: the number of the handle if the point is on a handle, or -1 if not.
- */
-gint          gimp_cage_config_is_on_handle           (GimpCageConfig  *gcc,
-                                                       GimpCageMode     mode,
-                                                       gdouble          x,
-                                                       gdouble          y,
-                                                       gint             handle_size);
+
 
 /**
  * gimp_cage_config_move_cage_point:
diff --git a/app/gegl/gimpoperationcagetransform.c b/app/gegl/gimpoperationcagetransform.c
index d8ac0c8..bcca9a0 100644
--- a/app/gegl/gimpoperationcagetransform.c
+++ b/app/gegl/gimpoperationcagetransform.c
@@ -212,14 +212,14 @@ gimp_operation_cage_transform_process (GeglOperation       *operation,
   GimpVector2 p1_s, p2_s, p3_s, p4_s;
   GimpVector2 plain_color;
 
+  /* pre-fill the out buffer with no-displacement coordinate */
+  GeglBufferIterator *it = gegl_buffer_iterator_new (out_buf, roi, NULL, GEGL_BUFFER_WRITE);
+
   plain_color.x = (gint) config->cage_vertices[0].x;
   plain_color.y = (gint) config->cage_vertices[0].y;
 
   printf ("Color fill picked from (%f, %f)\n", plain_color.x, plain_color.y);
 
-  /* pre-fill the out buffer with no-displacement coordinate */
-  GeglBufferIterator *it = gegl_buffer_iterator_new (out_buf, roi, NULL, GEGL_BUFFER_WRITE);
-
   while (gegl_buffer_iterator_next (it))
   {
     /* iterate inside the roi */
diff --git a/app/tools/gimpcagetool.c b/app/tools/gimpcagetool.c
index 2b07e28..08547ef 100644
--- a/app/tools/gimpcagetool.c
+++ b/app/tools/gimpcagetool.c
@@ -100,6 +100,14 @@ static void         gimp_cage_tool_oper_update        (GimpTool              *to
                                                        GdkModifierType        state,
                                                        gboolean               proximity,
                                                        GimpDisplay           *display);
+
+static gint         gimp_cage_tool_is_on_handle       (GimpCageConfig  *gcc,
+                                                       GimpDrawTool    *draw_tool,
+                                                       GimpDisplay     *display,
+                                                       GimpCageMode     mode,
+                                                       gdouble          x,
+                                                       gdouble          y,
+                                                       gint             handle_size);
 static void         gimp_cage_tool_draw               (GimpDrawTool          *draw_tool);
 static void         gimp_cage_tool_switch_to_deform   (GimpCageTool          *ct);
 static void         gimp_cage_tool_remove_last_handle (GimpCageTool          *ct);
@@ -115,16 +123,16 @@ G_DEFINE_TYPE (GimpCageTool, gimp_cage_tool, GIMP_TYPE_DRAW_TOOL)
 
 #define parent_class gimp_cage_tool_parent_class
 
-#define HANDLE_SIZE             14
+#define HANDLE_SIZE             25
 
 void
 gimp_cage_tool_register (GimpToolRegisterCallback  callback,
                            gpointer                  data)
 {
-  (* callback) (GIMP_TYPE_CAGE_TOOL, //Tool type
-                GIMP_TYPE_CAGE_OPTIONS, //Tool options type
-                gimp_cage_options_gui, //Tool opions gui
-                0, //context_props
+  (* callback) (GIMP_TYPE_CAGE_TOOL, /* Tool type */
+                GIMP_TYPE_CAGE_OPTIONS, /*Tool options type*/
+                gimp_cage_options_gui, /*Tool opions gui*/
+                0, /*context_props*/
                 "gimp-cage-tool",
                 _("Cage Transform"),
                 _("Cage Transform: Deform a selection with a cage"),
@@ -328,11 +336,13 @@ gimp_cage_tool_oper_update  (GimpTool         *tool,
   gint             active_handle = -1;
 
   if (config)
-    active_handle = gimp_cage_config_is_on_handle (config,
-                                                   options->cage_mode,
-                                                   coords->x,
-                                                   coords->y,
-                                                   HANDLE_SIZE);
+    active_handle = gimp_cage_tool_is_on_handle (config,
+                                                 draw_tool,
+                                                 display,
+                                                 options->cage_mode,
+                                                 coords->x,
+                                                 coords->y,
+                                                 HANDLE_SIZE );
   if (!ct->cage_complete || (active_handle > -1))
     {
       gimp_draw_tool_pause (draw_tool);
@@ -371,11 +381,13 @@ gimp_cage_tool_button_press (GimpTool              *tool,
 
   if (ct->handle_moved < 0)
   {
-    ct->handle_moved = gimp_cage_config_is_on_handle  (config,
-                                                       options->cage_mode,
-                                                       coords->x,
-                                                       coords->y,
-                                                       HANDLE_SIZE);
+    ct->handle_moved = gimp_cage_tool_is_on_handle  (config,
+                                                     GIMP_DRAW_TOOL(ct),
+                                                     display,
+                                                     options->cage_mode,
+                                                     coords->x,
+                                                     coords->y,
+                                                     HANDLE_SIZE);
     if (ct->handle_moved > 0 && ct->idle_id > 0)
       {
         g_source_remove(ct->idle_id);
@@ -422,7 +434,6 @@ gimp_cage_tool_button_release (GimpTool              *tool,
   {
 
     GimpDisplayShell *shell = gimp_display_get_shell (tool->display);
-    GimpImage *image = gimp_display_get_image (tool->display);
     GimpItem         *item  = GIMP_ITEM (tool->drawable);
 
     gint              x, y;
@@ -553,11 +564,13 @@ gimp_cage_tool_draw (GimpDrawTool *draw_tool)
                               FALSE);
   }
 
-  on_handle = gimp_cage_config_is_on_handle (config,
-                                             options->cage_mode,
-                                             ct->cursor_position.x,
-                                             ct->cursor_position.y,
-                                             HANDLE_SIZE);
+  on_handle = gimp_cage_tool_is_on_handle (config,
+                                           draw_tool,
+                                           draw_tool->display,
+                                           options->cage_mode,
+                                           ct->cursor_position.x,
+                                           ct->cursor_position.y,
+                                           HANDLE_SIZE);
 
   for(i = 0; i < config->cage_vertice_number; i++)
   {
@@ -578,6 +591,54 @@ gimp_cage_tool_draw (GimpDrawTool *draw_tool)
   }
 }
 
+static gint
+gimp_cage_tool_is_on_handle (GimpCageConfig  *gcc,
+                             GimpDrawTool    *draw_tool,
+                             GimpDisplay     *display,
+                             GimpCageMode     mode,
+                             gdouble          x,
+                             gdouble          y,
+                             gint             handle_size)
+{
+  gint i;
+  gdouble vert_x;
+  gdouble vert_y;
+  gdouble dist = G_MAXDOUBLE;
+
+  g_return_val_if_fail (GIMP_IS_CAGE_CONFIG (gcc), -1);
+
+  if (gcc->cage_vertice_number == 0)
+    return -1;
+
+  for (i = 0; i < gcc->cage_vertice_number; i++)
+  {
+    if (mode == GIMP_CAGE_MODE_CAGE_CHANGE)
+    {
+      vert_x = gcc->cage_vertices[i].x;
+      vert_y = gcc->cage_vertices[i].y;
+    }
+    else
+    {
+      vert_x = gcc->cage_vertices_d[i].x;
+      vert_y = gcc->cage_vertices_d[i].y;
+    }
+
+    dist = gimp_draw_tool_calc_distance_square (GIMP_DRAW_TOOL (draw_tool),
+                                                display,
+                                                x,
+                                                y,
+                                                vert_x,
+                                                vert_y);
+
+    if (dist <= (handle_size * handle_size))
+    {
+      return i;
+    }
+  }
+
+  return -1;
+}
+
 static void
 gimp_cage_tool_remove_last_handle (GimpCageTool *ct)
 {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]