2013-04-15 22:10:18 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-04-15 22:10:18 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include <algorithm>
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "pbd/compose.h"
|
2013-04-05 11:27:26 -04:00
|
|
|
|
|
|
|
#include "canvas/canvas.h"
|
2020-04-10 11:57:18 -04:00
|
|
|
#include "canvas/poly_item.h"
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
PolyItem::PolyItem (Canvas* c)
|
|
|
|
: Item (c)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2014-06-12 14:53:44 -04:00
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
PolyItem::PolyItem (Item* parent)
|
|
|
|
: Item (parent)
|
2014-06-12 14:53:44 -04:00
|
|
|
{
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PolyItem::compute_bounding_box () const
|
|
|
|
{
|
2020-04-10 11:57:18 -04:00
|
|
|
if (!_points.empty ()) {
|
2014-03-07 11:24:51 -05:00
|
|
|
Rect bbox;
|
2020-04-10 11:57:18 -04:00
|
|
|
Points::const_iterator i = _points.begin ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-07 11:24:51 -05:00
|
|
|
bbox.x0 = bbox.x1 = i->x;
|
|
|
|
bbox.y0 = bbox.y1 = i->y;
|
2014-03-10 10:41:41 -04:00
|
|
|
|
|
|
|
++i;
|
|
|
|
|
2020-04-10 11:57:18 -04:00
|
|
|
while (i != _points.end ()) {
|
2013-04-04 00:32:52 -04:00
|
|
|
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);
|
2014-03-07 11:24:51 -05:00
|
|
|
++i;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2014-06-27 10:30:09 -04:00
|
|
|
_bounding_box = bbox.expand (_outline_width + 0.5);
|
2013-04-05 11:27:26 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
} else {
|
2017-01-19 14:54:24 -05:00
|
|
|
_bounding_box = Rect ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-27 00:02:22 -04:00
|
|
|
set_bbox_clean ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-10 12:28:26 -04:00
|
|
|
PolyItem::render_path (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2020-04-10 11:57:18 -04:00
|
|
|
if (_points.size () < 2) {
|
2014-01-03 14:05:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-13 09:28:06 -05:00
|
|
|
const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
|
2013-12-28 13:52:08 -05:00
|
|
|
|
2020-04-10 11:57:18 -04:00
|
|
|
Points::const_iterator i = _points.begin ();
|
|
|
|
Duple c0 = item_to_window (Duple (i->x, i->y));
|
|
|
|
|
2014-01-06 20:45:23 -05:00
|
|
|
++i;
|
2013-12-28 13:52:08 -05:00
|
|
|
|
2020-04-10 11:48:07 -04:00
|
|
|
while (c0.x < -1.) {
|
|
|
|
Duple c1 (item_to_window (Duple (i->x, i->y)));
|
2020-04-10 11:57:18 -04:00
|
|
|
if (interpolate_line (c0, c1, -1)) {
|
2020-04-10 11:48:07 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-04-10 11:57:18 -04:00
|
|
|
if (++i == _points.end ()) {
|
2020-04-10 11:48:07 -04:00
|
|
|
c1.x = 0;
|
|
|
|
context->move_to (c1.x + pixel_adjust, c1.y + pixel_adjust);
|
|
|
|
_left = _right = c1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c0 = c1;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->move_to (c0.x + pixel_adjust, c0.y + pixel_adjust);
|
|
|
|
_left = c0;
|
|
|
|
|
2020-04-10 11:57:18 -04:00
|
|
|
while (i != _points.end ()) {
|
2020-04-10 11:48:07 -04:00
|
|
|
Duple c = item_to_window (Duple (i->x, i->y));
|
2020-04-10 12:28:26 -04:00
|
|
|
if (c.x > area.x1) {
|
|
|
|
if (interpolate_line (c0, c, area.x1)) {
|
2020-04-10 11:48:07 -04:00
|
|
|
context->line_to (c0.x + pixel_adjust, c0.y + pixel_adjust);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-01-13 09:28:06 -05:00
|
|
|
context->line_to (c.x + pixel_adjust, c.y + pixel_adjust);
|
2020-04-10 11:48:07 -04:00
|
|
|
c0 = c;
|
2013-12-28 13:52:08 -05:00
|
|
|
++i;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2020-04-10 11:48:07 -04:00
|
|
|
_right = c0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PolyItem::interpolate_line (Duple& c0, Duple const& c1, Coord const x)
|
|
|
|
{
|
|
|
|
if (c1.x <= c0.x) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (x < c0.x || x > c1.x) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
c0.y += ((x - c0.x) / (c1.x - c0.x)) * (c1.y - c0.y);
|
|
|
|
c0.x = x;
|
|
|
|
return true;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-10 11:57:18 -04:00
|
|
|
PolyItem::set (Points const& points)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (_points != points) {
|
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
_points = points;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-03-11 07:36:09 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2023-06-23 18:47:09 -04:00
|
|
|
void
|
|
|
|
PolyItem::add_point (Duple const & d)
|
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
_points.push_back (d);
|
|
|
|
set_bbox_dirty ();
|
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:41:36 -04:00
|
|
|
void
|
|
|
|
PolyItem::pop_back ()
|
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
_points.pop_back ();
|
|
|
|
set_bbox_dirty ();
|
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
2023-09-30 12:53:52 -04:00
|
|
|
void
|
|
|
|
PolyItem::clear ()
|
|
|
|
{
|
|
|
|
if (!_points.empty()) {
|
|
|
|
begin_change ();
|
|
|
|
_points.clear ();
|
|
|
|
set_bbox_dirty ();
|
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 11:57:18 -04:00
|
|
|
Points const&
|
2013-04-04 00:32:52 -04:00
|
|
|
PolyItem::get () const
|
|
|
|
{
|
|
|
|
return _points;
|
|
|
|
}
|
|
|
|
|
2013-04-05 11:27:26 -04:00
|
|
|
void
|
|
|
|
PolyItem::dump (ostream& o) const
|
|
|
|
{
|
|
|
|
Item::dump (o);
|
|
|
|
|
2020-04-10 11:57:18 -04:00
|
|
|
o << _canvas->indent () << '\t' << _points.size () << " points" << endl;
|
|
|
|
for (Points::const_iterator i = _points.begin (); i != _points.end (); ++i) {
|
|
|
|
o << _canvas->indent () << "\t\t" << i->x << ", " << i->y << endl;
|
2013-04-05 11:27:26 -04:00
|
|
|
}
|
|
|
|
}
|