summaryrefslogtreecommitdiffstats
path: root/app/display/gimptoolrotategrid.c
blob: c58349043a9857853b679f761ddaea6c627a687a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimptoolrotategrid.c
 * Copyright (C) 2017 Michael Natterer <mitch@gimp.org>
 *
 * 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 <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"

#include "display-types.h"

#include "core/gimp-transform-utils.h"
#include "core/gimp-utils.h"

#include "gimpdisplayshell.h"
#include "gimptoolrotategrid.h"

#include "gimp-intl.h"


enum
{
  PROP_0,
  PROP_ANGLE
};


struct _GimpToolRotateGridPrivate
{
  gdouble  angle;

  gboolean rotate_grab;
  gdouble  real_angle;
  gdouble  last_x;
  gdouble  last_y;
};


/*  local function prototypes  */

static void   gimp_tool_rotate_grid_set_property (GObject             *object,
                                                  guint                property_id,
                                                  const GValue        *value,
                                                  GParamSpec          *pspec);
static void   gimp_tool_rotate_grid_get_property (GObject             *object,
                                                  guint                property_id,
                                                  GValue              *value,
                                                  GParamSpec          *pspec);

static gint   gimp_tool_rotate_grid_button_press (GimpToolWidget      *widget,
                                                  const GimpCoords    *coords,
                                                  guint32              time,
                                                  GdkModifierType      state,
                                                  GimpButtonPressType  press_type);
static void   gimp_tool_rotate_grid_motion       (GimpToolWidget      *widget,
                                                  const GimpCoords    *coords,
                                                  guint32              time,
                                                  GdkModifierType      state);


G_DEFINE_TYPE_WITH_PRIVATE (GimpToolRotateGrid, gimp_tool_rotate_grid,
                            GIMP_TYPE_TOOL_TRANSFORM_GRID)

#define parent_class gimp_tool_rotate_grid_parent_class


static void
gimp_tool_rotate_grid_class_init (GimpToolRotateGridClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  GimpToolWidgetClass *widget_class = GIMP_TOOL_WIDGET_CLASS (klass);

  object_class->set_property    = gimp_tool_rotate_grid_set_property;
  object_class->get_property    = gimp_tool_rotate_grid_get_property;

  widget_class->button_press    = gimp_tool_rotate_grid_button_press;
  widget_class->motion          = gimp_tool_rotate_grid_motion;

  g_object_class_install_property (object_class, PROP_ANGLE,
                                   g_param_spec_double ("angle",
                                                        NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE,
                                                        0.0,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));
}

static void
gimp_tool_rotate_grid_init (GimpToolRotateGrid *grid)
{
  grid->private = gimp_tool_rotate_grid_get_instance_private (grid);
}

static void
gimp_tool_rotate_grid_set_property (GObject      *object,
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  GimpToolRotateGrid        *grid    = GIMP_TOOL_ROTATE_GRID (object);
  GimpToolRotateGridPrivate *private = grid->private;

  switch (property_id)
    {
    case PROP_ANGLE:
      private->angle = g_value_get_double (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_tool_rotate_grid_get_property (GObject    *object,
                                    guint       property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  GimpToolRotateGrid        *grid    = GIMP_TOOL_ROTATE_GRID (object);
  GimpToolRotateGridPrivate *private = grid->private;

  switch (property_id)
    {
    case PROP_ANGLE:
      g_value_set_double (value, private->angle);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static gint
gimp_tool_rotate_grid_button_press (GimpToolWidget      *widget,
                                    const GimpCoords    *coords,
                                    guint32              time,
                                    GdkModifierType      state,
                                    GimpButtonPressType  press_type)
{
  GimpToolRotateGrid        *grid    = GIMP_TOOL_ROTATE_GRID (widget);
  GimpToolRotateGridPrivate *private = grid->private;
  GimpTransformHandle        handle;

  handle = GIMP_TOOL_WIDGET_CLASS (parent_class)->button_press (widget,
                                                                coords, time,
                                                                state,
                                                                press_type);

  if (handle == GIMP_TRANSFORM_HANDLE_ROTATION)
    {
      private->rotate_grab = TRUE;
      private->real_angle  = private->angle;
      private->last_x      = coords->x;
      private->last_y      = coords->y;
    }
  else
    {
      private->rotate_grab = FALSE;
    }

  return handle;
}

void
gimp_tool_rotate_grid_motion (GimpToolWidget   *widget,
                              const GimpCoords *coords,
                              guint32           time,
                              GdkModifierType   state)
{
  GimpToolRotateGrid        *grid    = GIMP_TOOL_ROTATE_GRID (widget);
  GimpToolRotateGridPrivate *private = grid->private;
  gdouble                    angle1, angle2, angle;
  gdouble                    pivot_x, pivot_y;
  gdouble                    x1, y1, x2, y2;
  gboolean                   constrain;
  GimpMatrix3                transform;

  if (! private->rotate_grab)
    {
      gdouble old_pivot_x;
      gdouble old_pivot_y;

      g_object_get (widget,
                    "pivot-x", &old_pivot_x,
                    "pivot-y", &old_pivot_y,
                    NULL);

      g_object_freeze_notify (G_OBJECT (widget));

      GIMP_TOOL_WIDGET_CLASS (parent_class)->motion (widget,
                                                     coords, time, state);

      g_object_get (widget,
                    "pivot-x", &pivot_x,
                    "pivot-y", &pivot_y,
                    NULL);

      if (old_pivot_x != pivot_x ||
          old_pivot_y != pivot_y)
        {
          gimp_matrix3_identity (&transform);
          gimp_transform_matrix_rotate_center (&transform,
                                               pivot_x, pivot_y,
                                               private->angle);

          g_object_set (widget,
                        "transform", &transform,
                        NULL);
       }

      g_object_thaw_notify (G_OBJECT (widget));

      return;
    }

  g_object_get (widget,
                "pivot-x",          &pivot_x,
                "pivot-y",          &pivot_y,
                "constrain-rotate", &constrain,
                NULL);

  x1 = coords->x       - pivot_x;
  x2 = private->last_x - pivot_x;
  y1 = pivot_y - coords->y;
  y2 = pivot_y - private->last_y;

  /*  find the first angle  */
  angle1 = atan2 (y1, x1);

  /*  find the angle  */
  angle2 = atan2 (y2, x2);

  angle = angle2 - angle1;

  if (angle > G_PI || angle < -G_PI)
    angle = angle2 - ((angle1 < 0) ? 2.0 * G_PI + angle1 : angle1 - 2.0 * G_PI);

  /*  increment the transform tool's angle  */
  private->real_angle += angle;

  /*  limit the angle to between -180 and 180 degrees  */
  if (private->real_angle < - G_PI)
    {
      private->real_angle += 2.0 * G_PI;
    }
  else if (private->real_angle > G_PI)
    {
      private->real_angle -= 2.0 * G_PI;
    }

  /*  constrain the angle to 15-degree multiples if ctrl is held down  */
#define FIFTEEN_DEG (G_PI / 12.0)

  if (constrain)
    {
      angle = FIFTEEN_DEG * (gint) ((private->real_angle +
                                     FIFTEEN_DEG / 2.0) / FIFTEEN_DEG);
    }
  else
    {
      angle = private->real_angle;
    }

  gimp_matrix3_identity (&transform);
  gimp_transform_matrix_rotate_center (&transform, pivot_x, pivot_y, angle);

  g_object_set (widget,
                "transform", &transform,
                "angle",     angle,
                NULL);

  private->last_x = coords->x;
  private->last_y = coords->y;
}


/*  public functions  */

GimpToolWidget *
gimp_tool_rotate_grid_new (GimpDisplayShell *shell,
                           gdouble           x1,
                           gdouble           y1,
                           gdouble           x2,
                           gdouble           y2,
                           gdouble           pivot_x,
                           gdouble           pivot_y,
                           gdouble           angle)
{
  GimpMatrix3 transform;

  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);

  gimp_matrix3_identity (&transform);
  gimp_transform_matrix_rotate_center (&transform, pivot_x, pivot_y, angle);

  return g_object_new (GIMP_TYPE_TOOL_ROTATE_GRID,
                       "shell",     shell,
                       "transform", &transform,
                       "x1",        x1,
                       "y1",        y1,
                       "x2",        x2,
                       "y2",        y2,
                       "pivot-x",   pivot_x,
                       "pivot-y",   pivot_y,
                       "angle",     angle,
                       NULL);
}