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 *
11 * Released under GNU GPL, read the file 'COPYING' for more information.
12 */
14 #include <map>
15 #include <list>
16 #include "libnr/nr-coord.h"
17 #include "libnr/nr-point.h"
18 #include "snapped-point.h"
20 struct SPNamedView;
21 struct SPItem;
23 namespace Inkscape
24 {
26 /// Parent for classes that can snap points to something
27 class Snapper
28 {
29 public:
30 Snapper() {}
31 Snapper(SPNamedView const *nv, ::NR::Coord const d);
32 virtual ~Snapper() {}
34 /// Point types to snap.
35 typedef int PointType;
36 static const PointType SNAPPOINT_NODE;
37 static const PointType SNAPPOINT_BBOX;
38 static const PointType SNAPPOINT_GUIDE;
40 void setSnapFrom(PointType t, bool s);
41 void setDistance(::NR::Coord d);
43 bool getSnapFrom(PointType t) const;
44 ::NR::Coord getDistance() const;
46 /**
47 * \return true if this Snapper will snap at least one kind of point.
48 */
49 virtual bool ThisSnapperMightSnap() const {return (_enabled && _snap_from != 0);} // will likely be overridden by derived classes
52 void setEnabled(bool s);
54 SnappedPoint freeSnap(PointType const &t,
55 NR::Point const &p,
56 SPItem const *it) const;
58 SnappedPoint freeSnap(PointType const &t,
59 NR::Point const &p,
60 std::list<SPItem const *> const &it) const;
62 class ConstraintLine
63 {
64 public:
65 ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
66 ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
68 bool hasPoint() const {
69 return _has_point;
70 }
72 NR::Point getPoint() const {
73 return _point;
74 }
76 NR::Point getDirection() const {
77 return _direction;
78 }
80 private:
82 bool _has_point;
83 NR::Point _point;
84 NR::Point _direction;
85 };
87 SnappedPoint constrainedSnap(PointType const &t,
88 NR::Point const &p,
89 ConstraintLine const &c,
90 SPItem const *it) const;
92 SnappedPoint constrainedSnap(PointType const &t,
93 NR::Point const &p,
94 ConstraintLine const &c,
95 std::list<SPItem const *> const &it) const;
96 protected:
97 SPNamedView const *_named_view;
98 int _snap_from; ///< bitmap of point types that we will snap from
99 bool _enabled; ///< true if this snapper is enabled, otherwise false
101 private:
103 /**
104 * Try to snap a point to whatever this snapper is interested in. Any
105 * snap that occurs will be to the nearest "interesting" thing (e.g. a
106 * grid or guide line)
107 *
108 * \param p Point to snap (desktop coordinates).
109 * \param it Items that should not be snapped to.
110 * \return Snapped point.
111 */
112 virtual SnappedPoint _doFreeSnap(PointType const &t,
113 NR::Point const &p,
114 std::list<SPItem const *> const &it) const = 0;
116 /**
117 * Try to snap a point to whatever this snapper is interested in, where
118 * the snap point is constrained to lie along a specified vector from the
119 * original point.
120 *
121 * \param p Point to snap (desktop coordinates).
122 * \param c Vector to constrain the snap to.
123 * \param it Items that should not be snapped to.
124 * \return Snapped point.
125 */
126 virtual SnappedPoint _doConstrainedSnap(PointType const &t,
127 NR::Point const &p,
128 ConstraintLine const &c,
129 std::list<SPItem const *> const &it) const = 0;
131 ::NR::Coord _distance; ///< snap distance (desktop coordinates)
132 };
134 }
136 #endif /* !SEEN_SNAPPER_H */
138 /*
139 Local Variables:
140 mode:c++
141 c-file-style:"stroustrup"
142 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
143 indent-tabs-mode:nil
144 fill-column:99
145 End:
146 */
147 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :