1 /*
2 * Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2013-2014 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
5 * Copyright (C) 2015 David Robillard <d@drobilla.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 /** @file canvas/arrow.cc
23 * @brief Implementation of the Arrow canvas object.
24 */
25
26 #include "pbd/compose.h"
27
28 #include "canvas/arrow.h"
29 #include "canvas/debug.h"
30 #include "canvas/polygon.h"
31 #include "canvas/line.h"
32
33 using namespace ArdourCanvas;
34
35 /** Construct an Arrow.
36 * @param parent Parent canvas group.
37 */
Arrow(Canvas * c)38 Arrow::Arrow (Canvas* c)
39 : Container (c)
40 {
41 setup ();
42 }
43
Arrow(Item * parent)44 Arrow::Arrow (Item* parent)
45 : Container (parent)
46 {
47 setup ();
48 }
49
50 void
setup()51 Arrow::setup ()
52 {
53 /* set up default arrow heads at each end */
54 for (int i = 0; i < 2; ++i) {
55 _heads[i].polygon = new Polygon (this);
56 _heads[i].outward = true;
57 _heads[i].width = 4;
58 _heads[i].height = 4;
59 setup_polygon (i);
60 CANVAS_DEBUG_NAME (_heads[i].polygon, string_compose ("arrow head %1", i));
61 }
62
63 _line = new Line (this);
64 CANVAS_DEBUG_NAME (_line, "arrow line");
65 }
66
67 void
compute_bounding_box() const68 Arrow::compute_bounding_box () const
69 {
70 /* Compute our bounding box manually rather than using the default
71 container algorithm, since having the bounding box with origin other
72 than zero causes strange problems for mysterious reasons. */
73
74 const double outline_pad = 0.5 + (_line->outline_width() / 2.0);
75 const double head_width = std::max(_heads[0].width, _heads[1].width);
76
77 _bounding_box = Rect(0,
78 0,
79 _line->x1() + (head_width / 2.0) + outline_pad,
80 _line->y1());
81
82 _bounding_box_dirty = false;
83 }
84
85 /** Set whether to show an arrow head at one end or other
86 * of the line.
87 * @param which 0 or 1 to specify the arrow head to set up.
88 * @param true if this arrow head should be shown.
89 */
90 void
set_show_head(int which,bool show)91 Arrow::set_show_head (int which, bool show)
92 {
93 assert (which == 0 || which == 1);
94
95 begin_change ();
96
97 if (!show) {
98 delete _heads[which].polygon;
99 _heads[which].polygon = 0;
100 } else {
101 setup_polygon (which);
102 }
103
104 _bounding_box_dirty = true;
105 end_change ();
106 }
107
108 /** Set whether a given arrow head points into the line or
109 * away from it.
110 * @param which 0 or 1 to specify the arrow head to set up.
111 * @param true if this arrow head should point out from the line,
112 * otherwise false to point in.
113 */
114 void
set_head_outward(int which,bool outward)115 Arrow::set_head_outward (int which, bool outward)
116 {
117 assert (which == 0 || which == 1);
118
119 begin_change ();
120
121 _heads[which].outward = outward;
122
123 setup_polygon (which);
124 _bounding_box_dirty = true;
125 end_change ();
126 }
127
128 /** Set the height of a given arrow head.
129 * @param which 0 or 1 to specify the arrow head to set up.
130 * @param height Height of the arrow head in pixels.
131 */
132 void
set_head_height(int which,Distance height)133 Arrow::set_head_height (int which, Distance height)
134 {
135 assert (which == 0 || which == 1);
136
137 begin_change ();
138
139 _heads[which].height = height;
140
141 setup_polygon (which);
142 _bounding_box_dirty = true;
143 end_change ();
144 }
145
146 /** Set the width of a given arrow head.
147 * @param which 0 or 1 to specify the arrow head to set up.
148 * @param width Width of the arrow head in pixels.
149 */
150 void
set_head_width(int which,Distance width)151 Arrow::set_head_width (int which, Distance width)
152 {
153 assert (which == 0 || which == 1);
154
155 begin_change ();
156
157 _heads[which].width = width;
158
159 setup_polygon (which);
160 _bounding_box_dirty = true;
161 end_change ();
162 }
163
164 /** Set the width of our line, and the outline of our arrow(s).
165 * @param width New width in pixels.
166 */
167 void
set_outline_width(Distance width)168 Arrow::set_outline_width (Distance width)
169 {
170 _line->set_outline_width (width);
171 if (_heads[0].polygon) {
172 _heads[0].polygon->set_outline_width (width);
173 }
174 if (_heads[1].polygon) {
175 _heads[1].polygon->set_outline_width (width);
176 }
177 _bounding_box_dirty = true;
178 }
179
180 /** Set the x position of our line.
181 * @param x New x position in pixels (in our coordinate system).
182 */
183 void
set_x(Coord x)184 Arrow::set_x (Coord x)
185 {
186 _line->set_x0 (x);
187 _line->set_x1 (x);
188 for (int i = 0; i < 2; ++i) {
189 if (_heads[i].polygon) {
190 _heads[i].polygon->set_x_position (x - _heads[i].width / 2);
191 }
192 }
193 _bounding_box_dirty = true;
194 }
195
196 /** Set the y position of end 0 of our line.
197 * @param y0 New y0 position in pixels (in our coordinate system).
198 */
199 void
set_y0(Coord y0)200 Arrow::set_y0 (Coord y0)
201 {
202 _line->set_y0 (y0);
203 if (_heads[0].polygon) {
204 _heads[0].polygon->set_y_position (y0);
205 }
206 _bounding_box_dirty = true;
207 }
208
209 /** Set the y position of end 1 of our line.
210 * @param y1 New y1 position in pixels (in our coordinate system).
211 */
212 void
set_y1(Coord y1)213 Arrow::set_y1 (Coord y1)
214 {
215 _line->set_y1 (y1);
216 if (_heads[1].polygon) {
217 _heads[1].polygon->set_y_position (y1 - _heads[1].height);
218 }
219 _bounding_box_dirty = true;
220 }
221
222 /** @return x position of our line in pixels (in our coordinate system) */
223 Coord
x() const224 Arrow::x () const
225 {
226 return _line->x0 ();
227 }
228
229 /** @return y position of end 1 of our line in pixels (in our coordinate system) */
230 Coord
y1() const231 Arrow::y1 () const
232 {
233 return _line->y1 ();
234 }
235
236 /** Set up the polygon used to represent a particular arrow head.
237 * @param which 0 or 1 to specify the arrow head to set up.
238 */
239 void
setup_polygon(int which)240 Arrow::setup_polygon (int which)
241 {
242 assert (which == 0 || which == 1);
243
244 Points points;
245
246 if ((which == 0 && _heads[which].outward) || (which == 1 && !_heads[which].outward)) {
247 /* this is an arrow head pointing towards -ve y */
248 points.push_back (Duple (_heads[which].width / 2, 0));
249 points.push_back (Duple (_heads[which].width, _heads[which].height));
250 points.push_back (Duple (0, _heads[which].height));
251 } else {
252 /* this is an arrow head pointing towards +ve y */
253 points.push_back (Duple (0, 0));
254 points.push_back (Duple (_heads[which].width, 0));
255 points.push_back (Duple (_heads[which].width / 2, _heads[which].height));
256 points.push_back (Duple (0, 0));
257 }
258
259 _heads[which].polygon->set (points);
260 }
261
262 /** Set the color of our line and arrow heads.
263 * @param color New color.
264 */
265 void
set_color(Gtkmm2ext::Color color)266 Arrow::set_color (Gtkmm2ext::Color color)
267 {
268 _line->set_outline_color (color);
269 for (int i = 0; i < 2; ++i) {
270 if (_heads[i].polygon) {
271 _heads[i].polygon->set_outline_color (color);
272 _heads[i].polygon->set_fill_color (color);
273 }
274 }
275 }
276
277 bool
covers(Duple const & point) const278 Arrow::covers (Duple const & point) const
279 {
280 if (_heads[0].polygon && _heads[0].polygon->covers (point)) {
281 return true;
282 }
283 if (_line && _line->covers (point)) {
284 return true;
285 }
286
287 if (_heads[1].polygon && _heads[1].polygon->covers (point)) {
288 return true;
289 }
290
291 return false;
292 }
293