1 #ifndef SEEN_SNAPPER_H
2 #define SEEN_SNAPPER_H
4 /**
5 * \file src/snapper.h
6 * \brief Snapper class.
7 *
8 * Authors:
9 * Carl Hetherington <inkscape@carlh.net>
10 * Diederik van Lierop <mail@diedenrezi.nl>
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information.
13 */
15 #include <map>
16 #include <list>
17 #include <boost/optional.hpp>
18 #include <glib.h> // for g_assert
20 #include "snapped-point.h"
21 #include "snapped-line.h"
22 #include "snapped-curve.h"
23 #include "snap-preferences.h"
24 #include "snap-candidate.h"
26 struct SnappedConstraints {
27 std::list<Inkscape::SnappedPoint> points;
28 std::list<Inkscape::SnappedLineSegment> lines;
29 std::list<Inkscape::SnappedLine> grid_lines;
30 std::list<Inkscape::SnappedLine> guide_lines;
31 std::list<Inkscape::SnappedCurve> curves;
32 };
34 class SnapManager;
35 struct SPItem;
37 namespace Inkscape
38 {
39 /// Parent for classes that can snap points to something
40 class Snapper
41 {
42 public:
43 Snapper() {}
44 Snapper(SnapManager *sm, ::Geom::Coord const t);
45 virtual ~Snapper() {}
47 virtual Geom::Coord getSnapperTolerance() const = 0; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
48 virtual bool getSnapperAlwaysSnap() const = 0; //if true, then the snapper will always snap, regardless of its tolerance
50 /**
51 * \return true if this Snapper will snap at least one kind of point.
52 */
53 virtual bool ThisSnapperMightSnap() const {return _snap_enabled;} // will likely be overridden by derived classes
55 // These four methods are only used for grids, for which snapping can be enabled individually
56 void setEnabled(bool s);
57 void setSnapVisibleOnly(bool s);
58 bool getEnabled() const {return _snap_enabled;}
59 bool getSnapVisibleOnly() const {return _snap_visible_only;}
61 virtual void freeSnap(SnappedConstraints &/*sc*/,
62 Inkscape::SnapCandidatePoint const &/*p*/,
63 Geom::OptRect const &/*bbox_to_snap*/,
64 std::vector<SPItem const *> const */*it*/,
65 std::vector<SnapCandidatePoint> */*unselected_nodes*/) const {};
67 // Class for storing the constraint for constrained snapping; can be
68 // - a line (infinite line with origin, running through _point pointing in _direction)
69 // - a direction (infinite line without origin, i.e. only a direction vector, stored in _direction)
70 // - a circle (_point denotes the center, _radius doesn't need an explanation, _direction contains
71 // the vector from the origin to the original untransformed point);
72 class SnapConstraint
73 {
74 private:
75 enum SnapConstraintType {LINE, DIRECTION, CIRCLE};
77 public:
78 // Constructs a direction constraint, e.g. horizontal or vertical but without a specified point
79 SnapConstraint(Geom::Point const &d) : _direction(d), _type(DIRECTION) {}
80 // Constructs a linear constraint
81 SnapConstraint(Geom::Point const &p, Geom::Point const &d) : _point(p), _direction(d), _type(LINE) {}
82 SnapConstraint(Geom::Line const &l) : _point(l.origin()), _direction(l.versor()), _type(LINE) {}
83 // Constructs a circular constraint
84 SnapConstraint(Geom::Point const &p, Geom::Point const &d, Geom::Coord const &r) : _point(p), _direction(d), _radius(r), _type(CIRCLE) {}
86 bool hasPoint() const {return _type != DIRECTION;}
88 Geom::Point getPoint() const {
89 g_assert(_type != DIRECTION);
90 return _point;
91 }
93 Geom::Point getDirection() const {
94 return _direction;
95 }
97 Geom::Coord getRadius() const {
98 g_assert(_type == CIRCLE);
99 return _radius;
100 }
102 bool isCircular() const { return _type == CIRCLE; }
103 bool isLinear() const { return _type == LINE; }
104 bool isDirection() const { return _type == DIRECTION; }
106 Geom::Point projection(Geom::Point const &p) const { // returns the projection of p on this constraint
107 if (_type == CIRCLE) {
108 // project on to a circular constraint
109 Geom::Point v_orig = p - _point;
110 Geom::Coord l = Geom::L2(v_orig);
111 if (l > 0) {
112 return _point + _radius * v_orig/l; // Length of _direction is equal to the radius
113 } else {
114 // point to be projected is exactly at the center of the circle, so any point on the circle is a projection
115 return _point + Geom::Point(_radius, 0);
116 }
117 } else {
118 // project on to a linear constraint
119 Geom::Point const p1_on_cl = (_type == LINE) ? _point : p;
120 Geom::Point const p2_on_cl = p1_on_cl + _direction;
121 return Geom::projection(p, Geom::Line(p1_on_cl, p2_on_cl));
122 }
123 }
125 private:
126 Geom::Point _point;
127 Geom::Point _direction;
128 Geom::Coord _radius;
129 SnapConstraintType _type;
130 };
132 virtual void constrainedSnap(SnappedConstraints &/*sc*/,
133 Inkscape::SnapCandidatePoint const &/*p*/,
134 Geom::OptRect const &/*bbox_to_snap*/,
135 SnapConstraint const &/*c*/,
136 std::vector<SPItem const *> const */*it*/) const {};
138 protected:
139 SnapManager *_snapmanager;
141 // This is only used for grids, for which snapping can be enabled individually
142 bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
143 bool _snap_visible_only;
144 };
146 }
148 #endif /* !SEEN_SNAPPER_H */
150 /*
151 Local Variables:
152 mode:c++
153 c-file-style:"stroustrup"
154 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155 indent-tabs-mode:nil
156 fill-column:99
157 End:
158 */
159 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :