Code

Various snapping cleanups and bug fixes.
[inkscape.git] / src / snapper.h
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"
18 #include "snapped-point.h"
20 struct SPNamedView;
21 struct SPItem;
23 namespace Inkscape
24 {
26 /// Parent for classes that can snap points to something
27 class Snapper
28 {
29 public:
30     Snapper() {}
31     Snapper(SPNamedView const *nv, ::NR::Coord const d);
32     virtual ~Snapper() {}
34     /// Point types to snap.
35     typedef int PointType;
36     static const PointType SNAP_POINT;
37     static const PointType BBOX_POINT;
39     typedef std::pair<PointType, NR::Point> PointWithType;
41     void setSnapTo(PointType t, bool s);
42     void setDistance(::NR::Coord d);
44     bool getSnapTo(PointType t) const;
45     ::NR::Coord getDistance() const;
47     bool willSnapSomething() const;
49     SnappedPoint freeSnap(PointType t,
50                           NR::Point const &p,
51                           SPItem const *it) const;
53     SnappedPoint freeSnap(PointType t,
54                           NR::Point const &p,
55                           std::list<SPItem const *> const &it) const;
57     class ConstraintLine
58     {
59     public:
60         ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
61         ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
63         bool hasPoint() const {
64             return _has_point;
65         }
67         NR::Point getPoint() const {
68             return _point;
69         }
71         NR::Point getDirection() const {
72             return _direction;
73         }
74         
75     private:
77         bool _has_point;
78         NR::Point _point;
79         NR::Point _direction;
80     };
82     SnappedPoint constrainedSnap(PointType t,
83                                  NR::Point const &p,
84                                  ConstraintLine const &c,
85                                  SPItem const *it) const;
87     SnappedPoint constrainedSnap(PointType t,
88                                  NR::Point const &p,
89                                  ConstraintLine const &c,
90                                  std::list<SPItem const *> const &it) const;
91 protected:
92     SPNamedView const *_named_view;
93     
94 private:
96     /**
97      *  Try to snap a point to whatever this snapper is interested in.  Any
98      *  snap that occurs will be to the nearest "interesting" thing (e.g. a
99      *  grid or guide line)
100      *
101      *  \param p Point to snap (desktop coordinates).
102      *  \param it Items that should not be snapped to.
103      *  \return Snapped point.
104      */
105     virtual SnappedPoint _doFreeSnap(NR::Point const &p,
106                                      std::list<SPItem const *> const &it) const = 0;
108     /**
109      *  Try to snap a point to whatever this snapper is interested in, where
110      *  the snap point is constrained to lie along a specified vector from the
111      *  original point.
112      *
113      *  \param p Point to snap (desktop coordinates).
114      *  \param c Vector to constrain the snap to.
115      *  \param it Items that should not be snapped to.
116      *  \return Snapped point.
117      */    
118     virtual SnappedPoint _doConstrainedSnap(NR::Point const &p,
119                                             ConstraintLine const &c,
120                                             std::list<SPItem const *> const &it) const = 0;
121     
122     ::NR::Coord _distance; ///< snap distance (desktop coordinates)
123     int _snap_to; ///< bitmap of point types that we will snap to 
124 };
128 #endif /* !SEEN_SNAPPER_H */
130 /*
131   Local Variables:
132   mode:c++
133   c-file-style:"stroustrup"
134   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135   indent-tabs-mode:nil
136   fill-column:99
137   End:
138 */
139 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :