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