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.
This commit is contained in:
@ -76,7 +76,8 @@ struct _GimpToolLinePrivate
|
|||||||
|
|
||||||
gdouble mouse_x;
|
gdouble mouse_x;
|
||||||
gdouble mouse_y;
|
gdouble mouse_y;
|
||||||
GimpToolLinePoint grabbed_point;
|
GimpToolLinePoint point;
|
||||||
|
gboolean point_grabbed;
|
||||||
|
|
||||||
GimpCanvasItem *line;
|
GimpCanvasItem *line;
|
||||||
GimpCanvasItem *start_handle_circle;
|
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,
|
static gboolean gimp_tool_line_point_motion (GimpToolLine *line,
|
||||||
gboolean constrain_angle);
|
gboolean constrain_angle);
|
||||||
|
|
||||||
|
static void gimp_tool_line_update_handles (GimpToolLine *line);
|
||||||
static void gimp_tool_line_update_hilight (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)
|
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_CANVAS_HANDLE_SIZE_CROSS,
|
GIMP_CANVAS_HANDLE_SIZE_CROSS,
|
||||||
GIMP_HANDLE_ANCHOR_CENTER);
|
GIMP_HANDLE_ANCHOR_CENTER);
|
||||||
|
|
||||||
|
gimp_tool_line_changed (widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -338,6 +340,7 @@ gimp_tool_line_changed (GimpToolWidget *widget)
|
|||||||
private->x2,
|
private->x2,
|
||||||
private->y2);
|
private->y2);
|
||||||
|
|
||||||
|
gimp_tool_line_update_handles (line);
|
||||||
gimp_tool_line_update_hilight (line);
|
gimp_tool_line_update_hilight (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,22 +354,19 @@ gimp_tool_line_button_press (GimpToolWidget *widget,
|
|||||||
GimpToolLine *line = GIMP_TOOL_LINE (widget);
|
GimpToolLine *line = GIMP_TOOL_LINE (widget);
|
||||||
GimpToolLinePrivate *private = line->private;
|
GimpToolLinePrivate *private = line->private;
|
||||||
|
|
||||||
private->grabbed_point = gimp_tool_line_get_point (line);
|
if (private->point != POINT_NONE)
|
||||||
|
|
||||||
if (state & GDK_MOD1_MASK)
|
|
||||||
private->grabbed_point = POINT_BOTH;
|
|
||||||
|
|
||||||
if (private->grabbed_point != POINT_NONE)
|
|
||||||
{
|
{
|
||||||
private->saved_x1 = private->x1;
|
private->saved_x1 = private->x1;
|
||||||
private->saved_y1 = private->y1;
|
private->saved_y1 = private->y1;
|
||||||
private->saved_x2 = private->x2;
|
private->saved_x2 = private->x2;
|
||||||
private->saved_y2 = private->y2;
|
private->saved_y2 = private->y2;
|
||||||
|
|
||||||
|
private->point_grabbed = TRUE;
|
||||||
|
|
||||||
gimp_tool_line_point_motion (line,
|
gimp_tool_line_point_motion (line,
|
||||||
state & gimp_get_constrain_behavior_mask ());
|
state & gimp_get_constrain_behavior_mask ());
|
||||||
|
|
||||||
return 1;
|
return private->point;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -392,7 +392,7 @@ gimp_tool_line_button_release (GimpToolWidget *widget,
|
|||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
private->grabbed_point = POINT_NONE;
|
private->point_grabbed = FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -403,22 +403,19 @@ gimp_tool_line_motion (GimpToolWidget *widget,
|
|||||||
{
|
{
|
||||||
GimpToolLine *line = GIMP_TOOL_LINE (widget);
|
GimpToolLine *line = GIMP_TOOL_LINE (widget);
|
||||||
GimpToolLinePrivate *private = line->private;
|
GimpToolLinePrivate *private = line->private;
|
||||||
gdouble last_x = private->mouse_x;
|
gdouble diff_x = coords->x - private->mouse_x;
|
||||||
gdouble last_y = private->mouse_y;
|
gdouble diff_y = coords->y - private->mouse_y;
|
||||||
|
|
||||||
private->mouse_x = coords->x;
|
private->mouse_x = coords->x;
|
||||||
private->mouse_y = coords->y;
|
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,
|
g_object_set (line,
|
||||||
"x1", private->x1 - dx,
|
"x1", private->x1 + diff_x,
|
||||||
"y1", private->y1 - dy,
|
"y1", private->y1 + diff_y,
|
||||||
"x2", private->x2 - dx,
|
"x2", private->x2 + diff_x,
|
||||||
"y2", private->y2 - dy,
|
"y2", private->y2 + diff_y,
|
||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -441,6 +438,29 @@ gimp_tool_line_hover (GimpToolWidget *widget,
|
|||||||
private->mouse_x = coords->x;
|
private->mouse_x = coords->x;
|
||||||
private->mouse_y = coords->y;
|
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);
|
gimp_tool_line_update_hilight (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -466,10 +486,10 @@ gimp_tool_line_get_cursor (GimpToolWidget *widget,
|
|||||||
GimpToolCursorType *tool_cursor,
|
GimpToolCursorType *tool_cursor,
|
||||||
GimpCursorModifier *cursor_modifier)
|
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) ||
|
if (private->point == POINT_BOTH)
|
||||||
(state & GDK_MOD1_MASK))
|
|
||||||
{
|
{
|
||||||
*cursor_modifier = GIMP_CURSOR_MODIFIER_MOVE;
|
*cursor_modifier = GIMP_CURSOR_MODIFIER_MOVE;
|
||||||
|
|
||||||
@ -487,7 +507,7 @@ gimp_tool_line_point_motion (GimpToolLine *line,
|
|||||||
gdouble x = private->mouse_x;
|
gdouble x = private->mouse_x;
|
||||||
gdouble y = private->mouse_y;
|
gdouble y = private->mouse_y;
|
||||||
|
|
||||||
switch (private->grabbed_point)
|
switch (private->point)
|
||||||
{
|
{
|
||||||
case POINT_START:
|
case POINT_START:
|
||||||
if (constrain_angle)
|
if (constrain_angle)
|
||||||
@ -521,15 +541,14 @@ gimp_tool_line_point_motion (GimpToolLine *line,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
gimp_tool_line_update_hilight (GimpToolLine *line)
|
gimp_tool_line_update_handles (GimpToolLine *line)
|
||||||
{
|
{
|
||||||
GimpToolLinePrivate *private = line->private;
|
GimpToolLinePrivate *private = line->private;
|
||||||
GimpToolLinePoint hilight_point;
|
|
||||||
gboolean start_visible, end_visible;
|
gboolean start_visible, end_visible;
|
||||||
gint start_diameter, end_diameter;
|
gint start_diameter, end_diameter;
|
||||||
|
|
||||||
/* Calculate handle visibility */
|
/* Calculate handle visibility */
|
||||||
if (private->grabbed_point)
|
if (private->point_grabbed)
|
||||||
{
|
{
|
||||||
start_visible = FALSE;
|
start_visible = FALSE;
|
||||||
end_visible = FALSE;
|
end_visible = FALSE;
|
||||||
@ -541,14 +560,14 @@ gimp_tool_line_update_hilight (GimpToolLine *line)
|
|||||||
private->mouse_y,
|
private->mouse_y,
|
||||||
0,
|
0,
|
||||||
2 * GIMP_CANVAS_HANDLE_SIZE_CROSS);
|
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,
|
end_diameter = gimp_canvas_handle_calc_size (private->end_handle_circle,
|
||||||
private->mouse_x,
|
private->mouse_x,
|
||||||
private->mouse_y,
|
private->mouse_y,
|
||||||
0,
|
0,
|
||||||
2 * GIMP_CANVAS_HANDLE_SIZE_CROSS);
|
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);
|
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)
|
if (end_visible)
|
||||||
gimp_canvas_handle_set_size (private->end_handle_circle,
|
gimp_canvas_handle_set_size (private->end_handle_circle,
|
||||||
end_diameter, end_diameter);
|
end_diameter, end_diameter);
|
||||||
|
|
||||||
/* Update hilights */
|
|
||||||
if (private->grabbed_point)
|
|
||||||
hilight_point = private->grabbed_point;
|
|
||||||
else
|
|
||||||
hilight_point = gimp_tool_line_get_point (line);
|
|
||||||
|
|
||||||
gimp_canvas_item_set_highlight (private->start_handle_circle,
|
|
||||||
hilight_point == POINT_START);
|
|
||||||
gimp_canvas_item_set_highlight (private->start_handle_cross,
|
|
||||||
hilight_point == POINT_START);
|
|
||||||
|
|
||||||
gimp_canvas_item_set_highlight (private->end_handle_circle,
|
|
||||||
hilight_point == POINT_END);
|
|
||||||
gimp_canvas_item_set_highlight (private->end_handle_cross,
|
|
||||||
hilight_point == POINT_END);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static GimpToolLinePoint
|
static void
|
||||||
gimp_tool_line_get_point (GimpToolLine *line)
|
gimp_tool_line_update_hilight (GimpToolLine *line)
|
||||||
{
|
{
|
||||||
GimpToolLinePrivate *private = line->private;
|
GimpToolLinePrivate *private = line->private;
|
||||||
|
|
||||||
/* Check the points in the reverse order of drawing */
|
gimp_canvas_item_set_highlight (private->start_handle_circle,
|
||||||
|
private->point == POINT_START);
|
||||||
|
gimp_canvas_item_set_highlight (private->start_handle_cross,
|
||||||
|
private->point == POINT_START);
|
||||||
|
|
||||||
/* Check end point */
|
gimp_canvas_item_set_highlight (private->end_handle_circle,
|
||||||
if (gimp_canvas_item_hit (private->end_handle_circle,
|
private->point == POINT_END);
|
||||||
private->mouse_x,
|
gimp_canvas_item_set_highlight (private->end_handle_cross,
|
||||||
private->mouse_y))
|
private->point == POINT_END);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user