pencil-tool.cpp revision 0dc651bab109d5f3a6362dbb287ea4c1e97fa5ef
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/** \file
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Pencil event context implementation.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Authors:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Lauris Kaplinski <lauris@kaplinski.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * bulia byak <buliabyak@users.sf.net>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Jon A. Cruz <jon@joncruz.org>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2000 Lauris Kaplinski
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2000-2001 Ximian, Inc.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2002 Lauris Kaplinski
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2004 Monash University
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Released under GNU GPL, read the file 'COPYING' for more information
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <gdk/gdkkeysyms.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "ui/tools/pencil-tool.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "desktop.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "desktop-handles.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "selection.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "selection-chemistry.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "draw-anchor.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "message-stack.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "message-context.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "modifier-fns.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sp-path.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "preferences.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "snap.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "pixmaps/cursor-pencil.xpm"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <2geom/sbasis-to-bezier.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <2geom/bezier-utils.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "display/canvas-bpath.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <glibmm/i18n.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "context-fns.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sp-namedview.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "xml/repr.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "document.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "desktop-style.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "macros.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "display/sp-canvas.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "display/curve.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "livarot/Path.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "tool-factory.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Inkscape {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace UI {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Tools {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstatic Geom::Point pencil_drag_origin_w(0, 0);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstatic bool pencil_within_tolerance = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstatic bool in_svg_plane(Geom::Point const &p) { return Geom::LInfty(p) < 1e18; }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ToolBase* createPencilContext() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return new PencilTool();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool pencilContextRegistered = ToolFactory::instance().registerObject("/tools/freehand/pencil", createPencilContext);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenconst std::string& PencilTool::getPrefsPath() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return PencilTool::prefsPath;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenconst std::string PencilTool::prefsPath = "/tools/freehand/pencil";
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenPencilTool::PencilTool()
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen : FreehandBase(cursor_pencil_xpm, 4, 4)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen , p()
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen , npoints(0)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen , state(SP_PENCIL_CONTEXT_IDLE)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen , req_tangent(0, 0)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen , is_drawing(false)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen , sketch_n(0)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenvoid PencilTool::setup() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Inkscape::Preferences *prefs = Inkscape::Preferences::get();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (prefs->getBool("/tools/freehand/pencil/selcue")) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->enableSelectionCue();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen FreehandBase::setup();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->is_drawing = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->anchor_statusbar = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenPencilTool::~PencilTool() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen/** Snaps new node relative to the previous node. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenvoid PencilTool::_endpointSnap(Geom::Point &p, guint const state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ((state & GDK_CONTROL_MASK)) { //CTRL enables constrained snapping
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (this->npoints > 0) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen spdc_endpoint_snap_rotation(this, p, this->p[0], state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //After all, the user explicitely asked for angular snapping by
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //pressing CTRL
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen boost::optional<Geom::Point> origin = this->npoints > 0 ? this->p[0] : boost::optional<Geom::Point>();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen spdc_endpoint_snap_free(this, p, origin, state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/**
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Callback for handling all pencil context events.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenbool PencilTool::root_handler(GdkEvent* event) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool ret = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (event->type) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case GDK_BUTTON_PRESS:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleButtonPress(event->button);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case GDK_MOTION_NOTIFY:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleMotionNotify(event->motion);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case GDK_BUTTON_RELEASE:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleButtonRelease(event->button);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case GDK_KEY_PRESS:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleKeyPress(get_group0_keyval (&event->key), event->key.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case GDK_KEY_RELEASE:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = this->_handleKeyRelease(get_group0_keyval (&event->key), event->key.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen default:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!ret) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = FreehandBase::root_handler(event);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return ret;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenbool PencilTool::_handleButtonPress(GdkEventButton const &bevent) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool ret = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( bevent.button == 1 && !this->space_panning) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Inkscape::Selection *selection = sp_desktop_selection(desktop);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (Inkscape::have_viable_layer(desktop, this->message_context) == false) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!this->grab) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Grab mouse, so release will not pass unnoticed */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->grab = SP_CANVAS_ITEM(desktop->acetate);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen sp_canvas_item_grab(this->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen GDK_BUTTON_RELEASE_MASK |
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen GDK_POINTER_MOTION_MASK ),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen NULL, bevent.time);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point const button_w(bevent.x, bevent.y);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Find desktop coordinates */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point p = this->desktop->w2d(button_w);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Test whether we hit any anchor. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SPDrawAnchor *anchor = spdc_test_inside(this, button_w);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen pencil_drag_origin_w = Geom::Point(bevent.x,bevent.y);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen pencil_within_tolerance = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (this->state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case SP_PENCIL_CONTEXT_ADDLINE:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Current segment will be finished with release */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen default:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Set first point of sequence */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SnapManager &m = desktop->namedview->snap_manager;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (bevent.state & GDK_CONTROL_MASK) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.setup(desktop);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!(bevent.state & GDK_SHIFT_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen spdc_create_single_dot(this, p, "/tools/freehand/pencil", bevent.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.unSetup();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (anchor) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen p = anchor->dp;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.setup(desktop);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!(bevent.state & GDK_SHIFT_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // This is the first click of a new curve; deselect item so that
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // this curve is not combined with it (unless it is drawn from its
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // anchor, which is handled by the sibling branch above)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen selection->clear();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen m.unSetup();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->sa = anchor;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->_setStartpoint(p);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->is_drawing = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return ret;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenbool PencilTool::_handleMotionNotify(GdkEventMotion const &mevent) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // mouse was accidentally moved during Ctrl+click;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // ignore the motion and create a single point
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->is_drawing = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool ret = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (this->space_panning || (mevent.state & GDK_BUTTON2_MASK) || (mevent.state & GDK_BUTTON3_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // allow scrolling
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( ( mevent.state & GDK_BUTTON1_MASK ) && !this->grab && this->is_drawing) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Grab mouse, so release will not pass unnoticed */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->grab = SP_CANVAS_ITEM(desktop->acetate);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen sp_canvas_item_grab(this->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen GDK_BUTTON_RELEASE_MASK |
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen GDK_POINTER_MOTION_MASK ),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen NULL, mevent.time);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Find desktop coordinates */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point p = desktop->w2d(Geom::Point(mevent.x, mevent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Test whether we hit any anchor. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SPDrawAnchor *anchor = spdc_test_inside(this, Geom::Point(mevent.x, mevent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (pencil_within_tolerance) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Inkscape::Preferences *prefs = Inkscape::Preferences::get();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( Geom::LInfty( Geom::Point(mevent.x,mevent.y) - pencil_drag_origin_w ) < tolerance ) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen return false; // Do not drag if we're within tolerance from origin.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // Once the user has moved farther than tolerance from the original location
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // (indicating they intend to move the object, not click), then always process the
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // motion notify coordinates as given (no snapping back to origin)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen pencil_within_tolerance = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (this->state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case SP_PENCIL_CONTEXT_ADDLINE:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Set red endpoint */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (anchor) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen p = anchor->dp;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point ptnr(p);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->_endpointSnap(ptnr, mevent.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen p = ptnr;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->_setEndpoint(p);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen default:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* We may be idle or already freehand */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( (mevent.state & GDK_BUTTON1_MASK) && this->is_drawing ) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (this->state == SP_PENCIL_CONTEXT_IDLE) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen sp_event_context_discard_delayed_snap_event(this);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->state = SP_PENCIL_CONTEXT_FREEHAND;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( !this->sa && !this->green_anchor ) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Create green anchor */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->green_anchor = sp_draw_anchor_new(this, this->green_curve, TRUE, this->p[0]);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (anchor) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen p = anchor->dp;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if ( this->npoints != 0) { // buttonpress may have happened before we entered draw context!
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (this->ps.empty()) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // Only in freehand mode we have to add the first point also to pc->ps (apparently)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // - We cannot add this point in spdc_set_startpoint, because we only need it for freehand
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // - We cannot do this in the button press handler because at that point we don't know yet
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // wheter we're going into freehand mode or not
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->ps.push_back(this->p[0]);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->_addFreehandPoint(p, mevent.state);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen ret = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (anchor && !this->anchor_statusbar) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->anchor_statusbar = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (!anchor && this->anchor_statusbar) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->clear();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->anchor_statusbar = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (!anchor) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (anchor && !this->anchor_statusbar) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->anchor_statusbar = true;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if (!anchor && this->anchor_statusbar) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->message_context->clear();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->anchor_statusbar = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen // Show the pre-snap indicator to communicate to the user where we would snap to if he/she were to
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen // a) press the mousebutton to start a freehand drawing, or
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen // b) release the mousebutton to finish a freehand drawing
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen if (!this->sp_event_context_knot_mouseover()) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen SnapManager &m = desktop->namedview->snap_manager;
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen m.setup(desktop);
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE));
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen m.unSetup();
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen }
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen break;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen return ret;
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen}
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelenbool PencilTool::_handleButtonRelease(GdkEventButton const &revent) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen bool ret = false;
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen if ( revent.button == 1 && this->is_drawing && !this->space_panning) {
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen this->is_drawing = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Find desktop coordinates */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Geom::Point p = desktop->w2d(Geom::Point(revent.x, revent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Test whether we hit any anchor. */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen SPDrawAnchor *anchor = spdc_test_inside(this, Geom::Point(revent.x, revent.y));
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen switch (this->state) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen case SP_PENCIL_CONTEXT_IDLE:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* Releasing button in idle mode means single click */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /* We have already set up start point/anchor in button_press */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if (!(revent.state & GDK_CONTROL_MASK)) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen this->state = SP_PENCIL_CONTEXT_ADDLINE;
}
ret = true;
break;
case SP_PENCIL_CONTEXT_ADDLINE:
/* Finish segment now */
if (anchor) {
p = anchor->dp;
} else {
this->_endpointSnap(p, revent.state);
}
this->ea = anchor;
this->_setEndpoint(p);
this->_finishEndpoint();
this->state = SP_PENCIL_CONTEXT_IDLE;
sp_event_context_discard_delayed_snap_event(this);
ret = true;
break;
case SP_PENCIL_CONTEXT_FREEHAND:
if (revent.state & GDK_MOD1_MASK) {
/* 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->green_anchor = sp_draw_anchor_destroy(this->green_anchor);
}
this->state = SP_PENCIL_CONTEXT_SKETCH;
} else {
/* Finish segment now */
/// \todo fixme: Clean up what follows (Lauris)
if (anchor) {
p = anchor->dp;
} else {
Geom::Point p_end = p;
this->_endpointSnap(p_end, revent.state);
if (p_end != p) {
// then we must have snapped!
this->_addFreehandPoint(p_end, revent.state);
}
}
this->ea = anchor;
/* Write curves to object */
desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
this->_interpolate();
spdc_concat_colors_and_flush(this, FALSE);
this->sa = NULL;
this->ea = NULL;
if (this->green_anchor) {
this->green_anchor = sp_draw_anchor_destroy(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 */
sp_canvas_item_ungrab(this->grab, revent.time);
this->grab = NULL;
}
ret = true;
}
return ret;
}
void PencilTool::_cancel() {
if (this->grab) {
/* Release grab now */
sp_canvas_item_ungrab(this->grab, 0);
this->grab = NULL;
}
this->is_drawing = false;
this->state = SP_PENCIL_CONTEXT_IDLE;
sp_event_context_discard_delayed_snap_event(this);
this->red_curve->reset();
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(this->red_bpath), NULL);
while (this->green_bpaths) {
sp_canvas_item_destroy(SP_CANVAS_ITEM(this->green_bpaths->data));
this->green_bpaths = g_slist_remove(this->green_bpaths, this->green_bpaths->data);
}
this->green_curve->reset();
if (this->green_anchor) {
this->green_anchor = sp_draw_anchor_destroy(this->green_anchor);
}
this->message_context->clear();
this->message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
this->desktop->canvas->endForcedFullRedraws();
}
bool PencilTool::_handleKeyPress(guint const keyval, guint const state) {
bool ret = false;
switch (keyval) {
case GDK_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_KP_Up:
case GDK_KEY_KP_Down:
// Prevent the zoom field from activation.
if (!mod_ctrl_only(state)) {
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 (mod_ctrl_only(state) && this->npoints != 0) {
// 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:
if (mod_shift_only(state)) {
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 PencilTool::_handleKeyRelease(guint const keyval, guint const /*state*/) {
bool ret = false;
switch (keyval) {
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;
this->sa = NULL;
this->ea = NULL;
if (this->green_anchor) {
this->green_anchor = sp_draw_anchor_destroy(this->green_anchor);
}
this->state = SP_PENCIL_CONTEXT_IDLE;
sp_event_context_discard_delayed_snap_event(this);
this->desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand sketch"));
ret = true;
}
break;
default:
break;
}
return ret;
}
/**
* Reset points and set new starting point.
*/
void PencilTool::_setStartpoint(Geom::Point const &p) {
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.
*/
void PencilTool::_setEndpoint(Geom::Point const &p) {
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 );
this->red_curve->reset();
if ( ( p == this->p[0] )
|| !in_svg_plane(p) )
{
this->npoints = 1;
} else {
this->p[1] = p;
this->npoints = 2;
this->red_curve->moveto(this->p[0]);
this->red_curve->lineto(this->p[1]);
this->red_curve_is_valid = true;
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(this->red_bpath), this->red_curve);
}
}
/**
* 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() {
if ( ( this->red_curve->is_empty() )
|| ( *(this->red_curve->first_point()) == *(this->red_curve->second_point()) ) )
{
this->red_curve->reset();
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(this->red_bpath), NULL);
} else {
/* Write curves to object. */
spdc_concat_colors_and_flush(this, FALSE);
this->sa = NULL;
this->ea = NULL;
}
}
void PencilTool::_addFreehandPoint(Geom::Point const &p, guint /*state*/) {
g_assert( this->npoints > 0 );
g_return_if_fail(unsigned(this->npoints) < G_N_ELEMENTS(this->p));
if ( ( p != this->p[ this->npoints - 1 ] )
&& in_svg_plane(p) )
{
this->ps.push_back(p);
this->p[this->npoints++] = p;
this->_fitAndSplit();
}
}
static inline double
square(double const x)
{
return x * x;
}
void PencilTool::_interpolate() {
if ( this->ps.size() <= 1 ) {
return;
}
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
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);
g_assert(is_zero(this->req_tangent) || is_unit_vector(this->req_tangent));
this->green_curve->reset();
this->red_curve->reset();
this->red_curve_is_valid = false;
int n_points = this->ps.size();
// worst case gives us a segment per point
int max_segs = 4 * n_points;
std::vector<Geom::Point> b(max_segs);
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++) {
this->green_curve->curveto(b[4 * c + 1], b[4 * c + 2], b[4 * c + 3]);
}
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(this->red_bpath), this->green_curve);
/* Fit and draw and copy last point */
g_assert(!this->green_curve->is_empty());
/* Set up direction of next curve. */
{
Geom::Curve const * last_seg = this->green_curve->last_segment();
g_assert( last_seg ); // Relevance: validity of (*last_seg)
this->p[0] = last_seg->finalPoint();
this->npoints = 1;
Geom::Curve *last_seg_reverse = last_seg->reverse();
Geom::Point const req_vec( -last_seg_reverse->unitTangentAt(0) );
delete last_seg_reverse;
this->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
? Geom::Point(0, 0)
: Geom::unit_vector(req_vec) );
}
}
this->ps.clear();
}
/* interpolates the sketched curve and tweaks the current sketch interpolation*/
void PencilTool::_sketchInterpolate() {
if ( this->ps.size() <= 1 ) {
return;
}
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
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);
bool average_all_sketches = prefs->getBool("/tools/freehand/pencil/average_all_sketches", true);
g_assert(is_zero(this->req_tangent) || is_unit_vector(this->req_tangent));
this->red_curve->reset();
this->red_curve_is_valid = false;
int n_points = this->ps.size();
// worst case gives us a segment per point
int max_segs = 4 * n_points;
std::vector<Geom::Point> b(max_segs);
int const n_segs = Geom::bezier_fit_cubic_r(b.data(), this->ps.data(), n_points, tolerance_sq, max_segs);
if (n_segs > 0) {
Geom::Path fit(b[0]);
for (int c = 0; c < n_segs; c++) {
fit.appendNew<Geom::CubicBezier>(b[4 * c + 1], b[4 * c + 2], b[4 * c + 3]);
}
Geom::Piecewise<Geom::D2<Geom::SBasis> > fit_pwd2 = fit.toPwSb();
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
t = this->sketch_n / (this->sketch_n + 1.);
} else {
t = 0.5;
}
this->sketch_interpolation = Geom::lerp(t, fit_pwd2, this->sketch_interpolation);
// simplify path, to eliminate small segments
Path path;
path.LoadPathVector(Geom::path_from_piecewise(this->sketch_interpolation, 0.01));
path.Simplify(0.5);
Geom::PathVector *pathv = path.MakePathVector();
this->sketch_interpolation = (*pathv)[0].toPwSb();
delete pathv;
} else {
this->sketch_interpolation = fit_pwd2;
}
this->sketch_n++;
this->green_curve->reset();
this->green_curve->set_pathvector(Geom::path_from_piecewise(this->sketch_interpolation, 0.01));
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(this->red_bpath), this->green_curve);
/* Fit and draw and copy last point */
g_assert(!this->green_curve->is_empty());
/* Set up direction of next curve. */
{
Geom::Curve const * last_seg = this->green_curve->last_segment();
g_assert( last_seg ); // Relevance: validity of (*last_seg)
this->p[0] = last_seg->finalPoint();
this->npoints = 1;
Geom::Curve *last_seg_reverse = last_seg->reverse();
Geom::Point const req_vec( -last_seg_reverse->unitTangentAt(0) );
delete last_seg_reverse;
this->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
? Geom::Point(0, 0)
: Geom::unit_vector(req_vec) );
}
}
this->ps.clear();
}
void PencilTool::_fitAndSplit() {
g_assert( this->npoints > 1 );
double const tolerance_sq = 0;
Geom::Point b[4];
g_assert(is_zero(this->req_tangent)
|| is_unit_vector(this->req_tangent));
Geom::Point const tHatEnd(0, 0);
int const n_segs = Geom::bezier_fit_cubic_full(b, NULL, this->p, this->npoints,
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 */
this->red_curve->reset();
this->red_curve->moveto(b[0]);
this->red_curve->curveto(b[1], b[2], b[3]);
sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(this->red_bpath), this->red_curve);
this->red_curve_is_valid = true;
} else {
/* Fit and draw and copy last point */
g_assert(!this->red_curve->is_empty());
/* Set up direction of next curve. */
{
Geom::Curve const * last_seg = this->red_curve->last_segment();
g_assert( last_seg ); // Relevance: validity of (*last_seg)
this->p[0] = last_seg->finalPoint();
this->npoints = 1;
Geom::Curve *last_seg_reverse = last_seg->reverse();
Geom::Point const req_vec( -last_seg_reverse->unitTangentAt(0) );
delete last_seg_reverse;
this->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
? Geom::Point(0, 0)
: Geom::unit_vector(req_vec) );
}
this->green_curve->append_continuous(this->red_curve, 0.0625);
SPCurve *curve = this->red_curve->copy();
/// \todo fixme:
SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(this->desktop), curve);
curve->unref();
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), this->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
this->green_bpaths = g_slist_prepend(this->green_bpaths, cshape);
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 :