X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fpoly_item.cc;h=855140856eb0f3ccdb60cbcd0d2f93870d67d22c;hb=a9c09af816b3d7da40221ac4a2bb4c6074708d89;hp=d6e67ede0c47b5094fbc929753f0dbec5d94b53e;hpb=9a8ee11f6532c73320f76b10aabec68081c9ebf4;p=ardour.git diff --git a/libs/canvas/poly_item.cc b/libs/canvas/poly_item.cc index d6e67ede0c..855140856e 100644 --- a/libs/canvas/poly_item.cc +++ b/libs/canvas/poly_item.cc @@ -1,6 +1,24 @@ +/* + 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 "pbd/xml++.h" #include "pbd/compose.h" #include "canvas/poly_item.h" @@ -9,99 +27,124 @@ using namespace std; using namespace ArdourCanvas; -PolyItem::PolyItem (Group* parent) - : Item (parent) - , Outline (parent) +PolyItem::PolyItem (Canvas* c) + : Item (c) { +} +PolyItem::PolyItem (Item* parent) + : Item (parent) +{ } void PolyItem::compute_bounding_box () const { - bool have_one = false; - Rect bbox; + if (!_points.empty()) { - for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) { - if (have_one) { + Rect bbox; + Points::const_iterator i = _points.begin(); + + bbox.x0 = bbox.x1 = i->x; + bbox.y0 = bbox.y1 = i->y; + + ++i; + + while (i != _points.end()) { bbox.x0 = min (bbox.x0, i->x); bbox.y0 = min (bbox.y0, i->y); bbox.x1 = max (bbox.x1, i->x); bbox.y1 = max (bbox.y1, i->y); - } else { - bbox.x0 = bbox.x1 = i->x; - bbox.y0 = bbox.y1 = i->y; - have_one = true; + ++i; } - } + + _bounding_box = bbox.expand (_outline_width + 0.5); - if (!have_one) { - _bounding_box = boost::optional (); } else { - _bounding_box = bbox.expand (_outline_width / 2); + _bounding_box = boost::optional (); } - + _bounding_box_dirty = false; } void -PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr context) const +PolyItem::render_path (Rect const & /* area */, Cairo::RefPtr context) const { - bool done_first = false; - for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) { - if (done_first) { - context->line_to (i->x, i->y); - } else { - context->move_to (i->x, i->y); - done_first = true; - } + if (_points.size() < 2) { + return; } -} -void -PolyItem::set (Points const & points) -{ - begin_change (); - - _points = points; - - _bounding_box_dirty = true; - end_change (); -} + Points::const_iterator i = _points.begin(); + Duple c (item_to_window (Duple (i->x, i->y))); + const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0); -Points const & -PolyItem::get () const -{ - return _points; + context->move_to (c.x + pixel_adjust, c.y + pixel_adjust); + ++i; + + while (i != _points.end()) { + c = item_to_window (Duple (i->x, i->y)); + context->line_to (c.x + pixel_adjust, c.y + pixel_adjust); + ++i; + } } void -PolyItem::add_poly_item_state (XMLNode* node) const +PolyItem::render_curve (Rect const & area, Cairo::RefPtr context, Points const & first_control_points, Points const & second_control_points) const { - add_item_state (node); - - for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) { - XMLNode* p = new XMLNode ("Point"); - p->add_property ("x", string_compose ("%1", i->x)); - p->add_property ("y", string_compose ("%1", i->y)); - node->add_child_nocopy (*p); + if (_points.size() <= 2) { + render_path (area, context); + return; + } + + Points::const_iterator cp1 = first_control_points.begin(); + Points::const_iterator cp2 = second_control_points.begin(); + Points::const_iterator p = _points.begin(); + const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0); + + Duple c = item_to_window (Duple (p->x, p->y)); + context->move_to (c.x + pixel_adjust, c.y + pixel_adjust); + ++p; + + while (p != _points.end()) { + + Duple c1 = item_to_window (Duple (cp1->x, cp1->y)); + Duple c2 = item_to_window (Duple (cp2->x, cp2->y)); + + c = item_to_window (Duple (p->x, p->y)); + + context->curve_to (c1.x + pixel_adjust, + c1.y + pixel_adjust, + c2.x + pixel_adjust, + c2.y + pixel_adjust, + c.x + pixel_adjust, + c.y + pixel_adjust); + + ++cp1; + ++cp2; + ++p; } } void -PolyItem::set_poly_item_state (XMLNode const * node) +PolyItem::set (Points const & points) { - XMLNodeList const & children = node->children (); - for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) { - Duple p; - p.x = atof ((*i)->property("x")->value().c_str()); - p.y = atof ((*i)->property("y")->value().c_str()); - _points.push_back (p); + if (_points != points) { + + begin_change (); + + _points = points; + + _bounding_box_dirty = true; + end_change (); } +} - _bounding_box_dirty = true; +Points const & +PolyItem::get () const +{ + return _points; } void