X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fline.cc;h=49d2987e3d6b1c4a9adb5a59198ffcc25cd1e1a0;hb=c8c6bca6587450ff64303dbc994a4cd28d6ce7aa;hp=b6a802b8df7ce4fce9977ecdca36f8f87107563b;hpb=77f5f4c4bf9074d953a1653658c8f96f38ae258c;p=ardour.git diff --git a/libs/canvas/line.cc b/libs/canvas/line.cc index b6a802b8df..49d2987e3d 100644 --- a/libs/canvas/line.cc +++ b/libs/canvas/line.cc @@ -29,18 +29,21 @@ using namespace std; using namespace ArdourCanvas; -Line::Line (Group* parent) - : Item (parent) - , Outline (parent) +Line::Line (Canvas* c) + : Item (c) { +} +Line::Line (Item* parent) + : Item (parent) +{ } void Line::compute_bounding_box () const { Rect bbox; - + bbox.x0 = min (_points[0].x, _points[1].x); bbox.y0 = min (_points[0].y, _points[1].y); bbox.x1 = max (_points[0].x, _points[1].x); @@ -56,8 +59,19 @@ void Line::render (Rect const & /*area*/, Cairo::RefPtr context) const { setup_outline_context (context); + Duple p0 = item_to_window (Duple (_points[0].x, _points[0].y)); Duple p1 = item_to_window (Duple (_points[1].x, _points[1].y)); + + if (_outline_width <= 1.0) { + /* See Cairo FAQ on single pixel lines to understand why we add 0.5 + */ + + const Duple half_a_pixel (0.5, 0.5); + p0 = p0.translate (half_a_pixel); + p1 = p1.translate (half_a_pixel); + } + context->move_to (p0.x, p0.y); context->line_to (p1.x, p1.y); context->stroke (); @@ -66,53 +80,55 @@ Line::render (Rect const & /*area*/, Cairo::RefPtr context) cons void Line::set (Duple a, Duple b) { - begin_change (); - - _points[0] = a; - _points[1] = b; + if (a != _points[0] || b != _points[1]) { + begin_change (); - _bounding_box_dirty = true; - end_change (); + _points[0] = a; + _points[1] = b; - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); + _bounding_box_dirty = true; + end_change (); + } } void Line::set_x (Coord x0, Coord x1) { - begin_change (); - - _points[0].x = x0; - _points[1].x = x1; + if (x0 != _points[0].x || x1 != _points[1].x) { + begin_change (); - _bounding_box_dirty = true; - end_change (); + _points[0].x = x0; + _points[1].x = x1; - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); -} + _bounding_box_dirty = true; + end_change (); + } +} void Line::set_x0 (Coord x0) { - begin_change (); - - _points[0].x = x0; + if (x0 != _points[0].x) { + begin_change (); - _bounding_box_dirty = true; - end_change (); + _points[0].x = x0; - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); + _bounding_box_dirty = true; + end_change (); + } } void Line::set_y0 (Coord y0) { - begin_change (); + if (y0 != _points[0].y) { + begin_change (); - _points[0].y = y0; + _points[0].y = y0; - _bounding_box_dirty = true; - end_change (); + _bounding_box_dirty = true; + end_change (); + } DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); } @@ -120,25 +136,75 @@ Line::set_y0 (Coord y0) void Line::set_x1 (Coord x1) { - begin_change (); - - _points[1].x = x1; + if (x1 != _points[1].x) { + begin_change (); - _bounding_box_dirty = true; - end_change (); + _points[1].x = x1; - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); + _bounding_box_dirty = true; + end_change (); + } } void Line::set_y1 (Coord y1) { - begin_change (); + if (y1 != _points[1].y) { + begin_change (); - _points[1].y = y1; + _points[1].y = y1; - _bounding_box_dirty = true; - end_change (); + _bounding_box_dirty = true; + end_change (); + } +} - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); +bool +Line::covers (Duple const & point) const +{ + const Duple p = window_to_item (point); + static const Distance threshold = 2.0; + + /* this quick check works for vertical and horizontal lines, which are + * common. + */ + + if (_points[0].x == _points[1].x) { + /* line is vertical, just check x coordinate */ + return fabs (_points[0].x - p.x) <= threshold; + } + + if (_points[0].y == _points[1].y) { + /* line is horizontal, just check y coordinate */ + return fabs (_points[0].y - p.y) <= threshold; + } + + Duple at; + double t; + Duple a (_points[0]); + Duple b (_points[1]); + const Rect visible (window_to_item (_canvas->visible_area())); + + /* + Clamp the line endpoints to the visible area of the canvas. If we do + not do this, we have a line segment extending to COORD_MAX and our + math goes wrong. + */ + + a.x = min (a.x, visible.x1); + a.y = min (a.y, visible.y1); + b.x = min (b.x, visible.x1); + b.y = min (b.y, visible.y1); + + double d = distance_to_segment_squared (p, a, b, t, at); + + if (t < 0.0 || t > 1.0) { + return false; + } + + if (d < threshold) { + return true; + } + + return false; }