node.cpp revision f200d31b8a03050faa7243929bbd5698db488399
/** @file
* Editable node - implementation
*/
/* Authors:
* Krzysztof Kosiński <tweenk.pl@gmail.com>
*
* Copyright (C) 2009 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <iostream>
#include <stdexcept>
#include <boost/utility.hpp>
#include <glib.h>
#include "display/sp-ctrlline.h"
#include "display/sp-canvas.h"
#include "display/sp-canvas-util.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "preferences.h"
#include "snap.h"
#include "snap-preferences.h"
#include "sp-metrics.h"
#include "sp-namedview.h"
#include "ui/tool/control-point-selection.h"
#include "ui/tool/event-utils.h"
#include "ui/tool/multi-path-manipulator.h"
#include "ui/tool/path-manipulator.h"
namespace Inkscape {
namespace UI {
{
{0xbfbfbf00, 0x000000ff}, // normal fill, stroke
{0xff000000, 0x000000ff}, // mouseover fill, stroke
{0xff000000, 0x000000ff} // clicked fill, stroke
},
{0x0000ffff, 0x000000ff}, // normal fill, stroke when selected
{0xff000000, 0x000000ff}, // mouseover fill, stroke when selected
{0xff000000, 0x000000ff} // clicked fill, stroke when selected
};
{0xffffffff, 0x000000ff}, // normal fill, stroke
{0xff000000, 0x000000ff}, // mouseover fill, stroke
{0xff000000, 0x000000ff} // clicked fill, stroke
};
{
switch(type) {
default: out << 'b'; break;
}
return out;
}
/** Computes an unit vector of the direction from first to second control point */
}
/**
* @class Handle
* @brief Control point of a cubic Bezier curve in a path.
*
* Handle keeps the node type invariant only for the opposite handle of the same node.
* Keeping the invariant on node moves is left to the %Node class.
*/
, _degenerate(true)
{
_cset = &handle_colors;
setVisible(false);
false));
}
{
}
void Handle::setVisible(bool v)
{
ControlPoint::setVisible(v);
if (v) sp_canvas_item_show(_handle_line);
else sp_canvas_item_hide(_handle_line);
}
{
} else {
}
// The handle becomes degenerate. If the segment between it and the node
// in its direction becomes linear and there are smooth nodes
// at its ends, make their handles colinear with the segment
}
}
}
return;
}
// restrict movement to the line joining the nodes
// project the relative position on the direction line
return;
}
case NODE_AUTO:
// fall through - auto nodes degrade into smooth nodes
case NODE_SMOOTH: {
/* for smooth nodes, we need to rotate the other handle so that it's colinear
* with the dragged one while conserving length. */
} break;
case NODE_SYMMETRIC:
// for symmetric nodes, place the other handle on the opposite side
break;
default: break;
}
}
{
ControlPoint::setPosition(p);
// update degeneration info and visibility
_degenerate = true;
else _degenerate = false;
setVisible(true);
} else {
setVisible(false);
}
// If both handles become degenerate, convert to parent cusp node
if (_parent->isDegenerate()) {
}
}
{
if (isDegenerate()) return;
}
{
}
{
}
{
}
{
switch(type) {
case NODE_CUSP: return _("Cusp node handle");
case NODE_SMOOTH: return _("Smooth node handle");
case NODE_SYMMETRIC: return _("Symmetric node handle");
case NODE_AUTO: return _("Auto-smooth node handle");
default: return "";
}
}
void Handle::_grabbedHandler()
{
}
{
// with Alt, preserve length
}
// with Ctrl, constrain to M_PI/rotationsnapsperpi increments.
if (held_control(*event)) {
}
}
void Handle::_ungrabbedHandler()
{
// hide the handle if it's less than dragtolerance away from the node
}
_drag_out = false;
}
static double snap_increment_degrees() {
return 180.0 / snaps;
}
{
if (state_held_alt(state)) {
if (state_held_control(state)) {
"<b>Ctrl+Alt</b>: preserve length and snap rotation angle to %f° increments"),
} else {
return C_("Path handle tip",
"<b>Alt:</b> preserve handle length while dragging");
}
} else {
if (state_held_control(state)) {
"<b>Ctrl:</b> snap rotation angle to %f° increments, click to retract"),
}
}
case NODE_AUTO:
return C_("Path handle tip",
"<b>Auto node handle:</b> drag to convert to smooth node");
default:
}
}
{
// report angle in mathematical convention
g_string_free(x, TRUE);
g_string_free(y, TRUE);
return ret;
}
/**
* @class Node
* @brief Curve endpoint in an editable path.
*
* The method move() keeps node type invariants during translations.
*/
, _handles_shown(false)
{
// NOTE we do not set type here, because the handles are still degenerate
// connect to own grabbed signal - dragging out handles
}
// NOTE: not using iterators won't make this much quicker because iterators can be 100% inlined.
{
if (n) return n.ptr();
return NULL;
}
{
if (p) return p.ptr();
return NULL;
}
{
// move handles when the node moves.
// if the node has a smooth handle after a line segment, it should be kept colinear
// with the segment
}
{
setPosition(position() * m);
/* Affine transforms keep handle invariants for smooth and symmetric nodes,
* but smooth nodes at ends of linear segments and auto nodes need special treatment */
}
{
return b;
}
{
/* This method restores handle invariants for neighboring nodes,
* and invariants that are based on positions of those nodes for this one. */
/* Fix auto handles */
}
/* Fix smooth handles at the ends of linear segments.
* Rotate the appropriate handle to be colinear with the segment.
* If there is a smooth node at the other end of the segment, rotate it too. */
if (_is_line_segment(this, _next())) {
} else if (_is_line_segment(_prev(), this)) {
} else return;
}
// also update the handle on the other end of the segment
}
}
void Node::_updateAutoHandles()
{
// Recompute the position of automatic handles.
// For endnodes, retract both handles. (It's only possible to create an end auto node
// through the XML editor.)
if (isEndNode()) {
return;
}
// Auto nodes automaticaly adjust their handles to give an appearance of smoothness,
// no matter what their surroundings are.
// "dir" is an unit vector perpendicular to the bisector of the angle created
// by the previous node, this auto node and the next node.
// Handle lengths are equal to 1/3 of the distance from the adjacent node.
} else {
// If any of the adjacent nodes coincides, retract both handles.
}
}
void Node::showHandles(bool v)
{
_handles_shown = v;
}
/** Sets the node type and optionally restores the invariants associated with the given type.
* @param type The type to set
* @param update_handles Whether to restore invariants associated with the given type.
* Passing false is useful e.g. wen initially creating the path,
* and when making cusp nodes during some node algorithms.
* Pass true when used in response to an UI node type button.
*/
{
if (type == NODE_PICK_BEST) {
pickBestType();
updateState(); // The size of the control might have changed
return;
}
// if update_handles is true, adjust handle positions to match the node type
// handle degenerate handles appropriately
if (update_handles) {
switch (type) {
case NODE_CUSP:
// if the existing type is also NODE_CUSP, retract handles
}
break;
case NODE_AUTO:
// auto handles make no sense for endnodes
if (isEndNode()) return;
break;
case NODE_SMOOTH: {
// rotate handles to be colinear
// for degenerate nodes set positions like auto handles
if (isDegenerate()) {
} else if (_front.isDegenerate()) {
// if the front handle is degenerate and this...next is a line segment,
// make back colinear; otherwise pull out the other handle
// to 1/3 of distance to prev
if (next_line) {
} else if (_prev()) {
}
} else if (_back.isDegenerate()) {
if (prev_line) {
} else if (_next()) {
}
} else {
// both handles are extended. make colinear while keeping length
// first make back colinear with the vector front ---> back,
// then make front colinear with back ---> node
// (not back ---> front because back's position was changed in the first call)
}
} break;
case NODE_SYMMETRIC:
if (isEndNode()) return; // symmetric handles make no sense for endnodes
if (isDegenerate()) {
// similar to auto handles but set the same length for both
if (len == 0) return;
} else {
// Both handles are extended. Compute average length, use direction from
// back handle to front handle. This also works correctly for degenerates
}
break;
default: break;
}
}
updateState();
}
/** Pick the best type for this node, based on the position of its handles.
* This is what assigns types to nodes created using the pen tool. */
void Node::pickBestType()
{
do {
// if both handles are degenerate, do nothing
if (both_degen) break;
// if neither are degenerate, check their respective positions
if (neither_degen) {
// for now do not automatically make nodes symmetric, it can be annoying
/*if (Geom::are_near(front_delta, -back_delta)) {
_type = NODE_SYMMETRIC;
break;
}*/
{
_type = NODE_SMOOTH;
break;
}
}
// check whether the handle aligns with the previous line segment.
// we know that if front is degenerate, back isn't, because
// both_degen was false
_type = NODE_SMOOTH;
break;
}
_type = NODE_SMOOTH;
break;
}
}
} while (false);
updateState();
}
{
}
/** Move the node to the bottom of its canvas group. Useful for node break, to ensure that
* the selected nodes are above the unselected ones. */
{
}
{
switch (x) {
case 'a': return NODE_AUTO;
case 'c': return NODE_CUSP;
case 's': return NODE_SMOOTH;
case 'z': return NODE_SYMMETRIC;
default: return NODE_PICK_BEST;
}
}
{
static int dir;
{
case GDK_SCROLL:
dir = 1;
dir = -1;
} else break;
} else {
}
return true;
default:
break;
}
}
// TODO Move this to 2Geom!
{
}
/** Select or deselect a node in this node's subpath based on its path distance from this node.
* @param dir If negative, shrink selection by one node; if positive, grow by one node */
{
// Interestingly, we do not need any help from PathManipulator when doing linear grow.
// First handle the trivial case of growing over an unselected node.
_selection.insert(this);
return;
}
double distance_back = 0, distance_front = 0;
// Linear grow is simple. We find the first unselected nodes in each direction
// and compare the linear distances to them.
if (dir > 0) {
if (!selected()) {
_selection.insert(this);
return;
}
// find first unselected nodes on both sides
fwd = n;
// there is no unselected node in this cyclic subpath
return;
}
// do the same for the second direction. Do not check for equality with
// this node, because there is at least one unselected node in the subpath,
// so we are guaranteed to stop.
rev = p;
}
else t = rev;
} else {
}
// Linear shrink is more complicated. We need to find the farthest selected node.
// This means we have to check the entire subpath. We go in the direction in which
// the distance we traveled is lower. We do this until we run out of nodes (ends of path)
// or the two iterators meet. On the way, we store the last selected node and its distance
// in each direction (if any). At the end, we choose the one that is farther and deselect it.
} else {
// both iterators that store last selected nodes are initially empty
double last_distance_back = 0, last_distance_front = 0;
}
fwd = n;
}
rev = p;
}
// Check whether we walked the entire cyclic subpath.
// This is initially true because both iterators start from this node,
// so this check cannot go in the while condition.
// When this happens, we need to check the last node, pointed to by the iterators.
} else {
}
break;
}
}
else t = last_rev;
} else {
}
}
}
{
// change node size to match type and selection state
switch (_type) {
case NODE_AUTO:
case NODE_CUSP:
else _setSize(9);
break;
default:
else _setSize(7);
break;
}
}
{
// Dragging out handles with Shift + drag on a node.
if (!held_shift(*event)) return false;
Handle *h;
// This should work even if dragtolerance is zero and evp coincides with node position.
double angle_next = HUGE_VAL;
double angle_prev = HUGE_VAL;
bool has_degenerate = false;
// determine which handle to drag out based on degeneration and the direction of drag
has_degenerate = true;
}
has_degenerate = true;
}
if (!has_degenerate) return false;
h->setVisible(true);
h->transferGrab(this, event);
return true;
}
{
// For a note on how snapping is implemented in Inkscape, see snap.h.
if (snap) {
/* setup
* TODO We are doing this every time a snap happens. It should once be done only once
* per drag - maybe in the grabbed handler?
* TODO Unselected nodes vector must be valid during the snap run, because it is not
* TODO Snapping to unselected segments of selected paths doesn't work yet. */
// Build the list of unselected nodes.
if (!(*i)->selected()) {
unselected.push_back(p);
}
}
}
if (held_control(*event)) {
// with Ctrl+Alt, constrain to handle lines
// project the new position onto a handle line that is closer
if (snap) {
} else {
}
} else {
} else {
}
}
} else {
// with Ctrl, constrain to axes
// TODO combine the two branches
if (snap) {
}
} else {
}
}
} else if (snap) {
}
}
{
return SNAPSOURCE_NODE_SMOOTH;
return SNAPSOURCE_NODE_CUSP;
}
{
return SNAPTARGET_NODE_SMOOTH;
return SNAPTARGET_NODE_CUSP;
}
{
if (state_held_shift(state)) {
if (state_held_control(state)) {
"<b>Shift+Ctrl:</b> drag out a handle and snap its angle "
"to %f° increments"), snap_increment_degrees());
}
return C_("Path node tip",
"<b>Shift:</b> drag out a handle, click to toggle selection");
}
}
if (state_held_control(state)) {
if (state_held_alt(state)) {
}
return C_("Path node tip",
"<b>Ctrl:</b> move along axes, click to change node type");
}
// assemble tip from node name
"<b>%s:</b> drag to shape the path, click to select this node"), nodetype);
}
{
g_string_free(x, TRUE);
g_string_free(y, TRUE);
return ret;
}
{
switch (type) {
case NODE_CUSP: return _("Cusp node");
case NODE_SMOOTH: return _("Smooth node");
case NODE_SYMMETRIC: return _("Symmetric node");
case NODE_AUTO: return _("Auto-smooth node");
default: return "";
}
}
/** Determine whether two nodes are joined by a linear segment. */
{
return false;
}
{
switch(type) {
case NODE_CUSP: return SP_CTRL_SHAPE_DIAMOND;
case NODE_SMOOTH: return SP_CTRL_SHAPE_SQUARE;
case NODE_AUTO: return SP_CTRL_SHAPE_CIRCLE;
case NODE_SYMMETRIC: return SP_CTRL_SHAPE_SQUARE;
default: return SP_CTRL_SHAPE_DIAMOND;
}
}
/**
* @class NodeList
* @brief An editable list of nodes representing a subpath.
*
* It can optionally be cyclic to represent a closed path.
* The list has iterators that act like plain node iterators, but can also be used
* to obtain shared pointers to nodes.
*/
, _closed(false)
{
this->list = this;
this->next = this;
this->prev = this;
}
{
clear();
}
{
return next == this;
}
{
return sz;
}
{
return _closed;
}
/** A subpath is degenerate if it has no segments - either one node in an open path
* or no nodes in a closed path */
bool NodeList::degenerate()
{
}
{
double intpart;
return ret;
}
// insert a node before i
{
return iterator(x);
}
{
}
{
++j;
}
{
}
}
{
// 1. make the list perfectly cyclic
// 2. find new begin
if (n > 0) {
} else {
}
// 3. relink begin to list
}
{
}
}
{
}
{
// some gymnastics are required to ensure that the node is valid when deleted;
// otherwise the code that updates handle visibility will break
++i;
delete rm;
return i;
}
// TODO this method is very ugly!
// converting SubpathList to an intrusive list might allow us to get rid of it
{
if (i->get() == this) {
return;
}
}
}
return *(n->list());
}
}
/**
* @class SubpathList
* @brief Editable path composed of one or more subpaths
*/
} // namespace UI
} // namespace Inkscape
/*
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:encoding=utf-8:textwidth=99 :