X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Feraser-context.cpp;h=68c0989bc945b1a80c8ad3e5b6764ad2df468ecf;hb=ec12cb48c1da4c3029b9e8c2eea263499d319599;hp=3fe661f99b1b6a414c32e13439218de4811c41a2;hpb=5209a05743e62584a7ec6afe050ffb0223f899f8;p=inkscape.git diff --git a/src/eraser-context.cpp b/src/eraser-context.cpp index 3fe661f99..68c0989bc 100644 --- a/src/eraser-context.cpp +++ b/src/eraser-context.cpp @@ -7,6 +7,7 @@ * bulia byak * MenTaLguY * Jon A. Cruz + * Abhishek Sharma * * The original dynadraw code: * Paul Haeberli @@ -34,7 +35,7 @@ #include "svg/svg.h" #include "display/canvas-bpath.h" -#include "display/bezier-utils.h" +#include <2geom/bezier-utils.h> #include #include "macros.h" @@ -43,19 +44,13 @@ #include "desktop.h" #include "desktop-events.h" #include "desktop-handles.h" -#include "desktop-affine.h" #include "desktop-style.h" #include "message-context.h" -#include "prefs-utils.h" +#include "preferences.h" #include "pixmaps/cursor-eraser.xpm" -#include "libnr/n-art-bpath.h" -#include "libnr/nr-path.h" -#include "libnr/nr-matrix-ops.h" -#include "libnr/nr-scale-translate-ops.h" #include "xml/repr.h" #include "context-fns.h" #include "sp-item.h" -#include "inkscape.h" #include "color.h" #include "rubberband.h" #include "splivarot.h" @@ -66,10 +61,13 @@ #include "display/canvas-bpath.h" #include "display/canvas-arena.h" #include "livarot/Shape.h" -#include "isnan.h" +#include <2geom/isnan.h> +#include <2geom/pathvector.h> #include "eraser-context.h" +using Inkscape::DocumentUndo; + #define ERC_RED_RGBA 0xff0000ff #define TOLERANCE_ERASER 0.1 @@ -88,40 +86,41 @@ static void sp_eraser_context_init(SPEraserContext *erc); static void sp_eraser_context_dispose(GObject *object); static void sp_eraser_context_setup(SPEventContext *ec); -static void sp_eraser_context_set(SPEventContext *ec, gchar const *key, gchar const *val); +static void sp_eraser_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val); static gint sp_eraser_context_root_handler(SPEventContext *ec, GdkEvent *event); static void clear_current(SPEraserContext *dc); static void set_to_accumulated(SPEraserContext *dc); -static void add_cap(SPCurve *curve, NR::Point const &pre, NR::Point const &from, NR::Point const &to, NR::Point const &post, double rounding); +static void add_cap(SPCurve *curve, Geom::Point const &pre, Geom::Point const &from, Geom::Point const &to, Geom::Point const &post, double rounding); static void accumulate_eraser(SPEraserContext *dc); static void fit_and_split(SPEraserContext *erc, gboolean release); -static void sp_eraser_reset(SPEraserContext *erc, NR::Point p); -static NR::Point sp_eraser_get_npoint(SPEraserContext const *erc, NR::Point v); -static NR::Point sp_eraser_get_vpoint(SPEraserContext const *erc, NR::Point n); +static void sp_eraser_reset(SPEraserContext *erc, Geom::Point p); +static Geom::Point sp_eraser_get_npoint(SPEraserContext const *erc, Geom::Point v); +static Geom::Point sp_eraser_get_vpoint(SPEraserContext const *erc, Geom::Point n); static void draw_temporary_box(SPEraserContext *dc); -static SPEventContextClass *parent_class; +static SPEventContextClass *eraser_parent_class = 0; -GtkType -sp_eraser_context_get_type(void) +GType sp_eraser_context_get_type(void) { static GType type = 0; if (!type) { GTypeInfo info = { sizeof(SPEraserContextClass), - NULL, NULL, - (GClassInitFunc) sp_eraser_context_class_init, - NULL, NULL, + 0, // base_init + 0, // base_finalize + (GClassInitFunc)sp_eraser_context_class_init, + 0, // class_finalize + 0, // class_data sizeof(SPEraserContext), - 4, - (GInstanceInitFunc) sp_eraser_context_init, - NULL, /* value_table */ + 0, // n_preallocs + (GInstanceInitFunc)sp_eraser_context_init, + 0 // value_table }; - type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPEraserContext", &info, (GTypeFlags)0); + type = g_type_register_static(SP_TYPE_COMMON_CONTEXT, "SPEraserContext", &info, static_cast(0)); } return type; } @@ -132,7 +131,7 @@ sp_eraser_context_class_init(SPEraserContextClass *klass) GObjectClass *object_class = (GObjectClass *) klass; SPEventContextClass *event_context_class = (SPEventContextClass *) klass; - parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass); + eraser_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass); object_class->dispose = sp_eraser_context_dispose; @@ -144,96 +143,39 @@ sp_eraser_context_class_init(SPEraserContextClass *klass) static void sp_eraser_context_init(SPEraserContext *erc) { - SPEventContext *event_context = SP_EVENT_CONTEXT(erc); - - event_context->cursor_shape = cursor_eraser_xpm; - event_context->hot_x = 4; - event_context->hot_y = 4; - - erc->accumulated = NULL; - erc->segments = NULL; - erc->currentcurve = NULL; - erc->currentshape = NULL; - erc->npoints = 0; - erc->cal1 = NULL; - erc->cal2 = NULL; - erc->repr = NULL; - - /* Eraser values */ - erc->cur = NR::Point(0,0); - erc->last = NR::Point(0,0); - erc->vel = NR::Point(0,0); - erc->vel_max = 0; - erc->acc = NR::Point(0,0); - erc->ang = NR::Point(0,0); - erc->del = NR::Point(0,0); - - /* attributes */ - erc->dragging = FALSE; - - erc->mass = 0.3; - erc->drag = DRAG_DEFAULT; - erc->angle = 30.0; - erc->width = 0.2; - erc->pressure = ERC_DEFAULT_PRESSURE; - - erc->vel_thin = 0.1; - erc->flatness = 0.9; - erc->cap_rounding = 0.0; - - erc->abs_width = false; + erc->cursor_shape = cursor_eraser_xpm; + erc->hot_x = 4; + erc->hot_y = 4; } static void sp_eraser_context_dispose(GObject *object) { - SPEraserContext *erc = SP_ERASER_CONTEXT(object); - - if (erc->accumulated) { - erc->accumulated = erc->accumulated->unref(); - } - - while (erc->segments) { - gtk_object_destroy(GTK_OBJECT(erc->segments->data)); - erc->segments = g_slist_remove(erc->segments, erc->segments->data); - } - - if (erc->currentcurve) erc->currentcurve = erc->currentcurve->unref(); - if (erc->cal1) erc->cal1 = erc->cal1->unref(); - if (erc->cal2) erc->cal2 = erc->cal2->unref(); - - if (erc->currentshape) { - gtk_object_destroy(GTK_OBJECT(erc->currentshape)); - erc->currentshape = 0; - } - - if (erc->_message_context) { - delete erc->_message_context; - erc->_message_context = 0; - } + //SPEraserContext *erc = SP_ERASER_CONTEXT(object); - G_OBJECT_CLASS(parent_class)->dispose(object); + G_OBJECT_CLASS(eraser_parent_class)->dispose(object); } static void sp_eraser_context_setup(SPEventContext *ec) { SPEraserContext *erc = SP_ERASER_CONTEXT(ec); + SPDesktop *desktop = ec->desktop; - if (((SPEventContextClass *) parent_class)->setup) - ((SPEventContextClass *) parent_class)->setup(ec); + if (((SPEventContextClass *) eraser_parent_class)->setup) + ((SPEventContextClass *) eraser_parent_class)->setup(ec); - erc->accumulated = new SPCurve(32); - erc->currentcurve = new SPCurve(4); + erc->accumulated = new SPCurve(); + erc->currentcurve = new SPCurve(); - erc->cal1 = new SPCurve(32); - erc->cal2 = new SPCurve(32); + erc->cal1 = new SPCurve(); + erc->cal2 = new SPCurve(); - erc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL); + erc->currentshape = sp_canvas_item_new(sp_desktop_sketch(desktop), SP_TYPE_CANVAS_BPATH, NULL); sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(erc->currentshape), ERC_RED_RGBA, SP_WIND_RULE_EVENODD); sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(erc->currentshape), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT); /* fixme: Cannot we cascade it to root more clearly? */ - g_signal_connect(G_OBJECT(erc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop); + g_signal_connect(G_OBJECT(erc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), desktop); /* static ProfileFloatElement f_profile[PROFILE_FLOAT_SIZE] = { @@ -262,9 +204,10 @@ static ProfileFloatElement f_profile[PROFILE_FLOAT_SIZE] = { erc->is_drawing = false; - erc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack()); + erc->_message_context = new Inkscape::MessageContext(desktop->messageStack()); - if (prefs_get_int_attribute("tools.eraser", "selcue", 0) != 0) { + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + if (prefs->getBool("/tools/eraser/selcue", 0) != 0) { ec->enableSelectionCue(); } // TODO temp force: @@ -273,42 +216,12 @@ static ProfileFloatElement f_profile[PROFILE_FLOAT_SIZE] = { } static void -sp_eraser_context_set(SPEventContext *ec, gchar const *key, gchar const *val) +sp_eraser_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val) { - SPEraserContext *erc = SP_ERASER_CONTEXT(ec); - - if (!strcmp(key, "mass")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.2 ); - erc->mass = CLAMP(dval, -1000.0, 1000.0); - } else if (!strcmp(key, "wiggle")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : (1 - DRAG_DEFAULT)); - erc->drag = CLAMP((1 - dval), DRAG_MIN, DRAG_MAX); // drag is inverse to wiggle - } else if (!strcmp(key, "angle")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0); - erc->angle = CLAMP (dval, -90, 90); - } else if (!strcmp(key, "width")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 ); - erc->width = CLAMP(dval, -1000.0, 1000.0); - } else if (!strcmp(key, "thinning")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 ); - erc->vel_thin = CLAMP(dval, -1.0, 1.0); - } else if (!strcmp(key, "tremor")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 ); - erc->tremor = CLAMP(dval, 0.0, 1.0); - } else if (!strcmp(key, "flatness")) { - double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 ); - erc->flatness = CLAMP(dval, 0, 1.0); - } else if (!strcmp(key, "usepressure")) { - erc->usepressure = (val && strcmp(val, "0")); - } else if (!strcmp(key, "usetilt")) { - erc->usetilt = (val && strcmp(val, "0")); - } else if (!strcmp(key, "abs_width")) { - erc->abs_width = (val && strcmp(val, "0")); - } else if (!strcmp(key, "cap_rounding")) { - erc->cap_rounding = ( val ? g_ascii_strtod (val, NULL) : 0.0 ); + //pass on up to parent class to handle common attributes. + if ( eraser_parent_class->set ) { + eraser_parent_class->set(ec, val); } - - //g_print("ERC: %g %g %g %g\n", erc->mass, erc->drag, erc->angle, erc->width); } static double @@ -318,32 +231,32 @@ flerp(double f0, double f1, double p) } /* Get normalized point */ -static NR::Point -sp_eraser_get_npoint(SPEraserContext const *dc, NR::Point v) +static Geom::Point +sp_eraser_get_npoint(SPEraserContext const *dc, Geom::Point v) { - NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area(); - double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] ); - return NR::Point(( v[NR::X] - drect.min()[NR::X] ) / max, ( v[NR::Y] - drect.min()[NR::Y] ) / max); + Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area(); + double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] ); + return Geom::Point(( v[Geom::X] - drect.min()[Geom::X] ) / max, ( v[Geom::Y] - drect.min()[Geom::Y] ) / max); } /* Get view point */ -static NR::Point -sp_eraser_get_vpoint(SPEraserContext const *dc, NR::Point n) +static Geom::Point +sp_eraser_get_vpoint(SPEraserContext const *dc, Geom::Point n) { - NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area(); - double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] ); - return NR::Point(n[NR::X] * max + drect.min()[NR::X], n[NR::Y] * max + drect.min()[NR::Y]); + Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area(); + double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] ); + return Geom::Point(n[Geom::X] * max + drect.min()[Geom::X], n[Geom::Y] * max + drect.min()[Geom::Y]); } static void -sp_eraser_reset(SPEraserContext *dc, NR::Point p) +sp_eraser_reset(SPEraserContext *dc, Geom::Point p) { dc->last = dc->cur = sp_eraser_get_npoint(dc, p); - dc->vel = NR::Point(0,0); + dc->vel = Geom::Point(0,0); dc->vel_max = 0; - dc->acc = NR::Point(0,0); - dc->ang = NR::Point(0,0); - dc->del = NR::Point(0,0); + dc->acc = Geom::Point(0,0); + dc->ang = Geom::Point(0,0); + dc->del = Geom::Point(0,0); } static void @@ -367,16 +280,16 @@ sp_eraser_extinput(SPEraserContext *dc, GdkEvent *event) static gboolean -sp_eraser_apply(SPEraserContext *dc, NR::Point p) +sp_eraser_apply(SPEraserContext *dc, Geom::Point p) { - NR::Point n = sp_eraser_get_npoint(dc, p); + Geom::Point n = sp_eraser_get_npoint(dc, p); /* Calculate mass and drag */ double const mass = flerp(1.0, 160.0, dc->mass); double const drag = flerp(0.0, 0.5, dc->drag * dc->drag); /* Calculate force and acceleration */ - NR::Point force = n - dc->cur; + Geom::Point force = n - dc->cur; // If force is below the absolute threshold ERASER_EPSILON, // or we haven't yet reached ERASER_VEL_START (i.e. at the beginning of stroke) @@ -385,7 +298,7 @@ sp_eraser_apply(SPEraserContext *dc, NR::Point p) // This prevents flips, blobs, and jerks caused by microscopic tremor of the tablet pen, // especially bothersome at the start of the stroke where we don't yet have the inertia to // smooth them out. - if ( NR::L2(force) < ERASER_EPSILON || (dc->vel_max < ERASER_VEL_START && NR::L2(force) < ERASER_EPSILON_START)) { + if ( Geom::L2(force) < ERASER_EPSILON || (dc->vel_max < ERASER_VEL_START && Geom::L2(force) < ERASER_EPSILON_START)) { return FALSE; } @@ -394,8 +307,8 @@ sp_eraser_apply(SPEraserContext *dc, NR::Point p) /* Calculate new velocity */ dc->vel += dc->acc; - if (NR::L2(dc->vel) > dc->vel_max) - dc->vel_max = NR::L2(dc->vel); + if (Geom::L2(dc->vel) > dc->vel_max) + dc->vel_max = Geom::L2(dc->vel); /* Calculate angle of drawing tool */ @@ -405,7 +318,7 @@ sp_eraser_apply(SPEraserContext *dc, NR::Point p) gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);; if (length > 0) { - NR::Point ang1 = NR::Point(dc->ytilt/length, dc->xtilt/length); + Geom::Point ang1 = Geom::Point(dc->ytilt/length, dc->xtilt/length); a1 = atan2(ang1); } else @@ -414,16 +327,16 @@ sp_eraser_apply(SPEraserContext *dc, NR::Point p) else { // 1b. fixed dc->angle (absolutely flat nib): double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI; - NR::Point ang1 = NR::Point(-sin(radians), cos(radians)); + Geom::Point ang1 = Geom::Point(-sin(radians), cos(radians)); a1 = atan2(ang1); } // 2. perpendicular to dc->vel (absolutely non-flat nib): - gdouble const mag_vel = NR::L2(dc->vel); + gdouble const mag_vel = Geom::L2(dc->vel); if ( mag_vel < ERASER_EPSILON ) { return FALSE; } - NR::Point ang2 = NR::rot90(dc->vel) / mag_vel; + Geom::Point ang2 = Geom::rot90(dc->vel) / mag_vel; // 3. Average them using flatness parameter: // calculate angles @@ -445,15 +358,15 @@ sp_eraser_apply(SPEraserContext *dc, NR::Point p) // Try to detect a sudden flip when the new angle differs too much from the previous for the // current velocity; in that case discard this move - double angle_delta = NR::L2(NR::Point (cos (new_ang), sin (new_ang)) - dc->ang); - if ( angle_delta / NR::L2(dc->vel) > 4000 ) { + double angle_delta = Geom::L2(Geom::Point (cos (new_ang), sin (new_ang)) - dc->ang); + if ( angle_delta / Geom::L2(dc->vel) > 4000 ) { return FALSE; } // convert to point - dc->ang = NR::Point (cos (new_ang), sin (new_ang)); + dc->ang = Geom::Point (cos (new_ang), sin (new_ang)); -// g_print ("force %g acc %g vel_max %g vel %g a1 %g a2 %g new_ang %g\n", NR::L2(force), NR::L2(dc->acc), dc->vel_max, NR::L2(dc->vel), a1, a2, new_ang); +// g_print ("force %g acc %g vel_max %g vel %g a1 %g a2 %g new_ang %g\n", Geom::L2(force), Geom::L2(dc->acc), dc->vel_max, Geom::L2(dc->vel), a1, a2, new_ang); /* Apply drag */ dc->vel *= 1.0 - drag; @@ -478,12 +391,12 @@ sp_eraser_brush(SPEraserContext *dc) // get the real brush point, not the same as pointer (affected by hatch tracking and/or mass // drag) - NR::Point brush = sp_eraser_get_vpoint(dc, dc->cur); - NR::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush); + Geom::Point brush = sp_eraser_get_vpoint(dc, dc->cur); + Geom::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush); double trace_thick = 1; - double width = (pressure_thick * trace_thick - vel_thin * NR::L2(dc->vel)) * dc->width; + double width = (pressure_thick * trace_thick - vel_thin * Geom::L2(dc->vel)) * dc->width; double tremble_left = 0, tremble_right = 0; if (dc->tremor > 0) { @@ -503,8 +416,8 @@ sp_eraser_brush(SPEraserContext *dc) // (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths; // (3) deflection somewhat depends on speed, to prevent fast strokes looking // comparatively smooth and slow ones excessively jittery - tremble_left = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel)); - tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel)); + tremble_left = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*Geom::L2(dc->vel)); + tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*Geom::L2(dc->vel)); } if ( width < 0.02 * dc->width ) { @@ -516,8 +429,8 @@ sp_eraser_brush(SPEraserContext *dc) dezoomify_factor /= SP_EVENT_CONTEXT(dc)->desktop->current_zoom(); } - NR::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang; - NR::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang; + Geom::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang; + Geom::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang; dc->point1[dc->npoints] = brush + del_left; dc->point2[dc->npoints] = brush - del_right; @@ -567,15 +480,13 @@ sp_eraser_context_root_handler(SPEventContext *event_context, case GDK_BUTTON_PRESS: if (event->button.button == 1 && !event_context->space_panning) { - SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc); - if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) { return TRUE; } - NR::Point const button_w(event->button.x, + Geom::Point const button_w(event->button.x, event->button.y); - NR::Point const button_dt(desktop->w2d(button_w)); + Geom::Point const button_dt(desktop->w2d(button_w)); sp_eraser_reset(dc, button_dt); sp_eraser_extinput(dc, event); sp_eraser_apply(dc, button_dt); @@ -584,8 +495,8 @@ sp_eraser_context_root_handler(SPEventContext *event_context, dc->repr = NULL; } - Inkscape::Rubberband::get()->start(desktop, button_dt); - Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH); + Inkscape::Rubberband::get(desktop)->start(desktop, button_dt); + Inkscape::Rubberband::get(desktop)->setMode(RUBBERBAND_MODE_TOUCHPATH); /* initialize first point */ dc->npoints = 0; @@ -606,9 +517,9 @@ sp_eraser_context_root_handler(SPEventContext *event_context, break; case GDK_MOTION_NOTIFY: { - NR::Point const motion_w(event->motion.x, + Geom::Point const motion_w(event->motion.x, event->motion.y); - NR::Point motion_dt(desktop->w2d(motion_w)); + Geom::Point motion_dt(desktop->w2d(motion_w)); sp_eraser_extinput(dc, event); dc->_message_context->clear(); @@ -630,15 +541,15 @@ sp_eraser_context_root_handler(SPEventContext *event_context, } ret = TRUE; } - Inkscape::Rubberband::get()->move(motion_dt); + Inkscape::Rubberband::get(desktop)->move(motion_dt); } break; case GDK_BUTTON_RELEASE: { - NR::Point const motion_w(event->button.x, event->button.y); - NR::Point const motion_dt(desktop->w2d(motion_w)); + Geom::Point const motion_w(event->button.x, event->button.y); + Geom::Point const motion_dt(desktop->w2d(motion_w)); sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time); sp_canvas_end_forced_full_redraws(desktop->canvas); @@ -647,8 +558,6 @@ sp_eraser_context_root_handler(SPEventContext *event_context, if (dc->dragging && event->button.button == 1 && !event_context->space_panning) { dc->dragging = FALSE; - NR::Maybe const b = Inkscape::Rubberband::get()->getRectangle(); - sp_eraser_apply(dc, motion_dt); /* Remove all temporary line segments */ @@ -670,10 +579,13 @@ sp_eraser_context_root_handler(SPEventContext *event_context, dc->repr = NULL; } - Inkscape::Rubberband::get()->stop(); dc->_message_context->clear(); ret = TRUE; } + if (Inkscape::Rubberband::get(desktop)->is_started()) { + Inkscape::Rubberband::get(desktop)->stop(); + } + break; } @@ -739,7 +651,7 @@ sp_eraser_context_root_handler(SPEventContext *event_context, } break; case GDK_Escape: - Inkscape::Rubberband::get()->stop(); + Inkscape::Rubberband::get(desktop)->stop(); if (dc->is_drawing) { // if drawing, cancel, otherwise pass it up for deselecting eraser_cancel (dc); @@ -774,8 +686,8 @@ sp_eraser_context_root_handler(SPEventContext *event_context, } if (!ret) { - if (((SPEventContextClass *) parent_class)->root_handler) { - ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event); + if (((SPEventContextClass *) eraser_parent_class)->root_handler) { + ret = ((SPEventContextClass *) eraser_parent_class)->root_handler(event_context, event); } } @@ -805,48 +717,46 @@ set_to_accumulated(SPEraserContext *dc) bool workDone = false; if (!dc->accumulated->is_empty()) { - NArtBpath *abp; - gchar *str; - if (!dc->repr) { /* Create object */ - Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc(); Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); /* Set style */ - sp_desktop_apply_style_tool (desktop, repr, "tools.eraser", false); + sp_desktop_apply_style_tool (desktop, repr, "/tools/eraser", false); dc->repr = repr; SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr)); Inkscape::GC::release(dc->repr); - item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer()); + item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); item->updateRepr(); } - abp = nr_artpath_affine(dc->accumulated->first_bpath(), sp_desktop_dt2root_affine(desktop)); - str = sp_svg_write_path(abp); + Geom::PathVector pathv = dc->accumulated->get_pathvector() * desktop->dt2doc(); + gchar *str = sp_svg_write_path(pathv); g_assert( str != NULL ); - g_free(abp); dc->repr->setAttribute("d", str); g_free(str); if ( dc->repr ) { bool wasSelection = false; Inkscape::Selection *selection = sp_desktop_selection(desktop); - gint eraserMode = (prefs_get_int_attribute("tools.eraser", "mode", 0) != 0) ? 1 : 0; - Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc()); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + + gint eraserMode = prefs->getBool("/tools/eraser/mode") ? 1 : 0; + Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc(); SPItem* acid = SP_ITEM(desktop->doc()->getObjectByRepr(dc->repr)); - NR::Maybe eraserBbox = acid->getBounds(NR::identity()); - NR::Rect bounds = (*eraserBbox) * desktop->doc2dt(); + Geom::OptRect eraserBbox = acid->getBounds(Geom::identity()); + Geom::Rect bounds = (*eraserBbox) * desktop->doc2dt(); std::vector remainingItems; GSList* toWorkOn = 0; if (selection->isEmpty()) { if ( eraserMode ) { - toWorkOn = sp_document_partial_items_in_box(sp_desktop_document(desktop), desktop->dkey, bounds); + toWorkOn = sp_desktop_document(desktop)->getItemsPartiallyInBox(desktop->dkey, bounds); } else { - Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(); - toWorkOn = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints()); + Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop); + toWorkOn = sp_desktop_document(desktop)->getItemsAtPoints(desktop->dkey, r->getPoints()); } toWorkOn = g_slist_remove( toWorkOn, acid ); } else { @@ -859,7 +769,7 @@ set_to_accumulated(SPEraserContext *dc) for (GSList *i = toWorkOn ; i ; i = i->next ) { SPItem *item = SP_ITEM(i->data); if ( eraserMode ) { - NR::Maybe bbox = item->getBounds(NR::identity()); + Geom::OptRect bbox = item->getBounds(Geom::identity()); if (bbox && bbox->intersects(*eraserBbox)) { Inkscape::XML::Node* dup = dc->repr->duplicate(xml_doc); dc->repr->parent()->appendChild(dup); @@ -867,7 +777,7 @@ set_to_accumulated(SPEraserContext *dc) selection->set(item); selection->add(dup); - sp_selected_path_diff_skip_undo(); + sp_selected_path_diff_skip_undo(desktop); workDone = true; // TODO set this only if something was cut. if ( !selection->isEmpty() ) { // If the item was not completely erased, track the new remainder. @@ -897,7 +807,7 @@ set_to_accumulated(SPEraserContext *dc) g_slist_free(toWorkOn); if ( !eraserMode ) { - //sp_selection_delete(); + //sp_selection_delete(desktop); remainingItems.clear(); } @@ -922,39 +832,39 @@ set_to_accumulated(SPEraserContext *dc) if ( workDone ) { - sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ERASER, - _("Draw eraser stroke")); + DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ERASER, + _("Draw eraser stroke")); } else { - sp_document_cancel(sp_desktop_document(desktop)); + DocumentUndo::cancel(sp_desktop_document(desktop)); } } static void add_cap(SPCurve *curve, - NR::Point const &pre, NR::Point const &from, - NR::Point const &to, NR::Point const &post, + Geom::Point const &pre, Geom::Point const &from, + Geom::Point const &to, Geom::Point const &post, double rounding) { - NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0); - double mag = NR::L2(vel); + Geom::Point vel = rounding * Geom::rot90( to - from ) / sqrt(2.0); + double mag = Geom::L2(vel); - NR::Point v_in = from - pre; - double mag_in = NR::L2(v_in); + Geom::Point v_in = from - pre; + double mag_in = Geom::L2(v_in); if ( mag_in > ERASER_EPSILON ) { v_in = mag * v_in / mag_in; } else { - v_in = NR::Point(0, 0); + v_in = Geom::Point(0, 0); } - NR::Point v_out = to - post; - double mag_out = NR::L2(v_out); + Geom::Point v_out = to - post; + double mag_out = Geom::L2(v_out); if ( mag_out > ERASER_EPSILON ) { v_out = mag * v_out / mag_out; } else { - v_out = NR::Point(0, 0); + v_out = Geom::Point(0, 0); } - if ( NR::L2(v_in) > ERASER_EPSILON || NR::L2(v_out) > ERASER_EPSILON ) { + if ( Geom::L2(v_in) > ERASER_EPSILON || Geom::L2(v_out) > ERASER_EPSILON ) { curve->curveto(from + v_in, to + v_out, to); } } @@ -966,22 +876,27 @@ accumulate_eraser(SPEraserContext *dc) dc->accumulated->reset(); /* Is this required ?? */ SPCurve *rev_cal2 = dc->cal2->create_reverse(); - g_assert(dc->cal1->_end > 1); - g_assert(rev_cal2->_end > 1); - g_assert(SP_CURVE_SEGMENT(dc->cal1, 0)->code == NR_MOVETO_OPEN); - g_assert(SP_CURVE_SEGMENT(rev_cal2, 0)->code == NR_MOVETO_OPEN); - g_assert(SP_CURVE_SEGMENT(dc->cal1, 1)->code == NR_CURVETO); - g_assert(SP_CURVE_SEGMENT(rev_cal2, 1)->code == NR_CURVETO); - g_assert(SP_CURVE_SEGMENT(dc->cal1, dc->cal1->_end-1)->code == NR_CURVETO); - g_assert(SP_CURVE_SEGMENT(rev_cal2, rev_cal2->_end-1)->code == NR_CURVETO); + g_assert(dc->cal1->get_segment_count() > 0); + g_assert(rev_cal2->get_segment_count() > 0); + g_assert( ! dc->cal1->first_path()->closed() ); + g_assert( ! rev_cal2->first_path()->closed() ); + + Geom::CubicBezier const * dc_cal1_firstseg = dynamic_cast( dc->cal1->first_segment() ); + Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast( rev_cal2->first_segment() ); + Geom::CubicBezier const * dc_cal1_lastseg = dynamic_cast( dc->cal1->last_segment() ); + Geom::CubicBezier const * rev_cal2_lastseg = dynamic_cast( rev_cal2->last_segment() ); + g_assert( dc_cal1_firstseg ); + g_assert( rev_cal2_firstseg ); + g_assert( dc_cal1_lastseg ); + g_assert( rev_cal2_lastseg ); dc->accumulated->append(dc->cal1, FALSE); - add_cap(dc->accumulated, SP_CURVE_SEGMENT(dc->cal1, dc->cal1->_end-1)->c(2), SP_CURVE_SEGMENT(dc->cal1, dc->cal1->_end-1)->c(3), SP_CURVE_SEGMENT(rev_cal2, 0)->c(3), SP_CURVE_SEGMENT(rev_cal2, 1)->c(1), dc->cap_rounding); + add_cap(dc->accumulated, (*dc_cal1_lastseg)[2], (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], (*rev_cal2_firstseg)[1], dc->cap_rounding); dc->accumulated->append(rev_cal2, TRUE); - add_cap(dc->accumulated, SP_CURVE_SEGMENT(rev_cal2, rev_cal2->_end-1)->c(2), SP_CURVE_SEGMENT(rev_cal2, rev_cal2->_end-1)->c(3), SP_CURVE_SEGMENT(dc->cal1, 0)->c(3), SP_CURVE_SEGMENT(dc->cal1, 1)->c(1), dc->cap_rounding); + add_cap(dc->accumulated, (*rev_cal2_lastseg)[2], (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[0], (*dc_cal1_firstseg)[1], dc->cap_rounding); dc->accumulated->closepath(); @@ -1000,7 +915,9 @@ static double square(double const x) static void fit_and_split(SPEraserContext *dc, gboolean release) { - double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_ERASER ); + SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop; + + double const tolerance_sq = square( desktop->w2d().descrim() * TOLERANCE_ERASER ); #ifdef ERASER_VERBOSE g_print("[F&S:R=%c]", release?'T':'F'); @@ -1020,7 +937,7 @@ fit_and_split(SPEraserContext *dc, gboolean release) #endif /* Current eraser */ - if ( dc->cal1->_end == 0 || dc->cal2->_end == 0 ) { + if ( dc->cal1->is_empty() || dc->cal2->is_empty() ) { /* dc->npoints > 0 */ /* g_print("erasers(1|2) reset\n"); */ dc->cal1->reset(); @@ -1030,13 +947,13 @@ fit_and_split(SPEraserContext *dc, gboolean release) dc->cal2->moveto(dc->point2[0]); } - NR::Point b1[BEZIER_MAX_LENGTH]; - gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints, + Geom::Point b1[BEZIER_MAX_LENGTH]; + gint const nb1 = Geom::bezier_fit_cubic_r(b1, dc->point1, dc->npoints, tolerance_sq, BEZIER_MAX_BEZIERS); g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) ); - NR::Point b2[BEZIER_MAX_LENGTH]; - gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints, + Geom::Point b2[BEZIER_MAX_LENGTH]; + gint const nb2 = Geom::bezier_fit_cubic_r(b2, dc->point2, dc->npoints, tolerance_sq, BEZIER_MAX_BEZIERS); g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) ); @@ -1049,12 +966,12 @@ fit_and_split(SPEraserContext *dc, gboolean release) if (! release) { dc->currentcurve->reset(); dc->currentcurve->moveto(b1[0]); - for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) { + for (Geom::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) { dc->currentcurve->curveto(bp1[1], bp1[2], bp1[3]); } dc->currentcurve->lineto(b2[BEZIER_SIZE*(nb2-1) + 3]); - for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) { + for (Geom::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) { dc->currentcurve->curveto(bp2[2], bp2[1], bp2[0]); } // FIXME: dc->segments is always NULL at this point?? @@ -1066,10 +983,10 @@ fit_and_split(SPEraserContext *dc, gboolean release) } /* Current eraser */ - for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) { + for (Geom::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) { dc->cal1->curveto(bp1[1], bp1[2], bp1[3]); } - for (NR::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) { + for (Geom::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) { dc->cal2->curveto(bp2[1], bp2[2], bp2[3]); } } else { @@ -1092,27 +1009,28 @@ fit_and_split(SPEraserContext *dc, gboolean release) g_print("[%d]Yup\n", dc->npoints); #endif if (!release) { - gint eraserMode = (prefs_get_int_attribute("tools.eraser", "mode", 0) != 0) ? 1 : 0; + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + gint eraserMode = prefs->getBool("/tools/eraser/mode") ? 1 : 0; g_assert(!dc->currentcurve->is_empty()); - SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(SP_EVENT_CONTEXT(dc)->desktop), + SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(desktop), SP_TYPE_CANVAS_BPATH, NULL); SPCurve *curve = dc->currentcurve->copy(); sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve); curve->unref(); - guint32 fillColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.eraser", true); - //guint32 strokeColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.eraser", false); - double opacity = sp_desktop_get_master_opacity_tool (SP_ACTIVE_DESKTOP, "tools.eraser"); - double fillOpacity = sp_desktop_get_opacity_tool (SP_ACTIVE_DESKTOP, "tools.eraser", true); - //double strokeOpacity = sp_desktop_get_opacity_tool (SP_ACTIVE_DESKTOP, "tools.eraser", false); + guint32 fillColor = sp_desktop_get_color_tool (desktop, "/tools/eraser", true); + //guint32 strokeColor = sp_desktop_get_color_tool (desktop, "/tools/eraser", false); + double opacity = sp_desktop_get_master_opacity_tool (desktop, "/tools/eraser"); + double fillOpacity = sp_desktop_get_opacity_tool (desktop, "/tools/eraser", true); + //double strokeOpacity = sp_desktop_get_opacity_tool (desktop, "/tools/eraser", false); sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), ((fillColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*fillOpacity)), SP_WIND_RULE_EVENODD); //on second thougtht don't do stroke yet because we don't have stoke-width yet and because stoke appears between segments while drawing //sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), ((strokeColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*strokeOpacity)), 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT); sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT); /* fixme: Cannot we cascade it to root more clearly? */ - g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), SP_EVENT_CONTEXT(dc)->desktop); + g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), desktop); dc->segments = g_slist_prepend(dc->segments, cbp); @@ -1159,4 +1077,4 @@ draw_temporary_box(SPEraserContext *dc) fill-column:99 End: */ -// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :