1 /** @file
2 * @brief Path handler in node edit mode
3 */
4 /* Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 *
7 * This code is in public domain
8 */
10 #ifndef SEEN_SP_NODEPATH_H
11 #define SEEN_SP_NODEPATH_H
13 #include "libnr/nr-path-code.h"
14 #include <glibmm/ustring.h>
15 #include <gdk/gdkevents.h>
16 #include <list>
17 #include <2geom/point.h>
18 #include <2geom/matrix.h>
19 #include <boost/optional.hpp>
22 struct SPCanvasItem;
23 class SPCurve;
24 struct SPItem;
25 class SPObject;
26 class SPDesktop;
27 class SPPath;
28 class SPKnot;
29 class LivePathEffectObject;
31 namespace Inkscape {
32 namespace XML {
33 class Node;
34 }
36 namespace LivePathEffect {
37 class Effect;
38 }
39 }
41 typedef std::map<Inkscape::LivePathEffect::Effect *, std::vector<SPCanvasItem *> > HelperPathList;
43 /**
44 * Radial objects are represented by an angle and a distance from
45 * 0,0. 0,0 is represented by a == big_num.
46 */
47 class Radial{
48 public:
49 /** Radius */
50 double r;
51 /** Amplitude */
52 double a;
53 Radial() {}
54 // Radial(Geom::Point const &p); // Convert a point to radial coordinates
55 Radial(Radial &p) : r(p.r),a(p.a) {}
56 // operator Geom::Point() const;
58 /**
59 * Construct Radial from Geom::Point.
60 */
61 Radial(Geom::Point const &p)
62 {
63 r = Geom::L2(p);
64 if (r > 0) {
65 a = Geom::atan2 (p);
66 } else {
67 a = HUGE_VAL; //undefined
68 }
69 }
71 /**
72 * Cast Radial to cartesian Geom::Point.
73 */
74 operator Geom::Point() const
75 {
76 if (a == HUGE_VAL) {
77 return Geom::Point(0,0);
78 } else {
79 return r*Geom::Point(cos(a), sin(a));
80 }
81 }
83 };
85 class ShapeEditor;
87 namespace Inkscape {
88 namespace NodePath {
90 /**
91 * The entire nodepath, containing multiple subpaths
92 */
93 class Path;
95 /**
96 * A subpath is a continuous chain of linked nodes
97 */
98 class SubPath;
100 /**
101 * One side of a node, i.e. prev or next
102 */
103 class NodeSide;
105 /**
106 * A node on a subpath
107 */
108 class Node;
111 /**
112 * This is the lowest list item, a simple list of nodes.
113 */
114 class SubPath {
115 public:
116 /** The parent of this subpath */
117 Path * nodepath;
118 /** Is this path closed (no endpoints) or not?*/
119 gboolean closed;
120 /** The nodes in this subpath. */
121 GList * nodes;
122 /** The first node of the subpath (does not imply open/closed)*/
123 Node * first;
124 /** The last node of the subpath */
125 Node * last;
126 };
130 /**
131 * What kind of node is this? This is the value for the node->type
132 * field. NodeType indicates the degree of continuity required for
133 * the node. I think that the corresponding integer indicates which
134 * derivate is connected. (Thus 2 means that the node is continuous
135 * to the second derivative, i.e. has matching endpoints and tangents)
136 */
137 typedef enum {
138 /** A normal node */
139 NODE_NONE,
140 /** This node non-continuously joins two segments.*/
141 NODE_CUSP,
142 /** This node continuously joins two segments. */
143 NODE_SMOOTH,
144 /** This node has automatic handles. */
145 NODE_AUTO,
146 /** This node is symmetric. */
147 NODE_SYMM
148 } NodeType;
152 /**
153 * A NodeSide is a datarecord which may be on either side (n or p) of a node,
154 * which describes the segment going to the next node.
155 */
156 class NodeSide{
157 public:
158 /** Pointer to the next node, */
159 Node * other;
160 /** Position */
161 Geom::Point pos;
162 /** Origin (while dragging) in radial notation */
163 Radial origin_radial;
164 /** Origin (while dragging) in x/y notation */
165 Geom::Point origin;
166 /** Knots are Inkscape's way of providing draggable points. This
167 * Knot is the point on the curve representing the control point in a
168 * bezier curve.*/
169 SPKnot * knot;
170 /** What kind of rendering? */
171 SPCanvasItem * line;
172 };
174 /**
175 * A node along a NodePath
176 */
177 class Node {
178 public:
179 /** The parent subpath of this node */
180 SubPath * subpath;
181 /** Type is selected from NodeType.*/
182 guint type : 4;
183 /** Code refers to which ArtCode is used to represent the segment
184 * (which segment?).*/
185 guint code : 4;
186 /** Boolean. Am I currently selected or not? */
187 guint selected : 1;
188 /** */
189 Geom::Point pos;
190 /** */
191 Geom::Point origin;
192 /** Knots are Inkscape's way of providing draggable points. This
193 * Knot is the point on the curve representing the endpoint.*/
194 SPKnot * knot;
195 /** The NodeSide in the 'next' direction */
196 NodeSide n;
197 /** The NodeSide in the 'previous' direction */
198 NodeSide p;
200 /** The pointer to the nodeside which we are dragging out with Shift */
201 NodeSide *dragging_out;
203 /** Boolean. Am I being dragged? */
204 guint is_dragging : 1;
205 };
207 /**
208 * This is a collection of subpaths which contain nodes
209 *
210 * In the following data model. Nodepaths are made up of subpaths which
211 * are comprised of nodes.
212 *
213 * Nodes are linked thus:
214 * \verbatim
215 n other
216 node -----> nodeside ------> node \endverbatim
217 */
218 class Path {
219 public:
220 /** Constructor should private, people should create new nodepaths using sp_nodepath_new
221 * But for some reason I cannot make sp_nodepath_new a friend :-(
222 */
223 Path() {};
224 /** Destructor */
225 ~Path();
227 /** Pointer to the current desktop, for reporting purposes */
228 SPDesktop * desktop;
229 /** The parent path of this nodepath */
230 SPObject * object;
231 /** The parent livepatheffect of this nodepath, if applicable */
232 SPItem * item;
233 /** The context which created this nodepath. Important if this nodepath is deleted */
234 ShapeEditor *shape_editor;
235 /** The subpaths which comprise this NodePath */
236 GList * subpaths;
237 /** A list of nodes which are currently selected */
238 GList * selected;
239 /** Transforms (userspace <---> virtual space? someone please describe )
240 njh: I'd be guessing that these are item <-> desktop transforms.*/
241 Geom::Matrix i2d, d2i;
242 /** The DOM node which describes this NodePath */
243 Inkscape::XML::Node *repr;
244 gchar *repr_key;
245 gchar *repr_nodetypes_key;
246 //STL compliant method to get the selected nodes
247 void selection(std::list<Node *> &l);
249 guint numSelected() {return (selected? g_list_length(selected) : 0);}
250 Geom::Point& singleSelectedCoords() {return (((Node *) selected->data)->pos);}
252 /// draw a "sketch" of the path by using these variables
253 SPCanvasItem *helper_path;
254 SPCurve *curve;
255 bool show_helperpath;
256 guint32 helperpath_rgba;
257 gdouble helperpath_width;
259 // the helperpaths provided by all LPEs (and their paramaters) of the current item
260 HelperPathList helper_path_vec;
262 /// true if we changed repr, to tell this change from an external one such as from undo, simplify, or another desktop
263 unsigned int local_change;
265 /// true if we're showing selected nodes' handles
266 bool show_handles;
268 /// true if the path cannot contain curves, just straight lines
269 bool straight_path;
271 /// active_node points to the node that is currently mouseovered (= NULL if
272 /// there isn't any); we also consider the node mouseovered if it is covered
273 /// by one of its handles and the latter is mouseovered
274 static Node *active_node;
276 /// Location of mouse pointer when we started dragging, needed for snapping
277 Geom::Point drag_origin_mouse;
279 };
281 } // namespace NodePath
282 } // namespace Inkscape
284 enum {
285 SCULPT_PROFILE_LINEAR,
286 SCULPT_PROFILE_BELL,
287 SCULPT_PROFILE_ELLIPTIC
288 };
290 // Do function documentation in nodepath.cpp
291 Inkscape::NodePath::Path * sp_nodepath_new (SPDesktop * desktop, SPObject *object, bool show_handles, const char * repr_key = NULL, SPItem *item = NULL);
292 void sp_nodepath_deselect (Inkscape::NodePath::Path *nodepath);
293 void sp_nodepath_select_all (Inkscape::NodePath::Path *nodepath, bool invert);
294 void sp_nodepath_select_all_from_subpath(Inkscape::NodePath::Path *nodepath, bool invert);
295 void sp_nodepath_select_next (Inkscape::NodePath::Path *nodepath);
296 void sp_nodepath_select_prev (Inkscape::NodePath::Path *nodepath);
297 void sp_nodepath_select_rect (Inkscape::NodePath::Path * nodepath, Geom::Rect const &b, gboolean incremental);
298 GList *save_nodepath_selection (Inkscape::NodePath::Path *nodepath);
299 void restore_nodepath_selection (Inkscape::NodePath::Path *nodepath, GList *r);
300 gboolean nodepath_repr_d_changed (Inkscape::NodePath::Path * np, const char *newd);
301 gboolean nodepath_repr_typestr_changed (Inkscape::NodePath::Path * np, const char *newtypestr);
302 gboolean node_key (GdkEvent * event);
303 void sp_nodepath_update_repr(Inkscape::NodePath::Path *np, const gchar *annotation);
304 void sp_nodepath_update_statusbar (Inkscape::NodePath::Path *nodepath);
305 void sp_nodepath_selected_align(Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis);
306 void sp_nodepath_selected_distribute(Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis);
307 void sp_nodepath_select_segment_near_point(Inkscape::NodePath::Path *nodepath, Geom::Point p, bool toggle);
308 void sp_nodepath_add_node_near_point(Inkscape::NodePath::Path *nodepath, Geom::Point p);
309 void sp_nodepath_curve_drag(Inkscape::NodePath::Path *nodepath, int node, double t, Geom::Point delta);
310 Inkscape::NodePath::Node * sp_nodepath_get_node_by_index(Inkscape::NodePath::Path *np, int index);
311 bool sp_node_side_is_line (Inkscape::NodePath::Node *node, Inkscape::NodePath::NodeSide *side);
313 /* possibly private functions */
315 void sp_node_selected_add_node (Inkscape::NodePath::Path *nodepath);
316 void sp_node_selected_break (Inkscape::NodePath::Path *nodepath);
317 void sp_node_selected_duplicate (Inkscape::NodePath::Path *nodepath);
318 void sp_node_selected_join (Inkscape::NodePath::Path *nodepath);
319 void sp_node_selected_join_segment (Inkscape::NodePath::Path *nodepath);
320 void sp_node_delete_preserve (GList *nodes_to_delete);
321 void sp_node_selected_delete (Inkscape::NodePath::Path *nodepath);
322 void sp_node_selected_delete_segment (Inkscape::NodePath::Path *nodepath);
323 void sp_node_selected_set_type (Inkscape::NodePath::Path *nodepath, Inkscape::NodePath::NodeType type);
324 void sp_node_selected_set_line_type (Inkscape::NodePath::Path *nodepath, NRPathcode code);
325 void sp_node_selected_move (Inkscape::NodePath::Path *nodepath, gdouble dx, gdouble dy);
326 void sp_node_selected_move_screen (SPDesktop *desktop, Inkscape::NodePath::Path *nodepath, gdouble dx, gdouble dy);
327 void sp_node_selected_move_absolute (Inkscape::NodePath::Path *nodepath, Geom::Coord val, Geom::Dim2 axis);
328 Geom::Rect sp_node_selected_bbox (Inkscape::NodePath::Path *nodepath);
329 boost::optional<Geom::Coord> sp_node_selected_common_coord (Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis);
331 void sp_nodepath_show_handles(Inkscape::NodePath::Path *nodepath, bool show);
332 SPCanvasItem *sp_nodepath_generate_helperpath(SPDesktop *desktop, SPCurve *curve, const Geom::Matrix & i2d, guint32 color);
333 SPCanvasItem *sp_nodepath_generate_helperpath(SPDesktop *desktop, SPItem *item);
334 void sp_nodepath_show_helperpath(Inkscape::NodePath::Path *nodepath, bool show);
335 void sp_nodepath_update_helperpaths(Inkscape::NodePath::Path *np);
336 void sp_nodepath_make_straight_path(Inkscape::NodePath::Path *np);
338 void sp_nodepath_selected_nodes_rotate (Inkscape::NodePath::Path * nodepath, gdouble angle, int which, bool screen);
340 void sp_nodepath_selected_nodes_scale (Inkscape::NodePath::Path * nodepath, gdouble grow, int which);
341 void sp_nodepath_selected_nodes_scale_screen (Inkscape::NodePath::Path * nodepath, gdouble grow, int which);
343 void sp_nodepath_flip (Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis, boost::optional<Geom::Point> center);
345 #endif