gimpsymmetry-mandala.c 19.1 KB
Newer Older
Jehan's avatar
Jehan committed
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpsymmetry-mandala.c
 * Copyright (C) 2015 Jehan <jehan@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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "libgimpconfig/gimpconfig.h"
#include "libgimpmath/gimpmath.h"

#include "core-types.h"

#include "gimp.h"
#include "gimp-cairo.h"
#include "gimpdrawable.h"
#include "gimpguide.h"
#include "gimpimage.h"
#include "gimpimage-guides.h"
#include "gimpimage-symmetry.h"
#include "gimpitem.h"
#include "gimpsymmetry-mandala.h"

#include "gimp-intl.h"

46

Jehan's avatar
Jehan committed
47 48 49 50 51 52 53 54 55
enum
{
  PROP_0,
  PROP_CENTER_X,
  PROP_CENTER_Y,
  PROP_SIZE,
  PROP_DISABLE_TRANSFORMATION,
};

56

Jehan's avatar
Jehan committed
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
/* Local function prototypes */

static void       gimp_mandala_constructed        (GObject      *object);
static void       gimp_mandala_finalize           (GObject      *object);
static void       gimp_mandala_set_property       (GObject      *object,
                                                   guint         property_id,
                                                   const GValue *value,
                                                   GParamSpec   *pspec);
static void       gimp_mandala_get_property       (GObject      *object,
                                                   guint         property_id,
                                                   GValue       *value,
                                                   GParamSpec   *pspec);
static void       gimp_mandala_active_changed     (GimpSymmetry *sym);

static void       gimp_mandala_add_guide          (GimpMandala         *mandala,
                                                   GimpOrientationType  orientation);
static void       gimp_mandala_remove_guide       (GimpMandala         *mandala,
                                                   GimpOrientationType  orientation);
static void       gimp_mandala_guide_removed_cb   (GObject      *object,
                                                   GimpMandala  *mandala);
static void       gimp_mandala_guide_position_cb  (GObject      *object,
                                                   GParamSpec   *pspec,
                                                   GimpMandala  *mandala);

static void       gimp_mandala_update_strokes     (GimpSymmetry *mandala,
                                                   GimpDrawable *drawable,
                                                   GimpCoords   *origin);
static GeglNode * gimp_mandala_get_operation      (GimpSymmetry *mandala,
                                                   gint          stroke,
                                                   gint          paint_width,
                                                   gint          paint_height);
88
static void    gimp_mandala_image_size_changed_cb (GimpImage    *image,
Jehan's avatar
Jehan committed
89 90 91 92 93 94
                                                   gint          previous_origin_x,
                                                   gint          previous_origin_y,
                                                   gint          previous_width,
                                                   gint          previous_height,
                                                   GimpSymmetry *sym);

95

Jehan's avatar
Jehan committed
96 97 98 99
G_DEFINE_TYPE (GimpMandala, gimp_mandala, GIMP_TYPE_SYMMETRY)

#define parent_class gimp_mandala_parent_class

100

Jehan's avatar
Jehan committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void
gimp_mandala_class_init (GimpMandalaClass *klass)
{
  GObjectClass      *object_class   = G_OBJECT_CLASS (klass);
  GimpSymmetryClass *symmetry_class = GIMP_SYMMETRY_CLASS (klass);

  object_class->constructed         = gimp_mandala_constructed;
  object_class->finalize            = gimp_mandala_finalize;
  object_class->set_property        = gimp_mandala_set_property;
  object_class->get_property        = gimp_mandala_get_property;

  symmetry_class->label             = _("Mandala");
  symmetry_class->update_strokes    = gimp_mandala_update_strokes;
  symmetry_class->get_operation     = gimp_mandala_get_operation;
  symmetry_class->active_changed    = gimp_mandala_active_changed;

117 118
  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_CENTER_X,
                           "center-x",
119
                           _("Center abscissa"),
120
                           NULL,
121
                           0.0, G_MAXDOUBLE, 0.0,
122 123 124 125 126 127 128
                           GIMP_PARAM_STATIC_STRINGS |
                           GIMP_SYMMETRY_PARAM_GUI);

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_CENTER_Y,
                           "center-y",
                           _("Center ordinate"),
                           NULL,
129
                           0.0, G_MAXDOUBLE, 0.0,
130 131
                           GIMP_PARAM_STATIC_STRINGS |
                           GIMP_SYMMETRY_PARAM_GUI);
132

133 134 135
  GIMP_CONFIG_PROP_INT (object_class, PROP_SIZE,
                        "size",
                        _("Number of points"),
136
                        NULL,
137 138 139 140 141 142 143 144 145 146 147
                        1, 100, 6.0,
                        GIMP_PARAM_STATIC_STRINGS |
                        GIMP_SYMMETRY_PARAM_GUI);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_DISABLE_TRANSFORMATION,
                            "disable-transformation",
                            _("Disable brush transform"),
                            _("Disable brush transformation (faster)"),
                            FALSE,
                            GIMP_PARAM_STATIC_STRINGS |
                            GIMP_SYMMETRY_PARAM_GUI);
Jehan's avatar
Jehan committed
148 149 150 151 152 153 154 155 156 157
}

static void
gimp_mandala_init (GimpMandala *mandala)
{
}

static void
gimp_mandala_constructed (GObject *object)
{
158 159 160
  GimpSymmetry *sym;
  gdouble      *x_max = g_new (gdouble, 1);
  gdouble      *y_max = g_new (gdouble, 1);
Jehan's avatar
Jehan committed
161 162 163

  sym = GIMP_SYMMETRY (object);

164 165
  *x_max = gimp_image_get_width (sym->image);
  *y_max = gimp_image_get_height (sym->image);
Jehan's avatar
Jehan committed
166

167 168
  g_object_set_data_full (object, "center-x:max", x_max, g_free);
  g_object_set_data_full (object, "center-y:max", y_max, g_free);
Jehan's avatar
Jehan committed
169 170 171 172 173 174 175 176 177

  g_signal_connect (sym->image, "size-changed-detailed",
                    G_CALLBACK (gimp_mandala_image_size_changed_cb),
                    sym);
}

static void
gimp_mandala_finalize (GObject *object)
{
178 179
  GimpSymmetry *sym     = GIMP_SYMMETRY (object);
  GimpMandala  *mandala = GIMP_MANDALA (object);
Jehan's avatar
Jehan committed
180

181 182 183
  g_signal_handlers_disconnect_by_func (sym->image,
                                        gimp_mandala_image_size_changed_cb,
                                        object);
Jehan's avatar
Jehan committed
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
  if (mandala->horizontal_guide)
    g_object_unref (mandala->horizontal_guide);
  mandala->horizontal_guide = NULL;

  if (mandala->vertical_guide)
    g_object_unref (mandala->vertical_guide);
  mandala->vertical_guide = NULL;

  if (mandala->ops)
    {
      GList *iter;

      for (iter = mandala->ops; iter; iter = g_list_next (iter))
        {
          if (iter->data)
            g_object_unref (G_OBJECT (iter->data));
        }
      g_list_free (mandala->ops);
      mandala->ops = NULL;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_mandala_set_property (GObject      *object,
210 211 212
                           guint         property_id,
                           const GValue *value,
                           GParamSpec   *pspec)
Jehan's avatar
Jehan committed
213 214
{
  GimpMandala *mandala = GIMP_MANDALA (object);
215
  GimpImage   *image   = GIMP_SYMMETRY (mandala)->image;
Jehan's avatar
Jehan committed
216 217 218 219 220 221

  switch (property_id)
    {
    case PROP_CENTER_X:
      mandala->center_x = g_value_get_double (value);
      if (mandala->vertical_guide)
222 223 224 225 226 227 228 229 230 231 232
        {
          g_signal_handlers_block_by_func (mandala->vertical_guide,
                                           gimp_mandala_guide_position_cb,
                                           mandala);
          gimp_image_move_guide (image, mandala->vertical_guide,
                                 mandala->center_x,
                                 FALSE);
          g_signal_handlers_unblock_by_func (mandala->vertical_guide,
                                             gimp_mandala_guide_position_cb,
                                             mandala);
        }
Jehan's avatar
Jehan committed
233 234 235 236
      break;
    case PROP_CENTER_Y:
      mandala->center_y = g_value_get_double (value);
      if (mandala->horizontal_guide)
237 238 239 240 241 242 243 244 245 246 247
        {
          g_signal_handlers_block_by_func (mandala->horizontal_guide,
                                           gimp_mandala_guide_position_cb,
                                           mandala);
          gimp_image_move_guide (image, mandala->horizontal_guide,
                                 mandala->center_y,
                                 FALSE);
          g_signal_handlers_unblock_by_func (mandala->horizontal_guide,
                                             gimp_mandala_guide_position_cb,
                                             mandala);
        }
Jehan's avatar
Jehan committed
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
      break;
    case PROP_SIZE:
      mandala->size = g_value_get_int (value);
      break;
    case PROP_DISABLE_TRANSFORMATION:
      mandala->disable_transformation = g_value_get_boolean (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_mandala_get_property (GObject    *object,
                          guint       property_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GimpMandala *mandala = GIMP_MANDALA (object);

  switch (property_id)
    {
    case PROP_CENTER_X:
      g_value_set_double (value, mandala->center_x);
      break;
    case PROP_CENTER_Y:
      g_value_set_double (value, mandala->center_y);
      break;
    case PROP_SIZE:
      g_value_set_int (value, mandala->size);
      break;
    case PROP_DISABLE_TRANSFORMATION:
      g_value_set_boolean (value, mandala->disable_transformation);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_mandala_active_changed (GimpSymmetry *sym)
{
  GimpMandala *mandala = GIMP_MANDALA (sym);

  if (sym->active)
    {
      if (! mandala->horizontal_guide)
        gimp_mandala_add_guide (mandala, GIMP_ORIENTATION_HORIZONTAL);
      if (! mandala->vertical_guide)
        gimp_mandala_add_guide (mandala, GIMP_ORIENTATION_VERTICAL);
    }
  else
    {
      if (mandala->horizontal_guide)
        gimp_mandala_remove_guide (mandala, GIMP_ORIENTATION_HORIZONTAL);
      if (mandala->vertical_guide)
        gimp_mandala_remove_guide (mandala, GIMP_ORIENTATION_VERTICAL);
    }
}

static void
gimp_mandala_add_guide (GimpMandala         *mandala,
                        GimpOrientationType  orientation)
{
314 315 316 317 318
  GimpSymmetry *sym = GIMP_SYMMETRY (mandala);
  GimpImage    *image;
  Gimp         *gimp;
  GimpGuide    *guide;
  gint          position;
Jehan's avatar
Jehan committed
319 320

  image = sym->image;
321
  gimp  = image->gimp;
Jehan's avatar
Jehan committed
322 323 324

  guide = gimp_guide_custom_new (orientation,
                                 gimp->next_guide_ID++,
325
                                 GIMP_GUIDE_STYLE_MANDALA);
Jehan's avatar
Jehan committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

  if (orientation == GIMP_ORIENTATION_HORIZONTAL)
    {
      mandala->horizontal_guide = guide;

      /* Mandala guide position at first activation is at canvas middle. */
      if (mandala->center_y < 1.0)
        mandala->center_y = (gdouble) gimp_image_get_height (image) / 2.0;
      position = (gint) mandala->center_y;
    }
  else
    {
      mandala->vertical_guide = guide;

      /* Mandala guide position at first activation is at canvas middle. */
      if (mandala->center_x < 1.0)
        mandala->center_x = (gdouble) gimp_image_get_width (image) / 2.0;
      position = (gint) mandala->center_x;
    }
345
  g_signal_connect (guide, "removed",
Jehan's avatar
Jehan committed
346 347 348
                    G_CALLBACK (gimp_mandala_guide_removed_cb),
                    mandala);

349
  gimp_image_add_guide (image, guide, position);
Jehan's avatar
Jehan committed
350

351
  g_signal_connect (guide, "notify::position",
Jehan's avatar
Jehan committed
352 353 354 355 356 357 358 359
                    G_CALLBACK (gimp_mandala_guide_position_cb),
                    mandala);
}

static void
gimp_mandala_remove_guide (GimpMandala         *mandala,
                           GimpOrientationType  orientation)
{
360
  GimpSymmetry *sym = GIMP_SYMMETRY (mandala);
Jehan's avatar
Jehan committed
361 362 363 364 365 366 367
  GimpImage    *image;
  GimpGuide    *guide;

  image = sym->image;
  guide = (orientation == GIMP_ORIENTATION_HORIZONTAL) ?
    mandala->horizontal_guide : mandala->vertical_guide;

368
  g_signal_handlers_disconnect_by_func (guide,
Jehan's avatar
Jehan committed
369 370
                                        gimp_mandala_guide_removed_cb,
                                        mandala);
371
  g_signal_handlers_disconnect_by_func (guide,
Jehan's avatar
Jehan committed
372 373
                                        gimp_mandala_guide_position_cb,
                                        mandala);
374

Jehan's avatar
Jehan committed
375 376 377 378 379 380 381 382 383 384 385 386 387
  gimp_image_remove_guide (image, guide, FALSE);
  g_object_unref (guide);

  if (orientation == GIMP_ORIENTATION_HORIZONTAL)
    mandala->horizontal_guide = NULL;
  else
    mandala->vertical_guide = NULL;
}

static void
gimp_mandala_guide_removed_cb (GObject     *object,
                               GimpMandala *mandala)
{
388
  GimpSymmetry *sym = GIMP_SYMMETRY (mandala);
Jehan's avatar
Jehan committed
389 390 391 392 393 394 395

  g_signal_handlers_disconnect_by_func (object,
                                        gimp_mandala_guide_removed_cb,
                                        mandala);
  g_signal_handlers_disconnect_by_func (object,
                                        gimp_mandala_guide_position_cb,
                                        mandala);
396

Jehan's avatar
Jehan committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  if (GIMP_GUIDE (object) == mandala->horizontal_guide)
    {
      g_object_unref (mandala->horizontal_guide);

      mandala->horizontal_guide = NULL;
      mandala->center_y         = 0.0;

      gimp_mandala_remove_guide (mandala, GIMP_ORIENTATION_VERTICAL);
    }
  else if (GIMP_GUIDE (object) == mandala->vertical_guide)
    {
      g_object_unref (mandala->vertical_guide);
      mandala->vertical_guide = NULL;
      mandala->center_x       = 0.0;

      gimp_mandala_remove_guide (mandala, GIMP_ORIENTATION_HORIZONTAL);
    }

  gimp_image_symmetry_remove (sym->image, sym);
}

static void
gimp_mandala_guide_position_cb (GObject     *object,
                                GParamSpec  *pspec,
                                GimpMandala *mandala)
{
423
  GimpGuide *guide = GIMP_GUIDE (object);
Jehan's avatar
Jehan committed
424 425 426

  if (guide == mandala->horizontal_guide)
    {
427 428 429
      g_object_set (G_OBJECT (mandala),
                    "center-y", (gdouble) gimp_guide_get_position (guide),
                    NULL);
Jehan's avatar
Jehan committed
430 431 432
    }
  else if (guide == mandala->vertical_guide)
    {
433 434 435
      g_object_set (G_OBJECT (mandala),
                    "center-x", (gdouble) gimp_guide_get_position (guide),
                    NULL);
Jehan's avatar
Jehan committed
436 437 438 439 440 441 442 443
    }
}

static void
gimp_mandala_update_strokes (GimpSymmetry *sym,
                             GimpDrawable *drawable,
                             GimpCoords   *origin)
{
444
  GimpMandala *mandala = GIMP_MANDALA (sym);
Jehan's avatar
Jehan committed
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
  GimpCoords  *coords;
  GimpMatrix3  matrix;
  gint         i;

  g_list_free_full (sym->strokes, g_free);
  sym->strokes = NULL;

  coords = g_memdup (sym->origin, sizeof (GimpCoords));
  sym->strokes = g_list_prepend (sym->strokes, coords);

  for (i = 1; i < mandala->size; i++)
    {
      gdouble new_x, new_y;

      coords = g_memdup (sym->origin, sizeof (GimpCoords));
      gimp_matrix3_identity (&matrix);
      gimp_matrix3_translate (&matrix,
                              - mandala->center_x,
                              - mandala->center_y);
      gimp_matrix3_rotate (&matrix, - i * 2.0 * G_PI / (gdouble) mandala->size);
      gimp_matrix3_translate (&matrix,
                              mandala->center_x,
                              mandala->center_y);
      gimp_matrix3_transform_point (&matrix,
                                    coords->x,
                                    coords->y,
                                    &new_x,
                                    &new_y);
      coords->x = new_x;
      coords->y = new_y;
      sym->strokes = g_list_prepend (sym->strokes, coords);
    }
477

Jehan's avatar
Jehan committed
478 479 480 481 482 483 484 485 486 487 488
  sym->strokes = g_list_reverse (sym->strokes);

  g_signal_emit_by_name (sym, "strokes-updated", sym->image);
}

static GeglNode *
gimp_mandala_get_operation (GimpSymmetry *sym,
                            gint          stroke,
                            gint          paint_width,
                            gint          paint_height)
{
489 490
  GimpMandala *mandala = GIMP_MANDALA (sym);
  GeglNode    *op      = NULL;
Jehan's avatar
Jehan committed
491 492 493 494 495 496 497 498 499 500 501 502
  gint         i;

  if (! mandala->disable_transformation &&
      stroke != 0                       &&
      paint_width != 0                  &&
      paint_height != 0)
    {
      if (mandala->size != mandala->cached_size      ||
          mandala->cached_paint_width != paint_width ||
          mandala->cached_paint_height != paint_height)
        {
          GList *iter;
503

Jehan's avatar
Jehan committed
504 505 506 507 508 509 510 511 512 513 514 515
          if (mandala->ops)
            {
              for (iter = mandala->ops; iter; iter = g_list_next (iter))
                {
                  if (iter->data)
                    g_object_unref (G_OBJECT (iter->data));
                }
              g_list_free (mandala->ops);
              mandala->ops = NULL;
            }

          mandala->ops = g_list_prepend (mandala->ops, NULL);
516

Jehan's avatar
Jehan committed
517 518 519 520 521 522 523 524 525 526 527 528 529
          for (i = 1; i < mandala->size; i++)
            {
              op = gegl_node_new_child (NULL,
                                        "operation", "gegl:rotate",
                                        "origin-x",
                                        (gdouble) paint_width / 2.0,
                                        "origin-y",
                                        (gdouble) paint_height / 2.0,
                                        "degrees",
                                        i * 360.0 / (gdouble) mandala->size,
                                        NULL);
              mandala->ops = g_list_prepend (mandala->ops, op);
            }
530

Jehan's avatar
Jehan committed
531
          mandala->ops = g_list_reverse (mandala->ops);
532 533 534

          mandala->cached_size         = mandala->size;
          mandala->cached_paint_width  = paint_width;
Jehan's avatar
Jehan committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
          mandala->cached_paint_height = paint_height;
        }

      op = g_list_nth_data (mandala->ops, stroke);
    }

  return op;
}

static void
gimp_mandala_image_size_changed_cb (GimpImage    *image,
                                    gint          previous_origin_x,
                                    gint          previous_origin_y,
                                    gint          previous_width,
                                    gint          previous_height,
                                    GimpSymmetry *sym)
{
  if (previous_width != gimp_image_get_width (image) ||
      previous_height != gimp_image_get_height (image))
554 555 556 557 558 559 560 561 562 563 564 565
    {
      gdouble *x_max = g_new (gdouble, 1);
      gdouble *y_max = g_new (gdouble, 1);

      *x_max = gimp_image_get_width (image);
      *y_max = gimp_image_get_height (image);

      g_object_set_data_full (G_OBJECT (sym), "center-x:max", x_max, g_free);
      g_object_set_data_full (G_OBJECT (sym), "center-y:max", y_max, g_free);

      g_signal_emit_by_name (sym, "update-ui", sym->image);
    }
Jehan's avatar
Jehan committed
566
}