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"
19 #include "snapped-point.h"
20 #include "snapped-line.h"
22 struct SnappedConstraints {
23 std::list<Inkscape::SnappedPoint> points;
24 std::list<Inkscape::SnappedLineSegment> lines;
25 std::list<Inkscape::SnappedLine> grid_lines;
26 std::list<Inkscape::SnappedLine> guide_lines;
27 };
29 struct SPNamedView;
30 struct SPItem;
32 namespace Inkscape
33 {
35 /// Parent for classes that can snap points to something
36 class Snapper
37 {
38 public:
39 Snapper() {}
40 Snapper(SPNamedView const *nv, ::NR::Coord const d);
41 virtual ~Snapper() {}
43 /// Point types to snap.
44 typedef int PointType;
45 static const PointType SNAPPOINT_NODE;
46 static const PointType SNAPPOINT_BBOX;
47 static const PointType SNAPPOINT_GUIDE;
49 void setSnapFrom(PointType t, bool s);
50 void setDistance(::NR::Coord d);
52 bool getSnapFrom(PointType t) const;
53 ::NR::Coord getDistance() const;
55 /**
56 * \return true if this Snapper will snap at least one kind of point.
57 */
58 virtual bool ThisSnapperMightSnap() const {return (_snap_enabled && _snap_from != 0);} // will likely be overridden by derived classes
60 void setEnabled(bool s);
61 bool getEnabled() const {return _snap_enabled;}
63 void freeSnap(SnappedConstraints &sc,
64 PointType const &t,
65 NR::Point const &p,
66 bool const &first_point,
67 std::vector<NR::Point> &points_to_snap,
68 SPItem const *it) const;
70 void freeSnap(SnappedConstraints &sc,
71 PointType const &t,
72 NR::Point const &p,
73 bool const &first_point,
74 std::vector<NR::Point> &points_to_snap,
75 std::list<SPItem const *> const &it) const;
77 class ConstraintLine
78 {
79 public:
80 ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
81 ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
83 bool hasPoint() const {
84 return _has_point;
85 }
87 NR::Point getPoint() const {
88 return _point;
89 }
91 NR::Point getDirection() const {
92 return _direction;
93 }
95 private:
97 bool _has_point;
98 NR::Point _point;
99 NR::Point _direction;
100 };
102 void constrainedSnap(SnappedConstraints &sc,
103 PointType const &t,
104 NR::Point const &p,
105 bool const &first_point,
106 std::vector<NR::Point> &points_to_snap,
107 ConstraintLine const &c,
108 SPItem const *it) const;
110 void constrainedSnap(SnappedConstraints &sc,
111 PointType const &t,
112 NR::Point const &p,
113 bool const &first_point,
114 std::vector<NR::Point> &points_to_snap,
115 ConstraintLine const &c,
116 std::list<SPItem const *> const &it) const;
118 protected:
119 SPNamedView const *_named_view;
120 int _snap_from; ///< bitmap of point types that we will snap from
121 bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
123 private:
125 /**
126 * Try to snap a point to whatever this snapper is interested in. Any
127 * snap that occurs will be to the nearest "interesting" thing (e.g. a
128 * grid or guide line)
129 *
130 * \param p Point to snap (desktop coordinates).
131 * \param it Items that should not be snapped to.
132 * \return Snapped point.
133 */
134 virtual void _doFreeSnap(SnappedConstraints &sc,
135 PointType const &t,
136 NR::Point const &p,
137 bool const &first_point,
138 std::vector<NR::Point> &points_to_snap,
139 std::list<SPItem const *> const &it) const = 0;
141 /**
142 * Try to snap a point to whatever this snapper is interested in, where
143 * the snap point is constrained to lie along a specified vector from the
144 * original point.
145 *
146 * \param p Point to snap (desktop coordinates).
147 * \param c Vector to constrain the snap to.
148 * \param it Items that should not be snapped to.
149 * \return Snapped point.
150 */
151 virtual void _doConstrainedSnap(SnappedConstraints &sc,
152 PointType const &t,
153 NR::Point const &p,
154 bool const &first_point,
155 std::vector<NR::Point> &points_to_snap,
156 ConstraintLine const &c,
157 std::list<SPItem const *> const &it) const = 0;
159 NR::Coord _distance; ///< snap distance (desktop coordinates)
160 };
162 }
164 #endif /* !SEEN_SNAPPER_H */
166 /*
167 Local Variables:
168 mode:c++
169 c-file-style:"stroustrup"
170 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
171 indent-tabs-mode:nil
172 fill-column:99
173 End:
174 */
175 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :