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/utils.h"
26 #include "canvas/xfade_curve.h"
27 #include "canvas/interpolated_curve.h"
29 using namespace ArdourCanvas;
33 XFadeCurve::XFadeCurve (Group* parent)
35 , points_per_segment (24)
36 , _xfadeposition (Start)
37 , _outline_color (0x000000ff)
38 , _fill_color (0x22448880)
42 XFadeCurve::XFadeCurve (Group* parent, XFadePosition pos)
44 , points_per_segment (24)
45 , _xfadeposition (pos)
46 , _outline_color (0x000000ff)
47 , _fill_color (0x22448880)
52 XFadeCurve::compute_bounding_box () const
54 if (!_in.points.empty() && !_out.points.empty()) {
57 Points::const_iterator i;
59 if (!_in.points.empty()) {
60 i = _in.points.begin();
61 bbox.x0 = bbox.x1 = i->x;
62 bbox.y0 = bbox.y1 = i->y;
66 while (i != _in.points.end()) {
67 bbox.x0 = min (bbox.x0, i->x);
68 bbox.y0 = min (bbox.y0, i->y);
69 bbox.x1 = max (bbox.x1, i->x);
70 bbox.y1 = max (bbox.y1, i->y);
74 i = _out.points.begin();
75 bbox.x0 = bbox.x1 = i->x;
76 bbox.y0 = bbox.y1 = i->y;
79 if (!_out.points.empty()) {
80 i = _out.points.begin();
81 while (i != _out.points.end()) {
82 bbox.x0 = min (bbox.x0, i->x);
83 bbox.y0 = min (bbox.y0, i->y);
84 bbox.x1 = max (bbox.x1, i->x);
85 bbox.y1 = max (bbox.y1, i->y);
90 _bounding_box = bbox.expand (1.0);
93 _bounding_box = boost::optional<Rect> ();
96 _bounding_box_dirty = false;
100 XFadeCurve::set_inout (Points const & in, Points const & out)
102 if (_in.points == in && _out.points == out) {
108 _bounding_box_dirty = true;
114 XFadeCurve::set_points_per_segment (uint32_t n)
116 points_per_segment = n;
122 XFadeCurve::interpolate ()
124 _in.samples.clear ();
125 InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
126 _in.n_samples = _in.samples.size();
128 _out.samples.clear ();
129 InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
130 _out.n_samples = _out.samples.size();
134 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
136 assert(c.points.size() > 1);
137 context->begin_new_path ();
140 if (c.points.size () == 2) {
142 window_space = item_to_window (c.points.front());
143 context->move_to (window_space.x, window_space.y);
144 window_space = item_to_window (c.points.back());
145 context->line_to (window_space.x, window_space.y);
149 /* find left and right-most sample */
150 Points::size_type left = 0;
151 Points::size_type right = c.n_samples;
153 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
155 window_space = item_to_window (Duple (c.samples[idx].x, 0.0));
156 if (window_space.x >= area.x0) break;
158 for (Points::size_type idx = c.n_samples; idx > left + 1; --idx) {
159 window_space = item_to_window (Duple (c.samples[idx].x, 0.0));
160 if (window_space.x <= area.x1) break;
164 /* draw line between samples */
165 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y));
166 context->move_to (window_space.x, window_space.y);
167 Coord last_x = round(window_space.x);
168 Coord last_y = round(window_space.y);
169 for (uint32_t idx = left + 1; idx < right; ++idx) {
170 window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y));
171 if (last_x == round(window_space.x)) continue;
172 if (last_y == round(window_space.y)) continue;
173 last_x = round(window_space.x);
174 last_y = round(window_space.y);
175 context->line_to (last_x - .5 , last_y + .5);
178 return context->copy_path ();
182 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
186 window_space = item_to_window (Duple(c.points.back().x, area.height()));
187 context->line_to (window_space.x, window_space.y);
188 window_space = item_to_window (Duple(c.points.front().x, area.height()));
189 context->line_to (window_space.x, window_space.y);
190 context->close_path();
192 window_space = item_to_window (Duple(c.points.back().x, 0.0));
193 context->line_to (window_space.x, window_space.y);
194 window_space = item_to_window (Duple(c.points.front().x, 0.0));
195 context->line_to (window_space.x, window_space.y);
196 context->close_path();
201 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
203 if (!_bounding_box) { return; }
204 if (_in.points.size() < 2) { return; }
205 if (_out.points.size() < 2) { return; }
207 Rect self = item_to_window (_bounding_box.get());
208 boost::optional<Rect> d = self.intersection (area);
210 Rect draw = d.get ();
213 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
216 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
217 * they will still occur, but cairo's clipping will hide them.
219 draw = draw.expand (4.0);
221 Cairo::Path *path_in = get_path(draw, context, _in);
222 Cairo::Path *path_out = get_path(draw, context, _out);
224 Color outline_shaded = _outline_color;
225 outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
227 Color fill_shaded = _fill_color;
228 fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
230 #define IS (_xfadeposition == Start)
232 /* fill primary fade */
233 context->begin_new_path ();
234 context->append_path (IS ? *path_in : *path_out);
235 close_path(draw, context, IS ?_in : _out, false);
236 set_source_rgba (context, _fill_color);
239 /* fill background fade */
241 context->begin_new_path ();
242 context->append_path (IS ? *path_in : *path_out);
243 close_path(draw, context, IS ? _in : _out, true);
244 //context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
246 context->begin_new_path ();
247 context->append_path (IS ? *path_out: *path_in);
248 close_path(draw, context, IS ? _out : _in, true);
249 set_source_rgba (context, fill_shaded);
250 //context->set_fill_rule (Cairo::FILL_RULE_WINDING);
254 /* draw lines over fills */
255 set_source_rgba (context, IS ? _outline_color : outline_shaded);
256 context->set_line_width (IS ? 1.0 : .5);
258 context->begin_new_path ();
259 context->append_path (*path_in);
262 set_source_rgba (context, IS ? outline_shaded :_outline_color);
263 context->set_line_width (IS ? .5 : 1.0);
265 context->begin_new_path ();
266 context->append_path (*path_out);