Code

Adding axis detection to new input dialog
[inkscape.git] / src / object-edit.cpp
index d96585509ca7882b059e4f0c883b21702ce0c062..b3d661586a2b08b2f2e012052f8779e6706bb1e7 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"
@@ -29,6 +30,8 @@
 #include "desktop-affine.h"
 #include <style.h>
 #include "desktop.h"
+#include "desktop-handles.h"
+#include "sp-namedview.h"
 
 #include "sp-pattern.h"
 #include "sp-path.h"
@@ -39,7 +42,6 @@
 
 #include <libnr/nr-scale-ops.h>
 
-
 #include "xml/repr.h"
 
 #include "isnan.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 *box3d_knot_holder(SPItem *item, SPDesktop *desktop);
 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);
 
@@ -60,6 +63,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_BOX3D(item)) {
+        return box3d_knot_holder(item, desktop);
     } else if (SP_IS_ARC(item)) {
         return sp_arc_knot_holder(item, desktop);
     } else if (SP_IS_STAR(item)) {
@@ -68,10 +73,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;
@@ -145,7 +150,7 @@ static NR::Point sp_pattern_angle_get(SPItem *item)
 }
 
 static void
-sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
 
@@ -170,7 +175,7 @@ sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin,
 }
 
 static void
-sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
@@ -212,6 +217,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::SNAPPOINT_NODE, s, item).getPoint();
+    return s * i2d.inverse();
+}
+
 static NR::Point sp_rect_rx_get(SPItem *item)
 {
     SPRect *rect = SP_RECT(item);
@@ -219,10 +234,14 @@ static NR::Point sp_rect_rx_get(SPItem *item)
     return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
 }
 
-static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+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;
         rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
@@ -244,10 +263,14 @@ static NR::Point sp_rect_ry_get(SPItem *item)
     return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
 }
 
-static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+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;
         rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
@@ -330,18 +353,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(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);
-    s = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, s, NULL).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(p);
+    NR::Point const s = snap_knot_position(rect, p);
 
     if (state & GDK_CONTROL_MASK) {
         // original width/height when drag started
@@ -419,7 +433,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(p);
+    NR::Point const s = snap_knot_position(rect, p);
 
     // mouse displacement since drag started
     gdouble minx = s[NR::X] - origin[NR::X];
@@ -512,6 +526,166 @@ static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
     return knot_holder;
 }
 
+/* Box3D (= the new 3D box structure) */
+
+static NR::Point box3d_knot_get(SPItem *item, guint knot_id)
+{
+    return box3d_get_corner_screen(SP_BOX3D(item), knot_id);
+}
+
+static void box3d_knot_set(SPItem *item, guint knot_id, NR::Point const &new_pos, NR::Point const &/*origin*/, guint state)
+{
+    NR::Point const s = snap_knot_position(item, new_pos);
+
+    g_assert(item != NULL);
+    SPBox3D *box = SP_BOX3D(item);
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+
+    Box3D::Axis movement;
+    if ((knot_id < 4) != (state & GDK_SHIFT_MASK)) {
+        movement = Box3D::XY;
+    } else {
+        movement = Box3D::Z;
+    }
+
+    box3d_set_corner (box, knot_id, s * i2d, movement, (state & GDK_CONTROL_MASK));
+    box3d_set_z_orders(box);
+    box3d_position_set(box);
+}
+
+static NR::Point box3d_knot_center_get (SPItem *item)
+{
+    return box3d_get_center_screen (SP_BOX3D(item));
+}
+
+static void box3d_knot_center_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    NR::Point const s = snap_knot_position(item, new_pos);
+
+    SPBox3D *box = SP_BOX3D(item);
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+
+    box3d_set_center (SP_BOX3D(item), s * i2d, origin * i2d, !(state & GDK_SHIFT_MASK) ? Box3D::XY : Box3D::Z,
+                      state & GDK_CONTROL_MASK);
+
+    box3d_set_z_orders(box);
+    box3d_position_set(box);
+}
+
+static void box3d_knot0_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 0, new_pos, origin, state);
+}
+
+static void box3d_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 1, new_pos, origin, state);
+}
+
+static void box3d_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 2, new_pos, origin, state);
+}
+
+static void box3d_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 3, new_pos, origin, state);
+}
+
+static void box3d_knot4_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 4, new_pos, origin, state);
+}
+
+static void box3d_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 5, new_pos, origin, state);
+}
+
+static void box3d_knot6_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 6, new_pos, origin, state);
+}
+
+static void box3d_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
+{
+    box3d_knot_set(item, 7, new_pos, origin, state);
+}
+
+static NR::Point box3d_knot0_get(SPItem *item)
+{
+    return box3d_knot_get(item, 0);
+}
+
+static NR::Point box3d_knot1_get(SPItem *item)
+{
+    return box3d_knot_get(item, 1);
+}
+
+static NR::Point box3d_knot2_get(SPItem *item)
+{
+    return box3d_knot_get(item, 2);
+}
+
+static NR::Point box3d_knot3_get(SPItem *item)
+{
+    return box3d_knot_get(item, 3);
+}
+
+static NR::Point box3d_knot4_get(SPItem *item)
+{
+    return box3d_knot_get(item, 4);
+}
+
+static NR::Point box3d_knot5_get(SPItem *item)
+{
+    return box3d_knot_get(item, 5);
+}
+
+static NR::Point box3d_knot6_get(SPItem *item)
+{
+    return box3d_knot_get(item, 6);
+}
+
+static NR::Point box3d_knot7_get(SPItem *item)
+{
+    return box3d_knot_get(item, 7);
+}
+
+SPKnotHolder *
+box3d_knot_holder(SPItem *item, SPDesktop *desktop)
+{
+    g_assert(item != NULL);
+    SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
+
+    sp_knot_holder_add(knot_holder, box3d_knot0_set, box3d_knot0_get, NULL,
+                       _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot1_set, box3d_knot1_get, NULL,
+                       _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot2_set, box3d_knot2_get, NULL,
+                       _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot3_set, box3d_knot3_get, NULL,
+                       _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot4_set, box3d_knot4_get, NULL,
+                       _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot5_set, box3d_knot5_get, NULL,
+                       _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot6_set, box3d_knot6_get, NULL,
+                       _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+    sp_knot_holder_add(knot_holder, box3d_knot7_set, box3d_knot7_get, NULL,
+                       _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
+
+    // center dragging
+    sp_knot_holder_add_full(knot_holder, box3d_knot_center_set, box3d_knot_center_get, NULL,
+                            SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,_("Move the box in perspective"));
+
+    sp_pat_knot_holder(item, knot_holder);
+
+    return knot_holder;
+}
+
+
+
 /* SPArc */
 
 /*
@@ -533,7 +707,7 @@ sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
 }
 
 static void
-sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
 
@@ -563,7 +737,7 @@ static NR::Point sp_arc_start_get(SPItem *item)
 }
 
 static void
-sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
 
@@ -605,12 +779,14 @@ sp_arc_startend_click(SPItem *item, guint state)
 
 
 static void
-sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
     SPArc *arc = SP_ARC(item);
 
-    ge->rx.computed = fabs( ge->cx.computed - p[NR::X] );
+    NR::Point const s = snap_knot_position(arc, p);
+
+    ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
 
     if ( state & GDK_CONTROL_MASK ) {
         ge->ry.computed = ge->rx.computed;
@@ -627,12 +803,14 @@ static NR::Point sp_arc_rx_get(SPItem *item)
 }
 
 static void
-sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
     SPArc *arc = SP_ARC(item);
 
-    ge->ry.computed = fabs( ge->cy.computed - p[NR::Y] );
+    NR::Point const s = snap_knot_position(arc, p);
+
+    ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
 
     if ( state & GDK_CONTROL_MASK ) {
         ge->rx.computed = ge->ry.computed;
@@ -696,11 +874,13 @@ sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
 /* SPStar */
 
 static void
-sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     SPStar *star = SP_STAR(item);
 
-    NR::Point d = p - star->center;
+    NR::Point const s = snap_knot_position(star, p);
+
+    NR::Point d = s - star->center;
 
     double arg1 = atan2(d);
     double darg1 = arg1 - star->arg[0];
@@ -720,11 +900,14 @@ sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, gui
 }
 
 static void
-sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+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];
@@ -810,7 +993,7 @@ sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
  *   [control] constrain inner arg to round per PI/4
  */
 static void
-sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
 
@@ -852,7 +1035,7 @@ sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, g
  *   [control] constrain inner arg to round per PI/4
  */
 static void
-sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint state)
 {
     int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
 
@@ -970,7 +1153,7 @@ sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
 /* SPOffset */
 
 static void
-sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
+sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
 {
     SPOffset *offset = SP_OFFSET(item);
 
@@ -1005,9 +1188,9 @@ 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)
+    if ((SP_OBJECT(item)->style->fill.isPaintserver())
         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
     {
         SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
@@ -1022,7 +1205,7 @@ sp_path_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, inste
 static void
 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
 {
-    if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
+    if ((SP_OBJECT(item)->style->fill.isPaintserver())
         && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
     {
         sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,