2 * Copyright (C) 2016 Paul Davis
3 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <cairomm/context.h>
24 #include "ardour/automation_control.h"
25 #include "ardour/value_as_string.h"
26 #include "ardour/dB.h"
27 #include "ardour/utils.h"
29 #include "gtkmm2ext/colors.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/rgb_macros.h"
33 #include "canvas/text.h"
35 #include "maschine2.h"
36 #include "m2controls.h"
43 #define Rect ArdourCanvas::Rect
47 using namespace ARDOUR;
48 using namespace ArdourSurface;
49 using namespace ArdourCanvas;
51 Maschine2Knob::Maschine2Knob (PBD::EventLoop* el, Item* parent)
59 Pango::FontDescription fd ("Sans 10px");
61 text = new Text (this);
62 text->set_font_description (fd);
63 text->set_position (Duple (-_radius, _radius + 2));
64 text->set_color (0xffffffff);
65 _bounding_box_dirty = true;
68 Maschine2Knob::~Maschine2Knob ()
73 Maschine2Knob::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
76 /* no controllable, nothing to draw */
80 // TODO consider a "bar" circular shape + 1bit b/w is ugly
82 const float scale = 2.f * _radius;
83 const float pointer_thickness = std::max (1.f, 3.f * (scale / 80.f));
85 const float start_angle = ((180.f - 65.f) * M_PI) / 180.f;
86 const float end_angle = ((360.f + 65.f) * M_PI) / 180.f;
90 const float value_angle = start_angle + (_val * (end_angle - start_angle));
91 const float zero_angle = start_angle + (zero * (end_angle - start_angle));
93 float value_x = cos (value_angle);
94 float value_y = sin (value_angle);
96 /* translate so that all coordinates are based on the center of the
97 * knob (which is also its position()
100 Duple origin = item_to_window (Duple (0, 0));
101 context->translate (origin.x - 0.5, origin.y - 0.5);
102 context->begin_new_path ();
104 float center_radius = 0.48*scale;
105 float border_width = 0.8;
107 const bool arc = true;
110 center_radius = scale * 0.33;
112 float inner_progress_radius = scale * 0.38;
113 float outer_progress_radius = scale * 0.48;
114 float progress_width = (outer_progress_radius-inner_progress_radius);
115 float progress_radius = inner_progress_radius + progress_width/2.0;
118 context->set_source_rgb (1, 1, 1);
119 context->set_line_width (progress_width);
120 if (zero_angle > value_angle) {
121 context->arc (0, 0, progress_radius, value_angle, zero_angle);
123 context->arc (0, 0, progress_radius, zero_angle, value_angle);
129 context->set_line_width (border_width);
130 context->set_source_rgb (1, 1, 1);
131 context->arc (0, 0, center_radius, 0, 2.0*G_PI);
135 context->set_source_rgb (0, 0, 0);
136 context->set_line_cap (Cairo::LINE_CAP_ROUND);
137 context->set_line_width (pointer_thickness);
138 context->move_to ((center_radius * value_x), (center_radius * value_y));
139 context->line_to (((center_radius * 0.2) * value_x), ((center_radius * 0.2) * value_y));
142 /* reset all translations, scaling etc. */
145 render_children (area, context);
149 Maschine2Knob::compute_bounding_box () const
151 if (!_canvas || _radius == 0) {
152 _bounding_box = Rect ();
153 _bounding_box_dirty = false;
157 if (_bounding_box_dirty) {
158 _bounding_box = Rect (- _radius, - _radius, _radius, _radius);
159 _bounding_box_dirty = false;
162 add_child_bounding_boxes ();
166 Maschine2Knob::set_controllable (boost::shared_ptr<AutomationControl> c)
168 watch_connection.disconnect ();
171 _controllable.reset ();
176 _controllable->Changed.connect (watch_connection, invalidator(*this), boost::bind (&Maschine2Knob::controllable_changed, this), _eventloop);
178 controllable_changed ();
179 // set _controllable->desc()->label
183 Maschine2Knob::set_control (M2EncoderInterface* ctrl)
185 encoder_connection.disconnect ();
190 ctrl->changed.connect_same_thread (encoder_connection, boost::bind (&Maschine2Knob::encoder_changed, this, _1));
194 Maschine2Knob::encoder_changed (int delta)
196 if (!_controllable) {
199 const double d = delta * 0.5 / _ctrl->range ();
200 boost::shared_ptr<AutomationControl> ac = _controllable;
201 ac->set_value (ac->interface_to_internal (min (ac->upper(), max (ac->lower(), ac->internal_to_interface (ac->get_value()) + d))), PBD::Controllable::UseGroup);
205 Maschine2Knob::controllable_changed ()
208 _normal = _controllable->internal_to_interface (_controllable->normal());
209 _val = _controllable->internal_to_interface (_controllable->get_value());
211 const ParameterDescriptor& desc (_controllable->desc());
214 switch (_controllable->parameter().type()) {
215 case ARDOUR::PanAzimuthAutomation:
216 snprintf (buf, sizeof (buf), _("L:%3d R:%3d"), (int) rint (100.0 * (1.0 - _val)), (int) rint (100.0 * _val));
220 case ARDOUR::PanWidthAutomation:
221 snprintf (buf, sizeof (buf), "%d%%", (int) floor (_val*100));
225 case ARDOUR::GainAutomation:
226 case ARDOUR::BusSendLevel:
227 case ARDOUR::TrimAutomation:
228 snprintf (buf, sizeof (buf), "%+4.1f dB", accurate_coefficient_to_dB (_controllable->get_value()));
233 text->set (ARDOUR::value_as_string (desc, _val));