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), _distance(d), _enabled(true)
25 {
26 g_assert(_named_view != NULL);
27 g_assert(SP_IS_NAMEDVIEW(_named_view));
29 setSnapTo(BBOX_POINT, true);
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 * \return true if this Snapper will snap at least one kind of point.
74 */
75 bool Inkscape::Snapper::willSnapSomething() const
76 {
77 return (_enabled && _snap_to != 0);
78 }
81 /**
82 * \param s true to enable this snapper, otherwise false.
83 */
85 void Inkscape::Snapper::setEnabled(bool s)
86 {
87 _enabled = s;
88 }
91 /**
92 * Try to snap a point to whatever this snapper is interested in. Any
93 * snap that occurs will be to the nearest "interesting" thing (e.g. a
94 * grid or guide line)
95 *
96 * \param t Point type.
97 * \param p Point to snap (desktop coordinates).
98 * \param it Item that should not be snapped to.
99 * \return Snapped point.
100 */
102 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType t,
103 NR::Point const &p,
104 SPItem const *it) const
105 {
106 std::list<SPItem const *> lit;
107 lit.push_back(it);
108 return freeSnap(t, p, lit);
109 }
112 /**
113 * Try to snap a point to whatever this snapper is interested in. Any
114 * snap that occurs will be to the nearest "interesting" thing (e.g. a
115 * grid or guide line)
116 *
117 * \param t Point type.
118 * \param p Point to snap (desktop coordinates).
119 * \param it Items that should not be snapped to.
120 * \return Snapped point.
121 */
123 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType t,
124 NR::Point const &p,
125 std::list<SPItem const *> const &it) const
126 {
127 if (_enabled == false || getSnapTo(t) == false) {
128 return SnappedPoint(p, NR_HUGE);
129 }
131 return _doFreeSnap(p, it);
132 }
137 /**
138 * Try to snap a point to whatever this snapper is interested in, where
139 * the snap point is constrained to lie along a specified vector from the
140 * original point.
141 *
142 * \param p Point to snap (desktop coordinates).
143 * \param c Vector to constrain the snap to.
144 * \param it Items that should not be snapped to.
145 * \return Snapped point.
146 */
148 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType t,
149 NR::Point const &p,
150 ConstraintLine const &c,
151 SPItem const *it) const
152 {
153 std::list<SPItem const *> lit;
154 lit.push_back(it);
155 return constrainedSnap(t, p, c, lit);
156 }
159 /**
160 * Try to snap a point to whatever this snapper is interested in, where
161 * the snap point is constrained to lie along a specified vector from the
162 * original point.
163 *
164 * \param p Point to snap (desktop coordinates).
165 * \param c Vector to constrain the snap to.
166 * \param it Items that should not be snapped to.
167 * \return Snapped point.
168 */
170 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType t,
171 NR::Point const &p,
172 ConstraintLine const &c,
173 std::list<SPItem const *> const &it) const
174 {
175 if (_enabled == false || getSnapTo(t) == false) {
176 return SnappedPoint(p, NR_HUGE);
177 }
179 return _doConstrainedSnap(p, c, it);
180 }
182 /*
183 Local Variables:
184 mode:c++
185 c-file-style:"stroustrup"
186 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
187 indent-tabs-mode:nil
188 fill-column:99
189 End:
190 */
191 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :