eraser-context.cpp revision 6ccdb18a54dcf42ccf8a0854542a6cfc973c9061
/*
* 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-handles.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 "eraser-context.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
static void add_cap(SPCurve *curve, Geom::Point const &pre, Geom::Point const &from, Geom::Point const &to, Geom::Point const &post, double rounding);
#include "sp-factory.h"
namespace {
return new SPEraserContext();
}
bool eraserContextRegistered = ToolFactory::instance().registerObject("/tools/eraser", createEraserContext);
}
return SPEraserContext::prefsPath;
}
static void
{
// event_context_class->setup = sp_eraser_context_setup;
// event_context_class->set = sp_eraser_context_set;
// event_context_class->root_handler = sp_eraser_context_root_handler;
}
this->sperasercontext = erasercontext;
}
SPEraserContext* erc = this;
delete erc->ccommoncontext;
}
static void
{
new (erc) SPEraserContext();
}
static void
{
}
static void
{
}
void CEraserContext::setup() {
// if ((SP_EVENT_CONTEXT_CLASS(sp_eraser_context_parent_class))->setup)
// (SP_EVENT_CONTEXT_CLASS(sp_eraser_context_parent_class))->setup(ec);
CCommonContext::setup();
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), 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}
};
*/
erc->is_drawing = false;
ec->enableSelectionCue();
}
// TODO temp force:
ec->enableSelectionCue();
}
static void
{
}
//pass on up to parent class to handle common attributes.
// if (SP_EVENT_CONTEXT_CLASS(sp_eraser_context_parent_class)->set ) {
// SP_EVENT_CONTEXT_CLASS(sp_eraser_context_parent_class)->set(ec, val);
// }
}
static double
{
}
/* Get normalized point */
{
return Geom::Point(( v[Geom::X] - drect.min()[Geom::X] ) / max, ( v[Geom::Y] - drect.min()[Geom::Y] ) / max);
}
/* Get view point */
{
return Geom::Point(n[Geom::X] * max + drect.min()[Geom::X], n[Geom::Y] * max + drect.min()[Geom::Y]);
}
static void
{
}
static void
{
else
else
else
}
static gboolean
{
/* 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 || (dc->vel_max < ERASER_VEL_START && Geom::L2(force) < ERASER_EPSILON_START)) {
return FALSE;
}
/* Calculate new velocity */
/* Calculate angle of drawing tool */
double a1;
// 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;
}
static void
{
// 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;
// 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
}
}
}
}
static void
{
}
static void
{
dc->is_drawing = false;
/* Remove all temporary line segments */
}
/* reset accumulated curve */
}
}
{
}
case GDK_BUTTON_PRESS:
return TRUE;
}
}
/* initialize first point */
NULL,
dc->is_drawing = true;
}
break;
case GDK_MOTION_NOTIFY:
{
break;
}
}
}
}
break;
case GDK_BUTTON_RELEASE:
{
dc->is_drawing = false;
/* Remove all temporary line segments */
}
/* Create object */
/* reset accumulated curve */
}
}
}
break;
}
case GDK_KEY_PRESS:
case GDK_KEY_Up:
case GDK_KEY_KP_Up:
if (!MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Down:
case GDK_KEY_KP_Down:
if (!MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Right:
case GDK_KEY_KP_Right:
if (!MOD__CTRL_ONLY) {
sp_erc_update_toolbox (desktop, "altx-eraser", dc->width * 100); // the same spinbutton is for alt+x
}
break;
case GDK_KEY_Left:
case GDK_KEY_KP_Left:
if (!MOD__CTRL_ONLY) {
}
break;
case GDK_KEY_Home:
case GDK_KEY_KP_Home:
break;
case GDK_KEY_End:
case GDK_KEY_KP_End:
break;
case GDK_KEY_x:
case GDK_KEY_X:
if (MOD__ALT_ONLY) {
}
break;
case GDK_KEY_Escape:
if (dc->is_drawing) {
// if drawing, cancel, otherwise pass it up for deselecting
eraser_cancel (dc);
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
// if drawing, cancel, otherwise pass it up for undo
eraser_cancel (dc);
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
break;
default:
break;
}
default:
break;
}
if (!ret) {
// if ((SP_EVENT_CONTEXT_CLASS(sp_eraser_context_parent_class))->root_handler) {
// ret = (SP_EVENT_CONTEXT_CLASS(sp_eraser_context_parent_class))->root_handler(event_context, event);
// }
}
return ret;
}
static void
{
// reset bpath
// reset curve
// reset points
}
static void
{
bool workDone = false;
/* Create object */
/* Set style */
item->updateRepr();
}
bool wasSelection = false;
if ( eraserMode ) {
} else {
}
} else {
wasSelection = true;
}
if ( g_slist_length(toWorkOn) > 0 ) {
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 {
}
item->deleteObject(true);
workDone = true;
}
}
if ( !eraserMode ) {
//sp_selection_delete(desktop);
}
if ( wasSelection ) {
if ( !remainingItems.empty() ) {
}
}
}
// Remove the eraser stroke itself:
}
} else {
}
}
if ( workDone ) {
_("Draw eraser stroke"));
} else {
}
}
static void
double rounding)
{
if ( mag_in > ERASER_EPSILON ) {
} else {
}
if ( mag_out > ERASER_EPSILON ) {
} else {
}
}
}
static void
{
Geom::CubicBezier const * dc_cal1_firstseg = dynamic_cast<Geom::CubicBezier const *>( dc->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 *>( dc->cal1->last_segment() );
Geom::CubicBezier const * rev_cal2_lastseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
add_cap(dc->accumulated, (*dc_cal1_lastseg)[2], (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], (*rev_cal2_firstseg)[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);
}
}
static double square(double const x)
{
return x * x;
}
static void
{
#ifdef ERASER_VERBOSE
#endif
return; // just clicked
#define BEZIER_SIZE 4
#define BEZIER_MAX_BEZIERS 8
#ifdef ERASER_VERBOSE
g_print("[F&S:#] dc->npoints:%d, release:%s\n",
#endif
/* Current eraser */
/* dc->npoints > 0 */
/* g_print("erasers(1|2) reset\n"); */
}
/* Fit and draw and reset state */
#ifdef ERASER_VERBOSE
#endif
/* CanvasShape */
if (! release) {
}
}
// FIXME: dc->segments is always NULL at this point??
}
}
/* Current eraser */
}
}
} else {
/* fixme: ??? */
#ifdef ERASER_VERBOSE
g_print("[fit_and_split] failed to fit-cubic.\n");
#endif
}
}
}
/* Fit and draw and copy last point */
#ifdef ERASER_VERBOSE
#endif
if (!release) {
NULL);
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 ) {
}
}
} else {
}
}
static void
{
}
}
add_cap(dc->currentcurve, dc->point2[dc->npoints-2], dc->point2[dc->npoints-1], dc->point1[dc->npoints-1], dc->point1[dc->npoints-2], dc->cap_rounding);
}
}
/*
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 :