2022-12-26 15:28:20 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2013-2014 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cairomm/context.h>
|
|
|
|
#include "pbd/compose.h"
|
|
|
|
#include "canvas/lollipop.h"
|
|
|
|
#include "canvas/types.h"
|
|
|
|
#include "canvas/debug.h"
|
|
|
|
#include "canvas/utils.h"
|
|
|
|
#include "canvas/canvas.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
|
|
|
Lollipop::Lollipop (Canvas* c)
|
|
|
|
: Item (c)
|
2023-01-12 19:06:43 -05:00
|
|
|
, _radius (8)
|
|
|
|
, _length (0)
|
2023-06-21 18:18:14 -04:00
|
|
|
, bounding_parent (0)
|
2023-06-22 10:39:41 -04:00
|
|
|
, line_color_is_fill (true)
|
2022-12-26 15:28:20 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Lollipop::Lollipop (Item* parent)
|
|
|
|
: Item (parent)
|
2023-01-12 19:06:43 -05:00
|
|
|
, _radius (8)
|
|
|
|
, _length (0)
|
2023-06-21 18:18:14 -04:00
|
|
|
, bounding_parent (0)
|
2023-06-22 10:39:41 -04:00
|
|
|
, line_color_is_fill (true)
|
2022-12-26 15:28:20 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:18:14 -04:00
|
|
|
void
|
|
|
|
Lollipop::set_bounding_parent (Item* bp)
|
|
|
|
{
|
|
|
|
bounding_parent = bp;
|
|
|
|
}
|
|
|
|
|
2022-12-26 15:28:20 -05:00
|
|
|
void
|
|
|
|
Lollipop::compute_bounding_box () const
|
|
|
|
{
|
2023-06-20 15:46:53 -04:00
|
|
|
_bounding_box = Rect (_center.x -_radius, _center.y -_radius, _center.x + _radius, _center.y + _length + _radius);
|
|
|
|
_bounding_box = _bounding_box.expand (2. * _outline_width);
|
2022-12-26 15:28:20 -05:00
|
|
|
set_bbox_clean ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-01-15 10:37:46 -05:00
|
|
|
Lollipop::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
2022-12-26 15:28:20 -05:00
|
|
|
{
|
2023-06-20 15:46:53 -04:00
|
|
|
Duple p = _parent->item_to_window (Duple (_center.x, _center.y));
|
|
|
|
Duple l (p);
|
2022-12-26 15:28:20 -05:00
|
|
|
|
2023-06-20 15:46:53 -04:00
|
|
|
if (fmod (_outline_width, 2.0)) {
|
|
|
|
const Duple half_a_pixel (0.5 * _outline_width, 0.);
|
|
|
|
l = l.translate (half_a_pixel);
|
2022-12-26 15:28:20 -05:00
|
|
|
}
|
|
|
|
|
2023-06-22 10:39:41 -04:00
|
|
|
setup_outline_context (context);
|
2023-01-12 19:06:43 -05:00
|
|
|
|
2023-06-22 10:39:41 -04:00
|
|
|
if (!line_color_is_fill) {
|
|
|
|
/* draw line in outline color */
|
|
|
|
context->move_to (l.x, l.y + _radius);
|
|
|
|
context->line_to (l.x, l.y + _length);
|
|
|
|
context->stroke ();
|
|
|
|
}
|
2022-12-26 15:28:20 -05:00
|
|
|
|
2023-06-21 18:18:14 -04:00
|
|
|
/* the circle: clip to avoid weirdness at top and bottom of parent */
|
|
|
|
|
|
|
|
if (bounding_parent) {
|
|
|
|
context->save ();
|
|
|
|
Rect b (bounding_parent->item_to_window (bounding_parent->bounding_box()));
|
|
|
|
context->rectangle (b.x0, b.y0, b.width(), b.height());
|
|
|
|
context->clip();
|
|
|
|
}
|
2022-12-26 15:28:20 -05:00
|
|
|
|
2023-01-12 19:06:43 -05:00
|
|
|
context->arc (p.x, p.y, _radius, 0.0 * (M_PI/180.0), 360.0 * (M_PI/180.0));
|
2022-12-26 15:28:20 -05:00
|
|
|
|
2023-06-22 10:39:41 -04:00
|
|
|
|
2022-12-26 15:28:20 -05:00
|
|
|
if (fill()) {
|
|
|
|
setup_fill_context (context);
|
|
|
|
if (outline()) {
|
|
|
|
context->fill_preserve ();
|
|
|
|
} else {
|
|
|
|
context->fill ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outline()) {
|
|
|
|
setup_outline_context (context);
|
|
|
|
context->stroke ();
|
|
|
|
}
|
2023-01-15 10:37:46 -05:00
|
|
|
|
2023-06-22 10:39:41 -04:00
|
|
|
if (line_color_is_fill) {
|
|
|
|
setup_fill_context (context);
|
|
|
|
/* draw line in fill color */
|
|
|
|
context->move_to (l.x, l.y + _radius);
|
|
|
|
context->line_to (l.x, l.y + _length);
|
|
|
|
context->stroke ();
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:18:14 -04:00
|
|
|
if (bounding_parent) {
|
|
|
|
context->restore ();
|
|
|
|
}
|
|
|
|
|
2023-01-15 10:37:46 -05:00
|
|
|
render_children (area, context);
|
2022-12-26 15:28:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Lollipop::set_radius (Coord r)
|
|
|
|
{
|
|
|
|
if (_radius != r) {
|
|
|
|
begin_change ();
|
|
|
|
_radius = r;
|
|
|
|
set_bbox_dirty ();
|
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Lollipop::set_x (Coord x)
|
|
|
|
{
|
2023-06-20 15:46:53 -04:00
|
|
|
if (x != _center.x) {
|
2022-12-26 15:28:20 -05:00
|
|
|
begin_change ();
|
2023-06-20 15:46:53 -04:00
|
|
|
_center.x = x;
|
2022-12-26 15:28:20 -05:00
|
|
|
set_bbox_dirty ();
|
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 23:41:47 -05:00
|
|
|
void
|
|
|
|
Lollipop::set_length (Coord len)
|
|
|
|
{
|
2023-01-12 19:06:43 -05:00
|
|
|
if (_length != len) {
|
2023-01-10 23:41:47 -05:00
|
|
|
begin_change ();
|
2023-01-12 19:06:43 -05:00
|
|
|
_length = len;
|
|
|
|
set_bbox_dirty ();
|
2023-01-10 23:41:47 -05:00
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Lollipop::set (Duple const & d, Coord l, Coord r)
|
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
|
2023-01-11 13:51:03 -05:00
|
|
|
_radius = r;
|
2023-01-12 19:06:43 -05:00
|
|
|
_length = l;
|
2023-06-20 15:46:53 -04:00
|
|
|
_center = d;
|
2023-01-10 23:41:47 -05:00
|
|
|
|
2023-01-12 19:06:43 -05:00
|
|
|
set_bbox_dirty ();
|
2023-01-10 23:41:47 -05:00
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
2022-12-26 15:28:20 -05:00
|
|
|
bool
|
|
|
|
Lollipop::covers (Duple const & point) const
|
|
|
|
{
|
2023-01-12 19:06:43 -05:00
|
|
|
const Duple p = _parent->window_to_item (point);
|
2022-12-26 15:28:20 -05:00
|
|
|
static const Distance threshold = 2.0;
|
|
|
|
|
2023-01-12 19:06:43 -05:00
|
|
|
/* only the circle is considered as "covering" */
|
2022-12-26 15:28:20 -05:00
|
|
|
|
2023-06-20 15:46:53 -04:00
|
|
|
if (((fabs (_center.x - p.x)) <= (_radius + threshold)) &&
|
|
|
|
((fabs (_center.y - p.y)) <= (_radius + threshold))) {
|
2023-01-12 19:06:43 -05:00
|
|
|
/* inside circle */
|
2022-12-26 15:28:20 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-22 10:39:41 -04:00
|
|
|
|