Code

Filter effects dialog:
[inkscape.git] / src / object-edit.cpp
index 5ca3469771ae463c2eeab558fe90a97bbda7f13d..487caa09c503923bce9a949f5abdb701814e46cf 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "sp-item.h"
 #include "sp-rect.h"
+#include "box3d.h"
 #include "sp-ellipse.h"
 #include "sp-star.h"
 #include "sp-spiral.h"
 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
 
 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
+//static 
+SPKnotHolder *sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop, guint number_of_handles);
 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
-static SPKnotHolder *sp_path_knot_holder(SPItem *item, SPDesktop *desktop);
+static SPKnotHolder *sp_misc_knot_holder(SPItem *item, SPDesktop *desktop);
 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
 
@@ -61,6 +64,8 @@ sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
 {
     if (SP_IS_RECT(item)) {
         return sp_rect_knot_holder(item, desktop);
+    } else if (SP_IS_3DBOX(item)) {
+        return sp_3dbox_knot_holder(item, desktop, SP3DBoxContext::number_of_handles);
     } else if (SP_IS_ARC(item)) {
         return sp_arc_knot_holder(item, desktop);
     } else if (SP_IS_STAR(item)) {
@@ -69,10 +74,10 @@ sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
         return sp_spiral_knot_holder(item, desktop);
     } else if (SP_IS_OFFSET(item)) {
         return sp_offset_knot_holder(item, desktop);
-    } else if (SP_IS_PATH(item)) {
-        return sp_path_knot_holder(item, desktop);
     } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
         return sp_flowtext_knot_holder(item, desktop);
+    } else {
+        return sp_misc_knot_holder(item, desktop);
     }
 
     return NULL;
@@ -213,6 +218,16 @@ static NR::Point sp_pattern_scale_get(SPItem *item)
 
 /* SPRect */
 
+static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
+{
+    SPDesktop const *desktop = inkscape_active_desktop();
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+    NR::Point s = p * i2d;    
+    SnapManager const &m = desktop->namedview->snap_manager;
+    s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, item).getPoint();
+    return s * i2d.inverse();
+}
+
 static NR::Point sp_rect_rx_get(SPItem *item)
 {
     SPRect *rect = SP_RECT(item);
@@ -223,6 +238,10 @@ static NR::Point sp_rect_rx_get(SPItem *item)
 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
 {
     SPRect *rect = SP_RECT(item);
+    
+    //In general we cannot just snap this radius to an arbitrary point, as we have only a single
+    //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
+    //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
 
     if (state & GDK_CONTROL_MASK) {
         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
@@ -248,6 +267,10 @@ static NR::Point sp_rect_ry_get(SPItem *item)
 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
 {
     SPRect *rect = SP_RECT(item);
+    
+    //In general we cannot just snap this radius to an arbitrary point, as we have only a single
+    //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
+    //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
 
     if (state & GDK_CONTROL_MASK) {
         gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
@@ -331,18 +354,9 @@ static NR::Point sp_rect_wh_get(SPItem *item)
     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
 }
 
-static NR::Point rect_snap_knot_position(SPRect *rect, NR::Point const &p)
-{
-    SPDesktop const *desktop = inkscape_active_desktop();
-    NR::Point s = sp_desktop_dt2root_xy_point(desktop, p);
-    SnapManager const &m = desktop->namedview->snap_manager;
-    s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, rect).getPoint();
-    return sp_desktop_root2dt_xy_point(desktop, s);
-}
-
 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
 {
-    NR::Point const s = rect_snap_knot_position(rect, p);
+    NR::Point const s = snap_knot_position(rect, p);
 
     if (state & GDK_CONTROL_MASK) {
         // original width/height when drag started
@@ -420,7 +434,7 @@ static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &or
     gdouble w_orig = opposite_x - origin[NR::X];
     gdouble h_orig = opposite_y - origin[NR::Y];
 
-    NR::Point const s = rect_snap_knot_position(rect, p);
+    NR::Point const s = snap_knot_position(rect, p);
 
     // mouse displacement since drag started
     gdouble minx = s[NR::X] - origin[NR::X];
@@ -513,6 +527,225 @@ static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
     return knot_holder;
 }
 
+/* 3D Box */
+
+static void sp_3dbox_knot_set(SPItem *item, guint knot_id, Box3D::Axis direction,
+                              NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    g_assert(item != NULL);
+    SP3DBox *box = SP_3DBOX(item);
+
+    // FIXME: Why must the coordinates be flipped vertically???
+    SPDocument *doc = SP_OBJECT_DOCUMENT(box);
+    gdouble height = sp_document_height(doc);
+
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+    if (direction == Box3D::Z) {
+         sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, !(state & GDK_SHIFT_MASK));
+    } else {
+        sp_3dbox_move_corner_in_XY_plane (box, knot_id, new_pos * i2d, (state & GDK_SHIFT_MASK) ? direction : Box3D::XY);
+    }
+    sp_3dbox_update_curves (box);
+}
+
+static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
+{
+    g_assert(item != NULL);
+    SP3DBox *box = SP_3DBOX(item);
+
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+    return sp_3dbox_get_corner(box, knot_id) * i2d;
+}
+
+static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set (item, 1, Box3D::Y, new_pos, origin, state);
+}
+
+/*
+static void sp_3dbox_knot1_set_constrained(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set (item, 1, Box3D::Y, new_pos, origin, state ^ GDK_SHIFT_MASK);
+}
+*/
+
+static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set (item, 2, Box3D::X, new_pos, origin, state);
+}
+
+/*
+static void sp_3dbox_knot2_set_constrained(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set (item, 2, Box3D::X, new_pos, origin, state ^ GDK_SHIFT_MASK);
+}
+
+static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    if (!(state & GDK_SHIFT_MASK)) {
+        sp_3dbox_knot_set (item, 3, Box3D::Y, new_pos, origin, state);
+    } else {
+        sp_3dbox_knot_set (item, 3, Box3D::Z, new_pos, origin, state ^ GDK_SHIFT_MASK);
+    }
+}
+*/
+
+static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set (item, 5, Box3D::Z, new_pos, origin, state);
+}
+
+/*
+static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set (item, 7, Box3D::Z, new_pos, origin, state);
+}
+*/
+
+// defined a uniform behaviour for all knots
+static void sp_3dbox_knot_set_uniformly(SPItem *item, guint knot_id, Box3D::Axis direction, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    g_assert(item != NULL);
+    SP3DBox *box = SP_3DBOX(item);
+
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+    if (direction == Box3D::Z) {
+        sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, !(state & GDK_SHIFT_MASK));
+    } else {
+        sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d,  (state & GDK_SHIFT_MASK));
+    }
+    sp_3dbox_update_curves (box);
+}
+
+static void sp_3dbox_knot0_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 0, Box3D::XY, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot1_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 1, Box3D::XY, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot2_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 2, Box3D::XY, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot3_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 3, Box3D::XY, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot4_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 4, Box3D::Z, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot5_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 5, Box3D::Z, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot6_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 6, Box3D::Z, new_pos, origin, state);
+}
+
+static void sp_3dbox_knot7_set_uniformly(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    sp_3dbox_knot_set_uniformly(item, 7, Box3D::Z, new_pos, origin, state);
+}
+
+static NR::Point sp_3dbox_knot0_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 0);
+}
+
+static NR::Point sp_3dbox_knot1_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 1);
+}
+
+static NR::Point sp_3dbox_knot2_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 2);
+}
+
+static NR::Point sp_3dbox_knot3_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 3);
+}
+
+static NR::Point sp_3dbox_knot4_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 4);
+}
+
+static NR::Point sp_3dbox_knot5_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 5);
+}
+
+static NR::Point sp_3dbox_knot6_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 6);
+}
+
+static NR::Point sp_3dbox_knot7_get(SPItem *item)
+{
+    return sp_3dbox_knot_get(item, 7);
+}
+
+
+//static
+SPKnotHolder *
+sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop, guint number_of_handles)
+{
+    g_assert(item != NULL);
+    SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
+
+    switch (number_of_handles) {
+    case 3:
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,_("Resize box in X/Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,_("Resize box in X/Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,_("Resize box in Z direction"));
+        sp_pat_knot_holder(item, knot_holder);
+        break;
+    case 4:
+        /***
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set_constrained, sp_3dbox_knot1_get, NULL,_("Resize box in X/Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set_constrained, sp_3dbox_knot2_get, NULL,_("Resize box in X/Y direction"));
+        sp_knot_holder_add_full(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
+                                SP_KNOT_SHAPE_CIRCLE,  SP_KNOT_MODE_XOR, _("Resize box in Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,_("Resize box in Z direction"));
+        ***/
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set_uniformly, sp_3dbox_knot0_get, NULL,
+                           _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set_uniformly, sp_3dbox_knot1_get, NULL,
+                           _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set_uniformly, sp_3dbox_knot2_get, NULL,
+                           _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set_uniformly, sp_3dbox_knot3_get, NULL,
+                           _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set_uniformly, sp_3dbox_knot4_get, NULL,
+                           _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set_uniformly, sp_3dbox_knot5_get, NULL,
+                           _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set_uniformly, sp_3dbox_knot6_get, NULL,
+                           _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
+        sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set_uniformly, sp_3dbox_knot7_get, NULL,
+                           _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction"));
+        sp_pat_knot_holder(item, knot_holder);
+        break;
+    default:
+        g_print ("Wrong number of handles (%d): Not implemented yet.\n", number_of_handles);
+        break;
+    }
+
+    return knot_holder;
+}
+
 /* SPArc */
 
 /*
@@ -610,8 +843,10 @@ sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint s
 {
     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
     SPArc *arc = SP_ARC(item);
+    
+    NR::Point const s = snap_knot_position(arc, p);
 
-    ge->rx.computed = fabs( ge->cx.computed - p[NR::X] );
+    ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
 
     if ( state & GDK_CONTROL_MASK ) {
         ge->ry.computed = ge->rx.computed;
@@ -632,8 +867,10 @@ sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint s
 {
     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
     SPArc *arc = SP_ARC(item);
+    
+    NR::Point const s = snap_knot_position(arc, p);
 
-    ge->ry.computed = fabs( ge->cy.computed - p[NR::Y] );
+    ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
 
     if ( state & GDK_CONTROL_MASK ) {
         ge->rx.computed = ge->ry.computed;
@@ -700,8 +937,10 @@ static void
 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
 {
     SPStar *star = SP_STAR(item);
+    
+    NR::Point const s = snap_knot_position(star, p);
 
-    NR::Point d = p - star->center;
+    NR::Point d = s - star->center;
 
     double arg1 = atan2(d);
     double darg1 = arg1 - star->arg[0];
@@ -724,8 +963,11 @@ static void
 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
 {
     SPStar *star = SP_STAR(item);
+    
+    NR::Point const s = snap_knot_position(star, p);
+    
     if (star->flatsided == false) {
-        NR::Point d = p - star->center;
+        NR::Point d = s - star->center;
 
         double arg1 = atan2(d);
         double darg1 = arg1 - star->arg[1];
@@ -1006,7 +1248,7 @@ sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
 }
 
 static SPKnotHolder *
-sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
+sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
 {
     if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))