merge with master and fix 4 conflicts by hand
[ardour.git] / libs / canvas / xfade_curve.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3     Copyright (C) 2014 Robin Gareus <robin@gareus.org>
4
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.
9
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.
14
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.
18
19 */
20
21 #include <cmath>
22 #include <exception>
23 #include <algorithm>
24
25 #include "canvas/xfade_curve.h"
26 #include "canvas/interpolated_curve.h"
27 #include "canvas/utils.h"
28
29 using namespace ArdourCanvas;
30 using std::min;
31 using std::max;
32
33 XFadeCurve::XFadeCurve (Group* parent)
34         : Item (parent)
35         , points_per_segment (32)
36         , _xfadeposition (Start)
37         , _outline_color (0x000000ff)
38         , _fill_color (0x22448880)
39 {
40 }
41
42 XFadeCurve::XFadeCurve (Group* parent, XFadePosition pos)
43         : Item (parent)
44         , points_per_segment (32)
45         , _xfadeposition (pos)
46         , _outline_color (0x000000ff)
47         , _fill_color (0x22448880)
48 {
49 }
50
51 void
52 XFadeCurve::compute_bounding_box () const
53 {
54         if (!_in.points.empty() && !_out.points.empty()) {
55
56                 Rect bbox;
57                 Points::const_iterator i;
58
59                 if (!_in.points.empty()) {
60                         i = _in.points.begin();
61                         bbox.x0 = bbox.x1 = i->x;
62                         bbox.y0 = bbox.y1 = i->y;
63
64                         ++i;
65
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);
71                                 ++i;
72                         }
73                 } else {
74                         i = _out.points.begin();
75                         bbox.x0 = bbox.x1 = i->x;
76                         bbox.y0 = bbox.y1 = i->y;
77                 }
78
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);
86                                 ++i;
87                         }
88                 }
89
90                 _bounding_box = bbox.expand (1.0);
91
92         } else {
93                 _bounding_box = boost::optional<Rect> ();
94         }
95
96         _bounding_box_dirty = false;
97 }
98
99 void
100 XFadeCurve::set_inout (Points const & in, Points const & out)
101 {
102         if (_in.points == in && _out.points == out) {
103                 return;
104         }
105         begin_change ();
106         _in.points = in;
107         _out.points = out;
108         _bounding_box_dirty = true;
109         interpolate ();
110         end_change ();
111 }
112
113 void
114 XFadeCurve::set_points_per_segment (uint32_t n)
115 {
116         points_per_segment = n;
117         interpolate ();
118         redraw ();
119 }
120
121 void
122 XFadeCurve::interpolate ()
123 {
124         _in.samples.clear ();
125         InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
126         _in.n_samples = _in.samples.size();
127
128         _out.samples.clear ();
129         InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
130         _out.n_samples = _out.samples.size();
131 }
132
133 Cairo::Path *
134 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
135 {
136         assert(c.points.size() > 1);
137         context->begin_new_path ();
138         Duple window_space;
139
140         if (c.points.size () == 2) {
141
142                 window_space = item_to_window (c.points.front(), false);
143                 context->move_to (window_space.x, window_space.y);
144                 window_space = item_to_window (c.points.back(), false);
145                 context->line_to (window_space.x, window_space.y);
146
147         } else {
148
149                 /* find left and right-most sample */
150                 Points::size_type left = 0;
151                 Points::size_type right = c.n_samples;
152
153                 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
154                         left = idx;
155                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
156                         if (window_space.x >= area.x0) break;
157                 }
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), false);
160                         if (window_space.x <= area.x1) break;
161                         right = idx;
162                 }
163
164                 /* draw line between samples */
165                 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
166                 context->move_to (window_space.x, window_space.y);
167                 for (uint32_t idx = left + 1; idx < right; ++idx) {
168                         window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
169                         context->line_to (window_space.x, window_space.y);
170                 }
171         }
172         return context->copy_path ();
173 }
174
175 void
176 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
177 {
178         Duple window_space;
179         if (inside) {
180                 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
181                 context->line_to (window_space.x, window_space.y);
182                 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
183                 context->line_to (window_space.x, window_space.y);
184                 context->close_path();
185         } else {
186                 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
187                 context->line_to (window_space.x, window_space.y);
188                 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
189                 context->line_to (window_space.x, window_space.y);
190                 context->close_path();
191         }
192 }
193
194 void
195 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
196 {
197         if (!_bounding_box) { return; }
198         if (_in.points.size() < 2) { return; }
199         if (_out.points.size() < 2) { return; }
200
201         Rect self = item_to_window (_bounding_box.get());
202         boost::optional<Rect> d = self.intersection (area);
203         assert (d);
204         Rect draw = d.get ();
205
206         context->save ();
207         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
208         context->clip ();
209
210         /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
211          * they will still occur, but cairo's clipping will hide them.
212          */
213         draw = draw.expand (4.0);
214
215         Cairo::Path *path_in = get_path(draw, context, _in);
216         Cairo::Path *path_out = get_path(draw, context, _out);
217
218         Color outline_shaded = _outline_color;
219         outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
220
221         Color fill_shaded = _fill_color;
222         fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
223
224 #define IS (_xfadeposition == Start)
225
226         /* fill primary fade */
227         context->begin_new_path ();
228         context->append_path (IS ? *path_in : *path_out);
229         close_path(draw, context, IS ?_in : _out, false);
230         set_source_rgba (context, _fill_color);
231         context->fill ();
232
233         /* fill background fade */
234         context->save ();
235         context->begin_new_path ();
236         context->append_path (IS ? *path_in : *path_out);
237         close_path(draw, context, IS ? _in : _out, true);
238         context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
239         context->clip ();
240         context->begin_new_path ();
241         context->append_path (IS ? *path_out: *path_in);
242         close_path(draw, context, IS ? _out : _in, true);
243         set_source_rgba (context, fill_shaded);
244         context->set_fill_rule (Cairo::FILL_RULE_WINDING);
245         context->fill ();
246         context->restore ();
247
248         /* draw lines over fills */
249         set_source_rgba (context, IS ? _outline_color : outline_shaded);
250         context->set_line_width (IS ? 1.0 : .5);
251
252         context->begin_new_path ();
253         context->append_path (*path_in);
254         context->stroke();
255
256         set_source_rgba (context, IS ? outline_shaded :_outline_color);
257         context->set_line_width (IS ? .5 : 1.0);
258
259         context->begin_new_path ();
260         context->append_path (*path_out);
261         context->stroke();
262
263         context->restore ();
264
265         delete path_in;
266         delete path_out;
267 }