Only show user-presets in favorite sidebar
[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
28 using namespace ArdourCanvas;
29 using std::min;
30 using std::max;
31
32 #ifdef USE_TRACKS_CODE_FEATURES
33 static const bool show_bg_fades = false;
34 #else
35 static const bool show_bg_fades = true;
36 #endif
37
38 XFadeCurve::XFadeCurve (Canvas* c)
39         : Item (c)
40         , points_per_segment (32)
41         , _xfadeposition (Start)
42         , _outline_color (0x000000ff)
43         , _fill_color (0x22448880)
44         , show_background_fade (show_bg_fades)
45 {
46 }
47
48 XFadeCurve::XFadeCurve (Canvas* c, XFadePosition pos)
49         : Item (c)
50         , points_per_segment (32)
51         , _xfadeposition (pos)
52         , _outline_color (0x000000ff)
53         , _fill_color (0x22448880)
54         , show_background_fade (show_bg_fades)
55 {
56 }
57
58 XFadeCurve::XFadeCurve (Item* parent)
59         : 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)
65 {
66 }
67
68 XFadeCurve::XFadeCurve (Item* parent, XFadePosition pos)
69         : Item (parent)
70         , points_per_segment (32)
71         , _xfadeposition (pos)
72         , _outline_color (0x000000ff)
73         , _fill_color (0x22448880)
74         , show_background_fade (show_bg_fades)
75 {
76 }
77
78 void
79 XFadeCurve::compute_bounding_box () const
80 {
81         if (!_in.points.empty() && !_out.points.empty()) {
82
83                 Rect bbox;
84                 Points::const_iterator i;
85
86                 if (!_in.points.empty()) {
87                         i = _in.points.begin();
88                         bbox.x0 = bbox.x1 = i->x;
89                         bbox.y0 = bbox.y1 = i->y;
90
91                         ++i;
92
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);
98                                 ++i;
99                         }
100                 } else {
101                         i = _out.points.begin();
102                         bbox.x0 = bbox.x1 = i->x;
103                         bbox.y0 = bbox.y1 = i->y;
104                 }
105
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);
113                                 ++i;
114                         }
115                 }
116
117                 _bounding_box = bbox.expand (1.0);
118
119         } else {
120                 _bounding_box = Rect ();
121         }
122
123         _bounding_box_dirty = false;
124 }
125
126 void
127 XFadeCurve::set_inout (Points const & in, Points const & out)
128 {
129         if (_in.points == in && _out.points == out) {
130                 return;
131         }
132         begin_change ();
133         _in.points = in;
134         _out.points = out;
135         _bounding_box_dirty = true;
136         interpolate ();
137         end_change ();
138 }
139
140 void
141 XFadeCurve::set_points_per_segment (uint32_t n)
142 {
143         points_per_segment = n;
144         interpolate ();
145         redraw ();
146 }
147
148 void
149 XFadeCurve::interpolate ()
150 {
151         _in.samples.clear ();
152         InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
153         _in.n_samples = _in.samples.size();
154
155         _out.samples.clear ();
156         InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
157         _out.n_samples = _out.samples.size();
158 }
159
160 Cairo::Path *
161 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
162 {
163         assert(c.points.size() > 1);
164         context->begin_new_path ();
165         Duple window_space;
166
167         if (c.points.size () == 2) {
168
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);
173
174         } else {
175
176                 /* find left and right-most sample */
177                 Points::size_type left = 0;
178                 Points::size_type right = c.n_samples - 1;
179
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) {
183                         left = idx;
184                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
185                         if (window_space.x >= area.x0) break;
186                 }
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;
191                         right = idx;
192                 }
193
194                 assert(left < right);
195                 assert(left < c.n_samples);
196                 assert(right < c.n_samples);
197
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);
204                 }
205         }
206         return context->copy_path ();
207 }
208
209 void
210 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
211 {
212         Duple window_space;
213         if (inside) {
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();
219         } else {
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();
225         }
226 }
227
228 void
229 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
230 {
231         if (!_bounding_box) { return; }
232         if (_in.points.size() < 2) { return; }
233         if (_out.points.size() < 2) { return; }
234
235         Rect self = item_to_window (_bounding_box);
236         Rect d = self.intersection (area);
237         assert (d);
238         Rect draw = d;
239
240         context->save ();
241         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
242         context->clip ();
243
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.
246          */
247         draw = draw.expand (4.0);
248
249         Cairo::Path *path_in = get_path(draw, context, _in);
250         Cairo::Path *path_out = get_path(draw, context, _out);
251
252         Gtkmm2ext::Color outline_shaded = _outline_color;
253         outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
254
255         Gtkmm2ext::Color fill_shaded = _fill_color;
256         fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
257
258 #define IS_START (_xfadeposition == Start)
259
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);
265         context->fill ();
266
267         if (show_background_fade) {
268                 /* fill background fade */
269                 context->save ();
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);
274                 context->clip ();
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);
280                 context->fill ();
281                 context->restore ();
282         }
283
284         /* draw lines over fills */
285         /* fade in line */
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);
289
290                 context->begin_new_path ();
291                 context->append_path (*path_in);
292                 context->stroke();
293         }
294
295         /* fade out line */
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);
299
300                 context->begin_new_path ();
301                 context->append_path (*path_out);
302                 context->stroke();
303         }
304
305         context->restore ();
306
307         delete path_in;
308         delete path_out;
309 }