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"
13 #include "inkscape.h"
14 #include "desktop.h"
16 Inkscape::Snapper::PointType const Inkscape::Snapper::BBOX_POINT = 0x1;
17 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAP_POINT = 0x2;
19 /**
20 * Construct new Snapper for named view.
21 * \param nv Named view.
22 * \param d Snap distance.
23 */
24 Inkscape::Snapper::Snapper(SPNamedView const *nv, NR::Coord const d) : _named_view(nv), _enabled(true), _distance(d)
25 {
26 g_assert(_named_view != NULL);
27 g_assert(SP_IS_NAMEDVIEW(_named_view));
29 setSnapTo(BBOX_POINT | SNAP_POINT, true); //Snap any point. In v0.45 and earlier, this was controlled in the preferences tab
30 }
32 /**
33 * Set snap distance.
34 * \param d New snap distance (desktop coordinates)
35 */
36 void Inkscape::Snapper::setDistance(NR::Coord const d)
37 {
38 _distance = d;
39 }
41 /**
42 * \return Snap distance (desktop coordinates); depends on current zoom so that it's always the same in screen pixels
43 */
44 NR::Coord Inkscape::Snapper::getDistance() const
45 {
46 return _distance / SP_ACTIVE_DESKTOP->current_zoom();
47 }
49 /**
50 * Turn on/off snapping of specific point types.
51 * \param t Point type.
52 * \param s true to snap to this point type, otherwise false;
53 */
54 void Inkscape::Snapper::setSnapTo(PointType t, bool s)
55 {
56 if (s) {
57 _snap_to |= t;
58 } else {
59 _snap_to &= ~t;
60 }
61 }
63 /**
64 * \param t Point type.
65 * \return true if snapper will snap this type of point, otherwise false.
66 */
67 bool Inkscape::Snapper::getSnapTo(PointType t) const
68 {
69 return (_snap_to & t);
70 }
72 /**
73 * \param s true to enable this snapper, otherwise false.
74 */
76 void Inkscape::Snapper::setEnabled(bool s)
77 {
78 _enabled = s;
79 }
82 /**
83 * Try to snap a point to whatever this snapper is interested in. Any
84 * snap that occurs will be to the nearest "interesting" thing (e.g. a
85 * grid or guide line)
86 *
87 * \param t Point type.
88 * \param p Point to snap (desktop coordinates).
89 * \param it Item that should not be snapped to.
90 * \return Snapped point.
91 */
93 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType t,
94 NR::Point const &p,
95 SPItem const *it) const
96 {
97 std::list<SPItem const *> lit;
98 lit.push_back(it);
99 return freeSnap(t, p, lit);
100 }
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 t Point type.
109 * \param p Point to snap (desktop coordinates).
110 * \param it Items that should not be snapped to.
111 * \return Snapped point.
112 */
114 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType t,
115 NR::Point const &p,
116 std::list<SPItem const *> const &it) const
117 {
118 if (_enabled == false || getSnapTo(t) == false) {
119 return SnappedPoint(p, NR_HUGE);
120 }
122 return _doFreeSnap(p, it);
123 }
128 /**
129 * Try to snap a point to whatever this snapper is interested in, where
130 * the snap point is constrained to lie along a specified vector from the
131 * original point.
132 *
133 * \param p Point to snap (desktop coordinates).
134 * \param c Vector to constrain the snap to.
135 * \param it Items that should not be snapped to.
136 * \return Snapped point.
137 */
139 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType t,
140 NR::Point const &p,
141 ConstraintLine const &c,
142 SPItem const *it) const
143 {
144 std::list<SPItem const *> lit;
145 lit.push_back(it);
146 return constrainedSnap(t, p, c, lit);
147 }
150 /**
151 * Try to snap a point to whatever this snapper is interested in, where
152 * the snap point is constrained to lie along a specified vector from the
153 * original point.
154 *
155 * \param p Point to snap (desktop coordinates).
156 * \param c Vector to constrain the snap to.
157 * \param it Items that should not be snapped to.
158 * \return Snapped point.
159 */
161 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType t,
162 NR::Point const &p,
163 ConstraintLine const &c,
164 std::list<SPItem const *> const &it) const
165 {
166 if (_enabled == false || getSnapTo(t) == false) {
167 return SnappedPoint(p, NR_HUGE);
168 }
170 return _doConstrainedSnap(p, c, it);
171 }
173 /*
174 Local Variables:
175 mode:c++
176 c-file-style:"stroustrup"
177 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
178 indent-tabs-mode:nil
179 fill-column:99
180 End:
181 */
182 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :