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"
28 using namespace ArdourCanvas;
32 #ifdef USE_TRACKS_CODE_FEATURES
33 static const bool show_bg_fades = false;
35 static const bool show_bg_fades = true;
38 XFadeCurve::XFadeCurve (Canvas* c)
40 , points_per_segment (32)
41 , _xfadeposition (Start)
42 , _outline_color (0x000000ff)
43 , _fill_color (0x22448880)
44 , show_background_fade (show_bg_fades)
48 XFadeCurve::XFadeCurve (Canvas* c, XFadePosition pos)
50 , points_per_segment (32)
51 , _xfadeposition (pos)
52 , _outline_color (0x000000ff)
53 , _fill_color (0x22448880)
54 , show_background_fade (show_bg_fades)
58 XFadeCurve::XFadeCurve (Item* parent)
60 , points_per_segment (32)
61 , _xfadeposition (Start)
62 , _outline_color (0x000000ff)
63 , _fill_color (0x22448880)
64 , show_background_fade (show_bg_fades)
68 XFadeCurve::XFadeCurve (Item* parent, XFadePosition pos)
70 , points_per_segment (32)
71 , _xfadeposition (pos)
72 , _outline_color (0x000000ff)
73 , _fill_color (0x22448880)
74 , show_background_fade (show_bg_fades)
79 XFadeCurve::compute_bounding_box () const
81 if (!_in.points.empty() && !_out.points.empty()) {
84 Points::const_iterator i;
86 if (!_in.points.empty()) {
87 i = _in.points.begin();
88 bbox.x0 = bbox.x1 = i->x;
89 bbox.y0 = bbox.y1 = i->y;
93 while (i != _in.points.end()) {
94 bbox.x0 = min (bbox.x0, i->x);
95 bbox.y0 = min (bbox.y0, i->y);
96 bbox.x1 = max (bbox.x1, i->x);
97 bbox.y1 = max (bbox.y1, i->y);
101 i = _out.points.begin();
102 bbox.x0 = bbox.x1 = i->x;
103 bbox.y0 = bbox.y1 = i->y;
106 if (!_out.points.empty()) {
107 i = _out.points.begin();
108 while (i != _out.points.end()) {
109 bbox.x0 = min (bbox.x0, i->x);
110 bbox.y0 = min (bbox.y0, i->y);
111 bbox.x1 = max (bbox.x1, i->x);
112 bbox.y1 = max (bbox.y1, i->y);
117 _bounding_box = bbox.expand (1.0);
120 _bounding_box = Rect ();
123 _bounding_box_dirty = false;
127 XFadeCurve::set_inout (Points const & in, Points const & out)
129 if (_in.points == in && _out.points == out) {
135 _bounding_box_dirty = true;
141 XFadeCurve::set_points_per_segment (uint32_t n)
143 points_per_segment = n;
149 XFadeCurve::interpolate ()
151 _in.samples.clear ();
152 InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
153 _in.n_samples = _in.samples.size();
155 _out.samples.clear ();
156 InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
157 _out.n_samples = _out.samples.size();
161 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
163 assert(c.points.size() > 1);
164 context->begin_new_path ();
167 if (c.points.size () == 2) {
169 window_space = item_to_window (c.points.front(), false);
170 context->move_to (window_space.x, window_space.y);
171 window_space = item_to_window (c.points.back(), false);
172 context->line_to (window_space.x, window_space.y);
176 /* find left and right-most sample */
177 Points::size_type left = 0;
178 Points::size_type right = c.n_samples - 1;
180 assert (left < right);
181 // we should really really do a binary search rather than iterate
182 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
184 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
185 if (window_space.x >= area.x0) break;
187 for (Points::size_type idx = c.n_samples - 1; right > left;) {
188 if (--idx <= left) break;
189 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
190 if (window_space.x <= area.x1) break;
194 assert(left < right);
195 assert(left < c.n_samples);
196 assert(right < c.n_samples);
198 /* draw line between samples */
199 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
200 context->move_to (window_space.x, window_space.y);
201 for (uint32_t idx = left + 1; idx <= right; ++idx) {
202 window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
203 context->line_to (window_space.x, window_space.y);
206 return context->copy_path ();
210 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
214 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
215 context->line_to (window_space.x, window_space.y);
216 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
217 context->line_to (window_space.x, window_space.y);
218 context->close_path();
220 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
221 context->line_to (window_space.x, window_space.y);
222 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
223 context->line_to (window_space.x, window_space.y);
224 context->close_path();
229 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
231 if (!_bounding_box) { return; }
232 if (_in.points.size() < 2) { return; }
233 if (_out.points.size() < 2) { return; }
235 Rect self = item_to_window (_bounding_box);
236 Rect d = self.intersection (area);
241 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
244 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
245 * they will still occur, but cairo's clipping will hide them.
247 draw = draw.expand (4.0);
249 Cairo::Path *path_in = get_path(draw, context, _in);
250 Cairo::Path *path_out = get_path(draw, context, _out);
252 Gtkmm2ext::Color outline_shaded = _outline_color;
253 outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
255 Gtkmm2ext::Color fill_shaded = _fill_color;
256 fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
258 #define IS_START (_xfadeposition == Start)
260 /* fill primary fade */
261 context->begin_new_path ();
262 context->append_path (IS_START ? *path_in : *path_out);
263 close_path(draw, context, IS_START ?_in : _out, false);
264 Gtkmm2ext::set_source_rgba (context, _fill_color);
267 if (show_background_fade) {
268 /* fill background fade */
270 context->begin_new_path ();
271 context->append_path (IS_START ? *path_in : *path_out);
272 close_path(draw, context, IS_START ? _in : _out, true);
273 context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
275 context->begin_new_path ();
276 context->append_path (IS_START ? *path_out: *path_in);
277 close_path(draw, context, IS_START ? _out : _in, true);
278 Gtkmm2ext::set_source_rgba (context, fill_shaded);
279 context->set_fill_rule (Cairo::FILL_RULE_WINDING);
284 /* draw lines over fills */
286 if (IS_START || show_background_fade) {
287 Gtkmm2ext::set_source_rgba (context, IS_START ? _outline_color : outline_shaded);
288 context->set_line_width (IS_START ? 1.0 : .5);
290 context->begin_new_path ();
291 context->append_path (*path_in);
296 if (!IS_START || show_background_fade) {
297 Gtkmm2ext::set_source_rgba (context, IS_START ? outline_shaded :_outline_color);
298 context->set_line_width (IS_START ? .5 : 1.0);
300 context->begin_new_path ();
301 context->append_path (*path_out);