Code

fix 3d box drag
[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  *      Diederik van Lierop <mail@diedenrezi.nl>
11  *
12  *    Released under GNU GPL, read the file 'COPYING' for more information.
13  */
15 #include <map>
16 #include <list>
17 #include "libnr/nr-coord.h"
18 #include "libnr/nr-point.h"
20 #include "snapped-point.h"
21 #include "snapped-line.h"
23 struct SnappedConstraints {
24     std::list<Inkscape::SnappedPoint> points; 
25     std::list<Inkscape::SnappedLineSegment> lines;
26     std::list<Inkscape::SnappedLine> grid_lines;
27     std::list<Inkscape::SnappedLine> guide_lines;
28 };
30 struct SPNamedView;
31 struct SPItem;
33 namespace Inkscape
34 {
36 /// Parent for classes that can snap points to something
37 class Snapper
38 {
39 public:
40     Snapper() {}
41     Snapper(SPNamedView const *nv, ::NR::Coord const d);
42     virtual ~Snapper() {}
44     /// Point types to snap.
45     typedef int PointType;
46     static const PointType SNAPPOINT_NODE;
47     static const PointType SNAPPOINT_BBOX;
48     static const PointType SNAPPOINT_GUIDE;
50     void setSnapFrom(PointType t, bool s);
51     bool getSnapFrom(PointType t) const;
52     
53     void setSnapperTolerance(NR::Coord t);    
54     NR::Coord getSnapperTolerance() const; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
55     bool getSnapperAlwaysSnap() const; //if true, then the snapper will always snap, regardless of its tolerance
56     
57     /**
58     *  \return true if this Snapper will snap at least one kind of point.
59     */
60     virtual bool ThisSnapperMightSnap() const {return (_snap_enabled && _snap_from != 0);} // will likely be overridden by derived classes
62     void setEnabled(bool s);
63     bool getEnabled() const {return _snap_enabled;}
65     void freeSnap(SnappedConstraints &sc,
66                           PointType const &t,
67                           NR::Point const &p,
68                           bool const &first_point,                                             
69                           std::vector<NR::Point> &points_to_snap,                         
70                           SPItem const *it) const;
72     void freeSnap(SnappedConstraints &sc,
73                           PointType const &t,
74                           NR::Point const &p,
75                           bool const &first_point,                                             
76                           std::vector<NR::Point> &points_to_snap,                         
77                           std::list<SPItem const *> const &it) const;
79     class ConstraintLine
80     {
81     public:
82         ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
83         ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
85         bool hasPoint() const {
86             return _has_point;
87         }
89         NR::Point getPoint() const {
90             return _point;
91         }
93         NR::Point getDirection() const {
94             return _direction;
95         }
96         
97     private:
99         bool _has_point;
100         NR::Point _point;
101         NR::Point _direction;
102     };
104     void constrainedSnap(SnappedConstraints &sc,
105                                  PointType const &t,
106                                  NR::Point const &p,
107                                  bool const &first_point,
108                                  std::vector<NR::Point> &points_to_snap,       
109                                  ConstraintLine const &c,
110                                  SPItem const *it) const;
112     void constrainedSnap(SnappedConstraints &sc,
113                                  PointType const &t,
114                                  NR::Point const &p,
115                                  bool const &first_point,
116                                  std::vector<NR::Point> &points_to_snap,                         
117                                  ConstraintLine const &c,
118                                  std::list<SPItem const *> const &it) const;
119                                  
120 protected:
121     SPNamedView const *_named_view;
122     int _snap_from; ///< bitmap of point types that we will snap from
123     bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
124     
125 private:
126     NR::Coord _snapper_tolerance;   ///< snap tolerance in desktop coordinates 
127                                     // must be private to enforce the usage of getTolerance(), which retrieves 
128                                     // the tolerance in screen pixels (making it zoom independent)
131     /**
132      *  Try to snap a point to whatever this snapper is interested in.  Any
133      *  snap that occurs will be to the nearest "interesting" thing (e.g. a
134      *  grid or guide line)
135      *
136      *  \param p Point to snap (desktop coordinates).
137      *  \param it Items that should not be snapped to.
138      *  \return Snapped point.
139      */
140     virtual void _doFreeSnap(SnappedConstraints &sc,
141                                      PointType const &t,
142                                      NR::Point const &p,
143                                      bool const &first_point,                                             
144                                      std::vector<NR::Point> &points_to_snap,
145                                      std::list<SPItem const *> const &it) const = 0;
147     /**
148      *  Try to snap a point to whatever this snapper is interested in, where
149      *  the snap point is constrained to lie along a specified vector from the
150      *  original point.
151      *
152      *  \param p Point to snap (desktop coordinates).
153      *  \param c Vector to constrain the snap to.
154      *  \param it Items that should not be snapped to.
155      *  \return Snapped point.
156      */    
157     virtual void _doConstrainedSnap(SnappedConstraints &sc,
158                                             PointType const &t,
159                                             NR::Point const &p,
160                                             bool const &first_point,
161                                             std::vector<NR::Point> &points_to_snap,
162                                             ConstraintLine const &c,
163                                             std::list<SPItem const *> const &it) const = 0;
164 };
168 #endif /* !SEEN_SNAPPER_H */
170 /*
171   Local Variables:
172   mode:c++
173   c-file-style:"stroustrup"
174   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
175   indent-tabs-mode:nil
176   fill-column:99
177   End:
178 */
179 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :