Code

Implement constrained snapping when dragging the position and size handles of a recta...
[inkscape.git] / src / snap.h
1 #ifndef SEEN_SNAP_H
2 #define SEEN_SNAP_H
4 /**
5  * \file snap.h
6  * \brief SnapManager class.
7  *
8  * Authors:
9  *   Lauris Kaplinski <lauris@kaplinski.com>
10  *   Frank Felfe <innerspace@iname.com>
11  *   Carl Hetherington <inkscape@carlh.net>
12  *   Diederik van Lierop <mail@diedenrezi.nl>
13  *
14  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15  * Copyright (C) 2000-2002 Lauris Kaplinski
16  * Copyright (C) 2000-2008 Authors
17  *
18  * Released under GNU GPL, read the file 'COPYING' for more information
19  */
21 #include <vector>
23 #include "guide-snapper.h"
24 #include "object-snapper.h"
25 #include "snap-preferences.h"
27 class SPNamedView;
29 /// Class to coordinate snapping operations
31 /**
32  *  Each SPNamedView has one of these.  It offers methods to snap points to whatever
33  *  snappers are defined (e.g. grid, guides etc.).  It also allows callers to snap
34  *  points which have undergone some transformation (e.g. translation, scaling etc.)
35  */
37 class SnapManager
38 {
39 public:
40         enum Transformation {
41         TRANSLATION,
42         SCALE,
43         STRETCH,
44         SKEW
45     };
47         SnapManager(SPNamedView const *v);
49     typedef std::list<const Inkscape::Snapper*> SnapperList;
51     bool someSnapperMightSnap() const;
52     bool gridSnapperMightSnap() const;
54     void setup(SPDesktop const *desktop,
55                         bool snapindicator = true,
56                         SPItem const *item_to_ignore = NULL,
57                         std::vector<std::pair<Geom::Point, int> > *unselected_nodes = NULL,
58                         SPGuide *guide_to_ignore = NULL);
60     void setup(SPDesktop const *desktop,
61                 bool snapindicator,
62                 std::vector<SPItem const *> &items_to_ignore,
63                 std::vector<std::pair<Geom::Point, int> > *unselected_nodes = NULL,
64                 SPGuide *guide_to_ignore = NULL);
66     // freeSnapReturnByRef() is preferred over freeSnap(), because it only returns a
67     // point if snapping has occurred (by overwriting p); otherwise p is untouched
68     void freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
69                                                         Geom::Point &p,
70                                                         Inkscape::SnapSourceType const source_type,
71                                                         bool first_point = true,
72                                                         Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
75     Inkscape::SnappedPoint freeSnap(Inkscape::SnapPreferences::PointType point_type,
76                                                                         Geom::Point const &p,
77                                                             Inkscape::SnapSourceType const &source_type,
78                                         bool first_point = true,
79                                     Geom::OptRect const &bbox_to_snap = Geom::OptRect() ) const;
81     Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
83     // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
84     // point, by overwriting p, if snapping has occurred; otherwise p is untouched
85     void constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
86                                                                         Geom::Point &p,
87                                                                         Inkscape::SnapSourceType const source_type,
88                                                                         Inkscape::Snapper::ConstraintLine const &constraint,
89                                                                         bool snap_projection, //try snapping the projection of p onto the constraint line, not p itself
90                                                                         bool first_point = true,
91                                                                         Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
93     Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
94                                                                                    Geom::Point const &p,
95                                                                                    Inkscape::SnapSourceType const &source_type,
96                                                                                    Inkscape::Snapper::ConstraintLine const &constraint,
97                                                                                    bool const snap_projection,
98                                            bool first_point = true,
99                                            Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
101     void guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const;
103     Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
104                                                std::vector<std::pair<Geom::Point, int> > const &p,
105                                                Geom::Point const &pointer,
106                                                Geom::Point const &tr) const;
108     Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
109                                                       std::vector<std::pair<Geom::Point, int> > const &p,
110                                                       Geom::Point const &pointer,
111                                                       Inkscape::Snapper::ConstraintLine const &constraint,
112                                                       Geom::Point const &tr) const;
114     Inkscape::SnappedPoint freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
115                                          std::vector<std::pair<Geom::Point, int> > const &p,
116                                          Geom::Point const &pointer,
117                                          Geom::Scale const &s,
118                                          Geom::Point const &o) const;
120     Inkscape::SnappedPoint constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
121                                                 std::vector<std::pair<Geom::Point, int> > const &p,
122                                                 Geom::Point const &pointer,
123                                                 Geom::Scale const &s,
124                                                 Geom::Point const &o) const;
126     Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
127                                                   std::vector<std::pair<Geom::Point, int> > const &p,
128                                                   Geom::Point const &pointer,
129                                                   Geom::Coord const &s,
130                                                   Geom::Point const &o,
131                                                   Geom::Dim2 d,
132                                                   bool uniform) const;
134     Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
135                                                std::vector<std::pair<Geom::Point, int> > const &p,
136                                                Geom::Point const &pointer,
137                                                Inkscape::Snapper::ConstraintLine const &constraint,
138                                                Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
139                                                Geom::Point const &o,
140                                                Geom::Dim2 d) const;
142     Inkscape::GuideSnapper guide;      ///< guide snapper
143     Inkscape::ObjectSnapper object;    ///< snapper to other objects
144     Inkscape::SnapPreferences snapprefs;
146     SnapperList getSnappers() const;
147     SnapperList getGridSnappers() const;
149     SPDesktop const *getDesktop() const {return _desktop;}
150     SPNamedView const *getNamedView() const {return _named_view;}
151     SPDocument *getDocument() const;
152     SPGuide const *getGuideToIgnore() const {return _guide_to_ignore;}
154     bool getSnapIndicator() const {return _snapindicator;}
156 protected:
157     SPNamedView const *_named_view;
159 private:
160     std::vector<SPItem const *> *_items_to_ignore;
161     SPItem const *_item_to_ignore;
162     SPGuide *_guide_to_ignore;
163     SPDesktop const *_desktop;
164     bool _snapindicator;
165     std::vector<std::pair<Geom::Point, int> > *_unselected_nodes;
167     Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
168                                             std::vector<std::pair<Geom::Point, int> > const &points,
169                                             Geom::Point const &pointer,
170                                             bool constrained,
171                                             Inkscape::Snapper::ConstraintLine const &constraint,
172                                             Transformation transformation_type,
173                                             Geom::Point const &transformation,
174                                             Geom::Point const &origin,
175                                             Geom::Dim2 dim,
176                                             bool uniform) const;
178     Geom::Point _transformPoint(std::pair<Geom::Point, int> const &p,
179                                             Transformation const transformation_type,
180                                             Geom::Point const &transformation,
181                                             Geom::Point const &origin,
182                                             Geom::Dim2 const dim,
183                                             bool const uniform) const;
185     void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, std::pair<Geom::Point, int> const &p) const;
187     Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, Inkscape::SnapSourceType const source_type, SnappedConstraints &sc, bool constrained) const;
188 };
190 #endif /* !SEEN_SNAP_H */
192 /*
193   Local Variables:
194   mode:c++
195   c-file-style:"stroustrup"
196   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
197   indent-tabs-mode:nil
198   fill-column:99
199   End:
200 */
201 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :