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