Commit 80f528a7 authored by Michael Natterer's avatar Michael Natterer 😴
Browse files

app: reorder and clean up stuff in the cage tool before starting fixes

parent c0444e30
...@@ -19,14 +19,10 @@ ...@@ -19,14 +19,10 @@
#include "config.h" #include "config.h"
#include <string.h>
#include <stdlib.h>
#include <gegl.h> #include <gegl.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h" #include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h" #include "libgimpwidgets/gimpwidgets.h"
...@@ -36,12 +32,9 @@ ...@@ -36,12 +32,9 @@
#include "operations/gimpcageconfig.h" #include "operations/gimpcageconfig.h"
#include "core/gimp.h"
#include "core/gimpchannel.h"
#include "core/gimpdrawable-shadow.h"
#include "core/gimpdrawablefilter.h" #include "core/gimpdrawablefilter.h"
#include "core/gimpimage.h" #include "core/gimpimage.h"
#include "core/gimplayer.h" #include "core/gimpitem.h"
#include "core/gimpprogress.h" #include "core/gimpprogress.h"
#include "core/gimpprojection.h" #include "core/gimpprojection.h"
...@@ -75,12 +68,9 @@ enum ...@@ -75,12 +68,9 @@ enum
}; };
static void gimp_cage_tool_start (GimpCageTool *ct, static void gimp_cage_tool_control (GimpTool *tool,
GimpToolAction action,
GimpDisplay *display); GimpDisplay *display);
static void gimp_cage_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec);
static void gimp_cage_tool_button_press (GimpTool *tool, static void gimp_cage_tool_button_press (GimpTool *tool,
const GimpCoords *coords, const GimpCoords *coords,
guint32 time, guint32 time,
...@@ -93,16 +83,13 @@ static void gimp_cage_tool_button_release (GimpTool *tool ...@@ -93,16 +83,13 @@ static void gimp_cage_tool_button_release (GimpTool *tool
GdkModifierType state, GdkModifierType state,
GimpButtonReleaseType release_type, GimpButtonReleaseType release_type,
GimpDisplay *display); GimpDisplay *display);
static gboolean gimp_cage_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display);
static void gimp_cage_tool_motion (GimpTool *tool, static void gimp_cage_tool_motion (GimpTool *tool,
const GimpCoords *coords, const GimpCoords *coords,
guint32 time, guint32 time,
GdkModifierType state, GdkModifierType state,
GimpDisplay *display); GimpDisplay *display);
static void gimp_cage_tool_control (GimpTool *tool, static gboolean gimp_cage_tool_key_press (GimpTool *tool,
GimpToolAction action, GdkEventKey *kevent,
GimpDisplay *display); GimpDisplay *display);
static void gimp_cage_tool_cursor_update (GimpTool *tool, static void gimp_cage_tool_cursor_update (GimpTool *tool,
const GimpCoords *coords, const GimpCoords *coords,
...@@ -113,9 +100,14 @@ static void gimp_cage_tool_oper_update (GimpTool *tool ...@@ -113,9 +100,14 @@ static void gimp_cage_tool_oper_update (GimpTool *tool
GdkModifierType state, GdkModifierType state,
gboolean proximity, gboolean proximity,
GimpDisplay *display); GimpDisplay *display);
static void gimp_cage_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec);
static void gimp_cage_tool_draw (GimpDrawTool *draw_tool); static void gimp_cage_tool_draw (GimpDrawTool *draw_tool);
static void gimp_cage_tool_start (GimpCageTool *ct,
GimpDisplay *display);
static void gimp_cage_tool_halt (GimpCageTool *ct); static void gimp_cage_tool_halt (GimpCageTool *ct);
static void gimp_cage_tool_commit (GimpCageTool *ct); static void gimp_cage_tool_commit (GimpCageTool *ct);
...@@ -133,8 +125,7 @@ static gint gimp_cage_tool_is_on_edge (GimpCageTool *ct, ...@@ -133,8 +125,7 @@ static gint gimp_cage_tool_is_on_edge (GimpCageTool *ct,
static gboolean gimp_cage_tool_is_complete (GimpCageTool *ct); static gboolean gimp_cage_tool_is_complete (GimpCageTool *ct);
static void gimp_cage_tool_remove_last_handle (GimpCageTool *ct); static void gimp_cage_tool_remove_last_handle (GimpCageTool *ct);
static void gimp_cage_tool_compute_coef (GimpCageTool *ct); static void gimp_cage_tool_compute_coef (GimpCageTool *ct);
static void gimp_cage_tool_create_filter (GimpCageTool *ct, static void gimp_cage_tool_create_filter (GimpCageTool *ct);
GimpDrawable *drawable);
static void gimp_cage_tool_filter_flush (GimpDrawableFilter *filter, static void gimp_cage_tool_filter_flush (GimpDrawableFilter *filter,
GimpTool *tool); GimpTool *tool);
static void gimp_cage_tool_filter_update (GimpCageTool *ct); static void gimp_cage_tool_filter_update (GimpCageTool *ct);
...@@ -171,14 +162,14 @@ gimp_cage_tool_class_init (GimpCageToolClass *klass) ...@@ -171,14 +162,14 @@ gimp_cage_tool_class_init (GimpCageToolClass *klass)
GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass); GimpToolClass *tool_class = GIMP_TOOL_CLASS (klass);
GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass); GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
tool_class->options_notify = gimp_cage_tool_options_notify; tool_class->control = gimp_cage_tool_control;
tool_class->button_press = gimp_cage_tool_button_press; tool_class->button_press = gimp_cage_tool_button_press;
tool_class->button_release = gimp_cage_tool_button_release; tool_class->button_release = gimp_cage_tool_button_release;
tool_class->key_press = gimp_cage_tool_key_press; tool_class->key_press = gimp_cage_tool_key_press;
tool_class->motion = gimp_cage_tool_motion; tool_class->motion = gimp_cage_tool_motion;
tool_class->control = gimp_cage_tool_control;
tool_class->cursor_update = gimp_cage_tool_cursor_update; tool_class->cursor_update = gimp_cage_tool_cursor_update;
tool_class->oper_update = gimp_cage_tool_oper_update; tool_class->oper_update = gimp_cage_tool_oper_update;
tool_class->options_notify = gimp_cage_tool_options_notify;
draw_tool_class->draw = gimp_cage_tool_draw; draw_tool_class->draw = gimp_cage_tool_draw;
} }
...@@ -229,270 +220,6 @@ gimp_cage_tool_control (GimpTool *tool, ...@@ -229,270 +220,6 @@ gimp_cage_tool_control (GimpTool *tool,
GIMP_TOOL_CLASS (parent_class)->control (tool, action, display); GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
} }
static void
gimp_cage_tool_start (GimpCageTool *ct,
GimpDisplay *display)
{
GimpTool *tool = GIMP_TOOL (ct);
GimpImage *image = gimp_display_get_image (display);
GimpDrawable *drawable = gimp_image_get_active_drawable (image);
gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
tool->display = display;
if (ct->config)
{
g_object_unref (ct->config);
ct->config = NULL;
}
if (ct->coef)
{
g_object_unref (ct->coef);
ct->dirty_coef = TRUE;
ct->coef = NULL;
}
if (ct->filter)
{
gimp_drawable_filter_abort (ct->filter);
g_object_unref (ct->filter);
ct->filter = NULL;
}
if (ct->render_node)
{
g_object_unref (ct->render_node);
ct->render_node = NULL;
ct->coef_node = NULL;
ct->cage_node = NULL;
}
ct->config = g_object_new (GIMP_TYPE_CAGE_CONFIG, NULL);
ct->hovering_handle = -1;
ct->hovering_edge = -1;
ct->tool_state = CAGE_STATE_INIT;
/* Setting up cage offset to convert the cage point coords to
* drawable coords
*/
gimp_item_get_offset (GIMP_ITEM (drawable),
&ct->offset_x, &ct->offset_y);
gimp_draw_tool_start (GIMP_DRAW_TOOL (ct), display);
}
static void
gimp_cage_tool_options_notify (GimpTool *tool,
GimpToolOptions *options,
const GParamSpec *pspec)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GIMP_TOOL_CLASS (parent_class)->options_notify (tool, options, pspec);
if (! tool->display)
return;
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
if (strcmp (pspec->name, "cage-mode") == 0)
{
GimpCageMode mode;
g_object_get (options,
"cage-mode", &mode,
NULL);
if (mode == GIMP_CAGE_MODE_DEFORM)
{
/* switch to deform mode */
if (gimp_cage_config_get_n_points (ct->config) > 2)
{
gimp_cage_config_reset_displacement (ct->config);
gimp_cage_config_reverse_cage_if_needed (ct->config);
gimp_tool_push_status (tool, tool->display,
_("Press ENTER to commit the transform"));
ct->tool_state = DEFORM_STATE_WAIT;
if (! ct->render_node)
{
gimp_cage_tool_create_render_node (ct);
}
if (ct->dirty_coef)
{
gimp_cage_tool_compute_coef (ct);
gimp_cage_tool_render_node_update (ct);
}
if (! ct->filter)
{
GimpImage *image = gimp_display_get_image (tool->display);
GimpDrawable *drawable = gimp_image_get_active_drawable (image);
gimp_cage_tool_create_filter (ct, drawable);
}
gimp_cage_tool_filter_update (ct);
}
else
{
g_object_set (options,
"cage-mode", GIMP_CAGE_MODE_CAGE_CHANGE,
NULL);
}
}
else
{
/* switch to edit mode */
if (ct->filter)
{
gimp_drawable_filter_abort (ct->filter);
gimp_tool_pop_status (tool, tool->display);
ct->tool_state = CAGE_STATE_WAIT;
}
}
}
else if (strcmp (pspec->name, "fill-plain-color") == 0)
{
if (ct->tool_state == DEFORM_STATE_WAIT)
{
gimp_cage_tool_render_node_update (ct);
gimp_cage_tool_filter_update (ct);
}
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static gboolean
gimp_cage_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
if (! ct->config)
return FALSE;
switch (kevent->keyval)
{
case GDK_KEY_BackSpace:
if (ct->tool_state == CAGE_STATE_WAIT)
{
if (gimp_cage_config_get_n_points (ct->config) != 0)
gimp_cage_tool_remove_last_handle (ct);
}
else if (ct->tool_state == DEFORM_STATE_WAIT)
{
gimp_cage_config_remove_selected_points (ct->config);
/* if the cage have less than 3 handles, we reopen it */
if (gimp_cage_config_get_n_points (ct->config) <= 2)
{
ct->tool_state = CAGE_STATE_WAIT;
}
gimp_cage_tool_compute_coef (ct);
gimp_cage_tool_render_node_update (ct);
}
return TRUE;
case GDK_KEY_Return:
case GDK_KEY_KP_Enter:
case GDK_KEY_ISO_Enter:
if (! gimp_cage_tool_is_complete (ct) &&
gimp_cage_config_get_n_points (ct->config) > 2)
{
g_object_set (gimp_tool_get_options (tool),
"cage-mode", GIMP_CAGE_MODE_DEFORM,
NULL);
}
else if (ct->tool_state == DEFORM_STATE_WAIT)
{
gimp_tool_control (tool, GIMP_TOOL_ACTION_COMMIT, display);
gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
}
return TRUE;
case GDK_KEY_Escape:
gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
return TRUE;
default:
break;
}
return FALSE;
}
static void
gimp_cage_tool_motion (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
ct->cursor_x = coords->x;
ct->cursor_y = coords->y;
switch (ct->tool_state)
{
case CAGE_STATE_MOVE_HANDLE:
case CAGE_STATE_CLOSING:
case DEFORM_STATE_MOVE_HANDLE:
gimp_cage_config_add_displacement (ct->config,
options->cage_mode,
ct->cursor_x - ct->movement_start_x,
ct->cursor_y - ct->movement_start_y);
break;
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static void
gimp_cage_tool_oper_update (GimpTool *tool,
const GimpCoords *coords,
GdkModifierType state,
gboolean proximity,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
if (ct->config)
{
ct->hovering_handle = gimp_cage_tool_is_on_handle (ct,
draw_tool,
display,
coords->x,
coords->y,
GIMP_TOOL_HANDLE_SIZE_CIRCLE);
ct->hovering_edge = gimp_cage_tool_is_on_edge (ct,
coords->x,
coords->y,
GIMP_TOOL_HANDLE_SIZE_CIRCLE);
}
gimp_draw_tool_pause (draw_tool);
ct->cursor_x = coords->x;
ct->cursor_y = coords->y;
gimp_draw_tool_resume (draw_tool);
}
static void static void
gimp_cage_tool_button_press (GimpTool *tool, gimp_cage_tool_button_press (GimpTool *tool,
const GimpCoords *coords, const GimpCoords *coords,
...@@ -507,7 +234,12 @@ gimp_cage_tool_button_press (GimpTool *tool, ...@@ -507,7 +234,12 @@ gimp_cage_tool_button_press (GimpTool *tool,
gint edge = -1; gint edge = -1;
if (display != tool->display) if (display != tool->display)
gimp_cage_tool_start (ct, display); {
if (tool->display)
gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, tool->display);
gimp_cage_tool_start (ct, display);
}
gimp_tool_control_activate (tool->control); gimp_tool_control_activate (tool->control);
...@@ -672,89 +404,217 @@ gimp_cage_tool_button_release (GimpTool *tool, ...@@ -672,89 +404,217 @@ gimp_cage_tool_button_release (GimpTool *tool,
ct->tool_state = DEFORM_STATE_WAIT; ct->tool_state = DEFORM_STATE_WAIT;
break; break;
case DEFORM_STATE_SELECTING: case DEFORM_STATE_SELECTING:
ct->tool_state = DEFORM_STATE_WAIT; ct->tool_state = DEFORM_STATE_WAIT;
break; break;
}
gimp_cage_config_reset_displacement (ct->config);
}
else
{
/* Normal release */
switch (ct->tool_state)
{
case CAGE_STATE_CLOSING:
ct->dirty_coef = TRUE;
gimp_cage_config_commit_displacement (ct->config);
if (release_type == GIMP_BUTTON_RELEASE_CLICK)
g_object_set (options, "cage-mode", GIMP_CAGE_MODE_DEFORM, NULL);
break;
case CAGE_STATE_MOVE_HANDLE:
ct->dirty_coef = TRUE;
ct->tool_state = CAGE_STATE_WAIT;
gimp_cage_config_commit_displacement (ct->config);
break;
case CAGE_STATE_SELECTING:
{
GeglRectangle area =
{ MIN (ct->selection_start_x, coords->x) - ct->offset_x,
MIN (ct->selection_start_y, coords->y) - ct->offset_y,
ABS (ct->selection_start_x - coords->x),
ABS (ct->selection_start_y - coords->y) };
if (state & gimp_get_extend_selection_mask ())
{
gimp_cage_config_select_add_area (ct->config,
GIMP_CAGE_MODE_CAGE_CHANGE,
area);
}
else
{
gimp_cage_config_select_area (ct->config,
GIMP_CAGE_MODE_CAGE_CHANGE,
area);
}
ct->tool_state = CAGE_STATE_WAIT;
}
break;
case DEFORM_STATE_MOVE_HANDLE:
ct->tool_state = DEFORM_STATE_WAIT;
gimp_cage_config_commit_displacement (ct->config);
gegl_node_set (ct->cage_node,
"config", ct->config,
NULL);
gimp_cage_tool_filter_update (ct);
break;
case DEFORM_STATE_SELECTING:
{
GeglRectangle area =
{ MIN (ct->selection_start_x, coords->x) - ct->offset_x,
MIN (ct->selection_start_y, coords->y) - ct->offset_y,
ABS (ct->selection_start_x - coords->x),
ABS (ct->selection_start_y - coords->y) };
if (state & gimp_get_extend_selection_mask ())
{
gimp_cage_config_select_add_area (ct->config,
GIMP_CAGE_MODE_DEFORM, area);
}
else
{
gimp_cage_config_select_area (ct->config,
GIMP_CAGE_MODE_DEFORM, area);
}
ct->tool_state = DEFORM_STATE_WAIT;
}
break;
}
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static void
gimp_cage_tool_motion (GimpTool *tool,
const GimpCoords *coords,
guint32 time,
GdkModifierType state,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
ct->cursor_x = coords->x;
ct->cursor_y = coords->y;
switch (ct->tool_state)
{
case CAGE_STATE_MOVE_HANDLE:
case CAGE_STATE_CLOSING:
case DEFORM_STATE_MOVE_HANDLE:
gimp_cage_config_add_displacement (ct->config,
options->cage_mode,
ct->cursor_x - ct->movement_start_x,
ct->cursor_y - ct->movement_start_y);
break;
}
gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
}
static gboolean
gimp_cage_tool_key_press (GimpTool *tool,
GdkEventKey *kevent,
GimpDisplay *display)
{
GimpCageTool *ct = GIMP_CAGE_TOOL (tool);
if (! ct->config)
return FALSE;
switch (kevent->keyval)
{
case GDK_KEY_BackSpace:
if (ct->tool_state == CAGE_STATE_WAIT)
{
if (gimp_cage_config_get_n_points (ct->config) != 0)
gimp_cage_tool_remove_last_handle (ct);
}
else if (ct->tool_state == DEFORM_STATE_WAIT)
{
gimp_cage_config_remove_selected_points (ct->config);
/* if the cage have less than 3 handles, we reopen it */
if (gimp_cage_config_get_n_points (ct->config) <= 2)
{
ct->tool_state = CAGE_STATE_WAIT;
}
gimp_cage_tool_compute_coef (ct);
gimp_cage_tool_render_node_update (ct);
} }
return TRUE;
gimp_cage_config_reset_displacement (ct->config); case GDK_KEY_Return:
} case GDK_KEY_KP_Enter:
else case GDK_KEY_ISO_Enter:
{ if (! gimp_cage_tool_is_complete (ct) &&
/* Normal release */ gimp_cage_config_get_n_points (ct->config) > 2)
switch (ct->tool_state)
{ {
case CAGE_STATE_CLOSING: g_object_set (gimp_tool_get_options (tool),
ct->dirty_coef = TRUE; "cage-mode", GIMP_CAGE_MODE_DEFORM,