Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / pen-context.cpp
index 6074eaa4b83e42c7e6b6e01692d8258ee3cce805..30b09013b09f770a37fa83d8d72d61dcd8c8895d 100644 (file)
@@ -37,7 +37,6 @@
 #include "display/sp-ctrlline.h"
 #include "display/sodipodi-ctrl.h"
 #include <glibmm/i18n.h>
-#include "libnr/n-art-bpath.h"
 #include "libnr/nr-point-ops.h"
 #include "helper/units.h"
 #include "macros.h"
@@ -54,7 +53,7 @@ static gint sp_pen_context_root_handler(SPEventContext *ec, GdkEvent *event);
 static gint sp_pen_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
 
 static void spdc_pen_set_initial_point(SPPenContext *pc, NR::Point const p);
-static void spdc_pen_set_subsequent_point(SPPenContext *pc, NR::Point const p, bool statusbar);
+static void spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status = 0);
 static void spdc_pen_set_ctrl(SPPenContext *pc, NR::Point const p, guint state);
 static void spdc_pen_finish_segment(SPPenContext *pc, NR::Point p, guint state);
 
@@ -75,6 +74,12 @@ static bool pen_within_tolerance = false;
 
 static SPDrawContextClass *pen_parent_class;
 
+static int pen_next_paraxial_direction(const SPPenContext *const pc, NR::Point const &pt, NR::Point const &origin, guint state);
+static void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state);
+static NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt, guint const state);
+
+static int pen_last_paraxial_dir = 0; // last used direction in horizontal/vertical mode; 0 = horizontal, 1 = vertical
+
 /**
  * Register SPPenContext with Gdk and return its type.
  */
@@ -145,7 +150,7 @@ sp_pen_context_init(SPPenContext *pc)
     
     pc->events_disabled = 0;
 
-    pc->polylines_only = false;
+    pc->num_clicks = 0;
     pc->waiting_LPE = NULL;
 }
 
@@ -178,14 +183,19 @@ sp_pen_context_dispose(GObject *object)
 
     G_OBJECT_CLASS(pen_parent_class)->dispose(object);
 
-    pc->polylines_only = false;
-    pc->waiting_LPE = NULL;
     if (pc->expecting_clicks_for_LPE > 0) {
         // we received too few clicks to sanely set the parameter path so we remove the LPE from the item
         sp_lpe_item_remove_current_path_effect(pc->waiting_item, false);
     }
 }
 
+void
+sp_pen_context_set_polyline_mode(SPPenContext *const pc) {
+    guint mode = prefs_get_int_attribute("tools.freehand.pen", "freehand-mode", 0);
+    pc->polylines_only = (mode == 2 || mode == 3);
+    pc->polylines_paraxial = (mode == 3);
+}
+
 /**
  * Callback to initialize SPPenContext object.
  */
@@ -219,6 +229,8 @@ sp_pen_context_setup(SPEventContext *ec)
 
     pc->anchor_statusbar = false;
 
+    sp_pen_context_set_polyline_mode(pc);
+
     if (prefs_get_int_attribute("tools.freehand.pen", "selcue", 0) != 0) {
         ec->enableSelectionCue();
     }
@@ -227,6 +239,7 @@ sp_pen_context_setup(SPEventContext *ec)
 static void
 pen_cancel (SPPenContext *const pc) 
 {
+    pc->num_clicks = 0;
     pc->state = SP_PEN_CONTEXT_STOP;
     spdc_reset_colors(pc);
     sp_canvas_item_hide(pc->c0);
@@ -279,11 +292,21 @@ sp_pen_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
 static void
 spdc_endpoint_snap(SPPenContext const *const pc, NR::Point &p, guint const state)
 {
-    if (pc->npoints > 0) {
-        spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
+    if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
+        if (pc->npoints > 0) {
+            spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
+        }
+    } else {
+        if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
+                                         //After all, the user explicitely asked for angular snapping by
+                                         //pressing CTRL
+            spdc_endpoint_snap_free(pc, p, state);
+        }
+    }
+    if (pc->polylines_paraxial) {
+        // TODO: must we avoid one of the snaps in the previous case distinction in some situations?
+        pen_set_to_nearest_horiz_vert(pc, p, state);
     }
-
-    spdc_endpoint_snap_free(pc, p, state);
 }
 
 /**
@@ -295,8 +318,13 @@ spdc_endpoint_snap_handle(SPPenContext const *const pc, NR::Point &p, guint cons
     g_return_if_fail(( pc->npoints == 2 ||
                        pc->npoints == 5   ));
 
-    spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
-    spdc_endpoint_snap_free(pc, p, state);
+    if ((state & GDK_CONTROL_MASK)) { //CTRL enables angular snapping
+        spdc_endpoint_snap_rotation(pc, p, pc->p[pc->npoints - 2], state);
+    } else {
+        if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
+            spdc_endpoint_snap_free(pc, p, state);
+        }
+    }
 }
 
 static gint 
@@ -310,6 +338,9 @@ sp_pen_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
         case GDK_BUTTON_PRESS:
             ret = pen_handle_button_press(pc, event->button);
             break;
+        case GDK_BUTTON_RELEASE:
+            ret = pen_handle_button_release(pc, event->button);
+            break;
         default:
             break;
     }
@@ -386,13 +417,22 @@ static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const
 
     gint ret = FALSE;
     if (bevent.button == 1 && !event_context->space_panning
-        // when the last click for a waiting LPE occurs we want to finish the path
+        // make sure this is not the last click for a waiting LPE (otherwise we want to finish the path)
         && pc->expecting_clicks_for_LPE != 1) {
 
         if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
             return TRUE;
         }
 
+        if (!pc->grab ) {
+            /* Grab mouse, so release will not pass unnoticed */
+            pc->grab = SP_CANVAS_ITEM(desktop->acetate);
+            sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
+                                            GDK_BUTTON_RELEASE_MASK |
+                                            GDK_POINTER_MOTION_MASK  ),
+                                NULL, bevent.time);
+        }
+
         pen_drag_origin_w = event_w;
         pen_within_tolerance = true;
 
@@ -455,7 +495,11 @@ static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const
 
                                 /* Create green anchor */
                                 p = event_dt;
-                                spdc_endpoint_snap(pc, p, bevent.state);
+                                if (!pc->polylines_paraxial) {
+                                    // only snap the starting point if we're not in horizontal/vertical mode
+                                    // because otherwise it gets shifted; TODO: why do we snap here at all??
+                                    spdc_endpoint_snap(pc, p, bevent.state);
+                                }
                                 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, p);
                             }
                             spdc_pen_set_initial_point(pc, p);
@@ -465,7 +509,6 @@ static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const
                             pc->ea = anchor;
                             NR::Point p;
                             if (anchor) {
-
                                 p = anchor->dp;
                                 // we hit an anchor, will finish the curve (either with or without closing)
                                 // in release handler
@@ -480,7 +523,6 @@ static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const
                                 break;
 
                             } else {
-
                                 p = event_dt;
                                 spdc_endpoint_snap(pc, p, bevent.state); /* Snap node only if not hitting anchor. */
                                 spdc_pen_set_subsequent_point(pc, p, true);
@@ -488,8 +530,8 @@ static gint pen_handle_button_press(SPPenContext *const pc, GdkEventButton const
                                     spdc_pen_finish_segment(pc, p, bevent.state);
                                 }
                             }
-
                         }
+
                         pc->state = pc->polylines_only ? SP_PEN_CONTEXT_POINT : SP_PEN_CONTEXT_CONTROL;
                         ret = TRUE;
                         break;
@@ -538,12 +580,13 @@ pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
     gint ret = FALSE;
 
     SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
+    SPDesktop * const dt = SP_EVENT_CONTEXT_DESKTOP(event_context);
 
     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
         // allow scrolling
         return FALSE;
     }
-    
+   
     if (pc->events_disabled) {
         // skip motion events if pen events are disabled
         return FALSE;
@@ -563,16 +606,6 @@ pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
     // motion notify coordinates as given (no snapping back to origin)
     pen_within_tolerance = false;
 
-    SPDesktop *const dt = pc->desktop;
-    if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab ) {
-        /* Grab mouse, so release will not pass unnoticed */
-        pc->grab = SP_CANVAS_ITEM(dt->acetate);
-        sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK   |
-                                        GDK_BUTTON_RELEASE_MASK |
-                                        GDK_POINTER_MOTION_MASK  ),
-                            NULL, mevent.time);
-    }
-
     /* Find desktop coordinates */
     NR::Point p = dt->w2d(event_w);
 
@@ -614,7 +647,7 @@ pen_handle_motion_notify(SPPenContext *const pc, GdkEventMotion const &mevent)
                             spdc_endpoint_snap(pc, p, mevent.state);
                         }
 
-                        spdc_pen_set_subsequent_point(pc, p, !anchor);
+                        spdc_pen_set_subsequent_point(pc, p, !anchor, mevent.state);
 
                         if (anchor && !pc->anchor_statusbar) {
                             pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to close and finish the path."));
@@ -782,7 +815,7 @@ pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
     // TODO: can we be sure that the path was created correctly?
     // TODO: should we offer an option to collect the clicks in a list?
     if (pc->expecting_clicks_for_LPE == 0 && sp_pen_context_has_waiting_LPE(pc)) {
-        pc->polylines_only = false;
+        sp_pen_context_set_polyline_mode(pc);
 
         SPEventContext *ec = SP_EVENT_CONTEXT(pc);
         Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
@@ -792,7 +825,6 @@ pen_handle_button_release(SPPenContext *const pc, GdkEventButton const &revent)
             pc->waiting_LPE->acceptParamPath(SP_PATH(selection->singleItem()));
             selection->add(SP_OBJECT(pc->waiting_item));
             pc->waiting_LPE = NULL;
-            pc->polylines_only = false;
         } else {
             // the case that we need to create a new LPE and apply it to the just-drawn path is
             // handled in spdc_check_for_and_apply_waiting_LPE() in draw-context.cpp
@@ -850,11 +882,15 @@ pen_redraw_all (SPPenContext *const pc)
         sp_canvas_item_hide (pc->cl1);
     }
 
-    NArtBpath const * bpath = pc->green_curve->last_bpath();
-    if (bpath) {
-        if (bpath->code == NR_CURVETO && NR::Point(bpath->x2, bpath->y2) != pc->p[0]) {
-            SP_CTRL(pc->c0)->moveto(NR::Point(bpath->x2, bpath->y2));
-            sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), NR::Point(bpath->x2, bpath->y2), pc->p[0]);
+    Geom::Curve const * last_seg = pc->green_curve->last_segment();
+    if (last_seg) {
+        Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( last_seg );
+        if ( cubic &&
+             (*cubic)[2] != to_2geom(pc->p[0]) )
+        {
+            NR::Point p2 = (*cubic)[2];
+            SP_CTRL(pc->c0)->moveto(p2);
+            sp_ctrlline_set_coords(SP_CTRLLINE(pc->cl0), p2, pc->p[0]);
             sp_canvas_item_show (pc->c0);
             sp_canvas_item_show (pc->cl0);
         } else {
@@ -871,8 +907,7 @@ pen_lastpoint_move (SPPenContext *const pc, gdouble x, gdouble y)
         return;
 
     // green
-    NArtBpath const * bpath = pc->green_curve->last_bpath();
-    if (bpath) {
+    if (!pc->green_curve->is_empty()) {
         pc->green_curve->last_point_additive_move( Geom::Point(x,y) );
     } else {
         // start anchor too
@@ -899,10 +934,9 @@ pen_lastpoint_tocurve (SPPenContext *const pc)
     if (pc->npoints != 5)
         return;
 
-    // red
-    NArtBpath const * bpath = pc->green_curve->last_bpath();
-    if (bpath && bpath->code == NR_CURVETO) {
-        pc->p[1] = pc->p[0] + (NR::Point(bpath->x3, bpath->y3) - NR::Point(bpath->x2, bpath->y2));
+    Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>( pc->green_curve->last_segment() );
+    if ( cubic ) {
+        pc->p[1] = pc->p[0] + (NR::Point)( (*cubic)[3] - (*cubic)[2] );
     } else {
         pc->p[1] = pc->p[0] + (1./3)*(pc->p[3] - pc->p[0]);
     }
@@ -994,8 +1028,7 @@ pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
         case GDK_P:
         case GDK_p:
             if (MOD__SHIFT_ONLY) {
-                pc->polylines_only = !pc->polylines_only;
-                g_print ("polylines_only mode is now %s\n", pc->polylines_only ? "true" : "false");
+                sp_pen_context_wait_for_LPE_mouse_clicks(pc, Inkscape::LivePathEffect::PARALLEL, 2);
                 ret = TRUE;
             }
             break;
@@ -1088,20 +1121,21 @@ pen_handle_key_press(SPPenContext *const pc, GdkEvent *event)
                     pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
                 }
                 /* Get last segment */
-                NArtBpath const *const p = SP_CURVE_BPATH(pc->green_curve);
-                gint const e = SP_CURVE_LENGTH(pc->green_curve);
-                if ( e < 2 ) {
-                    g_warning("Green curve length is %d", e);
+                if ( pc->green_curve->is_empty() ) {
+                    g_warning("pen_handle_key_press, case GDK_KP_Delete: Green curve is empty");
                     break;
                 }
-                pc->p[0] = p[e - 2].c(3);
-                if (p[e - 1].code == NR_CURVETO) {
-                    pc->p[1] = p[e - 1].c(1);
+                // The code below assumes that pc->green_curve has only ONE path !
+                Geom::Path const & path = pc->green_curve->get_pathvector().back();
+                Geom::Curve const * crv = &path.back_default();
+                pc->p[0] = crv->initialPoint();
+                if ( Geom::CubicBezier const * cubic = dynamic_cast<Geom::CubicBezier const *>(crv)) {
+                    pc->p[1] = (*cubic)[1];
                 } else {
                     pc->p[1] = pc->p[0];
                 }
                 NR::Point const pt(( pc->npoints < 4
-                                     ? p[e - 1].c(3)
+                                     ? (NR::Point)(crv->finalPoint())
                                      : pc->p[3] ));
                 pc->npoints = 2;
                 pc->green_curve->backspace();
@@ -1182,7 +1216,7 @@ spdc_pen_set_angle_distance_status_message(SPPenContext *const pc, NR::Point con
 }
 
 static void
-spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar)
+spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool statusbar, guint status)
 {
     g_assert( pc->npoints != 0 );
     /* todo: Check callers to see whether 2 <= npoints is guaranteed. */
@@ -1192,15 +1226,25 @@ spdc_pen_set_subsequent_point(SPPenContext *const pc, NR::Point const p, bool st
     pc->p[4] = p;
     pc->npoints = 5;
     pc->red_curve->reset();
-    pc->red_curve->moveto(pc->p[0]);
     bool is_curve;
-    if (pc->p[1] != pc->p[0])
-    {
-        pc->red_curve->curveto(pc->p[1], p, p);
-        is_curve = true;
-    } else {
+    pc->red_curve->moveto(pc->p[0]);
+    if (pc->polylines_paraxial && !statusbar) {
+        // we are drawing horizontal/vertical lines and hit an anchor; draw an L-shaped path
+        NR::Point intermed = p;
+        pen_set_to_nearest_horiz_vert(pc, intermed, status);
+        pc->red_curve->lineto(intermed);
         pc->red_curve->lineto(p);
         is_curve = false;
+    } else {
+        // one of the 'regular' modes
+        if (pc->p[1] != pc->p[0])
+        {
+            pc->red_curve->curveto(pc->p[1], p, p);
+            is_curve = true;
+        } else {
+            pc->red_curve->lineto(p);
+            is_curve = false;
+        }
     }
 
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
@@ -1257,8 +1301,13 @@ spdc_pen_set_ctrl(SPPenContext *const pc, NR::Point const p, guint const state)
 }
 
 static void
-spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const /*p*/, guint const /*state*/)
+spdc_pen_finish_segment(SPPenContext *const pc, NR::Point const p, guint const state)
 {
+    if (pc->polylines_paraxial) {
+        pen_last_paraxial_dir = pen_next_paraxial_direction(pc, p, pc->p[0], state);
+    }
+    ++pc->num_clicks;
+
     if (!pc->red_curve->is_empty()) {
         pc->green_curve->append_continuous(pc->red_curve, 0.0625);
         SPCurve *curve = pc->red_curve->copy();
@@ -1285,6 +1334,8 @@ spdc_pen_finish(SPPenContext *const pc, gboolean const closed)
         return;
     }
 
+    pc->num_clicks = 0;
+
     pen_disable_events(pc);
     
     SPDesktop *const desktop = pc->desktop;
@@ -1334,9 +1385,56 @@ sp_pen_context_wait_for_LPE_mouse_clicks(SPPenContext *pc, Inkscape::LivePathEff
              Inkscape::LivePathEffect::LPETypeConverter.get_label(effect_type).c_str());
     pc->expecting_clicks_for_LPE = num_clicks;
     pc->polylines_only = use_polylines;
+    pc->polylines_paraxial = false; // TODO: think if this is correct for all cases
     pc->waiting_LPE_type = effect_type;
 }
 
+static int pen_next_paraxial_direction(const SPPenContext *const pc,
+                                       NR::Point const &pt, NR::Point const &origin, guint state) {
+    /*
+     * after the first mouse click we determine whether the mouse pointer is closest to a
+     * horizontal or vertical segment; for all subsequent mouse clicks, we use the direction
+     * orthogonal to the last one; pressing Shift toggles the direction
+     */
+    if (pc->num_clicks == 0) {
+        // first mouse click
+        double dist_h = fabs(pt[NR::X] - origin[NR::X]);
+        double dist_v = fabs(pt[NR::Y] - origin[NR::Y]);
+        int ret = (dist_h < dist_v) ? 1 : 0; // 0 = horizontal, 1 = vertical
+        pen_last_paraxial_dir = (state & GDK_SHIFT_MASK) ? 1 - ret : ret;
+        return pen_last_paraxial_dir;
+    } else {
+        // subsequent mouse click
+        return (state & GDK_SHIFT_MASK) ? pen_last_paraxial_dir : 1 - pen_last_paraxial_dir;
+    }
+}
+
+void pen_set_to_nearest_horiz_vert(const SPPenContext *const pc, NR::Point &pt, guint const state)
+{
+    NR::Point const &origin = pc->p[0];
+
+    int next_dir = pen_next_paraxial_direction(pc, pt, origin, state);
+
+    if (next_dir == 0) {
+        // line is forced to be horizontal
+        pt[NR::Y] = origin[NR::Y];
+    } else {
+        // line is forced to be vertical
+        pt[NR::X] = origin[NR::X];
+    }
+}
+
+NR::Point pen_get_intermediate_horiz_vert(const SPPenContext *const pc, NR::Point const &pt)
+{
+    NR::Point const &origin = pc->p[0];
+
+    if (pen_last_paraxial_dir == 0) {
+        return NR::Point (origin[NR::X], pt[NR::Y]);
+    } else {
+        return NR::Point (pt[NR::X], origin[NR::Y]);
+    }
+}
+
 /*
   Local Variables:
   mode:c++