1 #define __SP_3DBOX_CONTEXT_C__
3 /*
4 * 3D box drawing context
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 2007 Maximilian Albert <Anhalter42@gmx.de>
11 * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
12 * Copyright (C) 2000-2005 authors
13 * Copyright (C) 2000-2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #include "config.h"
20 #include <gdk/gdkkeysyms.h>
22 #include "macros.h"
23 #include "display/sp-canvas.h"
24 #include "document.h"
25 #include "sp-namedview.h"
26 #include "selection.h"
27 #include "desktop-handles.h"
28 #include "snap.h"
29 #include "display/curve.h"
30 #include "desktop.h"
31 #include "message-context.h"
32 #include "pixmaps/cursor-rect.xpm"
33 #include "box3d.h"
34 #include "box3d-context.h"
35 #include "sp-metrics.h"
36 #include <glibmm/i18n.h>
37 #include "object-edit.h"
38 #include "xml/repr.h"
39 #include "xml/node-event-vector.h"
40 #include "prefs-utils.h"
41 #include "context-fns.h"
43 static void sp_3dbox_context_class_init(SP3DBoxContextClass *klass);
44 static void sp_3dbox_context_init(SP3DBoxContext *box3d_context);
45 static void sp_3dbox_context_dispose(GObject *object);
47 static void sp_3dbox_context_setup(SPEventContext *ec);
48 static void sp_3dbox_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_3dbox_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint sp_3dbox_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void sp_3dbox_drag(SP3DBoxContext &rc, guint state);
54 static void sp_3dbox_finish(SP3DBoxContext *rc);
56 static SPEventContextClass *parent_class;
59 GtkType sp_3dbox_context_get_type()
60 {
61 static GType type = 0;
62 if (!type) {
63 GTypeInfo info = {
64 sizeof(SP3DBoxContextClass),
65 NULL, NULL,
66 (GClassInitFunc) sp_3dbox_context_class_init,
67 NULL, NULL,
68 sizeof(SP3DBoxContext),
69 4,
70 (GInstanceInitFunc) sp_3dbox_context_init,
71 NULL, /* value_table */
72 };
73 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SP3DBoxContext", &info, (GTypeFlags) 0);
74 }
75 return type;
76 }
78 static void sp_3dbox_context_class_init(SP3DBoxContextClass *klass)
79 {
80 GObjectClass *object_class = (GObjectClass *) klass;
81 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83 parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
85 object_class->dispose = sp_3dbox_context_dispose;
87 event_context_class->setup = sp_3dbox_context_setup;
88 event_context_class->set = sp_3dbox_context_set;
89 event_context_class->root_handler = sp_3dbox_context_root_handler;
90 event_context_class->item_handler = sp_3dbox_context_item_handler;
91 }
93 Box3D::Perspective3D * SP3DBoxContext::current_perspective = NULL;
94 guint SP3DBoxContext::number_of_handles = 3;
96 static void sp_3dbox_context_init(SP3DBoxContext *box3d_context)
97 {
98 SPEventContext *event_context = SP_EVENT_CONTEXT(box3d_context);
100 event_context->cursor_shape = cursor_rect_xpm;
101 event_context->hot_x = 4;
102 event_context->hot_y = 4;
103 event_context->xp = 0;
104 event_context->yp = 0;
105 event_context->tolerance = 0;
106 event_context->within_tolerance = false;
107 event_context->item_to_select = NULL;
109 event_context->shape_repr = NULL;
110 event_context->shape_knot_holder = NULL;
112 box3d_context->item = NULL;
114 box3d_context->rx = 0.0;
115 box3d_context->ry = 0.0;
117 box3d_context->ctrl_dragged = false;
118 box3d_context->extruded = false;
120 /* create an initial perspective */
121 if (!SP3DBoxContext::current_perspective) {
122 SP3DBoxContext::current_perspective = new Box3D::Perspective3D (
123 // VP in x-direction
124 Box3D::VanishingPoint( NR::Point( 50.0, 600.0),
125 NR::Point( -1.0, 0.0), Box3D::VP_INFINITE),
126 // VP in y-direction
127 Box3D::VanishingPoint( NR::Point(500.0,1000.0),
128 NR::Point( 0.0, 1.0), Box3D::VP_INFINITE),
129 // VP in z-direction
130 Box3D::VanishingPoint( NR::Point(700.0, 500.0),
131 NR::Point(sqrt(3.0),1.0), Box3D::VP_INFINITE));
132 }
134 new (&box3d_context->sel_changed_connection) sigc::connection();
135 }
137 static void sp_3dbox_context_dispose(GObject *object)
138 {
139 SP3DBoxContext *rc = SP_3DBOX_CONTEXT(object);
140 SPEventContext *ec = SP_EVENT_CONTEXT(object);
142 ec->enableGrDrag(false);
144 rc->sel_changed_connection.disconnect();
145 rc->sel_changed_connection.~connection();
147 /* fixme: This is necessary because we do not grab */
148 if (rc->item) {
149 sp_3dbox_finish(rc);
150 }
152 if (ec->shape_knot_holder) {
153 sp_knot_holder_destroy(ec->shape_knot_holder);
154 ec->shape_knot_holder = NULL;
155 }
157 if (ec->shape_repr) { // remove old listener
158 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
159 Inkscape::GC::release(ec->shape_repr);
160 ec->shape_repr = 0;
161 }
163 if (rc->_message_context) {
164 delete rc->_message_context;
165 }
167 G_OBJECT_CLASS(parent_class)->dispose(object);
168 }
170 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
171 NULL, /* child_added */
172 NULL, /* child_removed */
173 ec_shape_event_attr_changed,
174 NULL, /* content_changed */
175 NULL /* order_changed */
176 };
178 /**
179 \brief Callback that processes the "changed" signal on the selection;
180 destroys old and creates new knotholder
181 */
182 void sp_3dbox_context_selection_changed(Inkscape::Selection *selection, gpointer data)
183 {
184 SP3DBoxContext *rc = SP_3DBOX_CONTEXT(data);
185 SPEventContext *ec = SP_EVENT_CONTEXT(rc);
187 if (ec->shape_knot_holder) { // destroy knotholder
188 sp_knot_holder_destroy(ec->shape_knot_holder);
189 ec->shape_knot_holder = NULL;
190 }
192 if (ec->shape_repr) { // remove old listener
193 sp_repr_remove_listener_by_data(ec->shape_repr, ec);
194 Inkscape::GC::release(ec->shape_repr);
195 ec->shape_repr = 0;
196 }
198 SPItem *item = selection->singleItem();
199 if (item) {
200 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
201 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
202 if (shape_repr) {
203 ec->shape_repr = shape_repr;
204 Inkscape::GC::anchor(shape_repr);
205 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
206 }
207 }
208 }
210 static void sp_3dbox_context_setup(SPEventContext *ec)
211 {
212 SP3DBoxContext *rc = SP_3DBOX_CONTEXT(ec);
214 if (((SPEventContextClass *) parent_class)->setup) {
215 ((SPEventContextClass *) parent_class)->setup(ec);
216 }
218 SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
219 if (item) {
220 ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
221 Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
222 if (shape_repr) {
223 ec->shape_repr = shape_repr;
224 Inkscape::GC::anchor(shape_repr);
225 sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
226 }
227 }
229 rc->sel_changed_connection.disconnect();
230 rc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
231 sigc::bind(sigc::ptr_fun(&sp_3dbox_context_selection_changed), (gpointer)rc)
232 );
234 sp_event_context_read(ec, "rx");
235 sp_event_context_read(ec, "ry");
237 if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
238 ec->enableSelectionCue();
239 }
241 if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
242 ec->enableGrDrag();
243 }
245 rc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
246 }
248 static void sp_3dbox_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
249 {
250 SP3DBoxContext *rc = SP_3DBOX_CONTEXT(ec);
252 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
253 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
254 if ( strcmp(key, "rx") == 0 ) {
255 rc->rx = ( val
256 ? g_ascii_strtod (val, NULL)
257 : 0.0 );
258 } else if ( strcmp(key, "ry") == 0 ) {
259 rc->ry = ( val
260 ? g_ascii_strtod (val, NULL)
261 : 0.0 );
262 }
263 }
265 static gint sp_3dbox_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
266 {
267 SPDesktop *desktop = event_context->desktop;
269 gint ret = FALSE;
271 switch (event->type) {
272 case GDK_BUTTON_PRESS:
273 if ( event->button.button == 1 ) {
274 Inkscape::setup_for_drag_start(desktop, event_context, event);
275 ret = TRUE;
276 }
277 break;
278 // motion and release are always on root (why?)
279 default:
280 break;
281 }
283 if (((SPEventContextClass *) parent_class)->item_handler) {
284 ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
285 }
287 return ret;
288 }
290 static gint sp_3dbox_context_root_handler(SPEventContext *event_context, GdkEvent *event)
291 {
292 static bool dragging;
294 SPDesktop *desktop = event_context->desktop;
295 Inkscape::Selection *selection = sp_desktop_selection (desktop);
297 SP3DBoxContext *rc = SP_3DBOX_CONTEXT(event_context);
299 event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
301 gint ret = FALSE;
302 switch (event->type) {
303 case GDK_BUTTON_PRESS:
304 if ( event->button.button == 1 ) {
305 NR::Point const button_w(event->button.x,
306 event->button.y);
308 // save drag origin
309 event_context->xp = (gint) button_w[NR::X];
310 event_context->yp = (gint) button_w[NR::Y];
311 event_context->within_tolerance = true;
313 // remember clicked item, disregarding groups, honoring Alt
314 event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
316 dragging = true;
318 /* Position center */
319 NR::Point const button_dt(desktop->w2d(button_w));
320 rc->drag_origin = button_dt;
321 rc->drag_ptB = button_dt;
322 rc->drag_ptC = button_dt;
324 /* Snap center */
325 SnapManager const &m = desktop->namedview->snap_manager;
326 rc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
327 button_dt, rc->item).getPoint();
329 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
330 ( GDK_KEY_PRESS_MASK |
331 GDK_BUTTON_RELEASE_MASK |
332 GDK_POINTER_MOTION_MASK |
333 GDK_BUTTON_PRESS_MASK ),
334 NULL, event->button.time);
335 ret = TRUE;
336 }
337 break;
338 case GDK_MOTION_NOTIFY:
339 if ( dragging
340 && ( event->motion.state & GDK_BUTTON1_MASK ) )
341 {
342 if ( event_context->within_tolerance
343 && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
344 && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
345 break; // do not drag if we're within tolerance from origin
346 }
347 // Once the user has moved farther than tolerance from the original location
348 // (indicating they intend to draw, not click), then always process the
349 // motion notify coordinates as given (no snapping back to origin)
350 event_context->within_tolerance = false;
352 NR::Point const motion_w(event->motion.x,
353 event->motion.y);
354 NR::Point motion_dt(desktop->w2d(motion_w));
356 SnapManager const &m = desktop->namedview->snap_manager;
357 motion_dt = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, motion_dt, rc->item).getPoint();
359 rc->ctrl_dragged = event->motion.state & GDK_CONTROL_MASK;
361 if (event->motion.state & GDK_SHIFT_MASK && !rc->extruded) {
362 /* once shift is pressed, set rc->extruded and create the remaining faces */
363 rc->extruded = true;
365 /* The separate faces */
366 SP3DBox *box3d = SP_3DBOX(rc->item);
367 for (int i = 0; i < 6; ++i) {
368 if (i == 4 || box3d->faces[i]) continue;
369 box3d->faces[i] = new Box3DFace (box3d);
370 box3d->faces[i]->hook_path_to_3dbox();
371 }
372 }
374 if (!rc->extruded) {
375 rc->drag_ptB = motion_dt;
376 } else {
377 // Without Ctrl, motion of the extruded corner is constrained to the
378 // perspective line from drag_ptB to vanishing point Y.
379 if (!rc->ctrl_dragged) {
380 rc->drag_ptC = Box3D::perspective_line_snap (rc->drag_ptB, Box3D::Z, motion_dt);
381 } else {
382 rc->drag_ptC = motion_dt;
383 }
384 rc->drag_ptC = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, rc->drag_ptC, rc->item).getPoint();
385 if (rc->ctrl_dragged) {
386 Box3D::PerspectiveLine pl1 (NR::Point (event_context->xp, event_context->yp), Box3D::Y);
387 Box3D::PerspectiveLine pl2 (rc->drag_ptB, Box3D::X);
388 NR::Point corner1 = pl1.meet(pl2);
390 Box3D::PerspectiveLine pl3 (corner1, Box3D::X);
391 Box3D::PerspectiveLine pl4 (rc->drag_ptC, Box3D::Z);
392 rc->drag_ptB = pl3.meet(pl4);
393 }
394 }
397 sp_3dbox_drag(*rc, event->motion.state);
399 ret = TRUE;
400 }
401 ret = TRUE;
402 break;
403 case GDK_BUTTON_RELEASE:
404 event_context->xp = event_context->yp = 0;
405 if ( event->button.button == 1 ) {
406 dragging = false;
408 if (!event_context->within_tolerance) {
409 // we've been dragging, finish the rect
410 sp_3dbox_finish(rc);
411 } else if (event_context->item_to_select) {
412 // no dragging, select clicked item if any
413 if (event->button.state & GDK_SHIFT_MASK) {
414 selection->toggle(event_context->item_to_select);
415 } else {
416 selection->set(event_context->item_to_select);
417 }
418 } else {
419 // click in an empty space
420 selection->clear();
421 }
423 event_context->item_to_select = NULL;
424 ret = TRUE;
425 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
426 event->button.time);
427 }
428 break;
429 case GDK_KEY_PRESS:
430 switch (get_group0_keyval (&event->key)) {
431 case GDK_Alt_L:
432 case GDK_Alt_R:
433 case GDK_Control_L:
434 case GDK_Control_R:
435 case GDK_Shift_L:
436 case GDK_Shift_R:
437 case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
438 case GDK_Meta_R:
439 if (!dragging){
440 sp_event_show_modifier_tip (event_context->defaultMessageContext(), event,
441 _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
442 _("<b>Shift</b>: draw around the starting point"),
443 NULL);
444 }
445 break;
446 case GDK_Up:
447 case GDK_Down:
448 case GDK_KP_Up:
449 case GDK_KP_Down:
450 // prevent the zoom field from activation
451 if (!MOD__CTRL_ONLY)
452 ret = TRUE;
453 break;
455 case GDK_x:
456 case GDK_X:
457 if (MOD__ALT_ONLY) {
458 desktop->setToolboxFocusTo ("altx-rect");
459 ret = TRUE;
460 }
461 break;
463 case GDK_Escape:
464 sp_desktop_selection(desktop)->clear();
465 //TODO: make dragging escapable by Esc
466 break;
468 case GDK_space:
469 if (dragging) {
470 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
471 event->button.time);
472 dragging = false;
473 if (!event_context->within_tolerance) {
474 // we've been dragging, finish the rect
475 sp_3dbox_finish(rc);
476 }
477 // do not return true, so that space would work switching to selector
478 }
479 break;
481 default:
482 break;
483 }
484 break;
485 case GDK_KEY_RELEASE:
486 switch (get_group0_keyval (&event->key)) {
487 case GDK_Alt_L:
488 case GDK_Alt_R:
489 case GDK_Control_L:
490 case GDK_Control_R:
491 case GDK_Shift_L:
492 case GDK_Shift_R:
493 case GDK_Meta_L: // Meta is when you press Shift+Alt
494 case GDK_Meta_R:
495 event_context->defaultMessageContext()->clear();
496 break;
497 default:
498 break;
499 }
500 break;
501 default:
502 break;
503 }
505 if (!ret) {
506 if (((SPEventContextClass *) parent_class)->root_handler) {
507 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
508 }
509 }
511 return ret;
512 }
514 static void sp_3dbox_drag(SP3DBoxContext &rc, guint state)
515 {
516 SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
518 if (!rc.item) {
520 if (Inkscape::have_viable_layer(desktop, rc._message_context) == false) {
521 return;
522 }
524 /* Create object */
525 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&rc));
526 Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
527 repr->setAttribute("sodipodi:type", "inkscape:3dbox");
529 /* Set style */
530 //sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.3dbox", false);
532 rc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
533 Inkscape::GC::release(repr);
534 rc.item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
536 /* Hook path to the only currenctly existing face */
537 SP_3DBOX(rc.item)->faces[4]->hook_path_to_3dbox();
539 rc.item->updateRepr();
541 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
542 }
544 // FIXME: remove these extra points
545 NR::Point pt = rc.drag_ptB;
546 NR::Point shift_pt = rc.drag_ptC;
548 NR::Rect r;
549 if (!(state & GDK_SHIFT_MASK)) {
550 r = Inkscape::snap_rectangular_box(desktop, rc.item, pt, rc.center, state);
551 } else {
552 r = Inkscape::snap_rectangular_box(desktop, rc.item, shift_pt, rc.center, state);
553 }
555 /*** artefacts of rect ***
556 if ( rc.rx != 0.0 ) {
557 sp_3dbox_set_rx (SP_3DBOX(rc.item), TRUE, rc.rx);
558 }
559 if ( rc.ry != 0.0 ) {
560 if (rc.rx == 0.0)
561 sp_3dbox_set_ry (SP_3DBOX(rc.item), TRUE, CLAMP(rc.ry, 0, MIN(r.dimensions()[NR::X], r.dimensions()[NR::Y])/2));
562 else
563 sp_3dbox_set_ry (SP_3DBOX(rc.item), TRUE, CLAMP(rc.ry, 0, r.dimensions()[NR::Y]));
564 }
565 ***/
567 SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
568 NR::Point origin_w(ec->xp, ec->yp);
569 NR::Point origin(desktop->w2d(origin_w));
570 sp_3dbox_position_set(rc);
572 // status text
573 GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
574 GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
575 rc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Rectangle</b>: %s × %s; with <b>Ctrl</b> to make square or integer-ratio rectangle; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
576 g_string_free(xs, FALSE);
577 g_string_free(ys, FALSE);
578 }
580 static void sp_3dbox_finish(SP3DBoxContext *rc)
581 {
582 rc->_message_context->clear();
584 if ( rc->item != NULL ) {
585 SPDesktop * desktop;
587 desktop = SP_EVENT_CONTEXT_DESKTOP(rc);
589 SP_OBJECT(rc->item)->updateRepr();
591 sp_canvas_end_forced_full_redraws(desktop->canvas);
593 sp_desktop_selection(desktop)->set(rc->item);
594 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
595 _("Create 3d box"));
597 rc->item = NULL;
598 }
600 rc->ctrl_dragged = false;
601 rc->extruded = false;
602 }
604 /*
605 Local Variables:
606 mode:c++
607 c-file-style:"stroustrup"
608 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
609 indent-tabs-mode:nil
610 fill-column:99
611 End:
612 */
613 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :