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 Proj::Pt3 box3d_side_corner (Box3DSide *side, guint index);
39 static std::vector<Proj::Pt3> box3d_side_corners (Box3DSide *side);
41 static SPShapeClass *parent_class;
43 GType
44 box3d_side_get_type (void)
45 {
46 static GType type = 0;
48 if (!type) {
49 GTypeInfo info = {
50 sizeof (Box3DSideClass),
51 NULL, NULL,
52 (GClassInitFunc) box3d_side_class_init,
53 NULL, NULL,
54 sizeof (Box3DSide),
55 16,
56 (GInstanceInitFunc) box3d_side_init,
57 NULL, /* value_table */
58 };
59 type = g_type_register_static (SP_TYPE_SHAPE, "Box3DSide", &info, (GTypeFlags)0);
60 }
61 return type;
62 }
64 static void
65 box3d_side_class_init (Box3DSideClass *klass)
66 {
67 GObjectClass * gobject_class;
68 SPObjectClass * sp_object_class;
69 SPItemClass * item_class;
70 SPPathClass * path_class;
71 SPShapeClass * shape_class;
73 gobject_class = (GObjectClass *) klass;
74 sp_object_class = (SPObjectClass *) klass;
75 item_class = (SPItemClass *) klass;
76 path_class = (SPPathClass *) klass;
77 shape_class = (SPShapeClass *) klass;
79 parent_class = (SPShapeClass *)g_type_class_ref (SP_TYPE_SHAPE);
81 sp_object_class->build = box3d_side_build;
82 sp_object_class->write = box3d_side_write;
83 sp_object_class->set = box3d_side_set;
84 sp_object_class->update = box3d_side_update;
86 shape_class->set_shape = box3d_side_set_shape;
87 }
89 static void
90 box3d_side_init (Box3DSide * side)
91 {
92 side->dir1 = Box3D::NONE;
93 side->dir2 = Box3D::NONE;
94 side->front_or_rear = Box3D::FRONT;
95 }
97 static void
98 box3d_side_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
99 {
100 if (((SPObjectClass *) parent_class)->build)
101 ((SPObjectClass *) parent_class)->build (object, document, repr);
103 sp_object_read_attr(object, "inkscape:box3dsidetype");
104 }
106 static Inkscape::XML::Node *
107 box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
108 {
109 Box3DSide *side = SP_BOX3D_SIDE (object);
111 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
112 // this is where we end up when saving as plain SVG (also in other circumstances?)
113 // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
114 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
115 repr = xml_doc->createElement("svg:path");
116 }
118 if (flags & SP_OBJECT_WRITE_EXT) {
119 sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
120 }
122 sp_shape_set_shape ((SPShape *) object);
124 /* Duplicate the path */
125 SPCurve *curve = ((SPShape *) object)->curve;
126 //Nulls might be possible if this called iteratively
127 if ( !curve ) {
128 return NULL;
129 }
130 NArtBpath *bpath = SP_CURVE_BPATH(curve);
131 if ( !bpath ) {
132 return NULL;
133 }
134 char *d = sp_svg_write_path ( bpath );
135 repr->setAttribute("d", d);
136 g_free (d);
138 if (((SPObjectClass *) (parent_class))->write)
139 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
141 return repr;
142 }
144 static void
145 box3d_side_set (SPObject *object, unsigned int key, const gchar *value)
146 {
147 Box3DSide *side = SP_BOX3D_SIDE (object);
149 // TODO: In case the box was recreated (by undo, e.g.) we need to recreate the path
150 // (along with other info?) from the parent box.
152 /* fixme: we should really collect updates */
153 switch (key) {
154 case SP_ATTR_INKSCAPE_BOX3D_SIDE_TYPE:
155 if (value) {
156 guint desc = atoi (value);
158 if (!Box3D::is_face_id(desc)) {
159 g_print ("desc is not a face id: =%s=\n", value);
160 }
161 g_return_if_fail (Box3D::is_face_id (desc));
162 Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
163 plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
164 side->dir1 = Box3D::extract_first_axis_direction(plane);
165 side->dir2 = Box3D::extract_second_axis_direction(plane);
166 side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
168 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
169 }
170 break;
171 default:
172 if (((SPObjectClass *) parent_class)->set)
173 ((SPObjectClass *) parent_class)->set (object, key, value);
174 break;
175 }
176 }
178 static void
179 box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
180 {
181 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
182 flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
183 }
185 if (flags & (SP_OBJECT_MODIFIED_FLAG |
186 SP_OBJECT_STYLE_MODIFIED_FLAG |
187 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
188 sp_shape_set_shape ((SPShape *) object);
189 }
191 if (((SPObjectClass *) parent_class)->update)
192 ((SPObjectClass *) parent_class)->update (object, ctx, flags);
193 }
195 void
196 box3d_side_position_set (Box3DSide *side) {
197 box3d_side_set_shape (SP_SHAPE (side));
199 /* This call is responsible for live update of the sides during the initial drag */
200 SP_OBJECT(side)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
201 }
203 void
204 box3d_side_set_shape (SPShape *shape)
205 {
206 Box3DSide *side = SP_BOX3D_SIDE (shape);
207 if (!SP_OBJECT_DOCUMENT(side)->root) {
208 // avoid a warning caused by sp_document_height() (which is called from sp_item_i2d_affine() below)
209 // when reading a file containing 3D boxes
210 return;
211 }
213 if (!SP_IS_BOX3D(SP_OBJECT(side)->parent)) {
214 g_warning ("Parent of 3D box side is not a 3D box.\n");
215 return;
216 }
218 Persp3D *persp = box3d_side_perspective(side);
219 if (!persp) {
220 return;
221 }
223 SPCurve *c = sp_curve_new ();
224 // TODO: Draw the correct quadrangle here
225 // To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
226 // compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
227 // resulting path.
229 std::vector<Proj::Pt3> corners = box3d_side_corners (side);
231 NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(shape)));
233 // FIXME: This can better be implemented by using box3d_get_corner
234 sp_curve_moveto (c, persp->tmat.image(corners[0]).affine() * i2d);
235 sp_curve_lineto (c, persp->tmat.image(corners[1]).affine() * i2d);
236 sp_curve_lineto (c, persp->tmat.image(corners[2]).affine() * i2d);
237 sp_curve_lineto (c, persp->tmat.image(corners[3]).affine() * i2d);
239 sp_curve_closepath (c);
240 sp_shape_set_curve_insync (SP_SHAPE (side), c, TRUE);
241 sp_curve_unref (c);
242 }
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);
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 }
263 }
265 gchar *
266 box3d_side_axes_string(Box3DSide *side)
267 {
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;
284 }
286 static Proj::Pt3
287 box3d_side_corner (Box3DSide *side, guint index) {
288 SPBox3D *box = SP_BOX3D(SP_OBJECT_PARENT(side));
289 return Proj::Pt3 ((index & 0x1) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
290 (index & 0x2) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
291 (index & 0x4) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
292 1.0);
293 }
295 static std::vector<Proj::Pt3>
296 box3d_side_corners (Box3DSide *side) {
297 std::vector<Proj::Pt3> corners;
298 Box3D::Axis orth = Box3D::third_axis_direction (side->dir1, side->dir2);
299 unsigned int i0 = (side->front_or_rear ? orth : 0);
300 unsigned int i1 = i0 ^ side->dir1;
301 unsigned int i2 = i0 ^ side->dir1 ^ side->dir2;
302 unsigned int i3 = i0 ^ side->dir2;
304 corners.push_back (box3d_side_corner (side, i0));
305 corners.push_back (box3d_side_corner (side, i1));
306 corners.push_back (box3d_side_corner (side, i2));
307 corners.push_back (box3d_side_corner (side, i3));
308 return corners;
309 }
311 Persp3D *
312 box3d_side_perspective(Box3DSide *side) {
313 return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
314 }
316 Inkscape::XML::Node *
317 box3d_side_convert_to_path(Box3DSide *side) {
318 // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
319 SPDocument *doc = SP_OBJECT_DOCUMENT(side);
320 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
322 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
323 repr->setAttribute("d", SP_OBJECT_REPR(side)->attribute("d"));
324 repr->setAttribute("style", SP_OBJECT_REPR(side)->attribute("style"));
326 return repr;
327 }
329 /*
330 Local Variables:
331 mode:c++
332 c-file-style:"stroustrup"
333 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
334 indent-tabs-mode:nil
335 fill-column:99
336 End:
337 */
338 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :