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