13
0
livetrax/libs/canvas/lollipop.cc

188 lines
3.9 KiB
C++
Raw Normal View History

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)
, _radius (8)
, _length (0)
, bounding_parent (0)
, line_color_is_fill (true)
2022-12-26 15:28:20 -05:00
{
}
Lollipop::Lollipop (Item* parent)
: Item (parent)
, _radius (8)
, _length (0)
, bounding_parent (0)
, line_color_is_fill (true)
2022-12-26 15:28:20 -05: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
{
_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
Lollipop::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
2022-12-26 15:28:20 -05:00
{
Duple p = _parent->item_to_window (Duple (_center.x, _center.y));
Duple l (p);
2022-12-26 15:28:20 -05: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
}
setup_outline_context (context);
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
/* the circle: clip to avoid weirdness at top and bottom of parent */
if (bounding_parent) {
Rect b (bounding_parent->item_to_window (bounding_parent->bounding_box()));
b = b.intersection (area);
context->save ();
context->rectangle (b.x0, b.y0, b.width(), b.height());
context->clip();
}
2022-12-26 15:28:20 -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
if (fill()) {
setup_fill_context (context);
if (outline()) {
context->fill_preserve ();
} else {
context->fill ();
}
}
if (outline()) {
setup_outline_context (context);
context->stroke ();
}
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 ();
}
if (bounding_parent) {
context->restore ();
}
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)
{
if (x != _center.x) {
2022-12-26 15:28:20 -05:00
begin_change ();
_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)
{
if (_length != len) {
2023-01-10 23:41:47 -05:00
begin_change ();
_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;
_length = l;
_center = d;
2023-01-10 23:41:47 -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
{
const Duple p = _parent->window_to_item (point);
2022-12-26 15:28:20 -05:00
static const Distance threshold = 2.0;
/* only the circle is considered as "covering" */
2022-12-26 15:28:20 -05:00
if (((fabs (_center.x - p.x)) <= (_radius + threshold)) &&
((fabs (_center.y - p.y)) <= (_radius + threshold))) {
/* inside circle */
2022-12-26 15:28:20 -05:00
return true;
}
return false;
}