Code

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