eraser-tool.cpp revision 1072e790ede90357eae7b318f3ae87586727727a
/*
* Eraser drawing mode
*
* Authors:
* Mitsuru Oka <oka326@parkcity.ne.jp>
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* MenTaLguY <mental@rydia.net>
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* The original dynadraw code:
* Paul Haeberli <paul@sgi.com>
*
* Copyright (C) 1998 The Free Software Foundation
* Copyright (C) 1999-2005 authors
* Copyright (C) 2001-2002 Ximian, Inc.
* Copyright (C) 2005-2007 bulia byak
* Copyright (C) 2006 MenTaLguY
* Copyright (C) 2008 Jon A. Cruz
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#define noERASER_VERBOSE
#include "config.h"
#include <gdk/gdkkeysyms.h>
#include <string>
#include <cstring>
#include <numeric>
#include "display/sp-canvas.h"
#include "display/canvas-bpath.h"
#include <glib.h>
#include "macros.h"
#include "document.h"
#include "selection.h"
#include "desktop.h"
#include "desktop-events.h"
#include "desktop-style.h"
#include "message-context.h"
#include "preferences.h"
#include "pixmaps/cursor-eraser.xpm"
#include "context-fns.h"
#include "sp-item.h"
#include "color.h"
#include "rubberband.h"
#include "splivarot.h"
#include "sp-item-group.h"
#include "sp-shape.h"
#include "sp-path.h"
#include "sp-text.h"
#include "display/canvas-bpath.h"
#include "display/canvas-arena.h"
#include "document-undo.h"
#include "verbs.h"
#include "ui/tools/eraser-tool.h"
using Inkscape::DocumentUndo;
#define ERC_RED_RGBA 0xff0000ff
#define TOLERANCE_ERASER 0.1
#define ERASER_EPSILON 0.5e-6
#define ERASER_EPSILON_START 0.5e-2
#define ERASER_VEL_START 1e-5
#define DRAG_MIN 0.0
#define DRAG_DEFAULT 1.0
#define DRAG_MAX 1.0
namespace Inkscape {
namespace UI {
namespace Tools {
return EraserTool::prefsPath;
}
{
}
EraserTool::~EraserTool() {
}
void EraserTool::setup() {
DynamicBase::setup();
this->accumulated = new SPCurve();
this->currentcurve = new SPCurve();
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->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(this->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), desktop);
/*
static ProfileFloatElement f_profile[PROFILE_FLOAT_SIZE] = {
{"mass",0.02, 0.0, 1.0},
{"wiggle",0.0, 0.0, 1.0},
{"angle",30.0, -90.0, 90.0},
{"thinning",0.1, -1.0, 1.0},
{"tremor",0.0, 0.0, 1.0},
{"flatness",0.9, 0.0, 1.0},
{"cap_rounding",0.0, 0.0, 5.0}
};
*/
sp_event_context_read(this, "mass");
sp_event_context_read(this, "wiggle");
sp_event_context_read(this, "angle");
sp_event_context_read(this, "width");
sp_event_context_read(this, "thinning");
sp_event_context_read(this, "tremor");
sp_event_context_read(this, "flatness");
sp_event_context_read(this, "tracebackground");
sp_event_context_read(this, "usepressure");
sp_event_context_read(this, "usetilt");
sp_event_context_read(this, "abs_width");
sp_event_context_read(this, "cap_rounding");
this->is_drawing = false;
this->enableSelectionCue();
}
// TODO temp force:
this->enableSelectionCue();
}
static double
{
}
this->vel_max = 0;
}
else
this->pressure = ERC_DEFAULT_PRESSURE;
else
this->xtilt = ERC_DEFAULT_TILT;
else
this->ytilt = ERC_DEFAULT_TILT;
}
/* Calculate mass and drag */
/* Calculate force and acceleration */
// 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)
// _and_ the force is below the (higher) ERASER_EPSILON_START threshold,
// discard this move.
// 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 ( Geom::L2(force) < ERASER_EPSILON || (this->vel_max < ERASER_VEL_START && Geom::L2(force) < ERASER_EPSILON_START)) {
return FALSE;
}
/* Calculate new velocity */
/* Calculate angle of drawing tool */
double a1;
if (this->usetilt) {
// 1a. calculate nib angle from input device tilt:
if (length > 0) {
}
else
a1 = 0.0;
}
else {
// 1b. fixed dc->angle (absolutely flat nib):
}
// 2. perpendicular to dc->vel (absolutely non-flat nib):
if ( mag_vel < ERASER_EPSILON ) {
return FALSE;
}
// 3. Average them using flatness parameter:
// calculate angles
// flip a2 to force it to be in the same half-circle as a1
bool flipped = false;
flipped = true;
}
// normalize a2
// find the flatness-weighted bisector angle, unflip if a2 was flipped
// FIXME: when dc->vel is oscillating around the fixed angle, the new_ang flips back and forth. How to avoid this?
// 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
return FALSE;
}
// convert to point
// 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 */
/* Update position */
return TRUE;
}
void EraserTool::brush() {
// How much velocity thins strokestyle
// Influence of pressure on thickness
// drag)
//Geom::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush);
double trace_thick = 1;
double tremble_left = 0, tremble_right = 0;
if (this->tremor > 0) {
// obtain two normally distributed random variables, using polar Box-Muller transform
do {
} while ( w >= 1.0 );
// deflect both left and right edges randomly and independently, so that:
// (1) dc->tremor=1 corresponds to sigma=1, decreasing dc->tremor narrows the bell curve;
// (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
}
}
if (!this->abs_width) {
}
this->npoints++;
}
static void
{
}
void EraserTool::cancel() {
this->is_drawing = false;
/* Remove all temporary line segments */
while (this->segments) {
}
/* reset accumulated curve */
this->accumulated->reset();
this->clear_current();
if (this->repr) {
}
}
case GDK_BUTTON_PRESS:
return TRUE;
}
this->accumulated->reset();
if (this->repr) {
}
/* initialize first point */
this->npoints = 0;
NULL,
this->is_drawing = true;
}
break;
case GDK_MOTION_NOTIFY: {
);
this->message_context->clear();
break;
}
this->brush();
this->fit_and_split(false);
}
}
}
break;
case GDK_BUTTON_RELEASE: {
this->is_drawing = false;
/* Remove all temporary line segments */
while (this->segments) {
}
/* Create object */
this->fit_and_split(true);
this->accumulate();
this->set_to_accumulated(); // performs document_done
/* reset accumulated curve */
this->accumulated->reset();
this->clear_current();
if (this->repr) {
}
this->message_context->clear();
}
}
break;
}
case GDK_KEY_PRESS:
case GDK_KEY_Up:
case GDK_KEY_KP_Up:
if (!MOD__CTRL_ONLY(event)) {
this->angle += 5.0;
if (this->angle > 90.0) {
this->angle = 90.0;
}
}
break;
case GDK_KEY_Down:
case GDK_KEY_KP_Down:
if (!MOD__CTRL_ONLY(event)) {
this->angle -= 5.0;
if (this->angle < -90.0) {
this->angle = -90.0;
}
}
break;
case GDK_KEY_Right:
case GDK_KEY_KP_Right:
if (!MOD__CTRL_ONLY(event)) {
this->width += 0.01;
if (this->width > 1.0) {
this->width = 1.0;
}
sp_erc_update_toolbox (desktop, "altx-eraser", this->width * 100); // the same spinbutton is for alt+x
}
break;
case GDK_KEY_Left:
case GDK_KEY_KP_Left:
if (!MOD__CTRL_ONLY(event)) {
this->width -= 0.01;
if (this->width < 0.01) {
this->width = 0.01;
}
}
break;
case GDK_KEY_Home:
case GDK_KEY_KP_Home:
this->width = 0.01;
break;
case GDK_KEY_End:
case GDK_KEY_KP_End:
this->width = 1.0;
break;
case GDK_KEY_x:
case GDK_KEY_X:
if (MOD__ALT_ONLY(event)) {
}
break;
case GDK_KEY_Escape:
if (this->is_drawing) {
// if drawing, cancel, otherwise pass it up for deselecting
this->cancel();
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
// if drawing, cancel, otherwise pass it up for undo
this->cancel();
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
this->message_context->clear();
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
}
return ret;
}
void EraserTool::clear_current() {
// reset bpath
// reset curve
this->currentcurve->reset();
// reset points
this->npoints = 0;
}
void EraserTool::set_to_accumulated() {
bool workDone = false;
if (!this->accumulated->is_empty()) {
if (!this->repr) {
/* Create object */
/* Set style */
item->updateRepr();
}
if ( this->repr ) {
bool wasSelection = false;
if ( eraserMode ) {
} else {
}
} else {
wasSelection = true;
}
if ( eraserMode ) {
if ( eraserMode ) {
workDone = true; // TODO set this only if something was cut.
// If the item was not completely erased, track the new remainder.
}
}
} else {
}
}
}
} else {
sp_object_ref( *i, 0 );
}
item->deleteObject(true);
workDone = true;
}
}
if ( !eraserMode ) {
//sp_selection_delete(desktop);
}
if ( wasSelection ) {
if ( !remainingItems.empty() ) {
}
}
}
// Remove the eraser stroke itself:
sp_repr_unparent( this->repr );
this->repr = 0;
}
} else {
if (this->repr) {
sp_repr_unparent(this->repr);
this->repr = 0;
}
}
if ( workDone ) {
} else {
}
}
static void
double rounding)
{
if ( mag_in > ERASER_EPSILON ) {
} else {
}
if ( mag_out > ERASER_EPSILON ) {
} else {
}
}
}
void EraserTool::accumulate() {
Geom::CubicBezier const * dc_cal1_firstseg = dynamic_cast<Geom::CubicBezier const *>( this->cal1->first_segment() );
Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->first_segment() );
Geom::CubicBezier const * dc_cal1_lastseg = dynamic_cast<Geom::CubicBezier const *>( this->cal1->last_segment() );
Geom::CubicBezier const * rev_cal2_lastseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
add_cap(this->accumulated, (*dc_cal1_lastseg)[2], (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], (*rev_cal2_firstseg)[1], this->cap_rounding);
add_cap(this->accumulated, (*rev_cal2_lastseg)[2], (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[0], (*dc_cal1_firstseg)[1], this->cap_rounding);
this->accumulated->closepath();
}
}
static double square(double const x)
{
return x * x;
}
#ifdef ERASER_VERBOSE
#endif
return; // just clicked
#define BEZIER_SIZE 4
#define BEZIER_MAX_BEZIERS 8
#ifdef ERASER_VERBOSE
g_print("[F&S:#] this->npoints:%d, release:%s\n",
#endif
/* Current eraser */
/* dc->npoints > 0 */
/* g_print("erasers(1|2) reset\n"); */
}
gint const nb1 = Geom::bezier_fit_cubic_r(b1, this->point1, this->npoints, tolerance_sq, BEZIER_MAX_BEZIERS);
gint const nb2 = Geom::bezier_fit_cubic_r(b2, this->point2, this->npoints, tolerance_sq, BEZIER_MAX_BEZIERS);
/* Fit and draw and reset state */
#ifdef ERASER_VERBOSE
#endif
/* CanvasShape */
if (! release) {
this->currentcurve->reset();
}
}
// FIXME: this->segments is always NULL at this point??
if (!this->segments) { // first segment
}
this->currentcurve->closepath();
}
/* Current eraser */
}
}
} else {
/* fixme: ??? */
#ifdef ERASER_VERBOSE
g_print("[fit_and_split] failed to fit-cubic.\n");
#endif
this->draw_temporary_box();
}
}
}
/* Fit and draw and copy last point */
#ifdef ERASER_VERBOSE
#endif
if (!release) {
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? */
if ( !eraserMode ) {
sp_canvas_item_hide(this->currentshape);
}
}
this->npoints = 1;
} else {
this->draw_temporary_box();
}
}
void EraserTool::draw_temporary_box() {
this->currentcurve->reset();
}
}
if (this->npoints >= 2) {
add_cap(this->currentcurve, this->point2[this->npoints-2], this->point2[this->npoints-1], this->point1[this->npoints-1], this->point1[this->npoints-2], this->cap_rounding);
}
this->currentcurve->closepath();
}
}
}
}
/*
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 :