Code

- Created a SPLPEItem class that handles applying a LPE to an Item
[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 void box3d_side_set_shape (SPShape *shape);
38 static void box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]);
40 static SPShapeClass *parent_class;
42 GType
43 box3d_side_get_type (void)
44 {
45     static GType type = 0;
47     if (!type) {
48         GTypeInfo info = {
49             sizeof (Box3DSideClass),
50             NULL, NULL,
51             (GClassInitFunc) box3d_side_class_init,
52             NULL, NULL,
53             sizeof (Box3DSide),
54             16,
55             (GInstanceInitFunc) box3d_side_init,
56             NULL,       /* value_table */
57         };
58         type = g_type_register_static (SP_TYPE_SHAPE, "Box3DSide", &info, (GTypeFlags)0);
59     }
60     return type;
61 }
63 static void
64 box3d_side_class_init (Box3DSideClass *klass)
65 {
66     GObjectClass * gobject_class;
67     SPObjectClass * sp_object_class;
68     SPItemClass * item_class;
69     SPPathClass * path_class;
70     SPShapeClass * shape_class;
72     gobject_class = (GObjectClass *) klass;
73     sp_object_class = (SPObjectClass *) klass;
74     item_class = (SPItemClass *) klass;
75     path_class = (SPPathClass *) klass;
76     shape_class = (SPShapeClass *) klass;
78     parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
80     sp_object_class->build = box3d_side_build;
81     sp_object_class->write = box3d_side_write;
82     sp_object_class->set = box3d_side_set;
83     sp_object_class->update = box3d_side_update;
85     shape_class->set_shape = box3d_side_set_shape;
86 }
88 static void
89 box3d_side_init (Box3DSide * side)
90 {
91     side->dir1 = Box3D::NONE;
92     side->dir2 = Box3D::NONE;
93     side->front_or_rear = Box3D::FRONT;
94 }
96 static void
97 box3d_side_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
98 {
99     if (((SPObjectClass *) parent_class)->build)
100         ((SPObjectClass *) parent_class)->build (object, document, repr);
102     sp_object_read_attr(object, "inkscape:box3dsidetype");
105 static Inkscape::XML::Node *
106 box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
108     Box3DSide *side = SP_BOX3D_SIDE (object);
110     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
111         // this is where we end up when saving as plain SVG (also in other circumstances?)
112         // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
113         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
114         repr = xml_doc->createElement("svg:path");
115     }
117     if (flags & SP_OBJECT_WRITE_EXT) {
118         sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
119     }
121     sp_shape_set_shape ((SPShape *) object);
123     /* Duplicate the path */
124     SPCurve *curve = ((SPShape *) object)->curve;
125     //Nulls might be possible if this called iteratively
126     if ( !curve ) {
127         return NULL;
128     }
129     NArtBpath *bpath = SP_CURVE_BPATH(curve);
130     if ( !bpath ) {
131         return NULL;
132     }
133     char *d = sp_svg_write_path ( bpath );
134     repr->setAttribute("d", d);
135     g_free (d);
137     if (((SPObjectClass *) (parent_class))->write)
138         ((SPObjectClass *) (parent_class))->write (object, repr, flags);
140     return repr;
143 static void
144 box3d_side_set (SPObject *object, unsigned int key, const gchar *value)
146     Box3DSide *side = SP_BOX3D_SIDE (object);
148     // TODO: In case the box was recreated (by undo, e.g.) we need to recreate the path
149     //       (along with other info?) from the parent box.
151     /* fixme: we should really collect updates */
152     switch (key) {
153         case SP_ATTR_INKSCAPE_BOX3D_SIDE_TYPE:
154             if (value) {
155                 guint desc = atoi (value);
157                 if (!Box3D::is_face_id(desc)) {
158                     g_print ("desc is not a face id: =%s=\n", value);
159                 }
160                 g_return_if_fail (Box3D::is_face_id (desc));
161                 Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
162                 plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
163                 side->dir1 = Box3D::extract_first_axis_direction(plane);
164                 side->dir2 = Box3D::extract_second_axis_direction(plane);
165                 side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
167                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
168             }
169             break;
170     default:
171         if (((SPObjectClass *) parent_class)->set)
172             ((SPObjectClass *) parent_class)->set (object, key, value);
173         break;
174     }
177 static void
178 box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
180     if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
181         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
182     }
184     if (flags & (SP_OBJECT_MODIFIED_FLAG |
185                  SP_OBJECT_STYLE_MODIFIED_FLAG |
186                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
187         sp_shape_set_shape ((SPShape *) object);
188     }
190     if (((SPObjectClass *) parent_class)->update)
191         ((SPObjectClass *) parent_class)->update (object, ctx, flags);
194 void
195 box3d_side_position_set (Box3DSide *side) {
196     box3d_side_set_shape (SP_SHAPE (side));
198     /* This call is responsible for live update of the sides during the initial drag */
199     SP_OBJECT(side)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 void
203 box3d_side_set_shape (SPShape *shape)
205     Box3DSide *side = SP_BOX3D_SIDE (shape);
206     if (!SP_OBJECT_DOCUMENT(side)->root) {
207         // avoid a warning caused by sp_document_height() (which is called from sp_item_i2d_affine() below)
208         // when reading a file containing 3D boxes
209         return;
210     }
212     SPObject *parent = SP_OBJECT(side)->parent;
213     if (!SP_IS_BOX3D(parent)) {
214         g_warning ("Parent of 3D box side is not a 3D box.\n");
215         return;
216     }
217     SPBox3D *box = SP_BOX3D(parent);
219     Persp3D *persp = box3d_side_perspective(side);
220     if (!persp) {
221         return;
222     }
224     SPCurve *c = sp_curve_new ();
225     // TODO: Draw the correct quadrangle here
226     //       To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
227     //       compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
228     //       resulting path.
230     unsigned int corners[4];
231     box3d_side_compute_corner_ids(side, corners);
233     sp_curve_moveto (c, box3d_get_corner_screen(box, corners[0]));
234     sp_curve_lineto (c, box3d_get_corner_screen(box, corners[1]));
235     sp_curve_lineto (c, box3d_get_corner_screen(box, corners[2]));
236     sp_curve_lineto (c, box3d_get_corner_screen(box, corners[3]));
238     sp_curve_closepath (c);
239     sp_lpe_item_perform_path_effect(SP_LPE_ITEM (side), c);
240     sp_shape_set_curve_insync (SP_SHAPE (side), c, TRUE);
241     sp_curve_unref (c);
244 void
245 box3d_side_apply_style (Box3DSide *side) {
246     Inkscape::XML::Node *repr_face = SP_OBJECT_REPR(SP_OBJECT(side));
248     gchar *descr = g_strconcat ("desktop.", box3d_side_axes_string (side), NULL);
249     const gchar * cur_style = prefs_get_string_attribute(descr, "style");
250     g_free (descr);    
251     
252     SPDesktop *desktop = inkscape_active_desktop();
253     bool use_current = prefs_get_int_attribute("tools.shapes.3dbox", "usecurrent", 0);
254     if (use_current && cur_style !=NULL) {
255         /* use last used style */
256         repr_face->setAttribute("style", cur_style);
257     } else {
258         /* use default style */
259         GString *pstring = g_string_new("");
260         g_string_printf (pstring, "tools.shapes.3dbox.%s", box3d_side_axes_string(side));
261         sp_desktop_apply_style_tool (desktop, repr_face, pstring->str, false);
262     }
265 gchar *
266 box3d_side_axes_string(Box3DSide *side)
268     GString *pstring = g_string_new("");
269     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (side->dir1 ^ side->dir2)));
270     switch ((Box3D::Axis) (side->dir1 ^ side->dir2)) {
271         case Box3D::XY:
272             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "front" : "rear");
273             break;
274         case Box3D::XZ:
275             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "top" : "bottom");
276             break;
277         case Box3D::YZ:
278             g_string_append_printf (pstring, (side->front_or_rear == Box3D::FRONT) ? "right" : "left");
279             break;
280         default:
281             break;
282     }
283     return pstring->str;
286 static void
287 box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]) {
288     Box3D::Axis orth = Box3D::third_axis_direction (side->dir1, side->dir2);
290     corners[0] = (side->front_or_rear ? orth : 0);
291     corners[1] = corners[0] ^ side->dir1;
292     corners[2] = corners[0] ^ side->dir1 ^ side->dir2;
293     corners[3] = corners[0] ^ side->dir2;
296 Persp3D *
297 box3d_side_perspective(Box3DSide *side) {
298     return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
301 Inkscape::XML::Node *
302 box3d_side_convert_to_path(Box3DSide *side) {
303     // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
304     SPDocument *doc = SP_OBJECT_DOCUMENT(side);
305     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
307     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
308     repr->setAttribute("d", SP_OBJECT_REPR(side)->attribute("d"));
309     repr->setAttribute("style", SP_OBJECT_REPR(side)->attribute("style"));
311     return repr;
314 /*
315   Local Variables:
316   mode:c++
317   c-file-style:"stroustrup"
318   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
319   indent-tabs-mode:nil
320   fill-column:99
321   End:
322 */
323 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :