summaryrefslogtreecommitdiff
path: root/libs/surfaces/maschine2/m2_button.h
blob: cfe26b12d023fac440bc92ba128731779c888cc8 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
 *
 * 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, 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.
 */

#ifndef _ardour_surfaces_m2button_h_
#define _ardour_surfaces_m2button_h_

#include <stdint.h>
#include "gtkmm2ext/colors.h"
#include "pbd/signals.h"

namespace ArdourSurface {

class M2ButtonInterface
{
	public:
		M2ButtonInterface () {}
		virtual ~M2ButtonInterface () {}

		/* user API */
		PBD::Signal1<void, bool> changed;
		PBD::Signal0<void> pressed;
		PBD::Signal0<void> released;

		virtual void set_blinking (bool) {}
		virtual void set_color (uint32_t rgba) {}

		virtual bool is_pressed () const { return false; }
		virtual bool active () const { return is_pressed (); }

		virtual void ignore_release () {}

		// TODO allow to suspend *next* release signal
		// e.g. press + hold "grid", move encoder -> release "grid" -> noop

		/* internal API - called from device thread */
		virtual bool set_active (bool a) { return false; }
		virtual uint8_t lightness (float) const { return 0; }
		virtual uint32_t color (float) const { return 0; }
};

class M2Button : public M2ButtonInterface
{
	public:
		M2Button ()
			: M2ButtonInterface ()
			, _pressed (false)
			, _blink (false)
			, _ignore_release (false)
			, _lightness (0)
			, _rgba (0)
		{}

		/* user API */
		void set_blinking (bool en) {
			_blink = en;
		}

		virtual void set_color (uint32_t rgba) {
			_rgba = rgba;
			/* 7 bit color */
			const uint8_t r = ((rgba >> 24) & 0xff) >> 1;
			const uint8_t g = ((rgba >> 16) & 0xff) >> 1;
			const uint8_t b = ((rgba >>  8) & 0xff) >> 1;
			_lightness = std::max (r, std::max (g, b));
		}

		bool is_pressed () const { return _pressed; }

		void ignore_release () {
			if (_pressed) {
				_ignore_release = true;
			}
		}
		
		/* internal API - called from device thread */
		virtual bool set_active (bool a) {
			if (a == _pressed) {
				return false;
			}
			_pressed = a;

			if (a) {
				pressed (); /* EMIT SIGNAL */
			} else {
				if (_ignore_release) {
					_ignore_release = false;
				} else {
					released (); /* EMIT SIGNAL */
				}
			}
			changed (a); /* EMIT SIGNAL */
			return true;
		}

		uint8_t lightness (float blink) const {
			if (_blink && blink >= 0.f && blink <= 1.f) {
				return (uint8_t) floorf(blink * _lightness);
			}
			return _lightness;
		}

		uint32_t color (float blink) const {
			if (_blink && blink >= 0.f && blink <= 1.f) {
				Gtkmm2ext::HSV hsv (_rgba);
				Gtkmm2ext::HSV s (hsv.shade (blink));
				return s.color();
			}
			return _rgba;
		}

	protected:
		bool     _pressed;
		bool     _blink;
		bool     _ignore_release;
		uint8_t  _lightness;
		uint32_t _rgba;
};

class M2StatelessButton : public M2Button
{
	public:
		M2StatelessButton () : M2Button () {}

		bool set_active (bool a) {
			if (a == _pressed) {
				return false;
			}
			if (a) {
				set_color (0xffffffff);
			} else {
				set_color (0x000000ff);
			}
			return M2Button::set_active (a);
		}
};

class M2ToggleButton : public M2Button
{
	public:
		M2ToggleButton ()
		: M2Button ()
		, _active (false)
		{
			changed.connect_same_thread (changed_connection, boost::bind (&M2ToggleButton::change_event, this, _1));
		}

		PBD::Signal1<void, bool> toggled;
		bool active () const { return _active; }

	protected:
		void change_event (bool down) {
			if (down) { return; }
			_active = !_active;
			set_color (_active ? 0xffffffff : 0x000000ff);
			toggled (_active);
		}

		PBD::ScopedConnection changed_connection;
		bool _active;
};

class M2ToggleHoldButton : public M2Button
{
	public:
		M2ToggleHoldButton ()
		: M2Button ()
		, _active (false)
		, _active_on_release (false)
		{
			changed.connect_same_thread (changed_connection, boost::bind (&M2ToggleHoldButton::change_event, this, _1));
		}

		PBD::Signal1<void, bool> toggled;
		bool active () const { return _active; }
		void unset_active_on_release () { if (is_pressed ()) { _active_on_release = false; } }

	protected:
		void change_event (bool down) {
			if (down) {
				if (_active) {
					_active_on_release = false;
					return;
				}
				_active = true;
				_active_on_release = true;
			} else {
				if (_active == _active_on_release) {
					return;
				}
				_active = _active_on_release;
			}

			set_color (_active ? 0xffffffff : 0x000000ff);
			toggled (_active);
		}

		PBD::ScopedConnection changed_connection;
		bool _active;
		bool _active_on_release;
};


} /* namespace */
#endif /* _ardour_surfaces_m2button_h_ */