gimpcagetool.c 41.4 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4
 *
 * gimpcagetool.c
 * Copyright (C) 2010 Michael Muré <batolettre@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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"

22 23
#include <string.h>

24
#include <gegl.h>
25
#include <gtk/gtk.h>
26
#include <gdk/gdkkeysyms.h>
27 28 29 30 31 32

#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "tools-types.h"

33 34 35 36
#include "gegl/gimp-gegl-utils.h"

#include "operations/gimpcageconfig.h"

37
#include "core/gimpdrawablefilter.h"
38
#include "core/gimperror.h"
39
#include "core/gimpimage.h"
40
#include "core/gimpitem.h"
41
#include "core/gimpprogress.h"
42 43 44
#include "core/gimpprojection.h"

#include "widgets/gimphelp-ids.h"
45
#include "widgets/gimpwidgets-utils.h"
Michael Muré's avatar
Michael Muré committed
46

47
#include "display/gimpcanvasitem.h"
Michael Muré's avatar
Michael Muré committed
48 49
#include "display/gimpdisplay.h"

50
#include "gimpcagetool.h"
51
#include "gimpcageoptions.h"
52
#include "gimptoolcontrol.h"
53 54 55

#include "gimp-intl.h"

56

57 58 59 60 61
/* XXX: if this state list is updated, in particular if for some reason,
   a new CAGE_STATE_* was to be inserted after CAGE_STATE_CLOSING, check
   if the function gimp_cage_tool_is_complete() has to be updated.
   Current algorithm is that all DEFORM_* states are complete states,
   and all CAGE_* states are incomplete states. */
62 63 64 65 66 67 68 69 70 71 72 73 74
enum
{
  CAGE_STATE_INIT,
  CAGE_STATE_WAIT,
  CAGE_STATE_MOVE_HANDLE,
  CAGE_STATE_SELECTING,
  CAGE_STATE_CLOSING,
  DEFORM_STATE_WAIT,
  DEFORM_STATE_MOVE_HANDLE,
  DEFORM_STATE_SELECTING
};


75 76 77
static gboolean   gimp_cage_tool_initialize         (GimpTool              *tool,
                                                     GimpDisplay           *display,
                                                     GError               **error);
78 79
static void       gimp_cage_tool_control            (GimpTool              *tool,
                                                     GimpToolAction         action,
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                                                     GimpDisplay           *display);
static void       gimp_cage_tool_button_press       (GimpTool              *tool,
                                                     const GimpCoords      *coords,
                                                     guint32                time,
                                                     GdkModifierType        state,
                                                     GimpButtonPressType    press_type,
                                                     GimpDisplay           *display);
static void       gimp_cage_tool_button_release     (GimpTool              *tool,
                                                     const GimpCoords      *coords,
                                                     guint32                time,
                                                     GdkModifierType        state,
                                                     GimpButtonReleaseType  release_type,
                                                     GimpDisplay           *display);
static void       gimp_cage_tool_motion             (GimpTool              *tool,
                                                     const GimpCoords      *coords,
                                                     guint32                time,
                                                     GdkModifierType        state,
                                                     GimpDisplay           *display);
98 99
static gboolean   gimp_cage_tool_key_press          (GimpTool              *tool,
                                                     GdkEventKey           *kevent,
100 101 102 103 104 105 106 107 108 109
                                                     GimpDisplay           *display);
static void       gimp_cage_tool_cursor_update      (GimpTool              *tool,
                                                     const GimpCoords      *coords,
                                                     GdkModifierType        state,
                                                     GimpDisplay           *display);
static void       gimp_cage_tool_oper_update        (GimpTool              *tool,
                                                     const GimpCoords      *coords,
                                                     GdkModifierType        state,
                                                     gboolean               proximity,
                                                     GimpDisplay           *display);
110 111 112
static void       gimp_cage_tool_options_notify     (GimpTool              *tool,
                                                     GimpToolOptions       *options,
                                                     const GParamSpec      *pspec);
113 114 115

static void       gimp_cage_tool_draw               (GimpDrawTool          *draw_tool);

116 117
static void       gimp_cage_tool_start              (GimpCageTool          *ct,
                                                     GimpDisplay           *display);
118 119 120
static void       gimp_cage_tool_halt               (GimpCageTool          *ct);
static void       gimp_cage_tool_commit             (GimpCageTool          *ct);

121
static gint       gimp_cage_tool_is_on_handle       (GimpCageTool          *ct,
122 123 124 125 126
                                                     GimpDrawTool          *draw_tool,
                                                     GimpDisplay           *display,
                                                     gdouble                x,
                                                     gdouble                y,
                                                     gint                   handle_size);
127 128 129 130
static gint       gimp_cage_tool_is_on_edge         (GimpCageTool          *ct,
                                                     gdouble                x,
                                                     gdouble                y,
                                                     gint                   handle_size);
131

132
static gboolean   gimp_cage_tool_is_complete        (GimpCageTool          *ct);
133
static void       gimp_cage_tool_remove_last_handle (GimpCageTool          *ct);
134
static void       gimp_cage_tool_compute_coef       (GimpCageTool          *ct);
135
static void       gimp_cage_tool_create_filter      (GimpCageTool          *ct);
136
static void       gimp_cage_tool_filter_flush       (GimpDrawableFilter    *filter,
137
                                                     GimpTool              *tool);
138
static void       gimp_cage_tool_filter_update      (GimpCageTool          *ct);
139

140 141
static void       gimp_cage_tool_create_render_node (GimpCageTool          *ct);
static void       gimp_cage_tool_render_node_update (GimpCageTool          *ct);
142

143

144
G_DEFINE_TYPE (GimpCageTool, gimp_cage_tool, GIMP_TYPE_DRAW_TOOL)
145 146 147 148 149 150

#define parent_class gimp_cage_tool_parent_class


void
gimp_cage_tool_register (GimpToolRegisterCallback  callback,
151
                         gpointer                  data)
152
{
153 154 155 156
  (* callback) (GIMP_TYPE_CAGE_TOOL,
                GIMP_TYPE_CAGE_OPTIONS,
                gimp_cage_options_gui,
                0,
157
                "gimp-cage-tool",
158 159
                _("Cage Transform"),
                _("Cage Transform: Deform a selection with a cage"),
160
                N_("_Cage Transform"), "<shift>G",
161
                NULL, GIMP_HELP_TOOL_CAGE,
162
                GIMP_ICON_TOOL_CAGE,
163 164 165 166 167 168
                data);
}

static void
gimp_cage_tool_class_init (GimpCageToolClass *klass)
{
169 170 171
  GimpToolClass     *tool_class      = GIMP_TOOL_CLASS (klass);
  GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);

172
  tool_class->initialize     = gimp_cage_tool_initialize;
173
  tool_class->control        = gimp_cage_tool_control;
174 175 176 177 178 179
  tool_class->button_press   = gimp_cage_tool_button_press;
  tool_class->button_release = gimp_cage_tool_button_release;
  tool_class->key_press      = gimp_cage_tool_key_press;
  tool_class->motion         = gimp_cage_tool_motion;
  tool_class->cursor_update  = gimp_cage_tool_cursor_update;
  tool_class->oper_update    = gimp_cage_tool_oper_update;
180
  tool_class->options_notify = gimp_cage_tool_options_notify;
181 182

  draw_tool_class->draw      = gimp_cage_tool_draw;
183 184 185 186 187
}

static void
gimp_cage_tool_init (GimpCageTool *self)
{
188 189
  GimpTool *tool = GIMP_TOOL (self);

190
  gimp_tool_control_set_preserve    (tool->control, FALSE);
191 192 193 194
  gimp_tool_control_set_dirty_mask  (tool->control,
                                     GIMP_DIRTY_IMAGE           |
                                     GIMP_DIRTY_IMAGE_STRUCTURE |
                                     GIMP_DIRTY_DRAWABLE        |
195 196
                                     GIMP_DIRTY_SELECTION       |
                                     GIMP_DIRTY_ACTIVE_DRAWABLE);
197 198 199 200
  gimp_tool_control_set_wants_click (tool->control, TRUE);
  gimp_tool_control_set_tool_cursor (tool->control,
                                     GIMP_TOOL_CURSOR_PERSPECTIVE);

201 202
  self->config          = g_object_new (GIMP_TYPE_CAGE_CONFIG, NULL);
  self->hovering_handle = -1;
203
  self->tool_state      = CAGE_STATE_INIT;
204 205
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static gboolean
gimp_cage_tool_initialize (GimpTool     *tool,
                           GimpDisplay  *display,
                           GError      **error)
{
  GimpImage    *image    = gimp_display_get_image (display);
  GimpDrawable *drawable = gimp_image_get_active_drawable (image);

  if (! drawable)
    return FALSE;

  if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                           _("Cannot modify the pixels of layer groups."));
      return FALSE;
    }

  if (gimp_item_is_content_locked (GIMP_ITEM (drawable)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                           _("The active layer's pixels are locked."));
      return FALSE;
    }

  if (! gimp_item_is_visible (GIMP_ITEM (drawable)))
    {
      g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
                           _("The active layer is not visible."));
      return FALSE;
    }

  gimp_cage_tool_start (GIMP_CAGE_TOOL (tool), display);

  return TRUE;
}

243 244
static void
gimp_cage_tool_control (GimpTool       *tool,
245 246
                        GimpToolAction  action,
                        GimpDisplay    *display)
247
{
248 249
  GimpCageTool *ct = GIMP_CAGE_TOOL (tool);

250 251 252 253 254 255 256
  switch (action)
    {
    case GIMP_TOOL_ACTION_PAUSE:
    case GIMP_TOOL_ACTION_RESUME:
      break;

    case GIMP_TOOL_ACTION_HALT:
257 258
      gimp_cage_tool_halt (ct);
      break;
259

260 261
    case GIMP_TOOL_ACTION_COMMIT:
      gimp_cage_tool_commit (ct);
262 263 264 265 266 267
      break;
    }

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

Alexia Death's avatar
Alexia Death committed
268
static void
269 270 271 272 273 274
gimp_cage_tool_button_press (GimpTool            *tool,
                             const GimpCoords    *coords,
                             guint32              time,
                             GdkModifierType      state,
                             GimpButtonPressType  press_type,
                             GimpDisplay         *display)
275
{
276 277 278 279
  GimpCageTool *ct        = GIMP_CAGE_TOOL (tool);
  GimpDrawTool *draw_tool = GIMP_DRAW_TOOL (tool);
  gint          handle    = -1;
  gint          edge      = -1;
Alexia Death's avatar
Alexia Death committed
280

281 282
  gimp_tool_control_activate (tool->control);

283
  if (ct->config)
284 285 286 287 288 289 290 291 292 293 294 295 296
    {
      handle = gimp_cage_tool_is_on_handle (ct,
                                            draw_tool,
                                            display,
                                            coords->x,
                                            coords->y,
                                            GIMP_TOOL_HANDLE_SIZE_CIRCLE);
      edge = gimp_cage_tool_is_on_edge (ct,
                                        coords->x,
                                        coords->y,
                                        GIMP_TOOL_HANDLE_SIZE_CIRCLE);
    }

297 298
  ct->movement_start_x = coords->x;
  ct->movement_start_y = coords->y;
299 300 301

  switch (ct->tool_state)
    {
302 303 304 305 306 307 308 309 310 311
    case CAGE_STATE_INIT:
      /* No handle yet, we add the first one and swith the tool to
       * moving handle state.
       */
      gimp_cage_config_add_cage_point (ct->config,
                                       coords->x - ct->offset_x,
                                       coords->y - ct->offset_y);
      gimp_cage_config_select_point (ct->config, 0);
      ct->tool_state = CAGE_STATE_MOVE_HANDLE;
      break;
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    case CAGE_STATE_WAIT:
      if (handle == -1 && edge <= 0)
        {
          /* User clicked on the background, we add a new handle
           * and move it
           */
          gimp_cage_config_add_cage_point (ct->config,
                                           coords->x - ct->offset_x,
                                           coords->y - ct->offset_y);
          gimp_cage_config_select_point (ct->config,
                                         gimp_cage_config_get_n_points (ct->config) - 1);
          ct->tool_state = CAGE_STATE_MOVE_HANDLE;
        }
      else if (handle == 0 && gimp_cage_config_get_n_points (ct->config) > 2)
        {
          /* User clicked on the first handle, we wait for
           * release for closing the cage and switching to
           * deform if possible
           */
          gimp_cage_config_select_point (ct->config, 0);
          ct->tool_state = CAGE_STATE_CLOSING;
        }
      else if (handle >= 0)
        {
          /* User clicked on a handle, so we move it */
338

339 340 341
          if (state & gimp_get_extend_selection_mask ())
            {
              /* Multiple selection */
342

343 344 345 346 347
              gimp_cage_config_toggle_point_selection (ct->config, handle);
            }
          else
            {
              /* New selection */
348

349 350 351 352 353
              if (! gimp_cage_config_point_is_selected (ct->config, handle))
                {
                  gimp_cage_config_select_point (ct->config, handle);
                }
            }
354

355 356 357 358 359
          ct->tool_state = CAGE_STATE_MOVE_HANDLE;
        }
      else if (edge > 0)
        {
          /* User clicked on an edge, we add a new handle here and select it */
360

361 362 363 364 365 366
          gimp_cage_config_insert_cage_point (ct->config, edge,
                                              coords->x, coords->y);
          gimp_cage_config_select_point (ct->config, edge);
          ct->tool_state = CAGE_STATE_MOVE_HANDLE;
        }
      break;
367

368 369 370 371 372 373 374 375 376 377
    case DEFORM_STATE_WAIT:
      if (handle == -1)
        {
          /* User clicked on the background, we start a rubber band
           * selection
           */
          ct->selection_start_x = coords->x;
          ct->selection_start_y = coords->y;
          ct->tool_state = DEFORM_STATE_SELECTING;
        }
378

379 380 381
      if (handle >= 0)
        {
          /* User clicked on a handle, so we move it */
382

383 384 385
          if (state & gimp_get_extend_selection_mask ())
            {
              /* Multiple selection */
386

387 388 389 390 391
              gimp_cage_config_toggle_point_selection (ct->config, handle);
            }
          else
            {
              /* New selection */
392

393 394 395 396 397 398 399 400 401
              if (! gimp_cage_config_point_is_selected (ct->config, handle))
                {
                  gimp_cage_config_select_point (ct->config, handle);
                }
            }

          ct->tool_state = DEFORM_STATE_MOVE_HANDLE;
        }
      break;
402
    }
403 404
}

Alexia Death's avatar
Alexia Death committed
405
void
406 407 408 409 410 411
gimp_cage_tool_button_release (GimpTool              *tool,
                               const GimpCoords      *coords,
                               guint32                time,
                               GdkModifierType        state,
                               GimpButtonReleaseType  release_type,
                               GimpDisplay           *display)
412
{
413
  GimpCageTool    *ct      = GIMP_CAGE_TOOL (tool);
414
  GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
415 416 417

  gimp_draw_tool_pause (GIMP_DRAW_TOOL (ct));

418 419
  gimp_tool_control_halt (tool->control);

420
  if (release_type == GIMP_BUTTON_RELEASE_CANCEL)
421
    {
422 423 424
      /* Cancelling */

      switch (ct->tool_state)
425
        {
426 427 428
        case CAGE_STATE_CLOSING:
          ct->tool_state = CAGE_STATE_WAIT;
          break;
429

430 431 432 433
        case CAGE_STATE_MOVE_HANDLE:
          gimp_cage_config_remove_last_cage_point (ct->config);
          ct->tool_state = CAGE_STATE_WAIT;
          break;
434

435 436 437
        case CAGE_STATE_SELECTING:
          ct->tool_state = CAGE_STATE_WAIT;
          break;
438

439
        case DEFORM_STATE_MOVE_HANDLE:
440
          gimp_cage_tool_filter_update (ct);
441 442
          ct->tool_state = DEFORM_STATE_WAIT;
          break;
443

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
        case DEFORM_STATE_SELECTING:
          ct->tool_state = DEFORM_STATE_WAIT;
          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);
593
        }
594
      return TRUE;
595

596 597 598 599 600
    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)
601
        {
602 603 604 605 606 607 608 609 610
          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);
        }
      return TRUE;
611

612 613 614
    case GDK_KEY_Escape:
      gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
      return TRUE;
615

616 617 618
    default:
      break;
    }
619

620 621
  return FALSE;
}
622

623 624 625 626 627 628 629 630 631
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);
632

633 634 635 636 637 638 639 640
  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);
641

642 643 644 645 646
      ct->hovering_edge = gimp_cage_tool_is_on_edge (ct,
                                                     coords->x,
                                                     coords->y,
                                                     GIMP_TOOL_HANDLE_SIZE_CIRCLE);
    }
647

648
  gimp_draw_tool_pause (draw_tool);
649

650 651
  ct->cursor_x = coords->x;
  ct->cursor_y = coords->y;
652

653
  gimp_draw_tool_resume (draw_tool);
654
}
655

656
static void
657 658 659 660
gimp_cage_tool_cursor_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              GimpDisplay      *display)
661
{
662
  GimpCageTool       *ct       = GIMP_CAGE_TOOL (tool);
663
  GimpCageOptions    *options  = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
664
  GimpCursorModifier  modifier = GIMP_CURSOR_MODIFIER_PLUS;
665

666
  if (tool->display)
667
    {
668
      if (ct->hovering_handle != -1)
669
        {
670
          modifier = GIMP_CURSOR_MODIFIER_MOVE;
671
        }
672 673
      else if (ct->hovering_edge  != -1 &&
               options->cage_mode == GIMP_CAGE_MODE_CAGE_CHANGE)
674 675 676
        {
          modifier = GIMP_CURSOR_MODIFIER_PLUS;
        }
677 678
      else
        {
679
          if (gimp_cage_tool_is_complete (ct))
680
            modifier = GIMP_CURSOR_MODIFIER_BAD;
681
        }
682
    }
683 684 685 686 687 688 689 690 691 692 693 694
  else
    {
      GimpImage    *image    = gimp_display_get_image (display);
      GimpDrawable *drawable = gimp_image_get_active_drawable (image);

      if (gimp_viewable_get_children (GIMP_VIEWABLE (drawable)) ||
          gimp_item_is_content_locked (GIMP_ITEM (drawable))    ||
          ! gimp_item_is_visible (GIMP_ITEM (drawable)))
        {
          modifier = GIMP_CURSOR_MODIFIER_BAD;
        }
    }
695 696 697 698

  gimp_tool_control_set_cursor_modifier (tool->control, modifier);

  GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
699 700
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
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)
                gimp_cage_tool_create_filter (ct);

              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));
}

782 783 784
static void
gimp_cage_tool_draw (GimpDrawTool *draw_tool)
{
785 786 787
  GimpCageTool    *ct        = GIMP_CAGE_TOOL (draw_tool);
  GimpCageOptions *options   = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
  GimpCageConfig  *config    = ct->config;
788
  GimpCanvasGroup *stroke_group;
789
  gint             n_vertices;
790
  gint             i;
791
  GimpHandleType   handle;
792

793 794 795 796
  n_vertices = gimp_cage_config_get_n_points (config);

  if (n_vertices == 0)
    return;
797

798
  if (ct->tool_state == CAGE_STATE_INIT)
799
    return;
800

801 802 803
  stroke_group = gimp_draw_tool_add_stroke_group (draw_tool);

  gimp_draw_tool_push_group (draw_tool, stroke_group);
804

805
  /* If needed, draw line to the cursor. */
806
  if (! gimp_cage_tool_is_complete (ct))
807
    {
808 809 810
      GimpVector2 last_point;

      last_point = gimp_cage_config_get_point_coordinate (ct->config,
811
                                                          options->cage_mode,
812 813
                                                          n_vertices - 1);

814 815 816 817 818
      gimp_draw_tool_add_line (draw_tool,
                               last_point.x + ct->offset_x,
                               last_point.y + ct->offset_y,
                               ct->cursor_x,
                               ct->cursor_y);
819
    }
820

821 822
  gimp_draw_tool_pop_group (draw_tool);

823
  /* Draw the cage with handles. */
824
  for (i = 0; i < n_vertices; i++)
825
    {
826 827
      GimpCanvasItem *item;
      GimpVector2     point1, point2;
828

829 830 831 832 833
      point1 = gimp_cage_config_get_point_coordinate (ct->config,
                                                      options->cage_mode,
                                                      i);
      point1.x += ct->offset_x;
      point1.y += ct->offset_y;
834

835
      if (i > 0 || gimp_cage_tool_is_complete (ct))
836
        {
837
          gint index_point2;
838 839

          if (i == 0)
840
            index_point2 = n_vertices - 1;
841
          else
842
            index_point2 = i - 1;
843

844 845 846 847 848
          point2 = gimp_cage_config_get_point_coordinate (ct->config,
                                                          options->cage_mode,
                                                          index_point2);
          point2.x += ct->offset_x;
          point2.y += ct->offset_y;
849

850 851 852 853 854
          if (i != ct->hovering_edge ||
              gimp_cage_tool_is_complete (ct))
            {
              gimp_draw_tool_push_group (draw_tool, stroke_group);
            }
855

856 857 858 859 860
          item = gimp_draw_tool_add_line (draw_tool,
                                          point1.x,
                                          point1.y,
                                          point2.x,
                                          point2.y);
861

862 863 864 865 866 867 868 869 870
          if (i == ct->hovering_edge &&
              ! gimp_cage_tool_is_complete (ct))
            {
              gimp_canvas_item_set_highlight (item, TRUE);
            }
          else
            {
              gimp_draw_tool_pop_group (draw_tool);
            }
871
        }
872

873
      if (gimp_cage_config_point_is_selected (ct->config, i))
874
        {
875 876 877 878
          if (i == ct->hovering_handle)
            handle = GIMP_HANDLE_FILLED_SQUARE;
          else
            handle = GIMP_HANDLE_SQUARE;
879
        }
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
      else
        {
          if (i == ct->hovering_handle)
            handle = GIMP_HANDLE_FILLED_CIRCLE;
          else
            handle = GIMP_HANDLE_CIRCLE;
        }

      item = gimp_draw_tool_add_handle (draw_tool,
                                        handle,
                                        point1.x,
                                        point1.y,
                                        GIMP_TOOL_HANDLE_SIZE_CIRCLE,
                                        GIMP_TOOL_HANDLE_SIZE_CIRCLE,
                                        GIMP_HANDLE_ANCHOR_CENTER);

      if (i == ct->hovering_handle)
        gimp_canvas_item_set_highlight (item, TRUE);
898
    }
899

900 901
  if (ct->tool_state == DEFORM_STATE_SELECTING ||
      ct->tool_state == CAGE_STATE_SELECTING)
902 903 904
    {
      gimp_draw_tool_add_rectangle (draw_tool,
                                    FALSE,
905 906
                                    MIN (ct->selection_start_x, ct->cursor_x),
                                    MIN (ct->selection_start_y, ct->cursor_y),
907 908
                                    ABS (ct->selection_start_x - ct->cursor_x),
                                    ABS (ct->selection_start_y - ct->cursor_y));
909
    }
910 911
}

912 913 914 915 916 917 918 919 920 921 922
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);

  tool->display  = display;
  tool->drawable = drawable;

923
  g_clear_object (&ct->config);
924

925 926
  g_clear_object (&ct->coef);
  ct->dirty_coef = TRUE;
927 928 929 930

  if (ct->filter)
    {
      gimp_drawable_filter_abort (ct->filter);
931
      g_clear_object (&ct->filter);
932 933 934 935
    }

  if (ct->render_node)
    {
936 937 938
      g_clear_object (&ct->render_node);
      ct->coef_node = NULL;
      ct->cage_node = NULL;
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
    }

  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 (tool->drawable),
                        &ct->offset_x, &ct->offset_y);

  gimp_draw_tool_start (GIMP_DRAW_TOOL (ct), display);
}

955 956 957 958 959
static void
gimp_cage_tool_halt (GimpCageTool *ct)
{
  GimpTool *tool = GIMP_TOOL (ct);

960 961 962 963 964
  g_clear_object (&ct->config);
  g_clear_object (&ct->coef);
  g_clear_object (&ct->render_node);
  ct->coef_node = NULL;
  ct->cage_node = NULL;
965

966
  if (ct->filter)
967 968 969
    {
      gimp_tool_control_push_preserve (tool->control, TRUE);

970
      gimp_drawable_filter_abort (ct->filter);
971
      g_clear_object (&ct->filter);
972 973 974 975 976 977

      gimp_tool_control_pop_preserve (tool->control);

      gimp_image_flush (gimp_display_get_image (tool->display));
    }

978 979
  tool->display  = NULL;
  tool->drawable = NULL;
980
  ct->tool_state = CAGE_STATE_INIT;
981 982 983 984 985 986 987 988 989

  g_object_set (gimp_tool_get_options (tool),
                "cage-mode", GIMP_CAGE_MODE_CAGE_CHANGE,
                NULL);
}

static void
gimp_cage_tool_commit (GimpCageTool *ct)
{
990
  if (ct->filter)
991 992
    {
      GimpTool *tool = GIMP_TOOL (ct);
993

994
      gimp_tool_control_push_preserve (tool->control, TRUE);
995

996
      gimp_drawable_filter_commit (ct->filter, GIMP_PROGRESS (tool), FALSE);
997
      g_clear_object (&ct->filter);
998

999
      gimp_tool_control_pop_preserve (tool->control);
1000

1001 1002
      gimp_image_flush (gimp_display_get_image (tool->display));
    }
1003 1004
}

Alexia Death's avatar
Alexia Death committed
1005
static gint
1006 1007 1008 1009 1010 1011
gimp_cage_tool_is_on_handle (GimpCageTool *ct,
                             GimpDrawTool *draw_tool,
                             GimpDisplay  *display,
                             gdouble       x,
                             gdouble       y,
                             gint          handle_size)
Alexia Death's avatar
Alexia Death committed
1012
{
1013 1014 1015
  GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
  GimpCageConfig  *config  = ct->config;
  gdouble          dist    = G_MAXDOUBLE;
1016 1017
  gint             i;
  GimpVector2      cage_point;
1018
  guint            n_cage_vertices;
Alexia Death's avatar
Alexia Death committed
1019

1020
  g_return_val_if_fail (GIMP_IS_CAGE_TOOL (ct), -1);
Alexia Death's avatar
Alexia Death committed
1021

1022 1023 1024
  n_cage_vertices = gimp_cage_config_get_n_points (config);

  if (n_cage_vertices == 0)
Alexia Death's avatar
Alexia Death committed
1025 1026
    return -1;

1027
  for (i = 0; i < n_cage_vertices; i++)
Alexia Death's avatar
Alexia Death committed
1028
    {
1029 1030 1031 1032 1033
      cage_point = gimp_cage_config_get_point_coordinate (config,
                                                          options->cage_mode,
                                                          i);
      cage_point.x += ct->offset_x;
      cage_point.y += ct->offset_y;
1034 1035 1036 1037

      dist = gimp_draw_tool_calc_distance_square (GIMP_DRAW_TOOL (draw_tool),
                                                  display,
                                                  x, y,
1038 1039
                                                  cage_point.x,
                                                  cage_point.y);
1040

1041
      if (dist <= SQR (handle_size / 2))
1042
        return i;
Alexia Death's avatar
Alexia Death committed
1043 1044 1045 1046 1047
    }

  return -1;
}

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
static gint
gimp_cage_tool_is_on_edge (GimpCageTool *ct,
                           gdouble       x,
                           gdouble       y,
                           gint          handle_size)
{
  GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
  GimpCageConfig  *config  = ct->config;
  gint             i;
  guint            n_cage_vertices;
  GimpVector2      A, B, C, AB, BC, AC;
  gdouble          lAB, lBC, lAC, lEB, lEC;

  g_return_val_if_fail (GIMP_IS_CAGE_TOOL (ct), -1);

  n_cage_vertices = gimp_cage_config_get_n_points (config);

  if (n_cage_vertices < 2)
    return -1;

  A = gimp_cage_config_get_point_coordinate (config,
                                             options->cage_mode,
                                             n_cage_vertices-1);
  B = gimp_cage_config_get_point_coordinate (config,
                                             options->cage_mode,
                                             0);
  C.x = x;
  C.y = y;

  for (i = 0; i < n_cage_vertices; i++)
    {
      gimp_vector2_sub (&AB, &A, &B);
      gimp_vector2_sub (&BC, &B, &C);
      gimp_vector2_sub (&AC, &A, &C);

      lAB = gimp_vector2_length (&AB);
      lBC = gimp_vector2_length (&BC);
      lAC = gimp_vector2_length (&AC);
      lEB = lAB / 2 + (SQR (lBC) - SQR (lAC)) / (2 * lAB);
      lEC = sqrt (SQR (lBC) - SQR (lEB));

1089
      if ((lEC < handle_size / 2) && (ABS (SQR (lBC) - SQR (lAC)) <= SQR (lAB)))
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        return i;

      A = B;
      B = gimp_cage_config_get_point_coordinate (config,
                                                 options->cage_mode,
                                                 (i+1) % n_cage_vertices);
    }

  return -1;
}

1101 1102 1103 1104 1105 1106
static gboolean
gimp_cage_tool_is_complete (GimpCageTool *ct)
{
  return (ct->tool_state > CAGE_STATE_CLOSING);
}

1107 1108 1109
static void
gimp_cage_tool_remove_last_handle (GimpCageTool *ct)
{
1110
  GimpCageConfig *config = ct->config;
1111

1112
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (ct));
1113

1114
  gimp_cage_config_remove_last_cage_point (config);
1115

1116
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (ct));
1117 1118 1119
}

static void
1120
gimp_cage_tool_compute_coef (GimpCageTool *ct)
1121
{
1122
  GimpCageConfig *config = ct->config;
1123
  GimpProgress   *progress;
Michael Natterer's avatar
Michael Natterer committed
1124
  const Babl     *format;
1125 1126 1127 1128 1129 1130 1131
  GeglNode       *gegl;
  GeglNode       *input;
  GeglNode       *output;
  GeglProcessor  *processor;
  GeglBuffer     *buffer;
  gdouble         value;

1132 1133
  progress = gimp_progress_start (GIMP_PROGRESS (ct), FALSE,
                                  _("Computing Cage Coefficients"));
1134

1135
  g_clear_object (&ct->coef);
1136

1137
  format = babl_format_n (babl_type ("float"),
1138
                          gimp_cage_config_get_n_points (config) * 2);
1139

1140

1141
  gegl = gegl_node_new ();
1142

1143
  input = gegl_node_new_child (gegl,
1144
                               "operation", "gimp:cage-coef-calc",
1145 1146
                               "config",    ct->config,
                               NULL);
1147

1148
  output = gegl_node_new_child (gegl,
1149 1150 1151 1152
                                "operation", "gegl:buffer-sink",
                                "buffer",    &buffer,
                                "format",    format,
                                NULL);
1153

1154
  gegl_node_connect_to (input, "output",
1155
                        output, "input");
1156

Alexia Death's avatar
Alexia Death committed
1157
  processor = gegl_node_new_processor (output, NULL);
1158

1159
  while (gegl_processor_work (processor, &value))
1160
    {
1161 1162
      if (progress)
        gimp_progress_set_value (progress, value);
1163
    }
1164

1165 1166
  if (progress)
    gimp_progress_end (progress);
1167

1168
  g_object_unref (processor);
1169 1170 1171

  ct->coef = buffer;
  g_object_unref (gegl);
1172

1173
  ct->dirty_coef = FALSE;
1174 1175
}

1176
static void
1177
gimp_cage_tool_create_render_node (GimpCageTool *ct)
1178
{
1179 1180 1181 1182
  GimpCageOptions *options = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
  GeglNode        *render;
  GeglNode        *input;
  GeglNode        *output;
1183

1184 1185 1186
  g_return_if_fail (ct->render_node == NULL);
  /* render_node is not supposed to be recreated */

1187
  ct->render_node = gegl_node_new ();
1188

1189 1190
  input  = gegl_node_get_input_proxy  (ct->render_node, "input");
  output = gegl_node_get_output_proxy (ct->render_node, "output");
1191

1192 1193 1194 1195
  ct->coef_node = gegl_node_new_child (ct->render_node,
                                       "operation", "gegl:buffer-source",
                                       "buffer",    ct->coef,
                                       NULL);
1196

1197 1198 1199 1200 1201
  ct->cage_node = gegl_node_new_child (ct->render_node,
                                       "operation",        "gimp:cage-transform",
                                       "config",           ct->config,
                                       "fill-plain-color", options->fill_plain_color,
                                       NULL);
1202

1203
  render = gegl_node_new_child (ct->render_node,
1204
                                "operation", "gegl:map-absolute",
1205 1206
                                NULL);

1207 1208
  gegl_node_connect_to (input,         "output",
                        ct->cage_node, "input");
1209

1210 1211
  gegl_node_connect_to (ct->coef_node, "output",
                        ct->cage_node, "aux");
1212

1213
  gegl_node_connect_to (input,  "output",
1214 1215
                        render, "input");

1216 1217
  gegl_node_connect_to (ct->cage_node, "output",
                        render,        "aux");
1218 1219 1220

  gegl_node_connect_to (render, "output",
                        output, "input");
1221

1222 1223
  gimp_gegl_progress_connect (ct->cage_node, GIMP_PROGRESS (ct),
                              _("Cage Transform"));
1224 1225 1226 1227 1228 1229
}

static void
gimp_cage_tool_render_node_update (GimpCageTool *ct)
{
  GimpCageOptions *options  = GIMP_CAGE_TOOL_GET_OPTIONS (ct);
1230
  gboolean         fill;
1231 1232 1233
  GeglBuffer      *buffer;

  gegl_node_get (ct->cage_node,
1234
                 "fill-plain-color", &fill,
1235 1236
                 NULL);

1237
  if (fill != options->fill_plain_color)
1238 1239
    {
      gegl_node_set (ct->cage_node,
1240
                     "fill-plain-color", options->fill_plain_color,
1241 1242 1243 1244 1245 1246 1247
                     NULL);
    }

  gegl_node_get (ct->coef_node,
                 "buffer", &buffer,
                 NULL);

1248
  if (buffer != ct->coef)
1249 1250
    {
      gegl_node_set (ct->coef_node,
1251
                     "buffer", ct->coef,
1252 1253
                     NULL);
    }
Michael Muré's avatar
Michael Muré committed
1254 1255

  if (buffer)
1256
    g_object_unref (buffer);
1257 1258
}

1259
static void
1260
gimp_cage_tool_create_filter (GimpCageTool *ct)
1261
{
1262
  if (! ct->render_node)
1263
    gimp_cage_tool_create_render_node (ct);
1264

1265
  ct->filter = gimp_drawable_filter_new (GIMP_TOOL (ct)->drawable,
1266 1267
                                         _("Cage transform"),
                                         ct->render_node,
1268
                                         GIMP_ICON_TOOL_CAGE);
1269

1270 1271
  g_signal_connect (ct->filter, "flush",
                    G_CALLBACK (gimp_cage_tool_filter_flush),
1272
                    ct);
1273 1274
}

1275
static void
1276 1277
gimp_cage_tool_filter_flush (GimpDrawableFilter *filter,
                             GimpTool           *tool)
1278
{
1279