2 * Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2014 David Robillard <d@drobilla.net>
4 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "gtkmm2ext/keyboard.h"
26 #include "evoral/Note.hpp"
28 #include "canvas/text.h"
30 #include "note_base.h"
31 #include "public_editor.h"
32 #include "editing_syms.h"
34 #include "midi_region_view.h"
37 using namespace Gtkmm2ext;
38 using ARDOUR::MidiModel;
39 using namespace ArdourCanvas;
41 /// dividing the hue circle in 16 parts, hand adjusted for equal look, courtesy Thorsten Wilms
42 const uint32_t NoteBase::midi_channel_colors[16] = {
43 0xd32d2dff, 0xd36b2dff, 0xd3972dff, 0xd3d12dff,
44 0xa0d32dff, 0x7dd32dff, 0x2dd45eff, 0x2dd3c4ff,
45 0x2da5d3ff, 0x2d6fd3ff, 0x432dd3ff, 0x662dd3ff,
46 0x832dd3ff, 0xa92dd3ff, 0xd32dbfff, 0xd32d67ff
49 bool NoteBase::_color_init = false;
50 Gtkmm2ext::Color NoteBase::_selected_col = 0;
51 Gtkmm2ext::SVAModifier NoteBase::color_modifier;
52 Gtkmm2ext::Color NoteBase::velocity_color_table[128];
55 NoteBase::set_colors ()
57 for (uint8_t i = 0; i < 128; ++i) {
58 velocity_color_table[i] = 0; /* out of bounds because zero alpha makes no sense */
60 _selected_col = UIConfiguration::instance().color ("midi note selected outline");
61 color_modifier = UIConfiguration::instance().modifier ("midi note");
64 NoteBase::NoteBase(MidiRegionView& region, bool with_events, const boost::shared_ptr<NoteType> note)
70 , _with_events (with_events)
73 , _mouse_x_fraction (-1.0)
74 , _mouse_y_fraction (-1.0)
77 NoteBase::set_colors();
84 _region.note_deleted (this);
90 NoteBase::set_item (Item* item)
93 _item->set_data ("notebase", this);
96 _item->Event.connect (sigc::mem_fun (*this, &NoteBase::event_handler));
101 NoteBase::invalidate ()
107 NoteBase::validate ()
113 NoteBase::show_velocity()
116 _text = new Text (_item->parent ());
117 _text->set_ignore_events (true);
118 _text->set_color (UIConfiguration::instance().color_mod ("midi note velocity text", "midi note velocity text"));
119 _text->set_alignment (Pango::ALIGN_CENTER);
122 _text->set_x_position ((x0() + x1()) / 2);
123 _text->set_y_position ((y0() + y1()) / 2);
124 ostringstream velo(ios::ate);
125 velo << int(_note->velocity());
126 _text->set (velo.str ());
128 _text->raise_to_top();
132 NoteBase::hide_velocity()
139 NoteBase::on_channel_selection_change(uint16_t selection)
141 // make note change its color if its channel is not marked active
142 if ( (selection & (1 << _note->channel())) == 0 ) {
143 const Gtkmm2ext::Color inactive_ch = UIConfiguration::instance().color ("midi note inactive channel");
144 set_fill_color(inactive_ch);
145 set_outline_color(calculate_outline(inactive_ch, _selected));
147 // set the color according to the notes selection state
148 set_selected(_selected);
150 // this forces the item to update..... maybe slow...
156 NoteBase::on_channel_change(uint8_t channel)
158 _region.note_selected(this, true);
159 _region.change_channel(channel);
163 NoteBase::set_selected(bool selected)
169 _selected = selected;
171 const uint32_t base_col = base_color();
172 set_fill_color (base_col);
174 set_outline_color(calculate_outline(base_col, _selected));
177 #define SCALE_USHORT_TO_UINT8_T(x) ((x) / 257)
180 NoteBase::base_color()
182 using namespace ARDOUR;
184 ColorMode mode = _region.color_mode();
186 const uint8_t min_opacity = 15;
187 uint8_t opacity = std::max(min_opacity, uint8_t(_note->velocity() + _note->velocity()));
192 const uint32_t region_color = _region.midi_stream_view()->get_region_color();
193 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (region_color, opacity), _selected_col,
198 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (NoteBase::midi_channel_colors[_note->channel()], opacity),
202 if (UIConfiguration::instance().get_use_note_color_for_velocity()) {
203 return meter_style_fill_color(_note->velocity(), selected());
205 const uint32_t region_color = _region.midi_stream_view()->get_region_color();
206 return UINT_INTERPOLATE (UINT_RGBA_CHANGE_A (region_color, opacity), _selected_col,
215 NoteBase::set_mouse_fractions (GdkEvent* ev)
218 bool set_cursor = false;
221 case GDK_MOTION_NOTIFY:
226 case GDK_ENTER_NOTIFY:
231 case GDK_BUTTON_PRESS:
232 case GDK_BUTTON_RELEASE:
237 _mouse_x_fraction = -1.0;
238 _mouse_y_fraction = -1.0;
242 boost::optional<ArdourCanvas::Rect> bbox = _item->bounding_box ();
245 _item->canvas_to_item (ix, iy);
247 /* hmm, something wrong here. w2i should give item-local coordinates
248 but it doesn't. for now, finesse this.
250 ix = ix - bbox.get().x0;
251 iy = iy - bbox.get().y0;
253 /* fraction of width/height */
258 xf = ix / bbox.get().width ();
259 yf = iy / bbox.get().height ();
261 if (xf != _mouse_x_fraction || yf != _mouse_y_fraction) {
265 _mouse_x_fraction = xf;
266 _mouse_y_fraction = yf;
269 if (big_enough_to_trim()) {
270 _region.note_mouse_position (_mouse_x_fraction, _mouse_y_fraction, set_cursor);
272 /* pretend the mouse is in the middle, because this is not big enough
275 _region.note_mouse_position (0.5, 0.5, set_cursor);
281 NoteBase::event_handler (GdkEvent* ev)
283 PublicEditor& editor = _region.get_time_axis_view().editor();
284 if (!editor.internal_editing()) {
289 case GDK_ENTER_NOTIFY:
290 _region.note_entered (this);
291 set_mouse_fractions (ev);
294 case GDK_LEAVE_NOTIFY:
295 set_mouse_fractions (ev);
296 _region.note_left (this);
299 case GDK_MOTION_NOTIFY:
300 set_mouse_fractions (ev);
303 case GDK_BUTTON_PRESS:
304 set_mouse_fractions (ev);
307 case GDK_BUTTON_RELEASE:
308 set_mouse_fractions (ev);
315 return editor.canvas_note_event (ev, _item);
319 NoteBase::mouse_near_ends () const
321 return (_mouse_x_fraction >= 0.0 && _mouse_x_fraction < 0.25) ||
322 (_mouse_x_fraction >= 0.75 && _mouse_x_fraction < 1.0);
326 NoteBase::big_enough_to_trim () const
328 return (x1() - x0()) > 10;
333 NoteBase::meter_style_fill_color(uint8_t vel, bool /* selected */)
335 /* note that because vel is uint8_t, we don't need bounds checking for
336 the color lookup table.
339 if (velocity_color_table[vel] == 0) {
341 Gtkmm2ext::Color col;
344 col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color0"), UIConfiguration::instance().color ("midi meter color1"), (vel / 32.0));
345 col = Gtkmm2ext::change_alpha (col, color_modifier.a());
346 } else if (vel < 64) {
347 col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color2"), UIConfiguration::instance().color ("midi meter color3"), ((vel-32) / 32.0));
348 col = Gtkmm2ext::change_alpha (col, color_modifier.a());
349 } else if (vel < 100) {
350 col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color4"), UIConfiguration::instance().color ("midi meter color5"), ((vel-64) / 36.0));
351 col = Gtkmm2ext::change_alpha (col, color_modifier.a());
352 } else if (vel < 112) {
353 col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color6"), UIConfiguration::instance().color ("midi meter color7"), ((vel-100) / 12.0));
354 col = Gtkmm2ext::change_alpha (col, color_modifier.a());
356 col = UINT_INTERPOLATE(UIConfiguration::instance().color ("midi meter color8"), UIConfiguration::instance().color ("midi meter color9"), ((vel-112) / 17.0));
357 col = Gtkmm2ext::change_alpha (col, color_modifier.a());
360 velocity_color_table[vel] = col;
364 return velocity_color_table[vel];