Code

Convert 3D boxes to ordinary groups before tweaking, ungrouping or applying 'convert...
[inkscape.git] / src / box3d-side.cpp
1 #define __BOX3D_SIDE_C__
3 /*
4  * 3D box face implementation
5  *
6  * Authors:
7  *   Maximilian Albert <Anhalter42@gmx.de>
8  *
9  * Copyright (C) 2007  Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "box3d-side.h"
15 #include "document.h"
16 #include "xml/document.h"
17 #include "xml/repr.h"
18 #include "display/curve.h"
19 #include "svg/svg.h"
20 #include "attributes.h"
21 #include "inkscape.h"
22 #include "persp3d.h"
23 #include "box3d-context.h"
24 #include "prefs-utils.h"
25 #include "desktop-style.h"
26 #include "box3d.h"
28 static void box3d_side_class_init (Box3DSideClass *klass);
29 static void box3d_side_init (Box3DSide *side);
31 static void box3d_side_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
32 static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
33 static void box3d_side_set (SPObject *object, unsigned int key, const gchar *value);
34 static void box3d_side_update (SPObject *object, SPCtx *ctx, guint flags);
36 //static gchar * box3d_side_description (SPItem * item);
37 //static void box3d_side_snappoints(SPItem const *item, SnapPointsIter p);
39 //static void box3d_side_set_shape (SPShape *shape);
40 //static void box3d_side_update_patheffect (SPShape *shape, bool write);
42 static Proj::Pt3 box3d_side_corner (Box3DSide *side, guint index);
43 static std::vector<Proj::Pt3> box3d_side_corners (Box3DSide *side);
44 // static gint box3d_side_descr_to_id (gchar const *descr);
46 static SPShapeClass *parent_class;
48 GType
49 box3d_side_get_type (void)
50 {
51     static GType type = 0;
53     if (!type) {
54         GTypeInfo info = {
55             sizeof (Box3DSideClass),
56             NULL, NULL,
57             (GClassInitFunc) box3d_side_class_init,
58             NULL, NULL,
59             sizeof (Box3DSide),
60             16,
61             (GInstanceInitFunc) box3d_side_init,
62             NULL,       /* value_table */
63         };
64         type = g_type_register_static (SP_TYPE_SHAPE, "Box3DSide", &info, (GTypeFlags)0);
65     }
66     return type;
67 }
69 static void
70 box3d_side_class_init (Box3DSideClass *klass)
71 {
72     GObjectClass * gobject_class;
73     SPObjectClass * sp_object_class;
74     SPItemClass * item_class;
75     SPPathClass * path_class;
76     SPShapeClass * shape_class;
78     gobject_class = (GObjectClass *) klass;
79     sp_object_class = (SPObjectClass *) klass;
80     item_class = (SPItemClass *) klass;
81     path_class = (SPPathClass *) klass;
82     shape_class = (SPShapeClass *) klass;
84     parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
86     sp_object_class->build = box3d_side_build;
87     sp_object_class->write = box3d_side_write;
88     sp_object_class->set = box3d_side_set;
89     sp_object_class->update = box3d_side_update;
91     //item_class->description = box3d_side_description;
92     //item_class->snappoints = box3d_side_snappoints;
94     shape_class->set_shape = box3d_side_set_shape;
95     //shape_class->update_patheffect = box3d_side_update_patheffect;
96 }
98 static void
99 box3d_side_init (Box3DSide * side)
101     side->dir1 = Box3D::NONE;
102     side->dir2 = Box3D::NONE;
103     side->front_or_rear = Box3D::FRONT;
106 static void
107 box3d_side_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
109     if (((SPObjectClass *) parent_class)->build)
110         ((SPObjectClass *) parent_class)->build (object, document, repr);
112     sp_object_read_attr(object, "inkscape:box3dsidetype");
115 static Inkscape::XML::Node *
116 box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
118     Box3DSide *side = SP_BOX3D_SIDE (object);
120     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
121         // this is where we end up when saving as plain SVG (also in other circumstances?)
122         // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
123         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
124         repr = xml_doc->createElement("svg:path");
125     }
127     if (flags & SP_OBJECT_WRITE_EXT) {
128         sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
129     }
131     sp_shape_set_shape ((SPShape *) object);
133     /* Duplicate the path */
134     SPCurve *curve = ((SPShape *) object)->curve;
135     //Nulls might be possible if this called iteratively
136     if ( !curve ) {
137         return NULL;
138     }
139     NArtBpath *bpath = SP_CURVE_BPATH(curve);
140     if ( !bpath ) {
141         return NULL;
142     }
143     char *d = sp_svg_write_path ( bpath );
144     repr->setAttribute("d", d);
145     g_free (d);
147     if (((SPObjectClass *) (parent_class))->write)
148         ((SPObjectClass *) (parent_class))->write (object, repr, flags);
150     return repr;
153 static void
154 box3d_side_set (SPObject *object, unsigned int key, const gchar *value)
156     Box3DSide *side = SP_BOX3D_SIDE (object);
158     // TODO: In case the box was recreated (by undo, e.g.) we need to recreate the path
159     //       (along with other info?) from the parent box.
161     /* fixme: we should really collect updates */
162     switch (key) {
163         case SP_ATTR_INKSCAPE_BOX3D_SIDE_TYPE:
164             if (value) {
165                 guint desc = atoi (value);
167                 if (!Box3D::is_face_id(desc)) {
168                     g_print ("desc is not a face id: =%s=\n", value);
169                 }
170                 g_return_if_fail (Box3D::is_face_id (desc));
171                 Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
172                 plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
173                 side->dir1 = Box3D::extract_first_axis_direction(plane);
174                 side->dir2 = Box3D::extract_second_axis_direction(plane);
175                 side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
177                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
178             }
179             break;
180     default:
181         if (((SPObjectClass *) parent_class)->set)
182             ((SPObjectClass *) parent_class)->set (object, key, value);
183         break;
184     }
187 static void
188 box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
190     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
191         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
192     }
194     //g_print ("box3d_side_update\n");
195     if (flags & (SP_OBJECT_MODIFIED_FLAG |
196                  SP_OBJECT_STYLE_MODIFIED_FLAG |
197                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
198         sp_shape_set_shape ((SPShape *) object);
199     }
201     if (((SPObjectClass *) parent_class)->update)
202         ((SPObjectClass *) parent_class)->update (object, ctx, flags);
205 /***
206 static void
207 box3d_side_update_patheffect(SPShape *shape, bool write)
209     box3d_side_set_shape(shape);
211     if (write) {
212         Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
213         if ( shape->curve != NULL ) {
214             NArtBpath *abp = sp_curve_first_bpath(shape->curve);
215             if (abp) {
216                 gchar *str = sp_svg_write_path(abp);
217                 repr->setAttribute("d", str);
218                 g_free(str);
219             } else {
220                 repr->setAttribute("d", "");
221             }
222         } else {
223             repr->setAttribute("d", NULL);
224         }
225     }
227     ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
229 ***/
231 /***
232 static gchar *
233 box3d_side_description (SPItem *item)
235     Box3DSide *side = SP_BOX3D_SIDE (item);
237     // while there will never be less than 3 vertices, we still need to
238     // make calls to ngettext because the pluralization may be different
239     // for various numbers >=3.  The singular form is used as the index.
240     if (side->flatsided == false )
241         return g_strdup_printf (ngettext("<b>Star</b> with %d vertex",
242                                          "<b>Star</b> with %d vertices",
243                                          star->sides), star->sides);
244     else
245         return g_strdup_printf (ngettext("<b>Polygon</b> with %d vertex",
246                                          "<b>Polygon</b> with %d vertices",
247                                          star->sides), star->sides);
249 ***/
251 void
252 box3d_side_position_set (Box3DSide *side) {
253     box3d_side_set_shape (SP_SHAPE (side));
255     /* This call is responsible for live update of the sides during the initial drag */
256     SP_OBJECT(side)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
259 void
260 box3d_side_set_shape (SPShape *shape)
262     //g_print ("box3d_side_set_shape\n");
263     Box3DSide *side = SP_BOX3D_SIDE (shape);
264     if (!SP_OBJECT_DOCUMENT(side)->root) {
265         // avoid a warning caused by sp_document_height() (which is called from sp_item_i2d_affine() below)
266         // when reading a file containing 3D boxes
267         return;
268     }
270     if (!SP_IS_BOX3D(SP_OBJECT(side)->parent)) {
271         g_warning ("Parent of 3D box side is not a 3D box.\n");
272         /**
273         g_print ("Removing the inkscape:box3dside attribute and returning from box3d_side_set_shape().\n");
274         SP_OBJECT_REPR (shape)->setAttribute("sodipodi:type", NULL);
275         SP_OBJECT_REPR (shape)->setAttribute("inkscape:box3dside", NULL);
276         **/
277         return;
278     }
280     Persp3D *persp = box3d_side_perspective(side);
281     //g_return_if_fail (persp != NULL);
282     if (!persp) {
283         //g_warning ("persp != NULL in box3d_side_set_shape failed!\n");
284         //persp = SP_OBJECT_DOCUMENT(side)->current_persp3d;
285         return;
286     }
288     SPCurve *c = sp_curve_new ();
289     // TODO: Draw the correct quadrangle here
290     //       To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
291     //       compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
292     //       resulting path.
294     std::vector<Proj::Pt3> corners = box3d_side_corners (side);
296     NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(shape)));
298     // FIXME: This can better be implemented by using box3d_get_corner
299     sp_curve_moveto (c, persp->tmat.image(corners[0]).affine() * i2d);
300     sp_curve_lineto (c, persp->tmat.image(corners[1]).affine() * i2d);
301     sp_curve_lineto (c, persp->tmat.image(corners[2]).affine() * i2d);
302     sp_curve_lineto (c, persp->tmat.image(corners[3]).affine() * i2d);
304     sp_curve_closepath (c);
305     //sp_shape_perform_path_effect(c, SP_SHAPE (side));
306     sp_shape_set_curve_insync (SP_SHAPE (side), c, TRUE);
307     sp_curve_unref (c);
310 void
311 box3d_side_apply_style (Box3DSide *side) {
312     Inkscape::XML::Node *repr_face = SP_OBJECT_REPR(SP_OBJECT(side));
314     gchar *descr = g_strconcat ("desktop.", box3d_side_axes_string (side), NULL);
315     const gchar * cur_style = prefs_get_string_attribute(descr, "style");
316     g_free (descr);    
317     
318     SPDesktop *desktop = inkscape_active_desktop();
319     bool use_current = prefs_get_int_attribute("tools.shapes.3dbox", "usecurrent", 0);
320     if (use_current && cur_style !=NULL) {
321         /* use last used style */
322         repr_face->setAttribute("style", cur_style);
323     } else {
324         /* use default style */
325         GString *pstring = g_string_new("");
326         g_string_printf (pstring, "tools.shapes.3dbox.%s", box3d_side_axes_string(side));
327         sp_desktop_apply_style_tool (desktop, repr_face, pstring->str, false);
328     }
331 gchar *
332 box3d_side_axes_string(Box3DSide *side)
334     GString *pstring = g_string_new("");
335     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (side->dir1 ^ side->dir2)));
336     switch ((Box3D::Axis) (side->dir1 ^ side->dir2)) {
337         case Box3D::XY:
338             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "front" : "rear");
339             break;
340         case Box3D::XZ:
341             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "top" : "bottom");
342             break;
343         case Box3D::YZ:
344             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "right" : "left");
345             break;
346         default:
347             break;
348     }
349     return pstring->str;
352 static Proj::Pt3
353 box3d_side_corner (Box3DSide *side, guint index) {
354     SPBox3D *box = SP_BOX3D(SP_OBJECT_PARENT(side));
355     return Proj::Pt3 ((index & 0x1) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
356                       (index & 0x2) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
357                       (index & 0x4) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
358                       1.0);
361 static std::vector<Proj::Pt3>
362 box3d_side_corners (Box3DSide *side) {
363     std::vector<Proj::Pt3> corners;
364     Box3D::Axis orth = Box3D::third_axis_direction (side->dir1, side->dir2);
365     unsigned int i0 = (side->front_or_rear ? orth : 0);
366     unsigned int i1 = i0 ^ side->dir1;
367     unsigned int i2 = i0 ^ side->dir1 ^ side->dir2;
368     unsigned int i3 = i0 ^ side->dir2;
370     corners.push_back (box3d_side_corner (side, i0));
371     corners.push_back (box3d_side_corner (side, i1));
372     corners.push_back (box3d_side_corner (side, i2));
373     corners.push_back (box3d_side_corner (side, i3));
374     return corners;
377 /*
378 static gint
379 box3d_side_descr_to_id (gchar const *descr)
381     if (!strcmp (descr, "XYrear")) { return 5; }
382     if (!strcmp (descr, "XYfront")) { return 2; }
383     if (!strcmp (descr, "XZbottom")) { return 1; }
384     if (!strcmp (descr, "XZtop")) { return 4; }
385     if (!strcmp (descr, "YZleft")) { return 3; }
386     if (!strcmp (descr, "YZright")) { return 0; }
388     g_warning ("Invalid description of 3D box face.\n");
389     g_print ("         (description is: %s)\n", descr);
390     return -1;
392 */
394 Persp3D *
395 box3d_side_perspective(Box3DSide *side) {
396     return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
399 Inkscape::XML::Node *
400 box3d_side_convert_to_path(Box3DSide *side) {
401     // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
402     SPDocument *doc = SP_OBJECT_DOCUMENT(side);
403     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
405     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
406     repr->setAttribute("d", SP_OBJECT_REPR(side)->attribute("d"));
407     repr->setAttribute("style", SP_OBJECT_REPR(side)->attribute("style"));
409     return repr;
412 /*
413   Local Variables:
414   mode:c++
415   c-file-style:"stroustrup"
416   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
417   indent-tabs-mode:nil
418   fill-column:99
419   End:
420 */
421 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :