fix initialization order
[ardour.git] / libs / canvas / xfade_curve.cc
1 /*
2  * Copyright (C) 2014-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2014-2017 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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <cmath>
21 #include <exception>
22 #include <algorithm>
23
24 #include "canvas/xfade_curve.h"
25 #include "canvas/interpolated_curve.h"
26
27 using namespace ArdourCanvas;
28 using std::min;
29 using std::max;
30
31 static const bool show_bg_fades = true;
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 (show_bg_fades)
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 (show_bg_fades)
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 (show_bg_fades)
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 (show_bg_fades)
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 = 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                 assert (left < right);
176                 // we should really really do a binary search rather than iterate
177                 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
178                         left = idx;
179                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
180                         if (window_space.x >= area.x0) break;
181                 }
182                 for (Points::size_type idx = c.n_samples - 1; right > left;) {
183                         if (--idx <= left) break;
184                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
185                         if (window_space.x <= area.x1) break;
186                         right = idx;
187                 }
188
189                 assert(left < right);
190                 assert(left < c.n_samples);
191                 assert(right < c.n_samples);
192
193                 /* draw line between samples */
194                 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
195                 context->move_to (window_space.x, window_space.y);
196                 for (uint32_t idx = left + 1; idx <= right; ++idx) {
197                         window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
198                         context->line_to (window_space.x, window_space.y);
199                 }
200         }
201         return context->copy_path ();
202 }
203
204 void
205 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
206 {
207         Duple window_space;
208         if (inside) {
209                 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
210                 context->line_to (window_space.x, window_space.y);
211                 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
212                 context->line_to (window_space.x, window_space.y);
213                 context->close_path();
214         } else {
215                 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
216                 context->line_to (window_space.x, window_space.y);
217                 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
218                 context->line_to (window_space.x, window_space.y);
219                 context->close_path();
220         }
221 }
222
223 void
224 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
225 {
226         if (!_bounding_box) { return; }
227         if (_in.points.size() < 2) { return; }
228         if (_out.points.size() < 2) { return; }
229
230         Rect self = item_to_window (_bounding_box);
231         Rect d = self.intersection (area);
232         assert (d);
233         Rect draw = d;
234
235         context->save ();
236         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
237         context->clip ();
238
239         /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
240          * they will still occur, but cairo's clipping will hide them.
241          */
242         draw = draw.expand (4.0);
243
244         Cairo::Path *path_in = get_path(draw, context, _in);
245         Cairo::Path *path_out = get_path(draw, context, _out);
246
247         Gtkmm2ext::Color outline_shaded = _outline_color;
248         outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
249
250         Gtkmm2ext::Color fill_shaded = _fill_color;
251         fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
252
253 #define IS_START (_xfadeposition == Start)
254
255         /* fill primary fade */
256         context->begin_new_path ();
257         context->append_path (IS_START ? *path_in : *path_out);
258         close_path(draw, context, IS_START ?_in : _out, false);
259         Gtkmm2ext::set_source_rgba (context, _fill_color);
260         context->fill ();
261
262         if (show_background_fade) {
263                 /* fill background fade */
264                 context->save ();
265                 context->begin_new_path ();
266                 context->append_path (IS_START ? *path_in : *path_out);
267                 close_path(draw, context, IS_START ? _in : _out, true);
268                 context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
269                 context->clip ();
270                 context->begin_new_path ();
271                 context->append_path (IS_START ? *path_out: *path_in);
272                 close_path(draw, context, IS_START ? _out : _in, true);
273                 Gtkmm2ext::set_source_rgba (context, fill_shaded);
274                 context->set_fill_rule (Cairo::FILL_RULE_WINDING);
275                 context->fill ();
276                 context->restore ();
277         }
278
279         /* draw lines over fills */
280         /* fade in line */
281         if (IS_START || show_background_fade) {
282                 Gtkmm2ext::set_source_rgba (context, IS_START ? _outline_color : outline_shaded);
283                 context->set_line_width (IS_START ? 1.0 : .5);
284
285                 context->begin_new_path ();
286                 context->append_path (*path_in);
287                 context->stroke();
288         }
289
290         /* fade out line */
291         if (!IS_START || show_background_fade) {
292                 Gtkmm2ext::set_source_rgba (context, IS_START ? outline_shaded :_outline_color);
293                 context->set_line_width (IS_START ? .5 : 1.0);
294
295                 context->begin_new_path ();
296                 context->append_path (*path_out);
297                 context->stroke();
298         }
299
300         context->restore ();
301
302         delete path_in;
303         delete path_out;
304 }