[gimp] app: some more cleanup in GimpToolLine



commit d2bfbfb748d79008b9b23e1a34e3f7e50b93ee8c
Author: Michael Natterer <mitch gimp org>
Date:   Tue Jun 20 19:07:24 2017 +0200

    app: some more cleanup in GimpToolLine
    
    it was already working perfectly, but it's so simple I'd like to make
    it "perfect" as a GimpToolWidget implementation example.

 app/display/gimptoolline.c |  120 +++++++++++++++++++++-----------------------
 1 files changed, 58 insertions(+), 62 deletions(-)
---
diff --git a/app/display/gimptoolline.c b/app/display/gimptoolline.c
index 125eb73..67cb6e6 100644
--- a/app/display/gimptoolline.c
+++ b/app/display/gimptoolline.c
@@ -76,7 +76,8 @@ struct _GimpToolLinePrivate
 
   gdouble            mouse_x;
   gdouble            mouse_y;
-  GimpToolLinePoint  grabbed_point;
+  GimpToolLinePoint  point;
+  gboolean           point_grabbed;
 
   GimpCanvasItem    *line;
   GimpCanvasItem    *start_handle_circle;
@@ -131,9 +132,8 @@ static gboolean gimp_tool_line_get_cursor      (GimpToolWidget        *widget,
 static gboolean gimp_tool_line_point_motion    (GimpToolLine          *line,
                                                 gboolean               constrain_angle);
 
+static void     gimp_tool_line_update_handles  (GimpToolLine          *line);
 static void     gimp_tool_line_update_hilight  (GimpToolLine          *line);
-static GimpToolLinePoint
-                gimp_tool_line_get_point       (GimpToolLine          *line);
 
 
 G_DEFINE_TYPE (GimpToolLine, gimp_tool_line, GIMP_TYPE_TOOL_WIDGET)
@@ -250,6 +250,8 @@ gimp_tool_line_constructed (GObject *object)
                                  GIMP_CANVAS_HANDLE_SIZE_CROSS,
                                  GIMP_CANVAS_HANDLE_SIZE_CROSS,
                                  GIMP_HANDLE_ANCHOR_CENTER);
+
+  gimp_tool_line_changed (widget);
 }
 
 static void
@@ -338,6 +340,7 @@ gimp_tool_line_changed (GimpToolWidget *widget)
                                    private->x2,
                                    private->y2);
 
+  gimp_tool_line_update_handles (line);
   gimp_tool_line_update_hilight (line);
 }
 
@@ -351,22 +354,19 @@ gimp_tool_line_button_press (GimpToolWidget      *widget,
   GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
   GimpToolLinePrivate *private = line->private;
 
-  private->grabbed_point = gimp_tool_line_get_point (line);
-
-  if (state & GDK_MOD1_MASK)
-    private->grabbed_point = POINT_BOTH;
-
-  if (private->grabbed_point != POINT_NONE)
+  if (private->point != POINT_NONE)
     {
       private->saved_x1 = private->x1;
       private->saved_y1 = private->y1;
       private->saved_x2 = private->x2;
       private->saved_y2 = private->y2;
 
+      private->point_grabbed = TRUE;
+
       gimp_tool_line_point_motion (line,
                                    state & gimp_get_constrain_behavior_mask ());
 
-      return 1;
+      return private->point;
     }
 
   return 0;
@@ -392,7 +392,7 @@ gimp_tool_line_button_release (GimpToolWidget        *widget,
                     NULL);
     }
 
-  private->grabbed_point = POINT_NONE;
+  private->point_grabbed = FALSE;
 }
 
 void
@@ -403,22 +403,19 @@ gimp_tool_line_motion (GimpToolWidget   *widget,
 {
   GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
   GimpToolLinePrivate *private = line->private;
-  gdouble              last_x  = private->mouse_x;
-  gdouble              last_y  = private->mouse_y;
+  gdouble              diff_x  = coords->x - private->mouse_x;
+  gdouble              diff_y  = coords->y - private->mouse_y;
 
   private->mouse_x = coords->x;
   private->mouse_y = coords->y;
 
-  if (private->grabbed_point == POINT_BOTH)
+  if (private->point == POINT_BOTH)
     {
-      gdouble dx = last_x - coords->x;
-      gdouble dy = last_y - coords->y;
-
       g_object_set (line,
-                    "x1", private->x1 - dx,
-                    "y1", private->y1 - dy,
-                    "x2", private->x2 - dx,
-                    "y2", private->y2 - dy,
+                    "x1", private->x1 + diff_x,
+                    "y1", private->y1 + diff_y,
+                    "x2", private->x2 + diff_x,
+                    "y2", private->y2 + diff_y,
                     NULL);
     }
   else
@@ -441,6 +438,29 @@ gimp_tool_line_hover (GimpToolWidget   *widget,
   private->mouse_x = coords->x;
   private->mouse_y = coords->y;
 
+  gimp_tool_line_update_handles (line);
+
+  if (state & GDK_MOD1_MASK)
+    {
+      private->point = POINT_BOTH;
+    }
+  else if (gimp_canvas_item_hit (private->end_handle_circle,
+                                 private->mouse_x,
+                                 private->mouse_y))
+    {
+      private->point = POINT_END;
+    }
+  else if (gimp_canvas_item_hit (private->start_handle_circle,
+                                 private->mouse_x,
+                                 private->mouse_y))
+    {
+      private->point = POINT_START;
+    }
+  else
+    {
+      private->point = POINT_NONE;
+    }
+
   gimp_tool_line_update_hilight (line);
 }
 
@@ -466,10 +486,10 @@ gimp_tool_line_get_cursor (GimpToolWidget     *widget,
                            GimpToolCursorType *tool_cursor,
                            GimpCursorModifier *cursor_modifier)
 {
-  GimpToolLine *line = GIMP_TOOL_LINE (widget);
+  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
+  GimpToolLinePrivate *private = line->private;
 
-  if (gimp_tool_line_get_point (line) ||
-      (state & GDK_MOD1_MASK))
+  if (private->point == POINT_BOTH)
     {
       *cursor_modifier = GIMP_CURSOR_MODIFIER_MOVE;
 
@@ -487,7 +507,7 @@ gimp_tool_line_point_motion (GimpToolLine *line,
   gdouble              x       = private->mouse_x;
   gdouble              y       = private->mouse_y;
 
-  switch (private->grabbed_point)
+  switch (private->point)
     {
     case POINT_START:
       if (constrain_angle)
@@ -521,15 +541,14 @@ gimp_tool_line_point_motion (GimpToolLine *line,
 }
 
 static void
-gimp_tool_line_update_hilight (GimpToolLine *line)
+gimp_tool_line_update_handles (GimpToolLine *line)
 {
   GimpToolLinePrivate *private = line->private;
-  GimpToolLinePoint    hilight_point;
   gboolean             start_visible,  end_visible;
   gint                 start_diameter, end_diameter;
 
   /* Calculate handle visibility */
-  if (private->grabbed_point)
+  if (private->point_grabbed)
     {
       start_visible = FALSE;
       end_visible   = FALSE;
@@ -541,14 +560,14 @@ gimp_tool_line_update_hilight (GimpToolLine *line)
                                                      private->mouse_y,
                                                      0,
                                                      2 * GIMP_CANVAS_HANDLE_SIZE_CROSS);
-      start_visible  = start_diameter > 2;
+      start_visible = start_diameter > 2;
 
       end_diameter = gimp_canvas_handle_calc_size (private->end_handle_circle,
                                                    private->mouse_x,
                                                    private->mouse_y,
                                                    0,
                                                    2 * GIMP_CANVAS_HANDLE_SIZE_CROSS);
-      end_visible  = end_diameter > 2;
+      end_visible = end_diameter > 2;
     }
 
   gimp_canvas_item_set_visible (private->start_handle_circle, start_visible);
@@ -561,45 +580,22 @@ gimp_tool_line_update_hilight (GimpToolLine *line)
   if (end_visible)
     gimp_canvas_handle_set_size (private->end_handle_circle,
                                  end_diameter, end_diameter);
+}
 
-  /* Update hilights */
-  if (private->grabbed_point)
-    hilight_point = private->grabbed_point;
-  else
-    hilight_point = gimp_tool_line_get_point (line);
+static void
+gimp_tool_line_update_hilight (GimpToolLine *line)
+{
+  GimpToolLinePrivate *private = line->private;
 
   gimp_canvas_item_set_highlight (private->start_handle_circle,
-                                  hilight_point == POINT_START);
+                                  private->point == POINT_START);
   gimp_canvas_item_set_highlight (private->start_handle_cross,
-                                  hilight_point == POINT_START);
+                                  private->point == POINT_START);
 
   gimp_canvas_item_set_highlight (private->end_handle_circle,
-                                  hilight_point == POINT_END);
+                                  private->point == POINT_END);
   gimp_canvas_item_set_highlight (private->end_handle_cross,
-                                  hilight_point == POINT_END);
-}
-
-static GimpToolLinePoint
-gimp_tool_line_get_point (GimpToolLine *line)
-{
-  GimpToolLinePrivate *private = line->private;
-
-  /* Check the points in the reverse order of drawing */
-
-  /* Check end point */
-  if (gimp_canvas_item_hit (private->end_handle_circle,
-                            private->mouse_x,
-                            private->mouse_y))
-    return POINT_END;
-
-  /* Check start point */
-  if (gimp_canvas_item_hit (private->start_handle_circle,
-                            private->mouse_x,
-                            private->mouse_y))
-    return POINT_START;
-
-  /* No point found */
-  return POINT_NONE;
+                                  private->point == POINT_END);
 }
 
 


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