more changes to broken-out tempo code
[ardour.git] / libs / surfaces / maschine2 / ui_knob.cc
1 /*
2  * Copyright (C) 2016 Paul Davis
3  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  */
19
20 #include <cmath>
21
22 #include <cairomm/context.h>
23
24 #include "ardour/automation_control.h"
25 #include "ardour/value_as_string.h"
26 #include "ardour/dB.h"
27 #include "ardour/utils.h"
28
29 #include "gtkmm2ext/colors.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/rgb_macros.h"
32
33 #include "canvas/text.h"
34
35 #include "maschine2.h"
36 #include "m2controls.h"
37
38 #include "ui_knob.h"
39
40 #include "pbd/i18n.h"
41
42 #ifdef __APPLE__
43 #define Rect ArdourCanvas::Rect
44 #endif
45
46 using namespace PBD;
47 using namespace ARDOUR;
48 using namespace ArdourSurface;
49 using namespace ArdourCanvas;
50
51 Maschine2Knob::Maschine2Knob (PBD::EventLoop* el, Item* parent)
52         : Container (parent)
53         , _ctrl (0)
54         , _eventloop (el)
55         , _radius (11)
56         , _val (0)
57         , _normal (0)
58 {
59         Pango::FontDescription fd ("Sans 10px");
60
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;
66 }
67
68 Maschine2Knob::~Maschine2Knob ()
69 {
70 }
71
72 void
73 Maschine2Knob::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
74 {
75         if (!_controllable) {
76                 /* no controllable, nothing to draw */
77                 return;
78         }
79
80         // TODO consider a "bar" circular shape + 1bit b/w is ugly
81
82         const float scale = 2.f * _radius;
83         const float pointer_thickness = std::max (1.f, 3.f * (scale / 80.f));
84
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;
87
88         float zero = 0;
89
90         const float value_angle = start_angle + (_val * (end_angle - start_angle));
91         const float zero_angle = start_angle + (zero * (end_angle - start_angle));
92
93         float value_x = cos (value_angle);
94         float value_y = sin (value_angle);
95
96         /* translate so that all coordinates are based on the center of the
97          * knob (which is also its position()
98          */
99         context->save ();
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 ();
103
104         float center_radius = 0.48*scale;
105         float border_width = 0.8;
106
107         const bool arc = true;
108
109         if (arc) {
110                 center_radius = scale * 0.33;
111
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;
116
117                 // draw the arc
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);
122                 } else {
123                         context->arc (0, 0, progress_radius, zero_angle, value_angle);
124                 }
125                 context->stroke ();
126         }
127
128         // knob body
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);
132         context->fill ();
133
134         // line
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));
140         context->stroke ();
141
142         /* reset all translations, scaling etc. */
143         context->restore ();
144
145         render_children (area, context);
146 }
147
148  void
149 Maschine2Knob::compute_bounding_box () const
150 {
151         if (!_canvas || _radius == 0) {
152                 _bounding_box = Rect ();
153                 _bounding_box_dirty = false;
154                 return;
155         }
156
157         if (_bounding_box_dirty) {
158                 _bounding_box = Rect (- _radius, - _radius, _radius, _radius);
159                 _bounding_box_dirty = false;
160         }
161
162         add_child_bounding_boxes ();
163 }
164
165 void
166 Maschine2Knob::set_controllable (boost::shared_ptr<AutomationControl> c)
167 {
168         watch_connection.disconnect ();
169
170         if (!c) {
171                 _controllable.reset ();
172                 return;
173         }
174
175         _controllable = c;
176         _controllable->Changed.connect (watch_connection, invalidator(*this), boost::bind (&Maschine2Knob::controllable_changed, this), _eventloop);
177
178         controllable_changed ();
179         // set _controllable->desc()->label
180 }
181
182 void
183 Maschine2Knob::set_control (M2EncoderInterface* ctrl)
184 {
185         encoder_connection.disconnect ();
186         _ctrl = ctrl;
187         if (!ctrl) {
188                 return;
189         }
190         ctrl->changed.connect_same_thread (encoder_connection, boost::bind (&Maschine2Knob::encoder_changed, this, _1));
191 }
192
193 void
194 Maschine2Knob::encoder_changed (int delta)
195 {
196         if (!_controllable) {
197                 return;
198         }
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);
202 }
203
204 void
205 Maschine2Knob::controllable_changed ()
206 {
207         if (_controllable) {
208                 _normal = _controllable->internal_to_interface (_controllable->normal());
209                 _val = _controllable->internal_to_interface (_controllable->get_value());
210
211                 const ParameterDescriptor& desc (_controllable->desc());
212
213                 char buf[64];
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));
217                                 text->set (buf);
218                                 break;
219
220                         case ARDOUR::PanWidthAutomation:
221                                 snprintf (buf, sizeof (buf), "%d%%", (int) floor (_val*100));
222                                 text->set (buf);
223                                 break;
224
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()));
229                                 text->set (buf);
230                                 break;
231
232                         default:
233                                 text->set (ARDOUR::value_as_string (desc, _val));
234                                 break;
235                 }
236         } else {
237                 text->set ("---");
238         }
239
240         redraw ();
241 }