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 XFadeCurve::XFadeCurve (Canvas* c)
35 , points_per_segment (32)
36 , _xfadeposition (Start)
37 , _outline_color (0x000000ff)
38 , _fill_color (0x22448880)
39 , show_background_fade (true)
43 XFadeCurve::XFadeCurve (Canvas* c, XFadePosition pos)
45 , points_per_segment (32)
46 , _xfadeposition (pos)
47 , _outline_color (0x000000ff)
48 , _fill_color (0x22448880)
49 , show_background_fade (true)
53 XFadeCurve::XFadeCurve (Item* parent)
55 , points_per_segment (32)
56 , _xfadeposition (Start)
57 , _outline_color (0x000000ff)
58 , _fill_color (0x22448880)
59 , show_background_fade (true)
63 XFadeCurve::XFadeCurve (Item* parent, XFadePosition pos)
65 , points_per_segment (32)
66 , _xfadeposition (pos)
67 , _outline_color (0x000000ff)
68 , _fill_color (0x22448880)
69 , show_background_fade (true)
74 XFadeCurve::compute_bounding_box () const
76 if (!_in.points.empty() && !_out.points.empty()) {
79 Points::const_iterator i;
81 if (!_in.points.empty()) {
82 i = _in.points.begin();
83 bbox.x0 = bbox.x1 = i->x;
84 bbox.y0 = bbox.y1 = i->y;
88 while (i != _in.points.end()) {
89 bbox.x0 = min (bbox.x0, i->x);
90 bbox.y0 = min (bbox.y0, i->y);
91 bbox.x1 = max (bbox.x1, i->x);
92 bbox.y1 = max (bbox.y1, i->y);
96 i = _out.points.begin();
97 bbox.x0 = bbox.x1 = i->x;
98 bbox.y0 = bbox.y1 = i->y;
101 if (!_out.points.empty()) {
102 i = _out.points.begin();
103 while (i != _out.points.end()) {
104 bbox.x0 = min (bbox.x0, i->x);
105 bbox.y0 = min (bbox.y0, i->y);
106 bbox.x1 = max (bbox.x1, i->x);
107 bbox.y1 = max (bbox.y1, i->y);
112 _bounding_box = bbox.expand (1.0);
115 _bounding_box = boost::optional<Rect> ();
118 _bounding_box_dirty = false;
122 XFadeCurve::set_inout (Points const & in, Points const & out)
124 if (_in.points == in && _out.points == out) {
130 _bounding_box_dirty = true;
136 XFadeCurve::set_points_per_segment (uint32_t n)
138 points_per_segment = n;
144 XFadeCurve::interpolate ()
146 _in.samples.clear ();
147 InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
148 _in.n_samples = _in.samples.size();
150 _out.samples.clear ();
151 InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
152 _out.n_samples = _out.samples.size();
156 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
158 assert(c.points.size() > 1);
159 context->begin_new_path ();
162 if (c.points.size () == 2) {
164 window_space = item_to_window (c.points.front(), false);
165 context->move_to (window_space.x, window_space.y);
166 window_space = item_to_window (c.points.back(), false);
167 context->line_to (window_space.x, window_space.y);
171 /* find left and right-most sample */
172 Points::size_type left = 0;
173 Points::size_type right = c.n_samples - 1;
175 // we should really really do a binary search rather than iterate
176 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
178 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
179 if (window_space.x >= area.x0) break;
181 for (Points::size_type idx = c.n_samples; idx >= left;) {
183 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
184 if (window_space.x <= area.x1) break;
188 assert(left < right);
189 assert(left < c.n_samples);
190 assert(right < c.n_samples);
192 /* draw line between samples */
193 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
194 context->move_to (window_space.x, window_space.y);
195 for (uint32_t idx = left + 1; idx <= right; ++idx) {
196 window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
197 context->line_to (window_space.x, window_space.y);
200 return context->copy_path ();
204 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
208 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
209 context->line_to (window_space.x, window_space.y);
210 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
211 context->line_to (window_space.x, window_space.y);
212 context->close_path();
214 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
215 context->line_to (window_space.x, window_space.y);
216 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
217 context->line_to (window_space.x, window_space.y);
218 context->close_path();
223 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
225 if (!_bounding_box) { return; }
226 if (_in.points.size() < 2) { return; }
227 if (_out.points.size() < 2) { return; }
229 Rect self = item_to_window (_bounding_box.get());
230 boost::optional<Rect> d = self.intersection (area);
232 Rect draw = d.get ();
235 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
238 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
239 * they will still occur, but cairo's clipping will hide them.
241 draw = draw.expand (4.0);
243 Cairo::Path *path_in = get_path(draw, context, _in);
244 Cairo::Path *path_out = get_path(draw, context, _out);
246 Color outline_shaded = _outline_color;
247 outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
249 Color fill_shaded = _fill_color;
250 fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
252 #define IS_START (_xfadeposition == Start)
254 /* fill primary fade */
255 context->begin_new_path ();
256 context->append_path (IS_START ? *path_in : *path_out);
257 close_path(draw, context, IS_START ?_in : _out, false);
258 set_source_rgba (context, _fill_color);
261 if (show_background_fade) {
262 /* fill background fade */
264 context->begin_new_path ();
265 context->append_path (IS_START ? *path_in : *path_out);
266 close_path(draw, context, IS_START ? _in : _out, true);
267 context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
269 context->begin_new_path ();
270 context->append_path (IS_START ? *path_out: *path_in);
271 close_path(draw, context, IS_START ? _out : _in, true);
272 set_source_rgba (context, fill_shaded);
273 context->set_fill_rule (Cairo::FILL_RULE_WINDING);
278 /* draw lines over fills */
280 if (IS_START || show_background_fade) {
281 set_source_rgba (context, IS_START ? _outline_color : outline_shaded);
282 context->set_line_width (IS_START ? 1.0 : .5);
284 context->begin_new_path ();
285 context->append_path (*path_in);
290 if (!IS_START || show_background_fade) {
291 set_source_rgba (context, IS_START ? outline_shaded :_outline_color);
292 context->set_line_width (IS_START ? .5 : 1.0);
294 context->begin_new_path ();
295 context->append_path (*path_out);