/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 *
 * pikawarptool.c
 * Copyright (C) 2011 Michael Muré <batolettre@gmail.com>
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gegl-plugin.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include "libpikamath/pikamath.h"
#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "config/pikadisplayconfig.h"
#include "config/pikaguiconfig.h"

#include "gegl/pika-gegl-apply-operation.h"

#include "core/pika.h"
#include "core/pikachannel.h"
#include "core/pikadrawablefilter.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikaprogress.h"
#include "core/pikaprojection.h"
#include "core/pikasubprogress.h"
#include "core/pikatoolinfo.h"

#include "widgets/pikahelp-ids.h"
#include "widgets/pikawidgets-utils.h"

#include "display/pikadisplay.h"

#include "pikawarptool.h"
#include "pikawarpoptions.h"
#include "pikatoolcontrol.h"
#include "pikatools-utils.h"

#include "pika-intl.h"


#define STROKE_TIMER_MAX_FPS 20
#define PREVIEW_SAMPLER      GEGL_SAMPLER_NEAREST


static void            pika_warp_tool_constructed               (GObject               *object);

static void            pika_warp_tool_control                   (PikaTool              *tool,
                                                                 PikaToolAction         action,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_button_press              (PikaTool              *tool,
                                                                 const PikaCoords      *coords,
                                                                 guint32                time,
                                                                 GdkModifierType        state,
                                                                 PikaButtonPressType    press_type,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_button_release            (PikaTool              *tool,
                                                                 const PikaCoords      *coords,
                                                                 guint32                time,
                                                                 GdkModifierType        state,
                                                                 PikaButtonReleaseType  release_type,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_motion                    (PikaTool              *tool,
                                                                 const PikaCoords      *coords,
                                                                 guint32                time,
                                                                 GdkModifierType        state,
                                                                 PikaDisplay           *display);
static gboolean        pika_warp_tool_key_press                 (PikaTool              *tool,
                                                                 GdkEventKey           *kevent,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_oper_update               (PikaTool              *tool,
                                                                 const PikaCoords      *coords,
                                                                 GdkModifierType        state,
                                                                 gboolean               proximity,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_cursor_update             (PikaTool              *tool,
                                                                 const PikaCoords      *coords,
                                                                 GdkModifierType        state,
                                                                 PikaDisplay           *display);
const gchar          * pika_warp_tool_can_undo                  (PikaTool              *tool,
                                                                 PikaDisplay           *display);
const gchar          * pika_warp_tool_can_redo                  (PikaTool              *tool,
                                                                 PikaDisplay           *display);
static gboolean        pika_warp_tool_undo                      (PikaTool              *tool,
                                                                 PikaDisplay           *display);
static gboolean        pika_warp_tool_redo                      (PikaTool              *tool,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_options_notify            (PikaTool              *tool,
                                                                 PikaToolOptions       *options,
                                                                 const GParamSpec      *pspec);

static void            pika_warp_tool_draw                      (PikaDrawTool          *draw_tool);

static void            pika_warp_tool_cursor_notify             (PikaDisplayConfig     *config,
                                                                 GParamSpec            *pspec,
                                                                 PikaWarpTool          *wt);

static gboolean        pika_warp_tool_can_stroke                (PikaWarpTool          *wt,
                                                                 PikaDisplay           *display,
                                                                 gboolean               show_message);

static gboolean        pika_warp_tool_start                     (PikaWarpTool          *wt,
                                                                 PikaDisplay           *display);
static void            pika_warp_tool_halt                      (PikaWarpTool          *wt);
static void            pika_warp_tool_commit                    (PikaWarpTool          *wt);

static void            pika_warp_tool_start_stroke_timer        (PikaWarpTool          *wt);
static void            pika_warp_tool_stop_stroke_timer         (PikaWarpTool          *wt);
static gboolean        pika_warp_tool_stroke_timer              (PikaWarpTool          *wt);

static void            pika_warp_tool_create_graph              (PikaWarpTool          *wt);
static void            pika_warp_tool_create_filter             (PikaWarpTool          *wt,
                                                                 PikaDrawable          *drawable);
static void            pika_warp_tool_set_sampler               (PikaWarpTool          *wt,
                                                                 gboolean               commit);
static GeglRectangle
                       pika_warp_tool_get_stroke_bounds         (GeglNode              *node);
static GeglRectangle   pika_warp_tool_get_node_bounds           (GeglNode              *node);
static void            pika_warp_tool_clear_node_bounds         (GeglNode              *node);
static GeglRectangle   pika_warp_tool_get_invalidated_by_change (PikaWarpTool          *wt,
                                                                 const GeglRectangle   *area);
static void            pika_warp_tool_update_bounds             (PikaWarpTool          *wt);
static void            pika_warp_tool_update_area               (PikaWarpTool          *wt,
                                                                 const GeglRectangle   *area,
                                                                 gboolean               synchronous);
static void            pika_warp_tool_update_stroke             (PikaWarpTool          *wt,
                                                                 GeglNode              *node);
static void            pika_warp_tool_stroke_append             (PikaWarpTool          *wt,
                                                                 gchar                  type,
                                                                 gdouble                x,
                                                                 gdouble                y);
static void            pika_warp_tool_filter_flush              (PikaDrawableFilter    *filter,
                                                                 PikaTool              *tool);
static void            pika_warp_tool_add_op                    (PikaWarpTool          *wt,
                                                                 GeglNode              *op);
static void            pika_warp_tool_remove_op                 (PikaWarpTool          *wt,
                                                                 GeglNode              *op);
static void            pika_warp_tool_free_op                   (GeglNode              *op);

static void            pika_warp_tool_animate                   (PikaWarpTool          *wt);


G_DEFINE_TYPE (PikaWarpTool, pika_warp_tool, PIKA_TYPE_DRAW_TOOL)

#define parent_class pika_warp_tool_parent_class


void
pika_warp_tool_register (PikaToolRegisterCallback  callback,
                         gpointer                  data)
{
  (* callback) (PIKA_TYPE_WARP_TOOL,
                PIKA_TYPE_WARP_OPTIONS,
                pika_warp_options_gui,
                0,
                "pika-warp-tool",
                _("Warp Transform"),
                _("Warp Transform: Deform with different tools"),
                N_("_Warp Transform"), "W",
                NULL, PIKA_HELP_TOOL_WARP,
                PIKA_ICON_TOOL_WARP,
                data);
}

static void
pika_warp_tool_class_init (PikaWarpToolClass *klass)
{
  GObjectClass      *object_class    = G_OBJECT_CLASS (klass);
  PikaToolClass     *tool_class      = PIKA_TOOL_CLASS (klass);
  PikaDrawToolClass *draw_tool_class = PIKA_DRAW_TOOL_CLASS (klass);

  object_class->constructed  = pika_warp_tool_constructed;

  tool_class->control        = pika_warp_tool_control;
  tool_class->button_press   = pika_warp_tool_button_press;
  tool_class->button_release = pika_warp_tool_button_release;
  tool_class->motion         = pika_warp_tool_motion;
  tool_class->key_press      = pika_warp_tool_key_press;
  tool_class->oper_update    = pika_warp_tool_oper_update;
  tool_class->cursor_update  = pika_warp_tool_cursor_update;
  tool_class->can_undo       = pika_warp_tool_can_undo;
  tool_class->can_redo       = pika_warp_tool_can_redo;
  tool_class->undo           = pika_warp_tool_undo;
  tool_class->redo           = pika_warp_tool_redo;
  tool_class->options_notify = pika_warp_tool_options_notify;

  draw_tool_class->draw      = pika_warp_tool_draw;
}

static void
pika_warp_tool_init (PikaWarpTool *self)
{
  PikaTool *tool = PIKA_TOOL (self);

  pika_tool_control_set_motion_mode     (tool->control, PIKA_MOTION_MODE_EXACT);
  pika_tool_control_set_scroll_lock     (tool->control, TRUE);
  pika_tool_control_set_preserve        (tool->control, FALSE);
  pika_tool_control_set_dirty_mask      (tool->control,
                                         PIKA_DIRTY_IMAGE           |
                                         PIKA_DIRTY_DRAWABLE        |
                                         PIKA_DIRTY_SELECTION       |
                                         PIKA_DIRTY_ACTIVE_DRAWABLE);
  pika_tool_control_set_dirty_action    (tool->control,
                                         PIKA_TOOL_ACTION_COMMIT);
  pika_tool_control_set_wants_click     (tool->control, TRUE);
  pika_tool_control_set_precision       (tool->control,
                                         PIKA_CURSOR_PRECISION_SUBPIXEL);
  pika_tool_control_set_tool_cursor     (tool->control,
                                         PIKA_TOOL_CURSOR_WARP);

  pika_tool_control_set_action_pixel_size (tool->control,
                                           "tools-warp-effect-pixel-size-set");
  pika_tool_control_set_action_size       (tool->control,
                                           "tools-warp-effect-size-set");
  pika_tool_control_set_action_hardness   (tool->control,
                                           "tools-warp-effect-hardness-set");

  self->show_cursor = TRUE;
  self->draw_brush  = TRUE;
  self->snap_brush  = FALSE;
}

static void
pika_warp_tool_constructed (GObject *object)
{
  PikaWarpTool      *wt   = PIKA_WARP_TOOL (object);
  PikaTool          *tool = PIKA_TOOL (object);
  PikaDisplayConfig *display_config;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  display_config = PIKA_DISPLAY_CONFIG (tool->tool_info->pika->config);

  wt->show_cursor = display_config->show_paint_tool_cursor;
  wt->draw_brush  = display_config->show_brush_outline;
  wt->snap_brush  = display_config->snap_brush_outline;

  g_signal_connect_object (display_config, "notify::show-paint-tool-cursor",
                           G_CALLBACK (pika_warp_tool_cursor_notify),
                           wt, 0);
  g_signal_connect_object (display_config, "notify::show-brush-outline",
                           G_CALLBACK (pika_warp_tool_cursor_notify),
                           wt, 0);
  g_signal_connect_object (display_config, "notify::snap-brush-outline",
                           G_CALLBACK (pika_warp_tool_cursor_notify),
                           wt, 0);
}

static void
pika_warp_tool_control (PikaTool       *tool,
                        PikaToolAction  action,
                        PikaDisplay    *display)
{
  PikaWarpTool *wt = PIKA_WARP_TOOL (tool);

  switch (action)
    {
    case PIKA_TOOL_ACTION_PAUSE:
    case PIKA_TOOL_ACTION_RESUME:
      break;

    case PIKA_TOOL_ACTION_HALT:
      pika_warp_tool_halt (wt);
      break;

    case PIKA_TOOL_ACTION_COMMIT:
      pika_warp_tool_commit (wt);
      break;
    }

  PIKA_TOOL_CLASS (parent_class)->control (tool, action, display);
}

static void
pika_warp_tool_button_press (PikaTool            *tool,
                             const PikaCoords    *coords,
                             guint32              time,
                             GdkModifierType      state,
                             PikaButtonPressType  press_type,
                             PikaDisplay         *display)
{
  PikaWarpTool    *wt      = PIKA_WARP_TOOL (tool);
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  GeglNode        *new_op;
  gint             off_x, off_y;

  if (tool->display && display != tool->display)
    pika_tool_control (tool, PIKA_TOOL_ACTION_HALT, tool->display);

  if (! tool->display)
    {
      if (! pika_warp_tool_start (wt, display))
        return;
    }

  if (! pika_warp_tool_can_stroke (wt, display, TRUE))
    return;

  g_return_if_fail (g_list_length (tool->drawables) == 1);

  wt->current_stroke = gegl_path_new ();

  wt->last_pos.x = coords->x;
  wt->last_pos.y = coords->y;

  wt->total_dist = 0.0;

  new_op = gegl_node_new_child (NULL,
                                "operation", "gegl:warp",
                                "behavior",  options->behavior,
                                "size",      options->effect_size,
                                "hardness",  options->effect_hardness / 100.0,
                                "strength",  options->effect_strength,
                                /* we implement spacing manually.
                                 * anything > 1 will do.
                                 */
                                "spacing",   10.0,
                                "stroke",    wt->current_stroke,
                                NULL);

  pika_warp_tool_add_op (wt, new_op);
  g_object_unref (new_op);

  pika_item_get_offset (PIKA_ITEM (tool->drawables->data), &off_x, &off_y);

  pika_warp_tool_stroke_append (wt,
                                'M', wt->last_pos.x - off_x,
                                     wt->last_pos.y - off_y);

  pika_warp_tool_start_stroke_timer (wt);

  pika_tool_control_activate (tool->control);
}

void
pika_warp_tool_button_release (PikaTool              *tool,
                               const PikaCoords      *coords,
                               guint32                time,
                               GdkModifierType        state,
                               PikaButtonReleaseType  release_type,
                               PikaDisplay           *display)
{
  PikaWarpTool *wt = PIKA_WARP_TOOL (tool);

  pika_draw_tool_pause (PIKA_DRAW_TOOL (wt));

  pika_tool_control_halt (tool->control);

  pika_warp_tool_stop_stroke_timer (wt);

#ifdef WARP_DEBUG
  g_printerr ("%s\n", gegl_path_to_string (wt->current_stroke));
#endif

  g_clear_object (&wt->current_stroke);

  if (release_type == PIKA_BUTTON_RELEASE_CANCEL)
    {
      pika_warp_tool_undo (tool, display);

      /*  the just undone stroke has no business on the redo stack  */
      pika_warp_tool_free_op (wt->redo_stack->data);
      wt->redo_stack = g_list_remove_link (wt->redo_stack, wt->redo_stack);
    }
  else
    {
      if (wt->redo_stack)
        {
          /*  the redo stack becomes invalid by actually doing a stroke  */
          g_list_free_full (wt->redo_stack,
                            (GDestroyNotify) pika_warp_tool_free_op);
          wt->redo_stack = NULL;
        }

      pika_tool_push_status (tool, tool->display,
                             _("Press ENTER to commit the transform"));
    }

  pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));

  /*  update the undo actions / menu items  */
  pika_image_flush (pika_display_get_image (PIKA_TOOL (wt)->display));
}

static void
pika_warp_tool_motion (PikaTool         *tool,
                       const PikaCoords *coords,
                       guint32           time,
                       GdkModifierType   state,
                       PikaDisplay      *display)
{
  PikaWarpTool    *wt             = PIKA_WARP_TOOL (tool);
  PikaWarpOptions *options        = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  PikaVector2      old_cursor_pos;
  PikaVector2      delta;
  gdouble          dist;
  gdouble          step;
  gboolean         stroke_changed = FALSE;

  if (! wt->snap_brush)
    pika_draw_tool_pause (PIKA_DRAW_TOOL (wt));

  old_cursor_pos = wt->cursor_pos;

  wt->cursor_pos.x = coords->x;
  wt->cursor_pos.y = coords->y;

  pika_vector2_sub (&delta, &wt->cursor_pos, &old_cursor_pos);
  dist = pika_vector2_length (&delta);

  step = options->effect_size * options->stroke_spacing / 100.0;

  while (wt->total_dist + dist >= step)
    {
      gdouble diff = step - wt->total_dist;

      pika_vector2_mul (&delta, diff / dist);
      pika_vector2_add (&old_cursor_pos, &old_cursor_pos, &delta);

      pika_vector2_sub (&delta, &wt->cursor_pos, &old_cursor_pos);
      dist -= diff;

      wt->last_pos   = old_cursor_pos;
      wt->total_dist = 0.0;

      if (options->stroke_during_motion)
        {
          gint off_x, off_y;

          if (! stroke_changed)
            {
              stroke_changed = TRUE;

              pika_draw_tool_pause (PIKA_DRAW_TOOL (tool));
            }

          pika_item_get_offset (PIKA_ITEM (tool->drawables->data), &off_x, &off_y);

          pika_warp_tool_stroke_append (wt,
                                        'L', wt->last_pos.x - off_x,
                                             wt->last_pos.y - off_y);
        }
    }

  wt->total_dist += dist;

  if (stroke_changed)
    {
      pika_warp_tool_start_stroke_timer (wt);

      pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));
    }

  if (! wt->snap_brush)
    pika_draw_tool_resume (PIKA_DRAW_TOOL (wt));
}

static gboolean
pika_warp_tool_key_press (PikaTool    *tool,
                          GdkEventKey *kevent,
                          PikaDisplay *display)
{
  switch (kevent->keyval)
    {
    case GDK_KEY_BackSpace:
      return TRUE;

    case GDK_KEY_Return:
    case GDK_KEY_KP_Enter:
    case GDK_KEY_ISO_Enter:
      pika_tool_control (tool, PIKA_TOOL_ACTION_COMMIT, display);
      return TRUE;

    case GDK_KEY_Escape:
      pika_tool_control (tool, PIKA_TOOL_ACTION_HALT, display);
      return TRUE;

    default:
      break;
    }

  return FALSE;
}

static void
pika_warp_tool_oper_update (PikaTool         *tool,
                            const PikaCoords *coords,
                            GdkModifierType   state,
                            gboolean          proximity,
                            PikaDisplay      *display)
{
  PikaWarpTool *wt        = PIKA_WARP_TOOL (tool);
  PikaDrawTool *draw_tool = PIKA_DRAW_TOOL (tool);

  if (proximity)
    {
      pika_draw_tool_pause (draw_tool);

      if (! tool->display || display == tool->display)
        {
          wt->cursor_pos.x = coords->x;
          wt->cursor_pos.y = coords->y;

          wt->last_pos = wt->cursor_pos;
        }

      if (! pika_draw_tool_is_active (draw_tool))
        pika_draw_tool_start (draw_tool, display);

      pika_draw_tool_resume (draw_tool);
    }
  else if (pika_draw_tool_is_active (draw_tool))
    {
      pika_draw_tool_stop (draw_tool);
    }
}

static void
pika_warp_tool_cursor_update (PikaTool         *tool,
                              const PikaCoords *coords,
                              GdkModifierType   state,
                              PikaDisplay      *display)
{
  PikaWarpTool       *wt       = PIKA_WARP_TOOL (tool);
  PikaWarpOptions    *options  = PIKA_WARP_TOOL_GET_OPTIONS (tool);
  PikaCursorModifier  modifier = PIKA_CURSOR_MODIFIER_NONE;

  if (! pika_warp_tool_can_stroke (wt, display, FALSE))
    {
      modifier = PIKA_CURSOR_MODIFIER_BAD;
    }
  else if (display == tool->display)
    {
#if 0
      /* FIXME have better cursors  */

      switch (options->behavior)
        {
        case PIKA_WARP_BEHAVIOR_MOVE:
        case PIKA_WARP_BEHAVIOR_GROW:
        case PIKA_WARP_BEHAVIOR_SHRINK:
        case PIKA_WARP_BEHAVIOR_SWIRL_CW:
        case PIKA_WARP_BEHAVIOR_SWIRL_CCW:
        case PIKA_WARP_BEHAVIOR_ERASE:
        case PIKA_WARP_BEHAVIOR_SMOOTH:
          modifier = PIKA_CURSOR_MODIFIER_MOVE;
          break;
        }
#else
      (void) options;
#endif
    }

  if (! wt->show_cursor && modifier != PIKA_CURSOR_MODIFIER_BAD)
    {
      pika_tool_set_cursor (tool, display,
                            PIKA_CURSOR_NONE,
                            PIKA_TOOL_CURSOR_NONE,
                            PIKA_CURSOR_MODIFIER_NONE);
      return;
    }

  pika_tool_control_set_cursor_modifier (tool->control, modifier);

  PIKA_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}

const gchar *
pika_warp_tool_can_undo (PikaTool    *tool,
                         PikaDisplay *display)
{
  PikaWarpTool *wt = PIKA_WARP_TOOL (tool);
  GeglNode     *to_delete;
  const gchar  *type;

  if (! wt->render_node)
    return NULL;

  to_delete = gegl_node_get_producer (wt->render_node, "aux", NULL);
  type = gegl_node_get_operation (to_delete);

  if (strcmp (type, "gegl:warp"))
    return NULL;

  return _("Warp Tool Stroke");
}

const gchar *
pika_warp_tool_can_redo (PikaTool    *tool,
                         PikaDisplay *display)
{
  PikaWarpTool *wt = PIKA_WARP_TOOL (tool);

  if (! wt->render_node || ! wt->redo_stack)
    return NULL;

  return _("Warp Tool Stroke");
}

static gboolean
pika_warp_tool_undo (PikaTool    *tool,
                     PikaDisplay *display)
{
  PikaWarpTool *wt = PIKA_WARP_TOOL (tool);
  GeglNode     *to_delete;
  GeglNode     *prev_node;

  to_delete = gegl_node_get_producer (wt->render_node, "aux", NULL);

  wt->redo_stack = g_list_prepend (wt->redo_stack, to_delete);

  /* we connect render_node to the previous node, but keep the current node
   * in the graph, connected to the previous node as well, so that it doesn't
   * get invalidated and maintains its cache.  this way, redoing it doesn't
   * require reprocessing.
   */
  prev_node = gegl_node_get_producer (to_delete, "input", NULL);

  gegl_node_connect (prev_node,       "output",
                     wt->render_node, "aux");

  pika_warp_tool_update_bounds (wt);
  pika_warp_tool_update_stroke (wt, to_delete);

  return TRUE;
}

static gboolean
pika_warp_tool_redo (PikaTool    *tool,
                     PikaDisplay *display)
{
  PikaWarpTool *wt = PIKA_WARP_TOOL (tool);
  GeglNode     *to_add;

  to_add = wt->redo_stack->data;

  gegl_node_connect (to_add,          "output",
                     wt->render_node, "aux");

  wt->redo_stack = g_list_remove_link (wt->redo_stack, wt->redo_stack);

  pika_warp_tool_update_bounds (wt);
  pika_warp_tool_update_stroke (wt, to_add);

  return TRUE;
}

static void
pika_warp_tool_options_notify (PikaTool         *tool,
                               PikaToolOptions  *options,
                               const GParamSpec *pspec)
{
  PikaWarpTool    *wt         = PIKA_WARP_TOOL (tool);
  PikaWarpOptions *wt_options = PIKA_WARP_OPTIONS (options);

  PIKA_TOOL_CLASS (parent_class)->options_notify (tool, options, pspec);

  if (! strcmp (pspec->name, "effect-size"))
    {
      pika_draw_tool_pause (PIKA_DRAW_TOOL (tool));
      pika_draw_tool_resume (PIKA_DRAW_TOOL (tool));
    }
  else if (! strcmp (pspec->name, "interpolation"))
    {
      pika_warp_tool_set_sampler (wt, /* commit = */ FALSE);
    }
  else if (! strcmp (pspec->name, "abyss-policy"))
    {
      if (wt->render_node)
        {
          gegl_node_set (wt->render_node,
                         "abyss-policy", wt_options->abyss_policy,
                         NULL);

          pika_warp_tool_update_stroke (wt, NULL);
        }
    }
  else if (! strcmp (pspec->name, "high-quality-preview"))
    {
      pika_warp_tool_set_sampler (wt, /* commit = */ FALSE);
    }
}

static void
pika_warp_tool_draw (PikaDrawTool *draw_tool)
{
  PikaWarpTool    *wt      = PIKA_WARP_TOOL (draw_tool);
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  gdouble          x, y;

  if (wt->snap_brush)
    {
      x = wt->last_pos.x;
      y = wt->last_pos.y;
    }
  else
    {
      x = wt->cursor_pos.x;
      y = wt->cursor_pos.y;
    }

  if (wt->draw_brush)
    {
      pika_draw_tool_add_arc (draw_tool,
                              FALSE,
                              x - options->effect_size * 0.5,
                              y - options->effect_size * 0.5,
                              options->effect_size,
                              options->effect_size,
                              0.0, 2.0 * G_PI);
    }
  else if (! wt->show_cursor)
    {
      /*  don't leave the user without any indication and draw
       *  a fallback crosshair
       */
      pika_draw_tool_add_handle (draw_tool,
                                 PIKA_HANDLE_CROSSHAIR,
                                 x, y,
                                 PIKA_TOOL_HANDLE_SIZE_CROSSHAIR,
                                 PIKA_TOOL_HANDLE_SIZE_CROSSHAIR,
                                 PIKA_HANDLE_ANCHOR_CENTER);
    }
}

static void
pika_warp_tool_cursor_notify (PikaDisplayConfig *config,
                              GParamSpec        *pspec,
                              PikaWarpTool      *wt)
{
  pika_draw_tool_pause (PIKA_DRAW_TOOL (wt));

  wt->show_cursor = config->show_paint_tool_cursor;
  wt->draw_brush  = config->show_brush_outline;
  wt->snap_brush  = config->snap_brush_outline;

  pika_draw_tool_resume (PIKA_DRAW_TOOL (wt));
}

static gboolean
pika_warp_tool_can_stroke (PikaWarpTool *wt,
                           PikaDisplay  *display,
                           gboolean      show_message)
{
  PikaTool        *tool        = PIKA_TOOL (wt);
  PikaWarpOptions *options     = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  PikaGuiConfig   *config      = PIKA_GUI_CONFIG (display->pika->config);
  PikaImage       *image       = pika_display_get_image (display);
  PikaItem        *locked_item = NULL;
  GList           *drawables   = pika_image_get_selected_drawables (image);
  PikaDrawable    *drawable;

  if (g_list_length (drawables) != 1)
    {
      if (show_message)
        {
          if (g_list_length (drawables) > 1)
            pika_tool_message_literal (tool, display,
                                       _("Cannot warp multiple layers. Select only one layer."));
          else
            pika_tool_message_literal (tool, display,
                                       _("No active drawables."));
        }

      g_list_free (drawables);

      return FALSE;
    }

  drawable = drawables->data;
  g_list_free (drawables);

  if (pika_viewable_get_children (PIKA_VIEWABLE (drawable)))
    {
      if (show_message)
        {
          pika_tool_message_literal (tool, display,
                                     _("Cannot warp layer groups."));
        }

      return FALSE;
    }

  if (pika_item_is_content_locked (PIKA_ITEM (drawable), &locked_item))
    {
      if (show_message)
        {
          pika_tool_message_literal (tool, display,
                                     _("The selected item's pixels are locked."));

          pika_tools_blink_lock_box (display->pika, locked_item);
        }

      return FALSE;
    }

  if (! pika_item_is_visible (PIKA_ITEM (drawable)) &&
      ! config->edit_non_visible)
    {
      if (show_message)
        {
          pika_tool_message_literal (tool, display,
                                     _("The selected item is not visible."));
        }

      return FALSE;
    }

  if (! options->stroke_during_motion &&
      ! options->stroke_periodically)
    {
      if (show_message)
        {
          pika_tool_message_literal (tool, display,
                                     _("No stroke events selected."));

          pika_tools_show_tool_options (display->pika);
          pika_widget_blink (options->stroke_frame);
        }

      return FALSE;
    }

  if (! wt->filter || ! pika_tool_can_undo (tool, display))
    {
      const gchar *message = NULL;

      switch (options->behavior)
        {
        case PIKA_WARP_BEHAVIOR_MOVE:
        case PIKA_WARP_BEHAVIOR_GROW:
        case PIKA_WARP_BEHAVIOR_SHRINK:
        case PIKA_WARP_BEHAVIOR_SWIRL_CW:
        case PIKA_WARP_BEHAVIOR_SWIRL_CCW:
          break;

        case PIKA_WARP_BEHAVIOR_ERASE:
          message = _("No warp to erase.");
          break;

        case PIKA_WARP_BEHAVIOR_SMOOTH:
          message = _("No warp to smooth.");
          break;
        }

      if (message)
        {
          if (show_message)
            {
              pika_tool_message_literal (tool, display, message);

              pika_tools_show_tool_options (display->pika);
              pika_widget_blink (options->behavior_combo);
            }

          return FALSE;
        }
    }

  return TRUE;
}

static gboolean
pika_warp_tool_start (PikaWarpTool *wt,
                      PikaDisplay  *display)
{
  PikaTool        *tool     = PIKA_TOOL (wt);
  PikaWarpOptions *options  = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  PikaImage       *image    = pika_display_get_image (display);
  PikaDrawable    *drawable;
  const Babl      *format;
  GeglRectangle    bbox;

  if (! pika_warp_tool_can_stroke (wt, display, TRUE))
    return FALSE;

  tool->display   = display;
  g_list_free (tool->drawables);
  tool->drawables = pika_image_get_selected_drawables (image);

  drawable = tool->drawables->data;

  /* Create the coords buffer, with the size of the selection */
  format = babl_format_n (babl_type ("float"), 2);

  pika_item_mask_intersect (PIKA_ITEM (drawable), &bbox.x, &bbox.y,
                            &bbox.width, &bbox.height);

#ifdef WARP_DEBUG
  g_printerr ("Initialize coordinate buffer (%d,%d) at %d,%d\n",
              bbox.width, bbox.height, bbox.x, bbox.y);
#endif

  wt->coords_buffer = gegl_buffer_new (&bbox, format);

  pika_warp_tool_create_filter (wt, drawable);

  if (! pika_draw_tool_is_active (PIKA_DRAW_TOOL (wt)))
    pika_draw_tool_start (PIKA_DRAW_TOOL (wt), display);

  if (options->animate_button)
    {
      g_signal_connect_swapped (options->animate_button, "clicked",
                                G_CALLBACK (pika_warp_tool_animate),
                                wt);

      gtk_widget_set_sensitive (options->animate_button, TRUE);
    }

  return TRUE;
}

static void
pika_warp_tool_halt (PikaWarpTool *wt)
{
  PikaTool        *tool    = PIKA_TOOL (wt);
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);

  g_clear_object (&wt->coords_buffer);

  g_clear_object (&wt->graph);
  wt->render_node = NULL;

  if (wt->filter)
    {
      pika_drawable_filter_abort (wt->filter);
      g_clear_object (&wt->filter);

      pika_image_flush (pika_display_get_image (tool->display));
    }

  if (wt->redo_stack)
    {
      g_list_free (wt->redo_stack);
      wt->redo_stack = NULL;
    }

  tool->display   = NULL;
  g_list_free (tool->drawables);
  tool->drawables = NULL;

  if (pika_draw_tool_is_active (PIKA_DRAW_TOOL (wt)))
    pika_draw_tool_stop (PIKA_DRAW_TOOL (wt));

  if (options->animate_button)
    {
      gtk_widget_set_sensitive (options->animate_button, FALSE);

      g_signal_handlers_disconnect_by_func (options->animate_button,
                                            pika_warp_tool_animate,
                                            wt);
    }
}

static void
pika_warp_tool_commit (PikaWarpTool *wt)
{
  PikaTool *tool = PIKA_TOOL (wt);

  /* don't commit a nop */
  if (tool->display && pika_tool_can_undo (tool, tool->display))
    {
      pika_tool_control_push_preserve (tool->control, TRUE);

      pika_warp_tool_set_sampler (wt, /* commit = */ TRUE);

      pika_drawable_filter_commit (wt->filter, PIKA_PROGRESS (tool), FALSE);
      g_clear_object (&wt->filter);

      pika_tool_control_pop_preserve (tool->control);

      pika_image_flush (pika_display_get_image (tool->display));
    }
}

static void
pika_warp_tool_start_stroke_timer (PikaWarpTool *wt)
{
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);

  pika_warp_tool_stop_stroke_timer (wt);

  if (options->stroke_periodically                    &&
      options->stroke_periodically_rate > 0.0         &&
      ! (options->behavior == PIKA_WARP_BEHAVIOR_MOVE &&
         options->stroke_during_motion))
    {
      gdouble fps;

      fps = STROKE_TIMER_MAX_FPS * options->stroke_periodically_rate / 100.0;

      wt->stroke_timer = g_timeout_add (1000.0 / fps,
                                        (GSourceFunc) pika_warp_tool_stroke_timer,
                                        wt);
    }
}

static void
pika_warp_tool_stop_stroke_timer (PikaWarpTool *wt)
{
  if (wt->stroke_timer)
    g_source_remove (wt->stroke_timer);

  wt->stroke_timer = 0;
}

static gboolean
pika_warp_tool_stroke_timer (PikaWarpTool *wt)
{
  PikaTool *tool = PIKA_TOOL (wt);
  gint      off_x, off_y;

  g_return_val_if_fail (g_list_length (tool->drawables) == 1, FALSE);

  pika_item_get_offset (PIKA_ITEM (tool->drawables->data), &off_x, &off_y);

  pika_warp_tool_stroke_append (wt,
                                'L', wt->last_pos.x - off_x,
                                     wt->last_pos.y - off_y);

  return TRUE;
}

static void
pika_warp_tool_create_graph (PikaWarpTool *wt)
{
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  GeglNode        *graph;           /* Wrapper to be returned */
  GeglNode        *input, *output;  /* Proxy nodes */
  GeglNode        *coords, *render; /* Render nodes */

  /* render_node is not supposed to be recreated */
  g_return_if_fail (wt->graph == NULL);

  graph = gegl_node_new ();

  input  = gegl_node_get_input_proxy  (graph, "input");
  output = gegl_node_get_output_proxy (graph, "output");

  coords = gegl_node_new_child (graph,
                               "operation", "gegl:buffer-source",
                               "buffer",    wt->coords_buffer,
                               NULL);

  render = gegl_node_new_child (graph,
                                "operation",    "gegl:map-relative",
                                "abyss-policy", options->abyss_policy,
                                NULL);

  gegl_node_link_many (input, render, output, NULL);
  gegl_node_connect (coords, "output",
                     render, "aux");


  wt->graph       = graph;
  wt->render_node = render;
}

static void
pika_warp_tool_create_filter (PikaWarpTool *wt,
                              PikaDrawable *drawable)
{
  if (! wt->graph)
    pika_warp_tool_create_graph (wt);

  pika_warp_tool_set_sampler (wt, /* commit = */ FALSE);

  wt->filter = pika_drawable_filter_new (drawable,
                                         _("Warp transform"),
                                         wt->graph,
                                         PIKA_ICON_TOOL_WARP);

  pika_drawable_filter_set_region (wt->filter, PIKA_FILTER_REGION_DRAWABLE);

#if 0
  g_object_set (wt->filter, "gegl-caching", TRUE, NULL);
#endif

  g_signal_connect (wt->filter, "flush",
                    G_CALLBACK (pika_warp_tool_filter_flush),
                    wt);
}

static void
pika_warp_tool_set_sampler (PikaWarpTool *wt,
                            gboolean      commit)
{
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  GeglSamplerType  sampler;
  GeglSamplerType  old_sampler;

  if (! wt->render_node)
    return;

  if (commit || options->high_quality_preview)
    sampler = (GeglSamplerType) options->interpolation;
  else
    sampler = PREVIEW_SAMPLER;

  gegl_node_get (wt->render_node,
                 "sampler-type", &old_sampler,
                 NULL);

  if (sampler != old_sampler)
    {
      gegl_node_set (wt->render_node,
                     "sampler-type", sampler,
                     NULL);

      pika_warp_tool_update_bounds (wt);
      pika_warp_tool_update_stroke (wt, NULL);
    }
}

static GeglRectangle
pika_warp_tool_get_stroke_bounds (GeglNode *node)
{
  GeglRectangle  bbox = {0, 0, 0, 0};
  GeglPath      *stroke;
  gdouble        size;

  gegl_node_get (node,
                 "stroke", &stroke,
                 "size",   &size,
                 NULL);

  if (stroke)
    {
      gdouble        min_x;
      gdouble        max_x;
      gdouble        min_y;
      gdouble        max_y;

      gegl_path_get_bounds (stroke, &min_x, &max_x, &min_y, &max_y);
      g_object_unref (stroke);

      bbox.x      = floor (min_x - size * 0.5);
      bbox.y      = floor (min_y - size * 0.5);
      bbox.width  = ceil (max_x + size * 0.5) - bbox.x;
      bbox.height = ceil (max_y + size * 0.5) - bbox.y;
    }

  return bbox;
}

static GeglRectangle
pika_warp_tool_get_node_bounds (GeglNode *node)
{
  GeglRectangle *bounds;

  if (! node || strcmp (gegl_node_get_operation (node), "gegl:warp"))
    return *GEGL_RECTANGLE (0, 0, 0, 0);

  bounds = g_object_get_data (G_OBJECT (node), "pika-warp-tool-bounds");

  if (! bounds)
    {
      GeglNode      *input_node;
      GeglRectangle  input_bounds;
      GeglRectangle  stroke_bounds;

      input_node   = gegl_node_get_producer (node, "input", NULL);
      input_bounds = pika_warp_tool_get_node_bounds (input_node);

      stroke_bounds = pika_warp_tool_get_stroke_bounds (node);

      gegl_rectangle_bounding_box (&input_bounds,
                                   &input_bounds, &stroke_bounds);

      bounds = gegl_rectangle_dup (&input_bounds);

      g_object_set_data_full (G_OBJECT (node), "pika-warp-tool-bounds",
                              bounds, g_free);
    }

  return *bounds;
}

static void
pika_warp_tool_clear_node_bounds (GeglNode *node)
{
  if (node && ! strcmp (gegl_node_get_operation (node), "gegl:warp"))
    g_object_set_data (G_OBJECT (node), "pika-warp-tool-bounds", NULL);
}

static GeglRectangle
pika_warp_tool_get_invalidated_by_change (PikaWarpTool        *wt,
                                          const GeglRectangle *area)
{
  GeglRectangle result = *area;

  if (! wt->filter)
    return result;

  if (wt->render_node)
    {
      GeglOperation *operation = gegl_node_get_gegl_operation (wt->render_node);

      result = gegl_operation_get_invalidated_by_change (operation,
                                                         "aux", area);
    }

  return result;
}

static void
pika_warp_tool_update_bounds (PikaWarpTool *wt)
{
  GeglRectangle bounds = {0, 0, 0, 0};

  if (! wt->filter)
    return;

  if (wt->render_node)
    {
      GeglNode *node = gegl_node_get_producer (wt->render_node, "aux", NULL);

      bounds = pika_warp_tool_get_node_bounds (node);

      bounds = pika_warp_tool_get_invalidated_by_change (wt, &bounds);
    }

  pika_drawable_filter_set_crop (wt->filter, &bounds, FALSE);
}

static void
pika_warp_tool_update_area (PikaWarpTool        *wt,
                            const GeglRectangle *area,
                            gboolean             synchronous)
{
  GeglRectangle rect;

  if (! wt->filter)
    return;

  rect = pika_warp_tool_get_invalidated_by_change (wt, area);

  if (synchronous)
    {
      PikaTool  *tool  = PIKA_TOOL (wt);
      PikaImage *image = pika_display_get_image (tool->display);

      g_signal_handlers_block_by_func (wt->filter,
                                       pika_warp_tool_filter_flush,
                                       wt);

      pika_drawable_filter_apply (wt->filter, &rect);

      pika_projection_flush_now (pika_image_get_projection (image), TRUE);
      pika_display_flush_now (tool->display);

      g_signal_handlers_unblock_by_func (wt->filter,
                                         pika_warp_tool_filter_flush,
                                         wt);
    }
  else
    {
      pika_drawable_filter_apply (wt->filter, &rect);
    }
}

static void
pika_warp_tool_update_stroke (PikaWarpTool *wt,
                              GeglNode     *node)
{
  GeglRectangle bounds = {0, 0, 0, 0};

  if (! wt->filter)
    return;

  if (node)
    {
      /* update just this stroke */
      bounds = pika_warp_tool_get_stroke_bounds (node);
    }
  else if (wt->render_node)
    {
      node = gegl_node_get_producer (wt->render_node, "aux", NULL);

      bounds = pika_warp_tool_get_node_bounds (node);
    }

  if (! gegl_rectangle_is_empty (&bounds))
    {
#ifdef WARP_DEBUG
  g_printerr ("update stroke: (%d,%d), %dx%d\n",
              bounds.x, bounds.y,
              bounds.width, bounds.height);
#endif

      pika_warp_tool_update_area (wt, &bounds, FALSE);
    }
}

static void
pika_warp_tool_stroke_append (PikaWarpTool *wt,
                              gchar         type,
                              gdouble       x,
                              gdouble       y)
{
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  GeglRectangle    area;

  if (! wt->filter)
    return;

  gegl_path_append (wt->current_stroke, type, x, y);

  area.x      = floor (x - options->effect_size * 0.5);
  area.y      = floor (y - options->effect_size * 0.5);
  area.width  = ceil  (x + options->effect_size * 0.5) - area.x;
  area.height = ceil  (y + options->effect_size * 0.5) - area.y;

#ifdef WARP_DEBUG
  g_printerr ("update rect: (%d,%d), %dx%d\n",
              area.x, area.y,
              area.width, area.height);
#endif

  if (wt->render_node)
    {
      GeglNode *node = gegl_node_get_producer (wt->render_node, "aux", NULL);

      pika_warp_tool_clear_node_bounds (node);

      pika_warp_tool_update_bounds (wt);
    }

  pika_warp_tool_update_area (wt, &area, options->real_time_preview);
}

static void
pika_warp_tool_filter_flush (PikaDrawableFilter *filter,
                             PikaTool           *tool)
{
  PikaImage *image = pika_display_get_image (tool->display);

  pika_projection_flush (pika_image_get_projection (image));
}

static void
pika_warp_tool_add_op (PikaWarpTool *wt,
                       GeglNode     *op)
{
  GeglNode *last_op;

  g_return_if_fail (GEGL_IS_NODE (wt->render_node));

  gegl_node_add_child (wt->graph, op);

  last_op = gegl_node_get_producer (wt->render_node, "aux", NULL);

  gegl_node_disconnect (wt->render_node, "aux");
  gegl_node_link (last_op, op);
  gegl_node_connect (op,              "output",
                     wt->render_node, "aux");
}

static void
pika_warp_tool_remove_op (PikaWarpTool *wt,
                          GeglNode     *op)
{
  GeglNode *previous;

  g_return_if_fail (GEGL_IS_NODE (wt->render_node));

  previous = gegl_node_get_producer (op, "input", NULL);

  gegl_node_disconnect (op,              "input");
  gegl_node_connect (previous,        "output",
                     wt->render_node, "aux");

  gegl_node_remove_child (wt->graph, op);
}

static void
pika_warp_tool_free_op (GeglNode *op)
{
  GeglNode *parent;

  parent = gegl_node_get_parent (op);

  pika_assert (parent != NULL);

  gegl_node_remove_child (parent, op);
}

static void
pika_warp_tool_animate (PikaWarpTool *wt)
{
  PikaTool        *tool    = PIKA_TOOL (wt);
  PikaWarpOptions *options = PIKA_WARP_TOOL_GET_OPTIONS (wt);
  PikaImage       *orig_image;
  PikaImage       *image;
  PikaLayer       *layer;
  PikaLayer       *first_layer;
  GeglNode        *scale_node;
  PikaProgress    *progress;
  GtkWidget       *widget;
  gint             i;

  g_return_if_fail (g_list_length (tool->drawables) == 1);

  if (! pika_warp_tool_can_undo (tool, tool->display))
    {
      pika_tool_message_literal (tool, tool->display,
                                 _("Please add some warp strokes first."));
      return;
    }

  /*  get rid of the image map so we can use wt->graph  */
  if (wt->filter)
    {
      pika_drawable_filter_abort (wt->filter);
      g_clear_object (&wt->filter);
    }

  pika_warp_tool_set_sampler (wt, /* commit = */ TRUE);

  pika_progress_start (PIKA_PROGRESS (tool), FALSE,
                       _("Rendering Frame %d"), 1);

  orig_image = pika_item_get_image (PIKA_ITEM (tool->drawables->data));

  image = pika_create_image (orig_image->pika,
                             pika_item_get_width  (PIKA_ITEM (tool->drawables->data)),
                             pika_item_get_height (PIKA_ITEM (tool->drawables->data)),
                             pika_drawable_get_base_type (tool->drawables->data),
                             pika_drawable_get_precision (tool->drawables->data),
                             TRUE);

  /*  the first frame is always the unwarped image  */
  layer = PIKA_LAYER (pika_item_convert (PIKA_ITEM (tool->drawables->data), image,
                                         PIKA_TYPE_LAYER));
  pika_object_take_name (PIKA_OBJECT (layer),
                         g_strdup_printf (_("Frame %d"), 1));

  pika_item_set_offset (PIKA_ITEM (layer), 0, 0);
  pika_item_set_visible (PIKA_ITEM (layer), TRUE, FALSE);
  pika_layer_set_mode (layer, pika_image_get_default_new_layer_mode (image),
                       FALSE);
  pika_layer_set_opacity (layer, PIKA_OPACITY_OPAQUE, FALSE);
  pika_image_add_layer (image, layer, NULL, 0, FALSE);

  first_layer = layer;

  scale_node = gegl_node_new_child (NULL,
                                    "operation",    "pika:scalar-multiply",
                                    "n-components", 2,
                                    NULL);
  pika_warp_tool_add_op (wt, scale_node);

  progress = pika_sub_progress_new (PIKA_PROGRESS (tool));

  for (i = 1; i < options->n_animation_frames; i++)
    {
      pika_progress_set_text (PIKA_PROGRESS (tool),
                              _("Rendering Frame %d"), i + 1);

      pika_sub_progress_set_step (PIKA_SUB_PROGRESS (progress),
                                  i, options->n_animation_frames);

      layer = PIKA_LAYER (pika_item_duplicate (PIKA_ITEM (first_layer),
                                               PIKA_TYPE_LAYER));
      pika_object_take_name (PIKA_OBJECT (layer),
                             g_strdup_printf (_("Frame %d"), i + 1));

      gegl_node_set (scale_node,
                     "factor", (gdouble) i /
                               (gdouble) (options->n_animation_frames - 1),
                     NULL);

      pika_gegl_apply_operation (pika_drawable_get_buffer (PIKA_DRAWABLE (first_layer)),
                                 progress,
                                 _("Frame"),
                                 wt->graph,
                                 pika_drawable_get_buffer (PIKA_DRAWABLE (layer)),
                                 NULL, FALSE);

      pika_image_add_layer (image, layer, NULL, 0, FALSE);
    }

  g_object_unref (progress);

  pika_warp_tool_remove_op (wt, scale_node);

  pika_progress_end (PIKA_PROGRESS (tool));

  /*  recreate the image map  */
  pika_warp_tool_create_filter (wt, tool->drawables->data);
  pika_warp_tool_update_stroke (wt, NULL);

  widget = GTK_WIDGET (pika_display_get_shell (tool->display));
  pika_create_display (orig_image->pika, image, PIKA_UNIT_PIXEL, 1.0,
                       G_OBJECT (pika_widget_get_monitor (widget)));
  g_object_unref (image);
}
