1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
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 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <cmath>
20 
21 #include <cairomm/context.h>
22 
23 #include "ardour/automation_control.h"
24 #include "ardour/value_as_string.h"
25 #include "ardour/dB.h"
26 #include "ardour/utils.h"
27 
28 #include "gtkmm2ext/colors.h"
29 #include "gtkmm2ext/gui_thread.h"
30 #include "gtkmm2ext/rgb_macros.h"
31 
32 #include "canvas/text.h"
33 
34 #include "maschine2.h"
35 #include "m2controls.h"
36 
37 #include "ui_knob.h"
38 
39 #include "pbd/i18n.h"
40 
41 #ifdef __APPLE__
42 #define Rect ArdourCanvas::Rect
43 #endif
44 
45 using namespace PBD;
46 using namespace ARDOUR;
47 using namespace ArdourSurface;
48 using namespace ArdourCanvas;
49 
Maschine2Knob(PBD::EventLoop * el,Item * parent)50 Maschine2Knob::Maschine2Knob (PBD::EventLoop* el, Item* parent)
51 	: Container (parent)
52 	, _ctrl (0)
53 	, _eventloop (el)
54 	, _radius (11)
55 	, _val (0)
56 	, _normal (0)
57 {
58 	Pango::FontDescription fd ("Sans 10px");
59 
60 	text = new Text (this);
61 	text->set_font_description (fd);
62 	text->set_position (Duple (-_radius, _radius + 2));
63 	text->set_color (0xffffffff);
64 	_bounding_box_dirty = true;
65 }
66 
~Maschine2Knob()67 Maschine2Knob::~Maschine2Knob ()
68 {
69 }
70 
71 void
render(Rect const & area,Cairo::RefPtr<Cairo::Context> context) const72 Maschine2Knob::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
73 {
74 	if (!_controllable) {
75 		/* no controllable, nothing to draw */
76 		return;
77 	}
78 
79 	// TODO consider a "bar" circular shape + 1bit b/w is ugly
80 
81 	const float scale = 2.f * _radius;
82 	const float pointer_thickness = std::max (1.f, 3.f * (scale / 80.f));
83 
84 	const float start_angle = ((180.f - 65.f) * M_PI) / 180.f;
85 	const float end_angle = ((360.f + 65.f) * M_PI) / 180.f;
86 
87 	float zero = 0;
88 
89 	const float value_angle = start_angle + (_val * (end_angle - start_angle));
90 	const float zero_angle = start_angle + (zero * (end_angle - start_angle));
91 
92 	float value_x = cos (value_angle);
93 	float value_y = sin (value_angle);
94 
95 	/* translate so that all coordinates are based on the center of the
96 	 * knob (which is also its position()
97 	 */
98 	context->save ();
99 	Duple origin = item_to_window (Duple (0, 0));
100 	context->translate (origin.x - 0.5, origin.y - 0.5);
101 	context->begin_new_path ();
102 
103 	float center_radius = 0.48*scale;
104 	float border_width = 0.8;
105 
106 	const bool arc = true;
107 
108 	if (arc) {
109 		center_radius = scale * 0.33;
110 
111 		float inner_progress_radius = scale * 0.38;
112 		float outer_progress_radius = scale * 0.48;
113 		float progress_width = (outer_progress_radius-inner_progress_radius);
114 		float progress_radius = inner_progress_radius + progress_width/2.0;
115 
116 		// draw the arc
117 		context->set_source_rgb (1, 1, 1);
118 		context->set_line_width (progress_width);
119 		if (zero_angle > value_angle) {
120 			context->arc (0, 0, progress_radius, value_angle, zero_angle);
121 		} else {
122 			context->arc (0, 0, progress_radius, zero_angle, value_angle);
123 		}
124 		context->stroke ();
125 	}
126 
127 	// knob body
128 	context->set_line_width (border_width);
129 	context->set_source_rgb (1, 1, 1);
130 	context->arc (0, 0, center_radius, 0, 2.0*G_PI);
131 	context->fill ();
132 
133 	// line
134 	context->set_source_rgb (0, 0, 0);
135 	context->set_line_cap (Cairo::LINE_CAP_ROUND);
136 	context->set_line_width (pointer_thickness);
137 	context->move_to ((center_radius * value_x), (center_radius * value_y));
138 	context->line_to (((center_radius * 0.2) * value_x), ((center_radius * 0.2) * value_y));
139 	context->stroke ();
140 
141 	/* reset all translations, scaling etc. */
142 	context->restore ();
143 
144 	render_children (area, context);
145 }
146 
147  void
compute_bounding_box() const148 Maschine2Knob::compute_bounding_box () const
149 {
150 	if (!_canvas || _radius == 0) {
151 		_bounding_box = Rect ();
152 		_bounding_box_dirty = false;
153 		return;
154 	}
155 
156 	if (_bounding_box_dirty) {
157 		_bounding_box = Rect (- _radius, - _radius, _radius, _radius);
158 		_bounding_box_dirty = false;
159 	}
160 
161 	add_child_bounding_boxes ();
162 }
163 
164 void
set_controllable(boost::shared_ptr<AutomationControl> c)165 Maschine2Knob::set_controllable (boost::shared_ptr<AutomationControl> c)
166 {
167 	watch_connection.disconnect ();
168 
169 	if (!c) {
170 		_controllable.reset ();
171 		return;
172 	}
173 
174 	_controllable = c;
175 	_controllable->Changed.connect (watch_connection, invalidator(*this), boost::bind (&Maschine2Knob::controllable_changed, this), _eventloop);
176 
177 	controllable_changed ();
178 	// set _controllable->desc()->label
179 }
180 
181 void
set_control(M2EncoderInterface * ctrl)182 Maschine2Knob::set_control (M2EncoderInterface* ctrl)
183 {
184 	encoder_connection.disconnect ();
185 	_ctrl = ctrl;
186 	if (!ctrl) {
187 		return;
188 	}
189 	ctrl->changed.connect_same_thread (encoder_connection, boost::bind (&Maschine2Knob::encoder_changed, this, _1));
190 }
191 
192 void
encoder_changed(int delta)193 Maschine2Knob::encoder_changed (int delta)
194 {
195 	if (!_controllable) {
196 		return;
197 	}
198 	const double d = delta * 0.5 / _ctrl->range ();
199 	boost::shared_ptr<AutomationControl> ac = _controllable;
200 	ac->set_value (ac->interface_to_internal (std::min (ac->upper(), std::max (ac->lower(), ac->internal_to_interface (ac->get_value()) + d))), PBD::Controllable::UseGroup);
201 }
202 
203 void
controllable_changed()204 Maschine2Knob::controllable_changed ()
205 {
206 	if (_controllable) {
207 		_normal = _controllable->internal_to_interface (_controllable->normal());
208 		_val = _controllable->internal_to_interface (_controllable->get_value());
209 
210 		const ParameterDescriptor& desc (_controllable->desc());
211 
212 		char buf[64];
213 		switch (_controllable->parameter().type()) {
214 			case ARDOUR::PanAzimuthAutomation:
215 				snprintf (buf, sizeof (buf), _("L:%3d R:%3d"), (int) rint (100.0 * (1.0 - _val)), (int) rint (100.0 * _val));
216 				text->set (buf);
217 				break;
218 
219 			case ARDOUR::PanWidthAutomation:
220 				snprintf (buf, sizeof (buf), "%d%%", (int) floor (_val*100));
221 				text->set (buf);
222 				break;
223 
224 			case ARDOUR::GainAutomation:
225 			case ARDOUR::BusSendLevel:
226 			case ARDOUR::TrimAutomation:
227 				snprintf (buf, sizeof (buf), "%+4.1f dB", accurate_coefficient_to_dB (_controllable->get_value()));
228 				text->set (buf);
229 				break;
230 
231 			default:
232 				text->set (ARDOUR::value_as_string (desc, _val));
233 				break;
234 		}
235 	} else {
236 		text->set ("---");
237 	}
238 
239 	redraw ();
240 }
241