Commit 2192f520 authored by Michael Natterer's avatar Michael Natterer 😴

app: add new GimpToolWidget subclass GimpToolPolygon

which is the free select curve.
parent 9e2a3c7a
......@@ -172,6 +172,8 @@ libappdisplay_a_sources = \
gimptoolline.h \
gimptoolpath.c \
gimptoolpath.h \
gimptoolpolygon.c \
gimptoolpolygon.h \
gimptoolrotategrid.c \
gimptoolrotategrid.h \
gimptoolsheargrid.c \
......
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimptoolpolygon.c
* Copyright (C) 2017 Michael Natterer <mitch@gimp.org>
*
* Based on GimpFreeSelectTool
*
* Major improvement to support polygonal segments
* Copyright (C) 2008 Martin Nordholts
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <gegl.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"
#include "display-types.h"
#include "core/gimp-utils.h"
#include "widgets/gimpwidgets-utils.h"
#include "gimpcanvashandle.h"
#include "gimpcanvasline.h"
#include "gimpcanvaspolygon.h"
#include "gimpdisplayshell.h"
#include "gimptoolpolygon.h"
#include "gimp-intl.h"
#define POINT_GRAB_THRESHOLD_SQ SQR (GIMP_CANVAS_HANDLE_SIZE_CIRCLE / 2)
#define POINT_SHOW_THRESHOLD_SQ SQR (GIMP_CANVAS_HANDLE_SIZE_CIRCLE * 7)
#define N_ITEMS_PER_ALLOC 1024
#define INVALID_INDEX (-1)
#define NO_CLICK_TIME_AVAILABLE 0
#if 0
enum
{
PROP_0,
PROP_X1,
PROP_Y1,
PROP_X2,
PROP_Y2
};
#endif
enum
{
COMMIT,
CANCEL,
LAST_SIGNAL
};
struct _GimpToolPolygonPrivate
{
/* Index of grabbed segment index. */
gint grabbed_segment_index;
/* We need to keep track of a number of points when we move a
* segment vertex
*/
GimpVector2 *saved_points_lower_segment;
GimpVector2 *saved_points_higher_segment;
gint max_n_saved_points_lower_segment;
gint max_n_saved_points_higher_segment;
/* Keeps track whether or not a modification of the polygon has been
* made between _button_press and _button_release
*/
gboolean polygon_modified;
/* Point which is used to draw the polygon but which is not part of
* it yet
*/
GimpVector2 pending_point;
gboolean show_pending_point;
/* The points of the polygon */
GimpVector2 *points;
gint max_n_points;
/* The number of points actually in use */
gint n_points;
/* Any int array containing the indices for the points in the
* polygon that connects different segments together
*/
gint *segment_indices;
gint max_n_segment_indices;
/* The number of segment indices actually in use */
gint n_segment_indices;
/* The selection operation active when the tool was started */
GimpChannelOps operation_at_start;
/* Whether or not to constrain the angle for newly created polygonal
* segments.
*/
gboolean constrain_angle;
/* Whether or not to suppress handles (so that new segments can be
* created immediately after an existing segment vertex.
*/
gboolean supress_handles;
/* Last _oper_update or _motion coords */
GimpVector2 last_coords;
/* A double-click commits the selection, keep track of last
* click-time and click-position.
*/
guint32 last_click_time;
GimpCoords last_click_coord;
/* Are we in a click-drag-release? */
gboolean button_down;
GimpCanvasItem *polygon;
GimpCanvasItem *pending_line;
GPtrArray *handles;
};
/* local function prototypes */
static void gimp_tool_polygon_constructed (GObject *object);
static void gimp_tool_polygon_finalize (GObject *object);
static void gimp_tool_polygon_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_tool_polygon_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_tool_polygon_changed (GimpToolWidget *widget);
static gint gimp_tool_polygon_button_press (GimpToolWidget *widget,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonPressType press_type);
static void gimp_tool_polygon_button_release (GimpToolWidget *widget,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpButtonReleaseType release_type);
static void gimp_tool_polygon_motion (GimpToolWidget *widget,
const GimpCoords *coords,
guint32 time,
GdkModifierType state);
static void gimp_tool_polygon_hover (GimpToolWidget *widget,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity);
static gboolean gimp_tool_polygon_key_press (GimpToolWidget *widget,
GdkEventKey *kevent);
static void gimp_tool_polygon_motion_modifier (GimpToolWidget *widget,
GdkModifierType key,
gboolean press,
GdkModifierType state);
static void gimp_tool_polygon_hover_modifier (GimpToolWidget *widget,
GdkModifierType key,
gboolean press,
GdkModifierType state);
static gboolean gimp_tool_polygon_get_cursor (GimpToolWidget *widget,
const GimpCoords *coords,
GdkModifierType state,
GimpCursorType *cursor,
GimpToolCursorType *tool_cursor,
GimpCursorModifier *cursor_modifier);
G_DEFINE_TYPE (GimpToolPolygon, gimp_tool_polygon, GIMP_TYPE_TOOL_WIDGET)
#define parent_class gimp_tool_polygon_parent_class
static guint polygon_signals[LAST_SIGNAL] = { 0 };
static const GimpVector2 vector2_zero = { 0.0, 0.0 };
static void
gimp_tool_polygon_class_init (GimpToolPolygonClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpToolWidgetClass *widget_class = GIMP_TOOL_WIDGET_CLASS (klass);
object_class->constructed = gimp_tool_polygon_constructed;
object_class->finalize = gimp_tool_polygon_finalize;
object_class->set_property = gimp_tool_polygon_set_property;
object_class->get_property = gimp_tool_polygon_get_property;
widget_class->changed = gimp_tool_polygon_changed;
widget_class->button_press = gimp_tool_polygon_button_press;
widget_class->button_release = gimp_tool_polygon_button_release;
widget_class->motion = gimp_tool_polygon_motion;
widget_class->hover = gimp_tool_polygon_hover;
widget_class->key_press = gimp_tool_polygon_key_press;
widget_class->motion_modifier = gimp_tool_polygon_motion_modifier;
widget_class->hover_modifier = gimp_tool_polygon_hover_modifier;
widget_class->get_cursor = gimp_tool_polygon_get_cursor;
polygon_signals[COMMIT] =
g_signal_new ("commit",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpToolPolygonClass, commit),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
polygon_signals[CANCEL] =
g_signal_new ("cancel",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpToolPolygonClass, cancel),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
#if 0
g_object_class_install_property (object_class, PROP_X1,
g_param_spec_double ("x1", NULL, NULL,
-GIMP_MAX_IMAGE_SIZE,
GIMP_MAX_IMAGE_SIZE, 0,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class, PROP_Y1,
g_param_spec_double ("y1", NULL, NULL,
-GIMP_MAX_IMAGE_SIZE,
GIMP_MAX_IMAGE_SIZE, 0,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class, PROP_X2,
g_param_spec_double ("x2", NULL, NULL,
-GIMP_MAX_IMAGE_SIZE,
GIMP_MAX_IMAGE_SIZE, 0,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class, PROP_Y2,
g_param_spec_double ("y2", NULL, NULL,
-GIMP_MAX_IMAGE_SIZE,
GIMP_MAX_IMAGE_SIZE, 0,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT));
#endif
g_type_class_add_private (klass, sizeof (GimpToolPolygonPrivate));
}
static void
gimp_tool_polygon_init (GimpToolPolygon *polygon)
{
polygon->private = G_TYPE_INSTANCE_GET_PRIVATE (polygon,
GIMP_TYPE_TOOL_POLYGON,
GimpToolPolygonPrivate);
polygon->private->grabbed_segment_index = INVALID_INDEX;
polygon->private->last_click_time = NO_CLICK_TIME_AVAILABLE;
}
static void
gimp_tool_polygon_constructed (GObject *object)
{
GimpToolPolygon *polygon = GIMP_TOOL_POLYGON (object);
GimpToolWidget *widget = GIMP_TOOL_WIDGET (object);
GimpToolPolygonPrivate *private = polygon->private;
GimpCanvasGroup *stroke_group;
G_OBJECT_CLASS (parent_class)->constructed (object);
stroke_group = gimp_tool_widget_add_stroke_group (widget);
gimp_tool_widget_push_group (widget, stroke_group);
private->polygon = gimp_tool_widget_add_polygon (widget, NULL,
NULL, 0, FALSE);
private->pending_line = gimp_tool_widget_add_line (widget, 0, 0, 0, 0);
gimp_tool_widget_pop_group (widget);
private->handles = g_ptr_array_new ();
gimp_tool_polygon_changed (widget);
}
static void
gimp_tool_polygon_finalize (GObject *object)
{
GimpToolPolygon *polygon = GIMP_TOOL_POLYGON (object);
GimpToolPolygonPrivate *private = polygon->private;
g_free (private->points);
g_free (private->segment_indices);
g_free (private->saved_points_lower_segment);
g_free (private->saved_points_higher_segment);
if (private->handles)
{
g_ptr_array_unref (private->handles);
private->handles = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_tool_polygon_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
switch (property_id)
{
#if 0
case PROP_X1:
private->x1 = g_value_get_double (value);
break;
case PROP_Y1:
private->y1 = g_value_get_double (value);
break;
case PROP_X2:
private->x2 = g_value_get_double (value);
break;
case PROP_Y2:
private->y2 = g_value_get_double (value);
break;
#endif
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_tool_polygon_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
switch (property_id)
{
#if 0
case PROP_X1:
g_value_set_double (value, private->x1);
break;
case PROP_Y1:
g_value_set_double (value, private->y1);
break;
case PROP_X2:
g_value_set_double (value, private->x2);
break;
case PROP_Y2:
g_value_set_double (value, private->y2);
break;
#endif
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_tool_polygon_get_segment (GimpToolPolygon *polygon,
GimpVector2 **points,
gint *n_points,
gint segment_start,
gint segment_end)
{
GimpToolPolygonPrivate *priv = polygon->private;
*points = &priv->points[priv->segment_indices[segment_start]];
*n_points = priv->segment_indices[segment_end] -
priv->segment_indices[segment_start] +
1;
}
static void
gimp_tool_polygon_get_segment_point (GimpToolPolygon *polygon,
gdouble *start_point_x,
gdouble *start_point_y,
gint segment_index)
{
GimpToolPolygonPrivate *priv = polygon->private;
*start_point_x = priv->points[priv->segment_indices[segment_index]].x;
*start_point_y = priv->points[priv->segment_indices[segment_index]].y;
}
static gboolean
gimp_tool_polygon_should_close (GimpToolPolygon *polygon,
guint32 time,
const GimpCoords *coords)
{
GimpToolWidget *widget = GIMP_TOOL_WIDGET (polygon);
GimpToolPolygonPrivate *priv = polygon->private;
gboolean double_click = FALSE;
gdouble dist;
if (priv->polygon_modified ||
priv->n_segment_indices <= 0)
return FALSE;
dist = gimp_canvas_item_transform_distance_square (priv->polygon,
coords->x,
coords->y,
priv->points[0].x,
priv->points[0].y);
/* Handle double-click. It must be within GTK+ global double-click
* time since last click, and it must be within GTK+ global
* double-click distance away from the last point
*/
if (time != NO_CLICK_TIME_AVAILABLE)
{
GimpDisplayShell *shell = gimp_tool_widget_get_shell (widget);
GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (shell));
gint double_click_time;
gint double_click_distance;
gint click_time_passed;
gdouble dist_from_last_point;
click_time_passed = time - priv->last_click_time;
dist_from_last_point =
gimp_canvas_item_transform_distance_square (priv->polygon,
coords->x,
coords->y,
priv->last_click_coord.x,
priv->last_click_coord.y);
g_object_get (settings,
"gtk-double-click-time", &double_click_time,
"gtk-double-click-distance", &double_click_distance,
NULL);
double_click = click_time_passed < double_click_time &&
dist_from_last_point < double_click_distance;
}
return ((! priv->supress_handles && dist < POINT_GRAB_THRESHOLD_SQ) ||
double_click);
}
static void
gimp_tool_polygon_revert_to_last_segment (GimpToolPolygon *polygon)
{
GimpToolPolygonPrivate *priv = polygon->private;
priv->n_points = priv->segment_indices[priv->n_segment_indices - 1] + 1;
}
static void
gimp_tool_polygon_remove_last_segment (GimpToolPolygon *polygon)
{
GimpToolPolygonPrivate *priv = polygon->private;
if (priv->n_segment_indices > 0)
{
GimpCanvasItem *handle;
priv->n_segment_indices--;
handle = g_ptr_array_index (priv->handles,
priv->n_segment_indices);
gimp_tool_widget_remove_item (GIMP_TOOL_WIDGET (polygon), handle);
g_ptr_array_remove (priv->handles, handle);
}
if (priv->n_segment_indices <= 0)
{
priv->grabbed_segment_index = INVALID_INDEX;
priv->show_pending_point = FALSE;
priv->n_points = 0;
priv->n_segment_indices = 0;
g_signal_emit (polygon, polygon_signals[CANCEL], 0);
}
else
{
gimp_tool_polygon_revert_to_last_segment (polygon);
gimp_tool_polygon_changed (GIMP_TOOL_WIDGET (polygon));
}
}
static void
gimp_tool_polygon_add_point (GimpToolPolygon *polygon,
gdouble x,
gdouble y)
{
GimpToolPolygonPrivate *priv = polygon->private;
if (priv->n_points >= priv->max_n_points)
{
priv->max_n_points += N_ITEMS_PER_ALLOC;
priv->points = g_realloc (priv->points,
sizeof (GimpVector2) * priv->max_n_points);
}
priv->points[priv->n_points].x = x;
priv->points[priv->n_points].y = y;
priv->n_points++;
}
static void
gimp_tool_polygon_add_segment_index (GimpToolPolygon *polygon,
gint index)
{
GimpToolPolygonPrivate *priv = polygon->private;
if (priv->n_segment_indices >= priv->max_n_segment_indices)
{
priv->max_n_segment_indices += N_ITEMS_PER_ALLOC;
priv->segment_indices = g_realloc (priv->segment_indices,
sizeof (GimpVector2) *
priv->max_n_segment_indices);
}
priv->segment_indices[priv->n_segment_indices] = index;
g_ptr_array_add (priv->handles,
gimp_tool_widget_add_handle (GIMP_TOOL_WIDGET (polygon),
GIMP_HANDLE_CROSS,
0, 0,
GIMP_CANVAS_HANDLE_SIZE_CIRCLE,
GIMP_CANVAS_HANDLE_SIZE_CIRCLE,
GIMP_HANDLE_ANCHOR_CENTER));
priv->n_segment_indices++;
}
static gboolean
gimp_tool_polygon_is_point_grabbed (GimpToolPolygon *polygon)
{
GimpToolPolygonPrivate *priv = polygon->private;
return priv->grabbed_segment_index != INVALID_INDEX;
}
static void
gimp_tool_polygon_fit_segment (GimpToolPolygon *polygon,
GimpVector2 *dest_points,
GimpVector2 dest_start_target,
GimpVector2 dest_end_target,
const GimpVector2 *source_points,
gint n_points)
{
GimpVector2 origo_translation_offset;
GimpVector2 untranslation_offset;
gdouble rotation;
gdouble scale;
/* Handle some quick special cases */
if (n_points <= 0)
{
return;
}
else if (n_points == 1)
{
dest_points[0] = dest_end_target;
return;
}
else if (n_points == 2)
{
dest_points[0] = dest_start_target;
dest_points[1] = dest_end_target;
return;
}
/* Copy from source to dest; we work on the dest data */
memcpy (dest_points, source_points, sizeof (GimpVector2) * n_points);
/* Transform the destination end point */
{
GimpVector2 *dest_end;
GimpVector2 origo_translated_end_target;
gdouble target_rotation;
gdouble current_rotation;
gdouble target_length;
gdouble current_length;
dest_end = &dest_points[n_points - 1];
/* Transate to origin */
gimp_vector2_sub (&origo_translation_offset,
&vector2_zero,
&dest_points[0]);
gimp_vector2_add (dest_end,
dest_end,
&origo_translation_offset);
/* Calculate origo_translated_end_target */
gimp_vector2_sub (&origo_translated_end_target,
&dest_end_target,
&dest_start_target);
/* Rotate */
target_rotation = atan2 (vector2_zero.y - origo_translated_end_target.y,
vector2_zero.x - origo_translated_end_target.x);
current_rotation = atan2 (vector2_zero.y - dest_end->y,
vector2_zero.x - dest_end->x);
rotation = current_rotation - target_rotation;
gimp_vector2_rotate (dest_end, rotation);
/* Scale */
target_length = gimp_vector2_length (&origo_translated_end_target);
current_length = gimp_vector2_length (dest_end);
scale = target_length / current_length;
gimp_vector2_mul (dest_end, scale);
/* Untranslate */
gimp_vector2_sub (&untranslation_offset,
&dest_end_target,
dest_end);
gimp_vector2_add (dest_end,
dest_end,
&untranslation_offset);
}
/* Do the same transformation for the rest of the points */
{
gint i;
for (i = 0; i < n_points - 1; i++)
{
/* Translate */
gimp_vector2_add (&dest_points[i],
&origo_translation_offset,
&dest_points[i]);
/* Rotate */
gimp_vector2_rotate (&dest_points[i],
rotation);
/* Scale */
gimp_vector2_mul (&dest_points[i],
scale);
/* Untranslate */
gimp_vector2_add (&dest_points[i],
&dest_points[i],
&untranslation_offset);
}
}
}
static void
gimp_tool_polygon_move_segment_vertex_to (GimpToolPolygon *polygon,
gint segment_index,
gdouble new_x,
gdouble new_y)
{
GimpToolPolygonPrivate *priv = polygon->private;
GimpVector2 cursor_point = { new_x, new_y };
GimpVector2 *dest;
GimpVector2 *dest_start_target;
GimpVector2 *dest_end_target;
gint n_points;
/* Handle the segment before the grabbed point */
if (segment_index > 0)
{
gimp_tool_polygon_get_segment (polygon,
&dest,
&n_points,
priv->grabbed_segment_index - 1,
priv->grabbed_segment_index);
dest_start_target = &dest[0];
dest_end_target = &cursor_point;
gimp_tool_polygon_fit_segment (polygon,
dest,
*dest_start_target,
*dest_end_target,
priv->saved_points_lower_segment,
n_points);
}