pencil-tool.cpp revision 39126e9b09dbffc2ff1f80df0c67d1f28a3f14f6
/** \file
* Pencil event context implementation.
*/
/*
* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright (C) 2000 Lauris Kaplinski
* Copyright (C) 2000-2001 Ximian, Inc.
* Copyright (C) 2002 Lauris Kaplinski
* Copyright (C) 2004 Monash University
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <gdk/gdkkeysyms.h>
#include "ui/tools/pencil-tool.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "selection.h"
#include "selection-chemistry.h"
#include "draw-anchor.h"
#include "message-stack.h"
#include "message-context.h"
#include "sp-path.h"
#include "preferences.h"
#include "snap.h"
#include "pixmaps/cursor-pencil.xpm"
#include "display/canvas-bpath.h"
#include "context-fns.h"
#include "sp-namedview.h"
#include "document.h"
#include "desktop-style.h"
#include "macros.h"
#include "display/sp-canvas.h"
#include "tool-factory.h"
#include "ui/tool/event-utils.h"
namespace Inkscape {
namespace UI {
namespace Tools {
static bool pencil_within_tolerance = false;
namespace {
return new PencilTool();
}
bool pencilContextRegistered = ToolFactory::instance().registerObject("/tools/freehand/pencil", createPencilContext);
}
return PencilTool::prefsPath;
}
, p()
, npoints(0)
, req_tangent(0, 0)
, is_drawing(false)
, sketch_n(0)
{
}
void PencilTool::setup() {
this->enableSelectionCue();
}
FreehandBase::setup();
this->is_drawing = false;
this->anchor_statusbar = false;
}
PencilTool::~PencilTool() {
}
/** Snaps new node relative to the previous node. */
if (this->npoints > 0) {
spdc_endpoint_snap_rotation(this, p, this->p[0], state);
}
} else {
//After all, the user explicitely asked for angular snapping by
//pressing CTRL
boost::optional<Geom::Point> origin = this->npoints > 0 ? this->p[0] : boost::optional<Geom::Point>();
}
}
}
/**
* Callback for handling all pencil context events.
*/
bool ret = false;
case GDK_BUTTON_PRESS:
break;
case GDK_MOTION_NOTIFY:
break;
case GDK_BUTTON_RELEASE:
break;
case GDK_KEY_PRESS:
break;
case GDK_KEY_RELEASE:
break;
default:
break;
}
if (!ret) {
}
return ret;
}
bool ret = false;
return true;
}
if (!this->grab) {
/* Grab mouse, so release will not pass unnoticed */
}
/* Find desktop coordinates */
/* Test whether we hit any anchor. */
pencil_within_tolerance = true;
switch (this->state) {
/* Current segment will be finished with release */
ret = true;
break;
default:
/* Set first point of sequence */
}
m.unSetup();
ret = true;
break;
}
if (anchor) {
} else {
// This is the first click of a new curve; deselect item so that
// this curve is not combined with it (unless it is drawn from its
// anchor, which is handled by the sibling branch above)
}
m.unSetup();
}
this->_setStartpoint(p);
ret = true;
break;
}
this->is_drawing = true;
}
return ret;
}
// mouse was accidentally moved during Ctrl+click;
// ignore the motion and create a single point
this->is_drawing = false;
return true;
}
bool ret = false;
if (this->space_panning || (mevent.state & GDK_BUTTON2_MASK) || (mevent.state & GDK_BUTTON3_MASK)) {
// allow scrolling
return false;
}
/* Grab mouse, so release will not pass unnoticed */
}
/* Find desktop coordinates */
/* Test whether we hit any anchor. */
if (pencil_within_tolerance) {
return false; // Do not drag if we're within tolerance from origin.
}
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to move the object, not click), then always process the
// motion notify coordinates as given (no snapping back to origin)
pencil_within_tolerance = false;
switch (this->state) {
/* Set red endpoint */
if (anchor) {
} else {
p = ptnr;
}
this->_setEndpoint(p);
ret = true;
break;
default:
/* We may be idle or already freehand */
if (this->state == SP_PENCIL_CONTEXT_IDLE) {
}
this->state = SP_PENCIL_CONTEXT_FREEHAND;
if ( !this->sa && !this->green_anchor ) {
/* Create green anchor */
}
if (anchor) {
}
if ( this->npoints != 0) { // buttonpress may have happened before we entered draw context!
// Only in freehand mode we have to add the first point also to this->ps (apparently)
// - We cannot add this point in spdc_set_startpoint, because we only need it for freehand
// - We cannot do this in the button press handler because at that point we don't know yet
// wheter we're going into freehand mode or not
}
ret = true;
}
if (anchor && !this->anchor_statusbar) {
this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
this->anchor_statusbar = true;
} else if (!anchor && this->anchor_statusbar) {
this->message_context->clear();
this->anchor_statusbar = false;
} else if (!anchor) {
}
} else {
if (anchor && !this->anchor_statusbar) {
this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
this->anchor_statusbar = true;
} else if (!anchor && this->anchor_statusbar) {
this->message_context->clear();
this->anchor_statusbar = false;
}
}
// a) press the mousebutton to start a freehand drawing, or
// b) release the mousebutton to finish a freehand drawing
if (!this->sp_event_context_knot_mouseover()) {
m.unSetup();
}
break;
}
return ret;
}
bool ret = false;
this->is_drawing = false;
/* Find desktop coordinates */
/* Test whether we hit any anchor. */
switch (this->state) {
case SP_PENCIL_CONTEXT_IDLE:
/* Releasing button in idle mode means single click */
// Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
this->state = SP_PENCIL_CONTEXT_ADDLINE;
}
ret = true;
break;
/* Finish segment now */
if (anchor) {
} else {
}
this->_setEndpoint(p);
this->_finishEndpoint();
this->state = SP_PENCIL_CONTEXT_IDLE;
ret = true;
break;
/* sketch mode: interpolate the sketched path and improve the current output path with the new interpolation. don't finish sketch */
this->_sketchInterpolate();
if (this->green_anchor) {
}
this->state = SP_PENCIL_CONTEXT_SKETCH;
} else {
/* Finish segment now */
/// \todo fixme: Clean up what follows (Lauris)
if (anchor) {
} else {
if (p_end != p) {
// then we must have snapped!
}
}
/* Write curves to object */
this->_interpolate();
spdc_concat_colors_and_flush(this, FALSE);
if (this->green_anchor) {
}
this->state = SP_PENCIL_CONTEXT_IDLE;
// reset sketch mode too
this->sketch_n = 0;
}
ret = true;
break;
case SP_PENCIL_CONTEXT_SKETCH:
default:
break;
}
if (this->grab) {
/* Release grab now */
}
ret = true;
}
return ret;
}
void PencilTool::_cancel() {
if (this->grab) {
/* Release grab now */
sp_canvas_item_ungrab(this->grab, 0);
}
this->is_drawing = false;
this->state = SP_PENCIL_CONTEXT_IDLE;
while (this->green_bpaths) {
}
this->green_curve->reset();
if (this->green_anchor) {
}
this->message_context->clear();
}
bool ret = false;
switch (get_group0_keyval(&event)) {
case GDK_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_KP_Up:
case GDK_KEY_KP_Down:
// Prevent the zoom field from activation.
ret = true;
}
break;
case GDK_KEY_Escape:
if (this->npoints != 0) {
// if drawing, cancel, otherwise pass it up for deselecting
if (this->state != SP_PENCIL_CONTEXT_IDLE) {
this->_cancel();
ret = true;
}
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
// if drawing, cancel, otherwise pass it up for undo
if (this->state != SP_PENCIL_CONTEXT_IDLE) {
this->_cancel();
ret = true;
}
}
break;
case GDK_KEY_g:
case GDK_KEY_G:
sp_selection_to_guides(this->desktop);
ret = true;
}
break;
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Meta_L:
case GDK_KEY_Meta_R:
if (this->state == SP_PENCIL_CONTEXT_IDLE) {
this->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("<b>Sketch mode</b>: holding <b>Alt</b> interpolates between sketched paths. Release <b>Alt</b> to finalize."));
}
break;
default:
break;
}
return ret;
}
bool ret = false;
switch (get_group0_keyval(&event)) {
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Meta_L:
case GDK_KEY_Meta_R:
if (this->state == SP_PENCIL_CONTEXT_SKETCH) {
spdc_concat_colors_and_flush(this, false);
this->sketch_n = 0;
if (this->green_anchor) {
}
this->state = SP_PENCIL_CONTEXT_IDLE;
ret = true;
}
break;
default:
break;
}
return ret;
}
/**
* Reset points and set new starting point.
*/
this->npoints = 0;
this->red_curve_is_valid = false;
if (in_svg_plane(p)) {
this->p[this->npoints++] = p;
}
}
/**
* Change moving endpoint position.
* <ul>
* <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
* <li>Otherwise we snap freely to whatever attractors are available.
* </ul>
*
* Number of points is (re)set to 2 always, 2nd point is modified.
* We change RED curve.
*/
if (this->npoints == 0) {
return;
/* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
* zoom setting).
*/
}
g_return_if_fail( this->npoints > 0 );
if ( ( p == this->p[0] )
|| !in_svg_plane(p) )
{
this->npoints = 1;
} else {
this->p[1] = p;
this->npoints = 2;
this->red_curve_is_valid = true;
}
}
/**
* Finalize addline.
*
* \todo
* fixme: I'd like remove red reset from concat colors (lauris).
* Still not sure, how it will make most sense.
*/
void PencilTool::_finishEndpoint() {
{
} else {
/* Write curves to object. */
spdc_concat_colors_and_flush(this, FALSE);
}
}
if ( ( p != this->p[ this->npoints - 1 ] )
&& in_svg_plane(p) )
{
this->p[this->npoints++] = p;
this->_fitAndSplit();
}
}
static inline double
square(double const x)
{
return x * x;
}
void PencilTool::_interpolate() {
return;
}
using Geom::X;
using Geom::Y;
double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
double const tolerance_sq = 0.02 * square(this->desktop->w2d().descrim() * tol) * exp(0.2 * tol - 2);
this->green_curve->reset();
this->red_curve_is_valid = false;
// worst case gives us a segment per point
int const n_segs = Geom::bezier_fit_cubic_r(b.data(), this->ps.data(), n_points, tolerance_sq, max_segs);
if (n_segs > 0) {
/* Fit and draw and reset state */
this->green_curve->moveto(b[0]);
for (int c = 0; c < n_segs; c++) {
// if we are in BSpline we modify the trace to create adhoc nodes
if(mode == 2){
}else{
}
}
/* Fit and draw and copy last point */
/* Set up direction of next curve. */
{
this->p[0] = last_seg->finalPoint();
this->npoints = 1;
delete last_seg_reverse;
}
}
}
/* interpolates the sketched curve and tweaks the current sketch interpolation*/
void PencilTool::_sketchInterpolate() {
return;
}
double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
double const tolerance_sq = 0.02 * square(this->desktop->w2d().descrim() * tol) * exp(0.2 * tol - 2);
this->red_curve_is_valid = false;
// worst case gives us a segment per point
int const n_segs = Geom::bezier_fit_cubic_r(b.data(), this->ps.data(), n_points, tolerance_sq, max_segs);
if (n_segs > 0) {
for (int c = 0; c < n_segs; c++) {
}
if (this->sketch_n > 0) {
double t;
if (average_all_sketches) {
// Average = (sum of all) / n
// = (sum of all + new one) / n+1
// = ((old average)*n + new one) / n+1
} else {
t = 0.5;
}
// simplify path, to eliminate small segments
} else {
this->sketch_interpolation = fit_pwd2;
}
this->sketch_n++;
this->green_curve->reset();
/* Fit and draw and copy last point */
/* Set up direction of next curve. */
{
this->p[0] = last_seg->finalPoint();
this->npoints = 1;
delete last_seg_reverse;
}
}
}
void PencilTool::_fitAndSplit() {
double const tolerance_sq = 0;
|| is_unit_vector(this->req_tangent));
this->req_tangent, tHatEnd,
tolerance_sq, 1);
if ( n_segs > 0
&& unsigned(this->npoints) < G_N_ELEMENTS(this->p) )
{
/* Fit and draw and reset state */
using Geom::X;
using Geom::Y;
// if we are in BSpline we modify the trace to create adhoc nodes
if(mode == 2){
}else{
}
this->red_curve_is_valid = true;
} else {
/* Fit and draw and copy last point */
/* Set up direction of next curve. */
{
this->p[0] = last_seg->finalPoint();
this->npoints = 1;
delete last_seg_reverse;
}
/// \todo fixme:
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), this->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
this->red_curve_is_valid = false;
}
}
}
}
}
/*
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 :