1
#define __SP_BOX3D_CONTEXT_C__
4
* 3D box drawing context
7
* Lauris Kaplinski <lauris@kaplinski.com>
8
* bulia byak <buliabyak@users.sf.net>
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.
15
* Released under GNU GPL, read the file 'COPYING' for more information
20
#include <gdk/gdkkeysyms.h>
23
#include "display/sp-canvas.h"
25
#include "sp-namedview.h"
26
#include "selection.h"
27
#include "selection-chemistry.h"
28
#include "desktop-handles.h"
30
#include "display/curve.h"
32
#include "message-context.h"
33
#include "pixmaps/cursor-3dbox.xpm"
35
#include "box3d-context.h"
36
#include "sp-metrics.h"
37
#include <glibmm/i18n.h>
38
#include "object-edit.h"
40
#include "xml/node-event-vector.h"
41
#include "preferences.h"
42
#include "context-fns.h"
43
#include "desktop-style.h"
44
#include "transf_mat_3x4.h"
45
#include "perspective-line.h"
47
#include "box3d-side.h"
48
#include "document-private.h"
49
#include "line-geometry.h"
50
#include "shape-editor.h"
52
static void sp_box3d_context_class_init(Box3DContextClass *klass);
53
static void sp_box3d_context_init(Box3DContext *box3d_context);
54
static void sp_box3d_context_dispose(GObject *object);
56
static void sp_box3d_context_setup(SPEventContext *ec);
58
static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEvent *event);
59
static gint sp_box3d_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
61
static void sp_box3d_drag(Box3DContext &bc, guint state);
62
static void sp_box3d_finish(Box3DContext *bc);
64
static SPEventContextClass *parent_class;
66
GtkType sp_box3d_context_get_type()
68
static GType type = 0;
71
sizeof(Box3DContextClass),
73
(GClassInitFunc) sp_box3d_context_class_init,
77
(GInstanceInitFunc) sp_box3d_context_init,
78
NULL, /* value_table */
80
type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "Box3DContext", &info, (GTypeFlags) 0);
85
static void sp_box3d_context_class_init(Box3DContextClass *klass)
87
GObjectClass *object_class = (GObjectClass *) klass;
88
SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
90
parent_class = (SPEventContextClass *) g_type_class_peek_parent(klass);
92
object_class->dispose = sp_box3d_context_dispose;
94
event_context_class->setup = sp_box3d_context_setup;
95
event_context_class->root_handler = sp_box3d_context_root_handler;
96
event_context_class->item_handler = sp_box3d_context_item_handler;
99
static void sp_box3d_context_init(Box3DContext *box3d_context)
101
SPEventContext *event_context = SP_EVENT_CONTEXT(box3d_context);
103
event_context->cursor_shape = cursor_3dbox_xpm;
104
event_context->hot_x = 4;
105
event_context->hot_y = 4;
106
event_context->xp = 0;
107
event_context->yp = 0;
108
event_context->tolerance = 0;
109
event_context->within_tolerance = false;
110
event_context->item_to_select = NULL;
112
box3d_context->item = NULL;
114
box3d_context->ctrl_dragged = false;
115
box3d_context->extruded = false;
117
box3d_context->_vpdrag = NULL;
119
new (&box3d_context->sel_changed_connection) sigc::connection();
122
static void sp_box3d_context_dispose(GObject *object)
124
Box3DContext *bc = SP_BOX3D_CONTEXT(object);
125
SPEventContext *ec = SP_EVENT_CONTEXT(object);
127
ec->enableGrDrag(false);
129
delete (bc->_vpdrag);
132
bc->sel_changed_connection.disconnect();
133
bc->sel_changed_connection.~connection();
135
delete ec->shape_editor;
136
ec->shape_editor = NULL;
138
/* fixme: This is necessary because we do not grab */
143
if (bc->_message_context) {
144
delete bc->_message_context;
147
G_OBJECT_CLASS(parent_class)->dispose(object);
151
\brief Callback that processes the "changed" signal on the selection;
152
destroys old and creates new knotholder
154
static void sp_box3d_context_selection_changed(Inkscape::Selection *selection, gpointer data)
156
Box3DContext *bc = SP_BOX3D_CONTEXT(data);
157
SPEventContext *ec = SP_EVENT_CONTEXT(bc);
159
ec->shape_editor->unset_item(SH_KNOTHOLDER);
160
SPItem *item = selection->singleItem();
161
ec->shape_editor->set_item(item, SH_KNOTHOLDER);
163
if (selection->perspList().size() == 1) {
164
// selecting a single box changes the current perspective
165
ec->desktop->doc()->current_persp3d = selection->perspList().front();
169
/* create a default perspective in document defs if none is present
170
(can happen after 'vacuum defs' or when a pre-0.46 file is opened) */
171
static void sp_box3d_context_check_for_persp_in_defs(SPDocument *document) {
172
SPDefs *defs = (SPDefs *) SP_DOCUMENT_DEFS(document);
174
bool has_persp = false;
175
for (SPObject *child = sp_object_first_child(defs); child != NULL; child = SP_OBJECT_NEXT(child) ) {
176
if (SP_IS_PERSP3D(child)) {
183
document->current_persp3d = persp3d_create_xml_element (document);
187
static void sp_box3d_context_setup(SPEventContext *ec)
189
Box3DContext *bc = SP_BOX3D_CONTEXT(ec);
191
if (((SPEventContextClass *) parent_class)->setup) {
192
((SPEventContextClass *) parent_class)->setup(ec);
195
sp_box3d_context_check_for_persp_in_defs(sp_desktop_document (ec->desktop));
197
ec->shape_editor = new ShapeEditor(ec->desktop);
199
SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
201
ec->shape_editor->set_item(item, SH_KNOTHOLDER);
204
bc->sel_changed_connection.disconnect();
205
bc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(
206
sigc::bind(sigc::ptr_fun(&sp_box3d_context_selection_changed), (gpointer)bc)
209
bc->_vpdrag = new Box3D::VPDrag(sp_desktop_document (ec->desktop));
210
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
212
if (prefs->getBool("/tools/shapes/selcue")) {
213
ec->enableSelectionCue();
216
if (prefs->getBool("/tools/shapes/gradientdrag")) {
220
bc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
223
static gint sp_box3d_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
225
SPDesktop *desktop = event_context->desktop;
229
switch (event->type) {
230
case GDK_BUTTON_PRESS:
231
if ( event->button.button == 1 && !event_context->space_panning) {
232
Inkscape::setup_for_drag_start(desktop, event_context, event);
236
// motion and release are always on root (why?)
241
if (((SPEventContextClass *) parent_class)->item_handler) {
242
ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
248
static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEvent *event)
250
static bool dragging;
252
SPDesktop *desktop = event_context->desktop;
253
Inkscape::Selection *selection = sp_desktop_selection (desktop);
254
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
255
int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
257
Box3DContext *bc = SP_BOX3D_CONTEXT(event_context);
258
g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
259
Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
261
event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
264
switch (event->type) {
265
case GDK_BUTTON_PRESS:
266
if ( event->button.button == 1 && !event_context->space_panning) {
267
Geom::Point const button_w(event->button.x,
271
event_context->xp = (gint) button_w[Geom::X];
272
event_context->yp = (gint) button_w[Geom::Y];
273
event_context->within_tolerance = true;
275
// remember clicked item, *not* disregarding groups (since a 3D box is a group), honoring Alt
276
event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, event->button.state & GDK_CONTROL_MASK);
279
sp_event_context_snap_window_open(event_context);
282
Geom::Point button_dt(desktop->w2d(button_w));
283
bc->drag_origin = from_2geom(button_dt);
284
bc->drag_ptB = from_2geom(button_dt);
285
bc->drag_ptC = from_2geom(button_dt);
287
/* Projective preimages of clicked point under current perspective */
288
bc->drag_origin_proj = cur_persp->tmat.preimage (from_2geom(button_dt), 0, Proj::Z);
289
bc->drag_ptB_proj = bc->drag_origin_proj;
290
bc->drag_ptC_proj = bc->drag_origin_proj;
291
bc->drag_ptC_proj.normalize();
292
bc->drag_ptC_proj[Proj::Z] = 0.25;
295
SnapManager &m = desktop->namedview->snap_manager;
296
m.setup(desktop, true, bc->item);
297
m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt, Inkscape::SNAPSOURCE_HANDLE);
298
bc->center = from_2geom(button_dt);
300
sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
301
( GDK_KEY_PRESS_MASK |
302
GDK_BUTTON_RELEASE_MASK |
303
GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK |
304
GDK_BUTTON_PRESS_MASK ),
305
NULL, event->button.time);
309
case GDK_MOTION_NOTIFY:
311
&& ( event->motion.state & GDK_BUTTON1_MASK ) && !event_context->space_panning)
313
if ( event_context->within_tolerance
314
&& ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
315
&& ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
316
break; // do not drag if we're within tolerance from origin
318
// Once the user has moved farther than tolerance from the original location
319
// (indicating they intend to draw, not click), then always process the
320
// motion notify coordinates as given (no snapping back to origin)
321
event_context->within_tolerance = false;
323
Geom::Point const motion_w(event->motion.x,
325
Geom::Point motion_dt(desktop->w2d(motion_w));
327
SnapManager &m = desktop->namedview->snap_manager;
328
m.setup(desktop, true, bc->item);
329
m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt, Inkscape::SNAPSOURCE_HANDLE);
331
bc->ctrl_dragged = event->motion.state & GDK_CONTROL_MASK;
333
if (event->motion.state & GDK_SHIFT_MASK && !bc->extruded && bc->item) {
334
// once shift is pressed, set bc->extruded
339
bc->drag_ptB = from_2geom(motion_dt);
340
bc->drag_ptC = from_2geom(motion_dt);
342
bc->drag_ptB_proj = cur_persp->tmat.preimage (from_2geom(motion_dt), 0, Proj::Z);
343
bc->drag_ptC_proj = bc->drag_ptB_proj;
344
bc->drag_ptC_proj.normalize();
345
bc->drag_ptC_proj[Proj::Z] = 0.25;
347
// Without Ctrl, motion of the extruded corner is constrained to the
348
// perspective line from drag_ptB to vanishing point Y.
349
if (!bc->ctrl_dragged) {
351
Box3D::PerspectiveLine pline (bc->drag_ptB, Proj::Z, SP_ACTIVE_DOCUMENT->current_persp3d);
352
bc->drag_ptC = pline.closest_to (from_2geom(motion_dt));
354
bc->drag_ptB_proj.normalize();
355
bc->drag_ptC_proj = cur_persp->tmat.preimage (bc->drag_ptC, bc->drag_ptB_proj[Proj::X], Proj::X);
357
bc->drag_ptC = from_2geom(motion_dt);
359
bc->drag_ptB_proj.normalize();
360
bc->drag_ptC_proj = cur_persp->tmat.preimage (from_2geom(motion_dt), bc->drag_ptB_proj[Proj::X], Proj::X);
362
Geom::Point pt2g = to_2geom(bc->drag_ptC);
363
m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
364
bc->drag_ptC = from_2geom(pt2g);
367
sp_box3d_drag(*bc, event->motion.state);
372
case GDK_BUTTON_RELEASE:
373
event_context->xp = event_context->yp = 0;
374
if ( event->button.button == 1 && !event_context->space_panning) {
376
sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
378
if (!event_context->within_tolerance) {
379
// we've been dragging, finish the box
381
} else if (event_context->item_to_select) {
382
// no dragging, select clicked item if any
383
if (event->button.state & GDK_SHIFT_MASK) {
384
selection->toggle(event_context->item_to_select);
386
selection->set(event_context->item_to_select);
389
// click in an empty space
393
event_context->item_to_select = NULL;
395
sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
400
switch (get_group0_keyval (&event->key)) {
405
// prevent the zoom field from activation
410
case GDK_bracketright:
411
persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, -180/snaps, MOD__ALT);
412
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
413
_("Change perspective (angle of PLs)"));
417
case GDK_bracketleft:
418
persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::X, 180/snaps, MOD__ALT);
419
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
420
_("Change perspective (angle of PLs)"));
425
persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, -180/snaps, MOD__ALT);
426
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
427
_("Change perspective (angle of PLs)"));
432
persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Y, 180/snaps, MOD__ALT);
433
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
434
_("Change perspective (angle of PLs)"));
439
persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, -180/snaps, MOD__ALT);
440
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
441
_("Change perspective (angle of PLs)"));
446
persp3d_rotate_VP (inkscape_active_document()->current_persp3d, Proj::Z, 180/snaps, MOD__ALT);
447
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
448
_("Change perspective (angle of PLs)"));
452
/* TODO: what is this???
454
if (MOD__CTRL && MOD__SHIFT) {
455
Box3D::create_canvas_point(persp3d_get_VP(inkscape_active_document()->current_persp3d, Proj::W).affine(),
464
if (MOD__SHIFT_ONLY) {
465
sp_selection_to_guides(desktop);
473
desktop->setToolboxFocusTo ("altx-box3d");
476
if (MOD__SHIFT_ONLY) {
477
persp3d_toggle_VPs(selection->perspList(), Proj::X);
478
bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
485
if (MOD__SHIFT_ONLY) {
486
persp3d_toggle_VPs(selection->perspList(), Proj::Y);
487
bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
494
if (MOD__SHIFT_ONLY) {
495
persp3d_toggle_VPs(selection->perspList(), Proj::Z);
496
bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
502
sp_desktop_selection(desktop)->clear();
503
//TODO: make dragging escapable by Esc
508
sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
511
sp_event_context_snap_window_closed(event_context);
512
if (!event_context->within_tolerance) {
513
// we've been dragging, finish the box
516
// do not return true, so that space would work switching to selector
529
if (((SPEventContextClass *) parent_class)->root_handler) {
530
ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
537
static void sp_box3d_drag(Box3DContext &bc, guint /*state*/)
539
SPDesktop *desktop = SP_EVENT_CONTEXT(&bc)->desktop;
543
if (Inkscape::have_viable_layer(desktop, bc._message_context) == false) {
548
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(&bc));
549
Inkscape::XML::Node *repr = xml_doc->createElement("svg:g");
550
repr->setAttribute("sodipodi:type", "inkscape:box3d");
553
sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/3dbox", false);
555
bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
556
Inkscape::GC::release(repr);
557
Inkscape::XML::Node *repr_side;
558
// TODO: Incorporate this in box3d-side.cpp!
559
for (int i = 0; i < 6; ++i) {
560
repr_side = xml_doc->createElement("svg:path");
561
repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
562
repr->addChild(repr_side, NULL);
564
Box3DSide *side = SP_BOX3D_SIDE(inkscape_active_document()->getObjectByRepr (repr_side));
566
guint desc = Box3D::int_to_face(i);
568
Box3D::Axis plane = (Box3D::Axis) (desc & 0x7);
569
plane = (Box3D::is_plane(plane) ? plane : Box3D::orth_plane_or_axis(plane));
570
side->dir1 = Box3D::extract_first_axis_direction(plane);
571
side->dir2 = Box3D::extract_second_axis_direction(plane);
572
side->front_or_rear = (Box3D::FrontOrRear) (desc & 0x8);
575
box3d_side_apply_style(side);
577
SP_OBJECT(side)->updateRepr(); // calls box3d_side_write() and updates, e.g., the axes string description
580
box3d_set_z_orders(SP_BOX3D(bc.item));
581
bc.item->updateRepr();
583
// TODO: It would be nice to show the VPs during dragging, but since there is no selection
584
// at this point (only after finishing the box), we must do this "manually"
585
/**** bc._vpdrag->updateDraggers(); ****/
587
sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
592
SPBox3D *box = SP_BOX3D(bc.item);
594
box->orig_corner0 = bc.drag_origin_proj;
595
box->orig_corner7 = bc.drag_ptC_proj;
597
box3d_check_for_swapped_coords(box);
599
/* we need to call this from here (instead of from box3d_position_set(), for example)
600
because z-order setting must not interfere with display updates during undo/redo */
601
box3d_set_z_orders (box);
603
box3d_position_set(box);
606
bc._message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>3D Box</b>; with <b>Shift</b> to extrude along the Z axis"));
609
static void sp_box3d_finish(Box3DContext *bc)
611
bc->_message_context->clear();
612
g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
614
if ( bc->item != NULL ) {
615
SPDesktop * desktop = SP_EVENT_CONTEXT_DESKTOP(bc);
617
SPBox3D *box = SP_BOX3D(bc->item);
619
box->orig_corner0 = bc->drag_origin_proj;
620
box->orig_corner7 = bc->drag_ptC_proj;
624
box3d_relabel_corners(box);
626
sp_canvas_end_forced_full_redraws(desktop->canvas);
628
sp_desktop_selection(desktop)->set(bc->item);
629
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_3DBOX,
635
bc->ctrl_dragged = false;
636
bc->extruded = false;
639
void sp_box3d_context_update_lines(SPEventContext *ec) {
640
/* update perspective lines if we are in the 3D box tool (so that infinite ones are shown correctly) */
641
if (SP_IS_BOX3D_CONTEXT (ec)) {
642
Box3DContext *bc = SP_BOX3D_CONTEXT (ec);
643
bc->_vpdrag->updateLines();
650
c-file-style:"stroustrup"
651
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
656
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :