X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fline.cc;h=8bd26b90678dfc49626169acbb7cab5b3fa44ea7;hb=e91a0f7e11d33694d693df443c9cff4644c751b9;hp=6c3a62e5dcae029aec470a392317132c73819ad7;hpb=bcd65def7da197a6f88eb1c4adb375f2b639f783;p=ardour.git diff --git a/libs/canvas/line.cc b/libs/canvas/line.cc index 6c3a62e5dc..8bd26b9067 100644 --- a/libs/canvas/line.cc +++ b/libs/canvas/line.cc @@ -1,20 +1,42 @@ +/* + Copyright (C) 2011-2013 Paul Davis + Author: Carl Hetherington + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + #include #include -#include "pbd/xml++.h" #include "pbd/compose.h" #include "canvas/line.h" #include "canvas/types.h" #include "canvas/debug.h" #include "canvas/utils.h" +#include "canvas/canvas.h" 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 @@ -27,7 +49,7 @@ Line::compute_bounding_box () const bbox.x1 = max (_points[0].x, _points[1].x); bbox.y1 = max (_points[0].y, _points[1].y); - bbox = bbox.expand (_outline_width / 2); + bbox = bbox.expand (0.5 + (_outline_width / 2)); _bounding_box = bbox; _bounding_box_dirty = false; @@ -38,52 +60,75 @@ Line::render (Rect const & /*area*/, Cairo::RefPtr context) cons { setup_outline_context (context); - Duple plot[2] = { - Duple (min (_points[0].x, CAIRO_MAX), min (_points[0].y, CAIRO_MAX)), - Duple (min (_points[1].x, CAIRO_MAX), min (_points[1].y, CAIRO_MAX)) - }; + 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 (plot[0].x, plot[0].y); - context->line_to (plot[1].x, plot[1].y); + context->move_to (p0.x, p0.y); + context->line_to (p1.x, p1.y); context->stroke (); } void Line::set (Duple a, Duple b) { - begin_change (); - - _points[0] = a; - _points[1] = b; - - _bounding_box_dirty = true; - end_change (); - - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); + if (a != _points[0] || b != _points[1]) { + begin_change (); + + _points[0] = a; + _points[1] = b; + + _bounding_box_dirty = true; + end_change (); + } } void -Line::set_x0 (Coord x0) +Line::set_x (Coord x0, Coord x1) { - begin_change (); - - _points[0].x = x0; - - _bounding_box_dirty = true; - end_change (); + if (x0 != _points[0].x || x1 != _points[1].x) { + begin_change (); + + _points[0].x = x0; + _points[1].x = x1; + + _bounding_box_dirty = true; + end_change (); + } +} - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); +void +Line::set_x0 (Coord x0) +{ + if (x0 != _points[0].x) { + begin_change (); + + _points[0].x = x0; + + _bounding_box_dirty = true; + end_change (); + } } void Line::set_y0 (Coord y0) { - begin_change (); - - _points[0].y = y0; - - _bounding_box_dirty = true; - end_change (); + if (y0 != _points[0].y) { + begin_change (); + + _points[0].y = y0; + + _bounding_box_dirty = true; + end_change (); + } DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); } @@ -91,58 +136,75 @@ Line::set_y0 (Coord y0) void Line::set_x1 (Coord x1) { - begin_change (); - - _points[1].x = x1; - - _bounding_box_dirty = true; - end_change (); - - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); + if (x1 != _points[1].x) { + begin_change (); + + _points[1].x = x1; + + _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; + + _bounding_box_dirty = true; + end_change (); + } +} - _points[1].y = y1; +bool +Line::covers (Duple const & point) const +{ + const Duple p = window_to_item (point); + static const Distance threshold = 2.0; - _bounding_box_dirty = true; - end_change (); + /* this quick check works for vertical and horizontal lines, which are + * common. + */ - DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n"); -} + if (_points[0].x == _points[1].x) { + /* line is vertical, just check x coordinate */ + return fabs (_points[0].x - p.x) <= threshold; + } -XMLNode * -Line::get_state () const -{ - XMLNode* node = new XMLNode ("Line"); -#ifdef CANVAS_DEBUG - if (!name.empty ()) { - node->add_property ("name", name); + if (_points[0].y == _points[1].y) { + /* line is horizontal, just check y coordinate */ + return fabs (_points[0].y - p.y) <= threshold; } -#endif - node->add_property ("x0", string_compose ("%1", _points[0].x)); - node->add_property ("y0", string_compose ("%1", _points[0].y)); - node->add_property ("x1", string_compose ("%1", _points[1].x)); - node->add_property ("y1", string_compose ("%1", _points[1].y)); - - add_item_state (node); - add_outline_state (node); - return node; -} -void -Line::set_state (XMLNode const * node) -{ - _points[0].x = atof (node->property("x0")->value().c_str()); - _points[0].y = atof (node->property("y0")->value().c_str()); - _points[1].x = atof (node->property("x1")->value().c_str()); - _points[1].y = atof (node->property("y1")->value().c_str()); + Duple at; + double t; + Duple a (_points[0]); + Duple b (_points[1]); + const Rect visible (window_to_item (_canvas->visible_area())); - set_item_state (node); - set_outline_state (node); + /* + 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; + } - _bounding_box_dirty = true; + return false; }