summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavmjong Bah <tavmjong@free.fr>2012-05-21 13:59:02 +0000
committertavmjong-free <tavmjong@free.fr>2012-05-21 13:59:02 +0000
commitcf7f6d596f7aec4b2775428db2196613de4feb7d (patch)
tree2b4b0a6801b09bc62348213804c51fb2c7cfd460
parentAdd a few new icons for meshes. (diff)
downloadinkscape-cf7f6d596f7aec4b2775428db2196613de4feb7d.tar.gz
inkscape-cf7f6d596f7aec4b2775428db2196613de4feb7d.zip
Added sp-ctrlcurve, usefull for meshes.
(bzr r11395)
-rw-r--r--src/display/CMakeLists.txt2
-rw-r--r--src/display/Makefile_insert2
-rw-r--r--src/display/sp-ctrlcurve.cpp218
-rw-r--r--src/display/sp-ctrlcurve.h54
-rw-r--r--src/ui/control-manager.cpp14
-rw-r--r--src/ui/control-manager.h3
6 files changed, 293 insertions, 0 deletions
diff --git a/src/display/CMakeLists.txt b/src/display/CMakeLists.txt
index 7b188b286..20424c845 100644
--- a/src/display/CMakeLists.txt
+++ b/src/display/CMakeLists.txt
@@ -52,6 +52,7 @@ set(display_SRC
sodipodi-ctrlrect.cpp
sp-canvas-util.cpp
sp-canvas.cpp
+ sp-ctrlcurve.cpp
sp-ctrlline.cpp
sp-ctrlpoint.cpp
sp-ctrlquadr.cpp
@@ -116,6 +117,7 @@ set(display_SRC
sp-canvas-item.h
sp-canvas-util.h
sp-canvas.h
+ sp-ctrlcurve.h
sp-ctrlline.h
sp-ctrlpoint.h
sp-ctrlquadr.h
diff --git a/src/display/Makefile_insert b/src/display/Makefile_insert
index 1c7a21dae..cf211108f 100644
--- a/src/display/Makefile_insert
+++ b/src/display/Makefile_insert
@@ -109,6 +109,8 @@ ink_common_sources += \
display/sp-canvas.h \
display/sp-canvas-util.cpp \
display/sp-canvas-util.h \
+ display/sp-ctrlcurve.cpp \
+ display/sp-ctrlcurve.h \
display/sp-ctrlline.cpp \
display/sp-ctrlline.h \
display/sp-ctrlpoint.cpp \
diff --git a/src/display/sp-ctrlcurve.cpp b/src/display/sp-ctrlcurve.cpp
new file mode 100644
index 000000000..ec078af48
--- /dev/null
+++ b/src/display/sp-ctrlcurve.cpp
@@ -0,0 +1,218 @@
+#define __INKSCAPE_CTRLCURVE_C__
+
+/*
+ * Simple bezier curve used for Mesh Gradients
+ *
+ * Author:
+ * Tavmjong Bah <tavmjong@free.fr>
+ *
+ * Copyright (C) 2011 Tavmjong Bah
+ * Copyright (C) 2007 Johan Engelen
+ * Copyright (C) 1999-2002 Lauris Kaplinski
+ *
+ * Released under GNU GPL
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "display/sp-ctrlcurve.h"
+#include "display/sp-canvas-util.h"
+#include "display/cairo-utils.h"
+#include "color.h"
+#include "display/sp-canvas.h"
+
+namespace {
+
+static void sp_ctrlcurve_class_init(SPCtrlCurveClass *klass, gpointer data);
+static void sp_ctrlcurve_init(SPCtrlCurve *ctrlcurve, gpointer g_class);
+static void sp_ctrlcurve_destroy(GtkObject *object);
+
+static void sp_ctrlcurve_update(SPCanvasItem *item, Geom::Affine const &affine, unsigned int flags);
+static void sp_ctrlcurve_render(SPCanvasItem *item, SPCanvasBuf *buf);
+
+static SPCanvasItemClass *parent_class;
+
+} // namespace
+
+GType SPCtrlCurve::getType()
+{
+ static GType type = 0;
+ if (!type) {
+ GTypeInfo info = {
+ sizeof(SPCtrlCurveClass),
+ NULL, NULL,
+ reinterpret_cast<GClassInitFunc>(sp_ctrlcurve_class_init),
+ NULL, NULL,
+ sizeof(SPCtrlCurve),
+ 0,
+ reinterpret_cast<GInstanceInitFunc>(sp_ctrlcurve_init),
+ NULL
+ };
+ type = g_type_register_static(SP_TYPE_CANVAS_ITEM, "SPCtrlCurve", &info, static_cast<GTypeFlags>(0));
+ }
+ return type;
+}
+
+namespace {
+
+void sp_ctrlcurve_class_init(SPCtrlCurveClass *klass, gpointer /*g_class*/)
+{
+ parent_class = reinterpret_cast<SPCanvasItemClass*>(g_type_class_peek_parent(klass));
+
+ klass->destroy = sp_ctrlcurve_destroy;
+
+ klass->update = sp_ctrlcurve_update;
+ klass->render = sp_ctrlcurve_render;
+}
+
+static void
+sp_ctrlcurve_init(SPCtrlCurve *ctrlcurve, gpointer /*g_class*/)
+{
+ // Points are initialized to 0,0
+ ctrlcurve->rgba = 0x0000ff7f;
+ ctrlcurve->item=NULL;
+}
+
+static void
+sp_ctrlcurve_destroy(GtkObject *object)
+{
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (SP_IS_CTRLCURVE (object));
+
+ SPCtrlCurve *ctrlcurve = SP_CTRLCURVE (object);
+
+ ctrlcurve->item=NULL;
+
+ if (GTK_OBJECT_CLASS (parent_class)->destroy)
+ (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
+}
+
+static void
+sp_ctrlcurve_render(SPCanvasItem *item, SPCanvasBuf *buf)
+{
+ SPCtrlCurve *cl = SP_CTRLCURVE (item);
+
+ if (!buf->ct)
+ return;
+
+ if ( cl->p0 == cl->p1 &&
+ cl->p1 == cl->p2 &&
+ cl->p2 == cl->p3 )
+ return;
+
+ ink_cairo_set_source_rgba32(buf->ct, cl->rgba);
+ cairo_set_line_width(buf->ct, 1);
+ cairo_new_path(buf->ct);
+
+ Geom::Point p0 = cl->p0 * cl->affine;
+ Geom::Point p1 = cl->p1 * cl->affine;
+ Geom::Point p2 = cl->p2 * cl->affine;
+ Geom::Point p3 = cl->p3 * cl->affine;
+
+ cairo_move_to (buf->ct, p0[Geom::X] - buf->rect.left(), p0[Geom::Y] - buf->rect.top());
+ cairo_curve_to (buf->ct,
+ p1[Geom::X] - buf->rect.left(), p1[Geom::Y] - buf->rect.top(),
+ p2[Geom::X] - buf->rect.left(), p2[Geom::Y] - buf->rect.top(),
+ p3[Geom::X] - buf->rect.left(), p3[Geom::Y] - buf->rect.top() );
+
+ cairo_stroke(buf->ct);
+}
+
+static void
+sp_ctrlcurve_update(SPCanvasItem *item, Geom::Affine const &affine, unsigned int flags)
+{
+ SPCtrlCurve *cl = SP_CTRLCURVE (item);
+
+ item->canvas->requestRedraw(item->x1, item->y1, item->x2, item->y2);
+
+ if (parent_class->update)
+ (* parent_class->update) (item, affine, flags);
+
+ sp_canvas_item_reset_bounds (item);
+
+ cl->affine = affine;
+
+ if (cl->p0 == cl->p1 && cl->p1 == cl->p2 && cl->p2 == cl->p3 ) {
+ item->x1 = item->x2 = item->y1 = item->y2 = 0;
+ } else {
+
+ Geom::Point p0 = cl->p0 * affine;
+ Geom::Point p1 = cl->p1 * affine;
+ Geom::Point p2 = cl->p2 * affine;
+ Geom::Point p3 = cl->p3 * affine;
+
+ double min_x = p0[Geom::X];
+ double min_y = p0[Geom::Y];
+ double max_x = p0[Geom::X];
+ double max_y = p0[Geom::Y];
+
+ min_x = MIN( min_x, p1[Geom::X] );
+ min_y = MIN( min_y, p1[Geom::Y] );
+ max_x = MAX( max_x, p1[Geom::X] );
+ max_y = MAX( max_y, p1[Geom::Y] );
+
+ min_x = MIN( min_x, p2[Geom::X] );
+ min_y = MIN( min_y, p2[Geom::Y] );
+ max_x = MAX( max_x, p2[Geom::X] );
+ max_y = MAX( max_y, p2[Geom::Y] );
+
+ min_x = MIN( min_x, p3[Geom::X] );
+ min_y = MIN( min_y, p3[Geom::Y] );
+ max_x = MAX( max_x, p3[Geom::X] );
+ max_y = MAX( max_y, p3[Geom::Y] );
+
+ item->x1 = round( min_x - 1 );
+ item->y1 = round( min_y - 1 );
+ item->x2 = round( max_x + 1 );
+ item->y2 = round( max_y + 1 );
+
+ item->canvas->requestRedraw(item->x1, item->y1, item->x2, item->y2);
+
+ }
+}
+
+} // namespace
+
+#define EPSILON 1e-6
+#define DIFFER(a,b) (fabs ((a) - (b)) > EPSILON)
+
+void SPCtrlCurve::setCoords( gdouble x0, gdouble y0, gdouble x1, gdouble y1,
+ gdouble x2, gdouble y2, gdouble x3, gdouble y3 )
+{
+
+ Geom::Point q0( x0, y0 );
+ Geom::Point q1( x1, y1 );
+ Geom::Point q2( x2, y2 );
+ Geom::Point q3( x3, y3 );
+
+ setCoords( q0, q1, q2, q3 );
+
+}
+
+void SPCtrlCurve::setCoords( Geom::Point const &q0, Geom::Point const &q1,
+ Geom::Point const &q2, Geom::Point const &q3)
+{
+ if (DIFFER(p0[Geom::X], q0[Geom::X]) || DIFFER(p0[Geom::Y], q0[Geom::Y]) ||
+ DIFFER(p1[Geom::X], q1[Geom::X]) || DIFFER(p1[Geom::Y], q1[Geom::Y]) ||
+ DIFFER(p2[Geom::X], q2[Geom::X]) || DIFFER(p2[Geom::Y], q2[Geom::Y]) ||
+ DIFFER(p3[Geom::X], q3[Geom::X]) || DIFFER(p3[Geom::Y], q3[Geom::Y]) ) {
+ p0 = q0;
+ p1 = q1;
+ p2 = q2;
+ p3 = q3;
+ sp_canvas_item_request_update( this );
+ }
+}
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/display/sp-ctrlcurve.h b/src/display/sp-ctrlcurve.h
new file mode 100644
index 000000000..15de20161
--- /dev/null
+++ b/src/display/sp-ctrlcurve.h
@@ -0,0 +1,54 @@
+#ifndef SEEN_INKSCAPE_CTRLCURVE_H
+#define SEEN_INKSCAPE_CTRLCURVE_H
+
+/*
+ * Simple bezier curve (used for Mesh Gradients)
+ *
+ * Author:
+ * Tavmjong Bah <tavmjong@free.fr>
+ * Derived from sp-ctrlline
+ *
+ * Copyright (C) 2011 Tavmjong Bah
+ * Copyright (C) 2007 Johan Engelen
+ * Copyright (C) 1999-2002 Lauris Kaplinski
+ *
+ * Released under GNU GPL
+ */
+
+#include "display/sp-ctrlline.h"
+
+struct SPItem;
+
+#define SP_TYPE_CTRLCURVE (SPCtrlCurve::getType())
+#define SP_CTRLCURVE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SP_TYPE_CTRLCURVE, SPCtrlCurve))
+#define SP_IS_CTRLCURVE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SP_TYPE_CTRLCURVE))
+
+struct SPCtrlCurve : public SPCtrlLine {
+
+ static GType getType();
+
+ void setCoords( gdouble x0, gdouble y0, gdouble x1, gdouble y1,
+ gdouble x2, gdouble y2, gdouble x3, gdouble y3 );
+
+ void setCoords( Geom::Point const &q0, Geom::Point const &q1,
+ Geom::Point const &q2, Geom::Point const &q3);
+
+ Geom::Point p0, p1, p2, p3;
+};
+
+struct SPCtrlCurveClass : public SPCtrlLineClass{};
+
+
+
+#endif // SEEN_INKSCAPE_CTRLCURVE_H
+
+/*
+ Local Variables:
+ mode:c++
+ c-file-style:"stroustrup"
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+ indent-tabs-mode:nil
+ fill-column:99
+ End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
diff --git a/src/ui/control-manager.cpp b/src/ui/control-manager.cpp
index 124e591a8..6bd2d6fd8 100644
--- a/src/ui/control-manager.cpp
+++ b/src/ui/control-manager.cpp
@@ -20,6 +20,7 @@
#include "display/sodipodi-ctrl.h" // for SP_TYPE_CTRL
#include "display/sp-canvas-item.h"
#include "display/sp-ctrlline.h"
+#include "display/sp-ctrlcurve.h"
#include "display/sp-ctrlpoint.h"
#include "preferences.h"
@@ -423,6 +424,19 @@ SPCtrlLine *ControlManager::createControlLine(SPCanvasGroup *parent, Geom::Point
return line;
}
+SPCtrlCurve *ControlManager::createControlCurve(SPCanvasGroup *parent, Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2, Geom::Point const &p3, CtrlLineType type)
+{
+ SPCtrlCurve *line = SP_CTRLCURVE(sp_canvas_item_new(parent, SP_TYPE_CTRLCURVE, NULL));
+ if (line) {
+ line->ctrlType = CTRL_TYPE_LINE;
+
+ line->setRgba32((type == CTLINE_PRIMARY) ? LINE_COLOR_PRIMARY :
+ (type == CTLINE_SECONDARY) ? LINE_COLOR_SECONDARY : LINE_COLOR_TERTIARY);
+ line->setCoords(p0, p1, p2, p3);
+ }
+ return line;
+}
+
void ControlManager::track(SPCanvasItem *item)
{
_impl->track(item);
diff --git a/src/ui/control-manager.h b/src/ui/control-manager.h
index 4abb03e43..05a53f6a0 100644
--- a/src/ui/control-manager.h
+++ b/src/ui/control-manager.h
@@ -19,6 +19,7 @@
struct SPCanvasGroup;
struct SPCanvasItem;
struct SPCtrlLine;
+struct SPCtrlCurve;
namespace Geom
{
@@ -54,6 +55,8 @@ public:
SPCtrlLine *createControlLine(SPCanvasGroup *parent, Geom::Point const &p1, Geom::Point const &p2, CtrlLineType type = CTLINE_PRIMARY);
+ SPCtrlCurve *createControlCurve(SPCanvasGroup *parent, Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2, Geom::Point const &p3, CtrlLineType type = CTLINE_PRIMARY);
+
void track(SPCanvasItem *item);
void updateItem(SPCanvasItem *item);