gfig-circle.c 5.74 KB
Newer Older
1 2 3
/*
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This is a plug-in for GIMP.
5 6 7 8 9
 *
 * Generates images containing vector type drawings.
 *
 * Copyright (C) 1997 Andy Thomas  alt@picnic.demon.co.uk
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13 14 15 16 17 18 19 20
 * (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
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 *
23 24
 */

25 26
#include "config.h"

27 28 29 30
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "gfig.h"
31
#include "gfig-dobject.h"
32
#include "gfig-poly.h"
33
#include "gfig-circle.h"
34 35

#include "libgimp/stdplugins-intl.h"
36

37 38
static gint        calc_radius     (GdkPoint *center,
                                    GdkPoint *edge);
39 40
static void        d_draw_circle   (GfigObject *obj,
                                    cairo_t    *cr);
41 42 43 44
static void        d_paint_circle  (GfigObject *obj);
static GfigObject *d_copy_circle   (GfigObject *obj);

static void        d_update_circle (GdkPoint   *pnt);
45

46 47 48 49 50
static gint
calc_radius (GdkPoint *center, GdkPoint *edge)
{
  gint dx = center->x - edge->x;
  gint dy = center->y - edge->y;
51

52 53 54
  return (gint) sqrt (dx * dx + dy * dy);
}

55
static void
56 57
d_draw_circle (GfigObject *obj,
               cairo_t    *cr)
58 59 60
{
  DobjPoints *center_pnt;
  DobjPoints *edge_pnt;
61
  gint        radius;
62 63 64 65 66 67

  center_pnt = obj->points;

  if (!center_pnt)
    return; /* End-of-line */

68 69
  draw_sqr (&center_pnt->pnt, obj == gfig_context->selected_obj, cr);

70 71 72
  edge_pnt = center_pnt->next;

  if (!edge_pnt)
73
    return;
74

75
  radius = calc_radius (&center_pnt->pnt, &edge_pnt->pnt);
76 77 78 79 80

  if (obj_creating == obj)
    draw_circle (&edge_pnt->pnt, TRUE, cr);
  else
    draw_sqr (&edge_pnt->pnt, obj == gfig_context->selected_obj, cr);
81

82
  gfig_draw_arc (center_pnt->pnt.x, center_pnt->pnt.y,
83
                 radius, radius, 0, 360, cr);
84 85 86
}

static void
87
d_paint_circle (GfigObject *obj)
88
{
89 90 91 92
  DobjPoints *center_pnt;
  DobjPoints *edge_pnt;
  gint        radius;
  gdouble     dpnts[4];
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  g_assert (obj != NULL);

  center_pnt = obj->points;

  if (!center_pnt)
    return; /* End-of-line */

  edge_pnt = center_pnt->next;

  if (!edge_pnt)
    {
      g_error ("Internal error - circle no edge pnt");
    }

108
  radius = calc_radius (&center_pnt->pnt, &edge_pnt->pnt);
109 110 111 112 113 114 115 116 117 118 119

  dpnts[0] = (gdouble) center_pnt->pnt.x - radius;
  dpnts[1] = (gdouble) center_pnt->pnt.y - radius;
  dpnts[3] = dpnts[2] = (gdouble) radius * 2;

  /* Scale before drawing */
  if (selvals.scaletoimage)
    scale_to_original_xy (&dpnts[0], 2);
  else
    scale_to_xy (&dpnts[0], 2);

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  if (gfig_context_get_current_style ()->fill_type != FILL_NONE)
    {
      gimp_context_push ();
      gimp_context_set_antialias (selopt.antia);
      gimp_context_set_feather (selopt.feather);
      gimp_context_set_feather_radius (selopt.feather_radius, selopt.feather_radius);
      gimp_image_select_ellipse (gfig_context->image_id,
                                 selopt.type,
                                 dpnts[0], dpnts[1],
                                 dpnts[2], dpnts[3]);
      gimp_context_pop ();

      paint_layer_fill (center_pnt->pnt.x - radius,
                        center_pnt->pnt.y - radius,
                        center_pnt->pnt.x + radius,
                        center_pnt->pnt.y + radius);
      gimp_selection_none (gfig_context->image_id);
    }
138

139 140 141
  /* Drawing a circle may be harder than stroking a circular selection,
   * but we have to do it or we will not be able to draw outside of the
   * layer. */
142
  if (obj->style.paint_type == PAINT_BRUSH_TYPE)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    {
      const gdouble r = dpnts[2] / 2;
      const gdouble cx = dpnts[0] + r, cy = dpnts[1] + r;
      gdouble       line_pnts[362];
      gdouble       angle = 0;
      gint          i = 0;

      while (i < 362)
        {
          static const gdouble step = 2 * G_PI / 180;

          line_pnts[i++] = cx + r * cos (angle);
          line_pnts[i++] = cy + r * sin (angle);
          angle += step;
        }

      gfig_paint (selvals.brshtype, gfig_context->drawable_id, i, line_pnts);
    }
161 162
}

163
static GfigObject *
164
d_copy_circle (GfigObject * obj)
165
{
166
  GfigObject *nc;
167 168 169

  g_assert (obj->type == CIRCLE);

170
  nc = d_new_object (CIRCLE, obj->points->pnt.x, obj->points->pnt.y);
171 172 173 174 175
  nc->points->next = d_copy_dobjpoints (obj->points->next);

  return nc;
}

176
void
177
d_circle_object_class_init (void)
178
{
179
  GfigObjectClass *class = &dobj_class[CIRCLE];
180 181

  class->type      = CIRCLE;
182
  class->name      = "CIRCLE";
183 184 185
  class->drawfunc  = d_draw_circle;
  class->paintfunc = d_paint_circle;
  class->copyfunc  = d_copy_circle;
186
  class->update    = d_update_circle;
187 188
}

189
static void
190 191 192 193 194 195 196 197 198 199 200
d_update_circle (GdkPoint *pnt)
{
  DobjPoints *center_pnt, *edge_pnt;

  center_pnt = obj_creating->points;

  if (!center_pnt)
    return; /* No points */

  if ((edge_pnt = center_pnt->next))
    {
201 202 203 204 205 206
      edge_pnt->pnt = *pnt;
    }
  else
    {
      edge_pnt = new_dobjpoint (pnt->x, pnt->y);
      center_pnt->next = edge_pnt;
207 208 209 210 211
    }
}

void
d_circle_start (GdkPoint *pnt,
212
                gboolean  shift_down)
213
{
214
  obj_creating = d_new_object (CIRCLE, pnt->x, pnt->y);
215 216 217 218
}

void
d_circle_end (GdkPoint *pnt,
219
              gboolean  shift_down)
220
{
luz.paz's avatar
luz.paz committed
221
  /* Under control point */
222 223 224 225 226 227 228
  if (!obj_creating->points->next)
    {
      /* No circle created */
      free_one_obj (obj_creating);
    }
  else
    {
229
      add_to_all_obj (gfig_context->current_obj, obj_creating);
230 231 232 233 234
    }

  obj_creating = NULL;
}