1 /**
2 * \file src/snapper.cpp
3 * \brief Snapper class.
4 *
5 * Authors:
6 * Carl Hetherington <inkscape@carlh.net>
7 *
8 * Released under GNU GPL, read the file 'COPYING' for more information.
9 */
11 #include "libnr/nr-values.h"
12 #include "sp-namedview.h"
14 Inkscape::Snapper::PointType const Inkscape::Snapper::BBOX_POINT = 0x1;
15 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAP_POINT = 0x2;
17 /**
18 * Construct new Snapper for named view.
19 * \param nv Named view.
20 * \param d Snap distance.
21 */
22 Inkscape::Snapper::Snapper(SPNamedView const *nv, NR::Coord const d) : _named_view(nv), _distance(d), _enabled(true)
23 {
24 g_assert(_named_view != NULL);
25 g_assert(SP_IS_NAMEDVIEW(_named_view));
27 setSnapTo(BBOX_POINT, true);
28 }
30 /**
31 * Set snap distance.
32 * \param d New snap distance (desktop coordinates)
33 */
34 void Inkscape::Snapper::setDistance(NR::Coord const d)
35 {
36 _distance = d;
37 }
39 /**
40 * \return Snap distance (desktop coordinates)
41 */
42 NR::Coord Inkscape::Snapper::getDistance() const
43 {
44 return _distance;
45 }
47 /**
48 * Turn on/off snapping of specific point types.
49 * \param t Point type.
50 * \param s true to snap to this point type, otherwise false;
51 */
52 void Inkscape::Snapper::setSnapTo(PointType t, bool s)
53 {
54 if (s) {
55 _snap_to |= t;
56 } else {
57 _snap_to &= ~t;
58 }
59 }
61 /**
62 * \param t Point type.
63 * \return true if snapper will snap this type of point, otherwise false.
64 */
65 bool Inkscape::Snapper::getSnapTo(PointType t) const
66 {
67 return (_snap_to & t);
68 }
70 /**
71 * \return true if this Snapper will snap at least one kind of point.
72 */
73 bool Inkscape::Snapper::willSnapSomething() const
74 {
75 return (_enabled && _snap_to != 0);
76 }
79 /**
80 * \param s true to enable this snapper, otherwise false.
81 */
83 void Inkscape::Snapper::setEnabled(bool s)
84 {
85 _enabled = s;
86 }
89 /**
90 * Try to snap a point to whatever this snapper is interested in. Any
91 * snap that occurs will be to the nearest "interesting" thing (e.g. a
92 * grid or guide line)
93 *
94 * \param t Point type.
95 * \param p Point to snap (desktop coordinates).
96 * \param it Item that should not be snapped to.
97 * \return Snapped point.
98 */
100 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType t,
101 NR::Point const &p,
102 SPItem const *it) const
103 {
104 std::list<SPItem const *> lit;
105 lit.push_back(it);
106 return freeSnap(t, p, lit);
107 }
110 /**
111 * Try to snap a point to whatever this snapper is interested in. Any
112 * snap that occurs will be to the nearest "interesting" thing (e.g. a
113 * grid or guide line)
114 *
115 * \param t Point type.
116 * \param p Point to snap (desktop coordinates).
117 * \param it Items that should not be snapped to.
118 * \return Snapped point.
119 */
121 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType t,
122 NR::Point const &p,
123 std::list<SPItem const *> const &it) const
124 {
125 if (_enabled == false || getSnapTo(t) == false) {
126 return SnappedPoint(p, NR_HUGE);
127 }
129 return _doFreeSnap(p, it);
130 }
135 /**
136 * Try to snap a point to whatever this snapper is interested in, where
137 * the snap point is constrained to lie along a specified vector from the
138 * original point.
139 *
140 * \param p Point to snap (desktop coordinates).
141 * \param c Vector to constrain the snap to.
142 * \param it Items that should not be snapped to.
143 * \return Snapped point.
144 */
146 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType t,
147 NR::Point const &p,
148 ConstraintLine const &c,
149 SPItem const *it) const
150 {
151 std::list<SPItem const *> lit;
152 lit.push_back(it);
153 return constrainedSnap(t, p, c, lit);
154 }
157 /**
158 * Try to snap a point to whatever this snapper is interested in, where
159 * the snap point is constrained to lie along a specified vector from the
160 * original point.
161 *
162 * \param p Point to snap (desktop coordinates).
163 * \param c Vector to constrain the snap to.
164 * \param it Items that should not be snapped to.
165 * \return Snapped point.
166 */
168 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType t,
169 NR::Point const &p,
170 ConstraintLine const &c,
171 std::list<SPItem const *> const &it) const
172 {
173 if (_enabled == false || getSnapTo(t) == false) {
174 return SnappedPoint(p, NR_HUGE);
175 }
177 return _doConstrainedSnap(p, c, it);
178 }
180 /*
181 Local Variables:
182 mode:c++
183 c-file-style:"stroustrup"
184 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
185 indent-tabs-mode:nil
186 fill-column:99
187 End:
188 */
189 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :