summaryrefslogtreecommitdiff
path: root/libs/canvas/note.cc
blob: 58431bdf6ff4607d813fcf4a5cb712465b0b20e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2018 Paul Davis <paul@linuxaudiosystems.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <cairomm/context.h>

#include "gtkmm2ext/colors.h"
#include "gtkmm2ext/rgb_macros.h"

#include "canvas/note.h"
#include "canvas/debug.h"

using namespace std;
using namespace ArdourCanvas;

bool Note::_show_velocity_bars = true;

void
Note::set_show_velocity_bars (bool yn)
{
	_show_velocity_bars = yn;
}

Note::Note (Canvas* c)
	: Rectangle (c)
	, _velocity (0.0)
	, _velocity_color (0)
{
}

Note::Note (Item* parent)
	: Rectangle (parent)
	, _velocity (0.0)
	, _velocity_color (0)
{
}

void
Note::set_velocity (double fract)
{
	_velocity = max (0.0, min (1.0, fract));
	redraw ();
}

void
Note::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
{
	Rectangle::render (area, context);

	if (_show_velocity_bars && _velocity > 0.0) {

		Rect self (item_to_window (Rectangle::get().translate (_position), false));

		if ((self.y1 - self.y0) < ((outline_width() * 2) + 1)) {
			/* not tall enough to show a velocity bar */
			return;
		}

		/* 2 pixel margin above and below (taking outline width into account).
		   outline_width() margin on left.
		   set width based on velocity.
		*/
		const double center = (self.y1 - self.y0) * 0.5;
		self.y1  = self.y0 + center + 2;
		self.y0  = self.y0 + center - 1;
		const double width = (self.x1 - self.x0) - (2 * outline_width());
		self.x0  = self.x0 + outline_width()+1;
		self.x1  = self.x0 + ((width-2) * _velocity);

		const Rect draw = self.intersection (area);

		if (!draw) {
			return;
		}

		Gtkmm2ext::set_source_rgba (context, _velocity_color);
		context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
		context->fill ();
	}
}

void
Note::set_fill_color (Gtkmm2ext::Color c)
{
	Fill::set_fill_color (c);
	_velocity_color = UINT_INTERPOLATE (c, 0x000000ff, 0.5);
}