2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef _ardour_surfaces_m2button_h_
20 #define _ardour_surfaces_m2button_h_
23 #include "gtkmm2ext/colors.h"
24 #include "pbd/signals.h"
26 namespace ArdourSurface {
28 class M2ButtonInterface
31 M2ButtonInterface () {}
32 virtual ~M2ButtonInterface () {}
35 PBD::Signal1<void, bool> changed;
36 PBD::Signal0<void> pressed;
37 PBD::Signal0<void> released;
39 virtual void set_blinking (bool) {}
40 virtual void set_color (uint32_t rgba) {}
42 virtual bool is_pressed () const { return false; }
43 virtual bool active () const { return is_pressed (); }
45 virtual void ignore_release () {}
47 // TODO allow to suspend *next* release signal
48 // e.g. press + hold "grid", move encoder -> release "grid" -> noop
50 /* internal API - called from device thread */
51 virtual bool set_active (bool a) { return false; }
52 virtual uint8_t lightness (float) const { return 0; }
53 virtual uint32_t color (float) const { return 0; }
56 class M2Button : public M2ButtonInterface
60 : M2ButtonInterface ()
63 , _ignore_release (false)
69 void set_blinking (bool en) {
73 virtual void set_color (uint32_t rgba) {
76 const uint8_t r = ((rgba >> 24) & 0xff) >> 1;
77 const uint8_t g = ((rgba >> 16) & 0xff) >> 1;
78 const uint8_t b = ((rgba >> 8) & 0xff) >> 1;
79 _lightness = std::max (r, std::max (g, b));
82 bool is_pressed () const { return _pressed; }
84 void ignore_release () {
86 _ignore_release = true;
90 /* internal API - called from device thread */
91 virtual bool set_active (bool a) {
98 pressed (); /* EMIT SIGNAL */
100 if (_ignore_release) {
101 _ignore_release = false;
103 released (); /* EMIT SIGNAL */
106 changed (a); /* EMIT SIGNAL */
110 uint8_t lightness (float blink) const {
111 if (_blink && blink >= 0.f && blink <= 1.f) {
112 return (uint8_t) floorf(blink * _lightness);
117 uint32_t color (float blink) const {
118 if (_blink && blink >= 0.f && blink <= 1.f) {
119 Gtkmm2ext::HSV hsv (_rgba);
120 Gtkmm2ext::HSV s (hsv.shade (blink));
129 bool _ignore_release;
134 class M2StatelessButton : public M2Button
137 M2StatelessButton () : M2Button () {}
139 bool set_active (bool a) {
144 set_color (0xffffffff);
146 set_color (0x000000ff);
148 return M2Button::set_active (a);
152 class M2ToggleButton : public M2Button
159 changed.connect_same_thread (changed_connection, boost::bind (&M2ToggleButton::change_event, this, _1));
162 PBD::Signal1<void, bool> toggled;
163 bool active () const { return _active; }
166 void change_event (bool down) {
167 if (down) { return; }
169 set_color (_active ? 0xffffffff : 0x000000ff);
173 PBD::ScopedConnection changed_connection;
177 class M2ToggleHoldButton : public M2Button
180 M2ToggleHoldButton ()
183 , _active_on_release (false)
185 changed.connect_same_thread (changed_connection, boost::bind (&M2ToggleHoldButton::change_event, this, _1));
188 PBD::Signal1<void, bool> toggled;
189 bool active () const { return _active; }
190 void unset_active_on_release () { if (is_pressed ()) { _active_on_release = false; } }
193 void change_event (bool down) {
196 _active_on_release = false;
200 _active_on_release = true;
202 if (_active == _active_on_release) {
205 _active = _active_on_release;
208 set_color (_active ? 0xffffffff : 0x000000ff);
212 PBD::ScopedConnection changed_connection;
214 bool _active_on_release;
219 #endif /* _ardour_surfaces_m2button_h_ */