remove debug output and get logic correct to cause first render to always use the...
[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 (Canvas* c)
34         : Item (c)
35         , points_per_segment (32)
36         , _xfadeposition (Start)
37         , _outline_color (0x000000ff)
38         , _fill_color (0x22448880)
39         , show_background_fade (true)
40 {
41 }
42
43 XFadeCurve::XFadeCurve (Canvas* c, XFadePosition pos)
44         : Item (c)
45         , points_per_segment (32)
46         , _xfadeposition (pos)
47         , _outline_color (0x000000ff)
48         , _fill_color (0x22448880)
49         , show_background_fade (true)
50 {
51 }
52
53 XFadeCurve::XFadeCurve (Item* parent)
54         : Item (parent)
55         , points_per_segment (32)
56         , _xfadeposition (Start)
57         , _outline_color (0x000000ff)
58         , _fill_color (0x22448880)
59         , show_background_fade (true)
60 {
61 }
62
63 XFadeCurve::XFadeCurve (Item* parent, XFadePosition pos)
64         : Item (parent)
65         , points_per_segment (32)
66         , _xfadeposition (pos)
67         , _outline_color (0x000000ff)
68         , _fill_color (0x22448880)
69         , show_background_fade (true)
70 {
71 }
72
73 void
74 XFadeCurve::compute_bounding_box () const
75 {
76         if (!_in.points.empty() && !_out.points.empty()) {
77
78                 Rect bbox;
79                 Points::const_iterator i;
80
81                 if (!_in.points.empty()) {
82                         i = _in.points.begin();
83                         bbox.x0 = bbox.x1 = i->x;
84                         bbox.y0 = bbox.y1 = i->y;
85
86                         ++i;
87
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);
93                                 ++i;
94                         }
95                 } else {
96                         i = _out.points.begin();
97                         bbox.x0 = bbox.x1 = i->x;
98                         bbox.y0 = bbox.y1 = i->y;
99                 }
100
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);
108                                 ++i;
109                         }
110                 }
111
112                 _bounding_box = bbox.expand (1.0);
113
114         } else {
115                 _bounding_box = boost::optional<Rect> ();
116         }
117
118         _bounding_box_dirty = false;
119 }
120
121 void
122 XFadeCurve::set_inout (Points const & in, Points const & out)
123 {
124         if (_in.points == in && _out.points == out) {
125                 return;
126         }
127         begin_change ();
128         _in.points = in;
129         _out.points = out;
130         _bounding_box_dirty = true;
131         interpolate ();
132         end_change ();
133 }
134
135 void
136 XFadeCurve::set_points_per_segment (uint32_t n)
137 {
138         points_per_segment = n;
139         interpolate ();
140         redraw ();
141 }
142
143 void
144 XFadeCurve::interpolate ()
145 {
146         _in.samples.clear ();
147         InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
148         _in.n_samples = _in.samples.size();
149
150         _out.samples.clear ();
151         InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
152         _out.n_samples = _out.samples.size();
153 }
154
155 Cairo::Path *
156 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
157 {
158         assert(c.points.size() > 1);
159         context->begin_new_path ();
160         Duple window_space;
161
162         if (c.points.size () == 2) {
163
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);
168
169         } else {
170
171                 /* find left and right-most sample */
172                 Points::size_type left = 0;
173                 Points::size_type right = c.n_samples - 1;
174
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) {
177                         left = idx;
178                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
179                         if (window_space.x >= area.x0) break;
180                 }
181                 for (Points::size_type idx = c.n_samples; idx >= left;) {
182                         --idx;
183                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
184                         if (window_space.x <= area.x1) break;
185                         right = idx;
186                 }
187
188                 assert(left < right);
189                 assert(left < c.n_samples);
190                 assert(right < c.n_samples);
191
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);
198                 }
199         }
200         return context->copy_path ();
201 }
202
203 void
204 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
205 {
206         Duple window_space;
207         if (inside) {
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();
213         } else {
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();
219         }
220 }
221
222 void
223 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
224 {
225         if (!_bounding_box) { return; }
226         if (_in.points.size() < 2) { return; }
227         if (_out.points.size() < 2) { return; }
228
229         Rect self = item_to_window (_bounding_box.get());
230         boost::optional<Rect> d = self.intersection (area);
231         assert (d);
232         Rect draw = d.get ();
233
234         context->save ();
235         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
236         context->clip ();
237
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.
240          */
241         draw = draw.expand (4.0);
242
243         Cairo::Path *path_in = get_path(draw, context, _in);
244         Cairo::Path *path_out = get_path(draw, context, _out);
245
246         Color outline_shaded = _outline_color;
247         outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
248
249         Color fill_shaded = _fill_color;
250         fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
251
252 #define IS_START (_xfadeposition == Start)
253
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);
259         context->fill ();
260
261         if (show_background_fade) {
262                 /* fill background fade */
263                 context->save ();
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);
268                 context->clip ();
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);
274                 context->fill ();
275                 context->restore ();
276         }
277
278         /* draw lines over fills */
279         /* fade in line */
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);
283         
284                 context->begin_new_path ();
285                 context->append_path (*path_in);
286                 context->stroke();
287         }
288
289         /* fade out line */
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);
293
294                 context->begin_new_path ();
295                 context->append_path (*path_out);
296                 context->stroke();
297         }
298
299         context->restore ();
300
301         delete path_in;
302         delete path_out;
303 }