Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / box3d-side.cpp
1 /*
2  * 3D box face implementation
3  *
4  * Authors:
5  *   Maximilian Albert <Anhalter42@gmx.de>
6  *   Abhishek Sharma
7  *
8  * Copyright (C) 2007  Authors
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #include "box3d-side.h"
14 #include "document.h"
15 #include "xml/document.h"
16 #include "xml/repr.h"
17 #include "display/curve.h"
18 #include "svg/svg.h"
19 #include "attributes.h"
20 #include "inkscape.h"
21 #include "persp3d.h"
22 #include "box3d-context.h"
23 #include "preferences.h"
24 #include "desktop-style.h"
25 #include "box3d.h"
27 static void box3d_side_class_init (Box3DSideClass *klass);
28 static void box3d_side_init (Box3DSide *side);
30 static void box3d_side_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
31 static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
32 static void box3d_side_set (SPObject *object, unsigned int key, const gchar *value);
33 static void box3d_side_update (SPObject *object, SPCtx *ctx, guint flags);
35 static void box3d_side_set_shape (SPShape *shape);
37 static void box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]);
39 static SPShapeClass *parent_class;
41 GType
42 box3d_side_get_type (void)
43 {
44     static GType type = 0;
46     if (!type) {
47         GTypeInfo info = {
48             sizeof (Box3DSideClass),
49             NULL, NULL,
50             (GClassInitFunc) box3d_side_class_init,
51             NULL, NULL,
52             sizeof (Box3DSide),
53             16,
54             (GInstanceInitFunc) box3d_side_init,
55             NULL,       /* value_table */
56         };
57         type = g_type_register_static (SP_TYPE_SHAPE, "Box3DSide", &info, (GTypeFlags)0);
58     }
59     return type;
60 }
62 static void
63 box3d_side_class_init (Box3DSideClass *klass)
64 {
65     GObjectClass * gobject_class;
66     SPObjectClass * sp_object_class;
67     SPItemClass * item_class;
68     SPPathClass * path_class;
69     SPShapeClass * shape_class;
71     gobject_class = (GObjectClass *) klass;
72     sp_object_class = (SPObjectClass *) klass;
73     item_class = (SPItemClass *) klass;
74     path_class = (SPPathClass *) klass;
75     shape_class = (SPShapeClass *) klass;
77     parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
79     sp_object_class->build = box3d_side_build;
80     sp_object_class->write = box3d_side_write;
81     sp_object_class->set = box3d_side_set;
82     sp_object_class->update = box3d_side_update;
84     shape_class->set_shape = box3d_side_set_shape;
85 }
87 static void
88 box3d_side_init (Box3DSide * side)
89 {
90     side->dir1 = Box3D::NONE;
91     side->dir2 = Box3D::NONE;
92     side->front_or_rear = Box3D::FRONT;
93 }
95 static void box3d_side_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
96 {
97     if (((SPObjectClass *) parent_class)->build) {
98         ((SPObjectClass *) parent_class)->build(object, document, repr);
99     }
101     object->readAttr( "inkscape:box3dsidetype" );
104 static Inkscape::XML::Node *
105 box3d_side_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
107     Box3DSide *side = SP_BOX3D_SIDE (object);
109     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
110         // this is where we end up when saving as plain SVG (also in other circumstances?)
111         // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
112         repr = xml_doc->createElement("svg:path");
113     }
115     if (flags & SP_OBJECT_WRITE_EXT) {
116         sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
117     }
119     static_cast<SPShape *>(object)->setShape();
121     /* Duplicate the path */
122     SPCurve const *curve = ((SPShape *) object)->curve;
123     //Nulls might be possible if this called iteratively
124     if ( !curve ) {
125         return NULL;
126     }
127     char *d = sp_svg_write_path ( curve->get_pathvector() );
128     repr->setAttribute("d", d);
129     g_free (d);
131     if (((SPObjectClass *) (parent_class))->write)
132         ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
134     return repr;
137 static void
138 box3d_side_set (SPObject *object, unsigned int key, const gchar *value)
140     Box3DSide *side = SP_BOX3D_SIDE (object);
142     // TODO: In case the box was recreated (by undo, e.g.) we need to recreate the path
143     //       (along with other info?) from the parent box.
145     /* fixme: we should really collect updates */
146     switch (key) {
147         case SP_ATTR_INKSCAPE_BOX3D_SIDE_TYPE:
148             if (value) {
149                 guint desc = atoi (value);
151                 if (!Box3D::is_face_id(desc)) {
152                     g_print ("desc is not a face id: =%s=\n", value);
153                 }
154                 g_return_if_fail (Box3D::is_face_id (desc));
155                 Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
156                 plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
157                 side->dir1 = Box3D::extract_first_axis_direction(plane);
158                 side->dir2 = Box3D::extract_second_axis_direction(plane);
159                 side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
161                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
162             }
163             break;
164     default:
165         if (((SPObjectClass *) parent_class)->set)
166             ((SPObjectClass *) parent_class)->set (object, key, value);
167         break;
168     }
171 static void
172 box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
174     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
175         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
176     }
178     if (flags & (SP_OBJECT_MODIFIED_FLAG |
179                  SP_OBJECT_STYLE_MODIFIED_FLAG |
180                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
181         static_cast<SPShape *>(object)->setShape ();
182     }
184     if (((SPObjectClass *) parent_class)->update)
185         ((SPObjectClass *) parent_class)->update (object, ctx, flags);
188 /* Create a new Box3DSide and append it to the parent box */
189 Box3DSide * Box3DSide::createBox3DSide(SPBox3D *box)
191         Box3DSide *box3d_side = 0;
192         Inkscape::XML::Document *xml_doc = box->document->rdoc;
193         Inkscape::XML::Node *repr_side = xml_doc->createElement("svg:path");
194         repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
195         box3d_side = static_cast<Box3DSide *>(box->appendChildRepr(repr_side));
196         return box3d_side;
199 /*
200  * Function which return the type attribute for Box3D. 
201  * Acts as a replacement for directly accessing the XML Tree directly.
202  */
203 long long int Box3DSide::getFaceId()
205             return this->getIntAttribute("inkscape:box3dsidetype", -1);
208 void
209 box3d_side_position_set (Box3DSide *side) {
210     box3d_side_set_shape (SP_SHAPE (side));
212     /* This call is responsible for live update of the sides during the initial drag */
213     SP_OBJECT(side)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
216 void
217 box3d_side_set_shape (SPShape *shape)
219     Box3DSide *side = SP_BOX3D_SIDE (shape);
220     if (!SP_OBJECT_DOCUMENT(side)->root) {
221         // avoid a warning caused by sp_document_height() (which is called from sp_item_i2d_affine() below)
222         // when reading a file containing 3D boxes
223         return;
224     }
226     SPObject *parent = SP_OBJECT(side)->parent;
227     if (!SP_IS_BOX3D(parent)) {
228         g_warning ("Parent of 3D box side is not a 3D box.\n");
229         return;
230     }
231     SPBox3D *box = SP_BOX3D(parent);
233     Persp3D *persp = box3d_side_perspective(side);
234     if (!persp) {
235         return;
236     }
238     // TODO: Draw the correct quadrangle here
239     //       To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
240     //       compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
241     //       resulting path.
243     unsigned int corners[4];
244     box3d_side_compute_corner_ids(side, corners);
246     SPCurve *c = new SPCurve();
248     if (!box3d_get_corner_screen(box, corners[0]).isFinite() ||
249         !box3d_get_corner_screen(box, corners[1]).isFinite() ||
250         !box3d_get_corner_screen(box, corners[2]).isFinite() ||
251         !box3d_get_corner_screen(box, corners[3]).isFinite() )
252     {
253         g_warning ("Trying to draw a 3D box side with invalid coordinates.\n");
254         return;
255     }
257     c->moveto(box3d_get_corner_screen(box, corners[0]));
258     c->lineto(box3d_get_corner_screen(box, corners[1]));
259     c->lineto(box3d_get_corner_screen(box, corners[2]));
260     c->lineto(box3d_get_corner_screen(box, corners[3]));
261     c->closepath();
263     /* Reset the shape'scurve to the "original_curve"
264      * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
265     shape->setCurveInsync( c, TRUE);
266     if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
267         SPCurve *c_lpe = c->copy();
268         bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
269         if (success) {
270             shape->setCurveInsync( c_lpe, TRUE);
271         }
272         c_lpe->unref();
273     }
274     c->unref();
277 gchar *box3d_side_axes_string(Box3DSide *side)
279     GString *pstring = g_string_new("");
280     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (side->dir1 ^ side->dir2)));
281     switch ((Box3D::Axis) (side->dir1 ^ side->dir2)) {
282         case Box3D::XY:
283             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "front" : "rear");
284             break;
285         case Box3D::XZ:
286             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "top" : "bottom");
287             break;
288         case Box3D::YZ:
289             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "right" : "left");
290             break;
291         default:
292             break;
293     }
294     return pstring->str;
297 static void
298 box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]) {
299     Box3D::Axis orth = Box3D::third_axis_direction (side->dir1, side->dir2);
301     corners[0] = (side->front_or_rear ? orth : 0);
302     corners[1] = corners[0] ^ side->dir1;
303     corners[2] = corners[0] ^ side->dir1 ^ side->dir2;
304     corners[3] = corners[0] ^ side->dir2;
307 Persp3D *
308 box3d_side_perspective(Box3DSide *side) {
309     return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
312 Inkscape::XML::Node *box3d_side_convert_to_path(Box3DSide *side) {
313     // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
314     SPDocument *doc = SP_OBJECT_DOCUMENT(side);
315     Inkscape::XML::Document *xml_doc = doc->getReprDoc();
317     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
318     repr->setAttribute("d", side->getAttribute("d"));
319     repr->setAttribute("style", side->getAttribute("style"));
321     return repr;
324 /*
325   Local Variables:
326   mode:c++
327   c-file-style:"stroustrup"
328   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
329   indent-tabs-mode:nil
330   fill-column:99
331   End:
332 */
333 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :