use window-based coordinates when picking current item so that we get per-item (per...
[ardour.git] / libs / canvas / arc.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <algorithm>
21
22 #include <cairomm/context.h>
23
24 #include "pbd/compose.h"
25 #include "canvas/circle.h"
26 #include "canvas/types.h"
27 #include "canvas/debug.h"
28 #include "canvas/utils.h"
29 #include "canvas/canvas.h"
30
31 using namespace std;
32 using namespace ArdourCanvas;
33
34 Arc::Arc (Group* parent)
35         : Item (parent)
36         , Outline (parent)
37         , Fill (parent)
38         , _radius (0.0)
39         , _arc_degrees (0.0)
40         , _start_degrees (0.0)
41 {
42
43 }
44
45 void
46 Arc::compute_bounding_box () const
47 {
48         Rect bbox;
49
50         /* this could be smaller in the case of small _arc values
51            but I can't be bothered to optimize it.
52         */
53         
54         bbox.x0 = _center.x - _radius;
55         bbox.y0 = _center.y - _radius;
56         bbox.x1 = _center.x + _radius;
57         bbox.y1 = _center.y + _radius;
58
59         bbox = bbox.expand (0.5 + (_outline_width / 2));
60
61         _bounding_box = bbox;
62         _bounding_box_dirty = false;
63 }
64
65 void
66 Arc::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
67 {
68         if (_radius <= 0.0 || _arc_degrees <= 0.0) {
69                 return;
70         }
71         
72         Duple c = item_to_window (Duple (_center.x, _center.y));
73
74         context->arc (c.x, c.y, _radius, _start_degrees * (M_PI/180.0), _arc_degrees * (M_PI/180.0));
75         setup_fill_context (context);
76         context->fill_preserve ();
77         setup_outline_context (context);
78         context->stroke ();
79 }
80
81 void
82 Arc::set_center (Duple const & c)
83 {
84         begin_change ();
85
86         _center = c;
87
88         _bounding_box_dirty = true;
89         end_change ();
90 }
91
92 void
93 Arc::set_radius (Coord r)
94 {
95         begin_change ();
96         
97         _radius = r;
98
99         _bounding_box_dirty = true;
100         end_change ();
101 }       
102
103 void
104 Arc::set_arc (double deg)
105 {
106         begin_change ();
107         
108         _arc_degrees = deg;
109
110         _bounding_box_dirty = true;
111         end_change ();
112 }       
113
114
115 void
116 Arc::set_start (double deg)
117 {
118         begin_change ();
119         
120         _start_degrees = deg;
121         
122         _bounding_box_dirty = true;
123         end_change ();
124 }       
125
126 bool
127 Arc::covers (Duple const & point) const
128 {
129         Duple p = window_to_item (point);
130
131         double angle_degs = atan (p.y/p.x) * 2.0 * M_PI;
132         double radius = sqrt (p.x * p.x + p.y * p.y);
133         
134         return (angle_degs >= _start_degrees) && 
135                 (angle_degs <= (_start_degrees + _arc_degrees)) && 
136                 (radius < _radius);
137 }