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