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