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