2 Copyright (C) 2013 Paul Davis
3 Copyright (C) 2014 Robin Gareus <robin@gareus.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "canvas/xfade_curve.h"
26 #include "canvas/interpolated_curve.h"
27 #include "canvas/utils.h"
29 using namespace ArdourCanvas;
33 #ifdef USE_TRACKS_CODE_FEATURES
34 static const bool show_bg_fades = false;
36 static const bool show_bg_fades = true;
39 XFadeCurve::XFadeCurve (Canvas* c)
41 , points_per_segment (32)
42 , _xfadeposition (Start)
43 , _outline_color (0x000000ff)
44 , _fill_color (0x22448880)
45 , show_background_fade (show_bg_fades)
49 XFadeCurve::XFadeCurve (Canvas* c, XFadePosition pos)
51 , points_per_segment (32)
52 , _xfadeposition (pos)
53 , _outline_color (0x000000ff)
54 , _fill_color (0x22448880)
55 , show_background_fade (show_bg_fades)
59 XFadeCurve::XFadeCurve (Item* parent)
61 , points_per_segment (32)
62 , _xfadeposition (Start)
63 , _outline_color (0x000000ff)
64 , _fill_color (0x22448880)
65 , show_background_fade (show_bg_fades)
69 XFadeCurve::XFadeCurve (Item* parent, XFadePosition pos)
71 , points_per_segment (32)
72 , _xfadeposition (pos)
73 , _outline_color (0x000000ff)
74 , _fill_color (0x22448880)
75 , show_background_fade (show_bg_fades)
80 XFadeCurve::compute_bounding_box () const
82 if (!_in.points.empty() && !_out.points.empty()) {
85 Points::const_iterator i;
87 if (!_in.points.empty()) {
88 i = _in.points.begin();
89 bbox.x0 = bbox.x1 = i->x;
90 bbox.y0 = bbox.y1 = i->y;
94 while (i != _in.points.end()) {
95 bbox.x0 = min (bbox.x0, i->x);
96 bbox.y0 = min (bbox.y0, i->y);
97 bbox.x1 = max (bbox.x1, i->x);
98 bbox.y1 = max (bbox.y1, i->y);
102 i = _out.points.begin();
103 bbox.x0 = bbox.x1 = i->x;
104 bbox.y0 = bbox.y1 = i->y;
107 if (!_out.points.empty()) {
108 i = _out.points.begin();
109 while (i != _out.points.end()) {
110 bbox.x0 = min (bbox.x0, i->x);
111 bbox.y0 = min (bbox.y0, i->y);
112 bbox.x1 = max (bbox.x1, i->x);
113 bbox.y1 = max (bbox.y1, i->y);
118 _bounding_box = bbox.expand (1.0);
121 _bounding_box = boost::optional<Rect> ();
124 _bounding_box_dirty = false;
128 XFadeCurve::set_inout (Points const & in, Points const & out)
130 if (_in.points == in && _out.points == out) {
136 _bounding_box_dirty = true;
142 XFadeCurve::set_points_per_segment (uint32_t n)
144 points_per_segment = n;
150 XFadeCurve::interpolate ()
152 _in.samples.clear ();
153 InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
154 _in.n_samples = _in.samples.size();
156 _out.samples.clear ();
157 InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
158 _out.n_samples = _out.samples.size();
162 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
164 assert(c.points.size() > 1);
165 context->begin_new_path ();
168 if (c.points.size () == 2) {
170 window_space = item_to_window (c.points.front(), false);
171 context->move_to (window_space.x, window_space.y);
172 window_space = item_to_window (c.points.back(), false);
173 context->line_to (window_space.x, window_space.y);
177 /* find left and right-most sample */
178 Points::size_type left = 0;
179 Points::size_type right = c.n_samples - 1;
181 assert (left < right);
182 // we should really really do a binary search rather than iterate
183 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
185 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
186 if (window_space.x >= area.x0) break;
188 for (Points::size_type idx = c.n_samples - 1; right > left;) {
189 if (--idx <= left) break;
190 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
191 if (window_space.x <= area.x1) break;
195 assert(left < right);
196 assert(left < c.n_samples);
197 assert(right < c.n_samples);
199 /* draw line between samples */
200 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
201 context->move_to (window_space.x, window_space.y);
202 for (uint32_t idx = left + 1; idx <= right; ++idx) {
203 window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
204 context->line_to (window_space.x, window_space.y);
207 return context->copy_path ();
211 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
215 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
216 context->line_to (window_space.x, window_space.y);
217 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
218 context->line_to (window_space.x, window_space.y);
219 context->close_path();
221 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
222 context->line_to (window_space.x, window_space.y);
223 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
224 context->line_to (window_space.x, window_space.y);
225 context->close_path();
230 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
232 if (!_bounding_box) { return; }
233 if (_in.points.size() < 2) { return; }
234 if (_out.points.size() < 2) { return; }
236 Rect self = item_to_window (_bounding_box.get());
237 boost::optional<Rect> d = self.intersection (area);
239 Rect draw = d.get ();
242 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
245 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
246 * they will still occur, but cairo's clipping will hide them.
248 draw = draw.expand (4.0);
250 Cairo::Path *path_in = get_path(draw, context, _in);
251 Cairo::Path *path_out = get_path(draw, context, _out);
253 Color outline_shaded = _outline_color;
254 outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
256 Color fill_shaded = _fill_color;
257 fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
259 #define IS_START (_xfadeposition == Start)
261 /* fill primary fade */
262 context->begin_new_path ();
263 context->append_path (IS_START ? *path_in : *path_out);
264 close_path(draw, context, IS_START ?_in : _out, false);
265 set_source_rgba (context, _fill_color);
268 if (show_background_fade) {
269 /* fill background fade */
271 context->begin_new_path ();
272 context->append_path (IS_START ? *path_in : *path_out);
273 close_path(draw, context, IS_START ? _in : _out, true);
274 context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
276 context->begin_new_path ();
277 context->append_path (IS_START ? *path_out: *path_in);
278 close_path(draw, context, IS_START ? _out : _in, true);
279 set_source_rgba (context, fill_shaded);
280 context->set_fill_rule (Cairo::FILL_RULE_WINDING);
285 /* draw lines over fills */
287 if (IS_START || show_background_fade) {
288 set_source_rgba (context, IS_START ? _outline_color : outline_shaded);
289 context->set_line_width (IS_START ? 1.0 : .5);
291 context->begin_new_path ();
292 context->append_path (*path_in);
297 if (!IS_START || show_background_fade) {
298 set_source_rgba (context, IS_START ? outline_shaded :_outline_color);
299 context->set_line_width (IS_START ? .5 : 1.0);
301 context->begin_new_path ();
302 context->append_path (*path_out);