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 "livarot/Path.h"
20 #include <glibmm/ustring.h>
21 #include <gdk/gdkevents.h>
22 #include <list>
24 class SPObject;
25 class SPDesktop;
26 class SPPath;
27 class SPKnot;
28 class LivePathEffectObject;
30 namespace Inkscape {
31 namespace XML {
32 class Node;
33 }
34 }
37 /**
38 * Radial objects are represented by an angle and a distance from
39 * 0,0. 0,0 is represented by a == big_num.
40 */
41 class Radial{
42 public:
43 /** Radius */
44 double r;
45 /** Amplitude */
46 double a;
47 Radial() {}
48 // Radial(NR::Point const &p); // Convert a point to radial coordinates
49 Radial(Radial &p) : r(p.r),a(p.a) {}
50 // operator NR::Point() const;
52 /**
53 * Construct Radial from NR::Point.
54 */
55 Radial(NR::Point const &p)
56 {
57 r = NR::L2(p);
58 if (r > 0) {
59 a = NR::atan2 (p);
60 } else {
61 a = HUGE_VAL; //undefined
62 }
63 }
65 /**
66 * Cast Radial to cartesian NR::Point.
67 */
68 operator NR::Point() const
69 {
70 if (a == HUGE_VAL) {
71 return NR::Point(0,0);
72 } else {
73 return r*NR::Point(cos(a), sin(a));
74 }
75 }
77 };
79 class ShapeEditor;
81 namespace Inkscape {
82 namespace NodePath {
84 /**
85 * The entire nodepath, containing multiple subpaths
86 */
87 class Path;
89 /**
90 * A subpath is a continuous chain of linked nodes
91 */
92 class SubPath;
94 /**
95 * One side of a node, i.e. prev or next
96 */
97 class NodeSide;
99 /**
100 * A node on a subpath
101 */
102 class Node;
105 /**
106 * This is the lowest list item, a simple list of nodes.
107 */
108 class SubPath {
109 public:
110 /** The parent of this subpath */
111 Path * nodepath;
112 /** Is this path closed (no endpoints) or not?*/
113 gboolean closed;
114 /** The nodes in this subpath. */
115 GList * nodes;
116 /** The first node of the subpath (does not imply open/closed)*/
117 Node * first;
118 /** The last node of the subpath */
119 Node * last;
120 };
124 /**
125 * What kind of node is this? This is the value for the node->type
126 * field. NodeType indicates the degree of continuity required for
127 * the node. I think that the corresponding integer indicates which
128 * derivate is connected. (Thus 2 means that the node is continuous
129 * to the second derivative, i.e. has matching endpoints and tangents)
130 */
131 typedef enum {
132 /** A normal node */
133 NODE_NONE,
134 /** This node non-continuously joins two segments.*/
135 NODE_CUSP,
136 /** This node continuously joins two segments. */
137 NODE_SMOOTH,
138 /** This node is symmetric. */
139 NODE_SYMM
140 } NodeType;
144 /**
145 * A NodeSide is a datarecord which may be on either side (n or p) of a node,
146 * which describes the segment going to the next node.
147 */
148 class NodeSide{
149 public:
150 /** Pointer to the next node, */
151 Node * other;
152 /** Position */
153 NR::Point pos;
154 /** Origin (while dragging) in radial notation */
155 Radial origin_radial;
156 /** Origin (while dragging) in x/y notation */
157 NR::Point origin;
158 /** Knots are Inkscape's way of providing draggable points. This
159 * Knot is the point on the curve representing the control point in a
160 * bezier curve.*/
161 SPKnot * knot;
162 /** What kind of rendering? */
163 SPCanvasItem * line;
164 };
166 /**
167 * A node along a NodePath
168 */
169 class Node {
170 public:
171 /** The parent subpath of this node */
172 SubPath * subpath;
173 /** Type is selected from NodeType.*/
174 guint type : 4;
175 /** Code refers to which ArtCode is used to represent the segment
176 * (which segment?).*/
177 guint code : 4;
178 /** Boolean. Am I currently selected or not? */
179 guint selected : 1;
180 /** */
181 NR::Point pos;
182 /** */
183 NR::Point origin;
184 /** Knots are Inkscape's way of providing draggable points. This
185 * Knot is the point on the curve representing the endpoint.*/
186 SPKnot * knot;
187 /** The NodeSide in the 'next' direction */
188 NodeSide n;
189 /** The NodeSide in the 'previous' direction */
190 NodeSide p;
192 /** The pointer to the nodeside which we are dragging out with Shift */
193 NodeSide *dragging_out;
195 /** Boolean. Am I being dragged? */
196 guint is_dragging : 1;
197 };
199 /**
200 * This is a collection of subpaths which contain nodes
201 *
202 * In the following data model. Nodepaths are made up of subpaths which
203 * are comprised of nodes.
204 *
205 * Nodes are linked thus:
206 * \verbatim
207 n other
208 node -----> nodeside ------> node \endverbatim
209 */
210 class Path {
211 public:
212 /** Pointer to the current desktop, for reporting purposes */
213 SPDesktop * desktop;
214 /** The parent path of this nodepath */
215 SPObject * object;
216 /** The parent livepatheffect of this nodepath, if applicable */
217 SPItem * item;
218 /** The context which created this nodepath. Important if this nodepath is deleted */
219 ShapeEditor *shape_editor;
220 /** The subpaths which comprise this NodePath */
221 GList * subpaths;
222 /** A list of nodes which are currently selected */
223 GList * selected;
224 /** Transforms (userspace <---> virtual space? someone please describe )
225 njh: I'd be guessing that these are item <-> desktop transforms.*/
226 NR::Matrix i2d, d2i;
227 /** The DOM node which describes this NodePath */
228 Inkscape::XML::Node *repr;
229 gchar *repr_key;
230 gchar *repr_nodetypes_key;
231 //STL compliant method to get the selected nodes
232 void selection(std::list<Node *> &l);
234 guint numSelected() {return (selected? g_list_length(selected) : 0);}
235 NR::Point& singleSelectedCoords() {return (((Node *) selected->data)->pos);}
237 /// livarot library is used for "point on path" and "nearest position on path", so we need to maintain its path representation as well
238 ::Path *livarot_path;
240 /// draw a "sketch" of the path by using these variables
241 SPCanvasItem *helper_path;
242 SPCurve *curve;
243 bool show_helperpath;
244 guint32 helperpath_rgba;
245 gdouble helperpath_width;
247 /// true if we changed repr, to tell this change from an external one such as from undo, simplify, or another desktop
248 unsigned int local_change;
250 /// true if we're showing selected nodes' handles
251 bool show_handles;
253 /// true if the path cannot contain curves, just straight lines
254 bool straight_path;
256 /// active_node points to the node that is currently mouseovered (= NULL if
257 /// there isn't any); we also consider the node mouseovered if it is covered
258 /// by one of its handles and the latter is mouseovered
259 static Node *active_node;
260 };
262 } // namespace NodePath
263 } // namespace Inkscape
265 enum {
266 SCULPT_PROFILE_LINEAR,
267 SCULPT_PROFILE_BELL,
268 SCULPT_PROFILE_ELLIPTIC
269 };
271 // Do function documentation in nodepath.cpp
272 Inkscape::NodePath::Path * sp_nodepath_new (SPDesktop * desktop, SPObject *object, bool show_handles, const char * repr_key = NULL, SPItem *item = NULL);
273 void sp_nodepath_destroy (Inkscape::NodePath::Path * nodepath);
274 void sp_nodepath_ensure_livarot_path(Inkscape::NodePath::Path *np);
275 void sp_nodepath_deselect (Inkscape::NodePath::Path *nodepath);
276 void sp_nodepath_select_all (Inkscape::NodePath::Path *nodepath, bool invert);
277 void sp_nodepath_select_all_from_subpath(Inkscape::NodePath::Path *nodepath, bool invert);
278 void sp_nodepath_select_next (Inkscape::NodePath::Path *nodepath);
279 void sp_nodepath_select_prev (Inkscape::NodePath::Path *nodepath);
280 void sp_nodepath_select_rect (Inkscape::NodePath::Path * nodepath, NR::Rect const &b, gboolean incremental);
281 GList *save_nodepath_selection (Inkscape::NodePath::Path *nodepath);
282 void restore_nodepath_selection (Inkscape::NodePath::Path *nodepath, GList *r);
283 gboolean nodepath_repr_d_changed (Inkscape::NodePath::Path * np, const char *newd);
284 gboolean nodepath_repr_typestr_changed (Inkscape::NodePath::Path * np, const char *newtypestr);
285 gboolean node_key (GdkEvent * event);
286 void sp_nodepath_update_repr(Inkscape::NodePath::Path *np, const gchar *annotation);
287 void sp_nodepath_update_statusbar (Inkscape::NodePath::Path *nodepath);
288 void sp_nodepath_selected_align(Inkscape::NodePath::Path *nodepath, NR::Dim2 axis);
289 void sp_nodepath_selected_distribute(Inkscape::NodePath::Path *nodepath, NR::Dim2 axis);
290 void sp_nodepath_select_segment_near_point(Inkscape::NodePath::Path *nodepath, NR::Point p, bool toggle);
291 void sp_nodepath_add_node_near_point(Inkscape::NodePath::Path *nodepath, NR::Point p);
292 void sp_nodepath_curve_drag(int node, double t, NR::Point delta);
293 Inkscape::NodePath::Node * sp_nodepath_get_node_by_index(int index);
294 /* possibly private functions */
296 void sp_node_selected_add_node (Inkscape::NodePath::Path *nodepath);
297 void sp_node_selected_break (Inkscape::NodePath::Path *nodepath);
298 void sp_node_selected_duplicate (Inkscape::NodePath::Path *nodepath);
299 void sp_node_selected_join (Inkscape::NodePath::Path *nodepath);
300 void sp_node_selected_join_segment (Inkscape::NodePath::Path *nodepath);
301 void sp_node_delete_preserve (GList *nodes_to_delete);
302 void sp_node_selected_delete (Inkscape::NodePath::Path *nodepath);
303 void sp_node_selected_delete_segment (Inkscape::NodePath::Path *nodepath);
304 void sp_node_selected_set_type (Inkscape::NodePath::Path *nodepath, Inkscape::NodePath::NodeType type);
305 void sp_node_selected_set_line_type (Inkscape::NodePath::Path *nodepath, NRPathcode code);
306 void sp_node_selected_move (Inkscape::NodePath::Path *nodepath, gdouble dx, gdouble dy);
307 void sp_node_selected_move_screen (Inkscape::NodePath::Path *nodepath, gdouble dx, gdouble dy);
308 void sp_node_selected_move_absolute (Inkscape::NodePath::Path *nodepath, NR::Coord val, NR::Dim2 axis);
309 NR::Rect sp_node_selected_bbox (Inkscape::NodePath::Path *nodepath);
310 NR::Maybe<NR::Coord> sp_node_selected_common_coord (Inkscape::NodePath::Path *nodepath, NR::Dim2 axis);
312 void sp_nodepath_show_handles(Inkscape::NodePath::Path *nodepath, bool show);
313 SPCanvasItem *sp_nodepath_generate_helperpath(SPDesktop *desktop, SPCurve *curve, const SPItem *item, guint32 color);
314 SPCanvasItem *sp_nodepath_generate_helperpath(SPDesktop *desktop, SPPath *path);
315 void sp_nodepath_show_helperpath(Inkscape::NodePath::Path *nodepath, bool show);
316 void sp_nodepath_make_straight_path(Inkscape::NodePath::Path *np);
318 void sp_nodepath_selected_nodes_rotate (Inkscape::NodePath::Path * nodepath, gdouble angle, int which, bool screen);
320 void sp_nodepath_selected_nodes_scale (Inkscape::NodePath::Path * nodepath, gdouble grow, int which);
321 void sp_nodepath_selected_nodes_scale_screen (Inkscape::NodePath::Path * nodepath, gdouble grow, int which);
323 void sp_nodepath_flip (Inkscape::NodePath::Path *nodepath, NR::Dim2 axis, NR::Maybe<NR::Point> center);
325 #endif