switch cartesian/spherical function names and make them use length. still a tweak...
[ardour2.git] / gtk2_ardour / panner2d.cc
blob51c42160ced220b9303cfcbc7ebb3dd02299f3d7
1 /*
2 Copyright (C) 2002 Paul Davis
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.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <cmath>
21 #include <climits>
22 #include <string.h>
24 #include <cairo.h>
25 #include <gtkmm/menu.h>
27 #include "gtkmm2ext/gtk_ui.h"
29 #include "pbd/error.h"
30 #include "pbd/cartesian.h"
31 #include "ardour/panner.h"
32 #include "ardour/pannable.h"
33 #include "ardour/speakers.h"
35 #include "panner2d.h"
36 #include "keyboard.h"
37 #include "gui_thread.h"
38 #include "utils.h"
39 #include "public_editor.h"
41 #include "i18n.h"
43 using namespace std;
44 using namespace Gtk;
45 using namespace ARDOUR;
46 using namespace PBD;
47 using Gtkmm2ext::Keyboard;
49 Panner2d::Target::Target (const AngularVector& a, const char *txt)
50 : position (a)
51 , text (txt)
52 , _selected (false)
56 Panner2d::Target::~Target ()
60 void
61 Panner2d::Target::set_text (const char* txt)
63 text = txt;
66 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
67 : panner (p)
68 , position (AngularVector (0.0, 0.0), "")
69 , width (0)
70 , height (h)
72 panner->StateChanged.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
74 panner->pannable()->pan_azimuth_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
75 panner->pannable()->pan_width_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
77 drag_target = 0;
78 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
80 handle_position_change ();
83 Panner2d::~Panner2d()
85 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
86 delete *i;
90 void
91 Panner2d::reset (uint32_t n_inputs)
93 uint32_t nouts = panner->out().n_audio();
95 /* pucks */
97 while (pucks.size() < n_inputs) {
98 add_puck ("", AngularVector());
101 if (pucks.size() > n_inputs) {
102 for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
103 delete pucks[i];
106 pucks.resize (n_inputs);
109 switch (n_inputs) {
110 case 0:
111 break;
113 case 1:
114 pucks[0]->set_text ("");
115 break;
117 case 2:
118 pucks[0]->set_text ("R");
119 pucks[1]->set_text ("L");
120 break;
122 default:
123 for (uint32_t i = 0; i < n_inputs; ++i) {
124 char buf[64];
125 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
126 pucks[i]->set_text (buf);
128 break;
131 for (uint32_t i = 0; i < n_inputs; ++i) {
132 pucks[i]->position = panner->signal_position (i);
135 /* add all outputs */
137 while (targets.size() < nouts) {
138 add_target (AngularVector());
141 if (targets.size() > nouts) {
142 for (uint32_t i = nouts; i < targets.size(); ++i) {
143 delete targets[i];
146 targets.resize (nouts);
149 for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
150 (*x)->visible = false;
153 vector<Speaker>& speakers (panner->get_speakers()->speakers());
155 for (uint32_t n = 0; n < nouts; ++n) {
156 char buf[16];
158 snprintf (buf, sizeof (buf), "%d", n+1);
159 targets[n]->set_text (buf);
160 targets[n]->position = speakers[n].angles();
161 targets[n]->visible = true;
164 queue_draw ();
167 void
168 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
170 width = alloc.get_width();
171 height = alloc.get_height();
173 /* our idea of our width/height must be "square
176 if (height > 100) {
177 width -= 20;
178 height -= 20;
181 dimen = min (width, height);
183 DrawingArea::on_size_allocate (alloc);
187 Panner2d::add_puck (const char* text, const AngularVector& a)
189 Target* puck = new Target (a, text);
190 pucks.push_back (puck);
191 puck->visible = true;
193 return 0;
197 Panner2d::add_target (const AngularVector& a)
199 Target* target = new Target (a, "");
200 targets.push_back (target);
201 target->visible = true;
202 queue_draw ();
204 return targets.size() - 1;
207 void
208 Panner2d::handle_state_change ()
210 queue_draw ();
213 void
214 Panner2d::handle_position_change ()
216 uint32_t n;
218 position.position = AngularVector (panner->pannable()->pan_azimuth_control->get_value() * 360.0, 0.0);
220 for (uint32_t i = 0; i < pucks.size(); ++i) {
221 pucks[i]->position = panner->signal_position (i);
224 vector<Speaker>& speakers (panner->get_speakers()->speakers());
226 for (n = 0; n < targets.size(); ++n) {
227 targets[n]->position = speakers[n].angles();
230 queue_draw ();
233 void
234 Panner2d::move_puck (int which, const AngularVector& a)
236 if (which >= int (targets.size())) {
237 return;
240 targets[which]->position = a;
241 queue_draw ();
244 Panner2d::Target *
245 Panner2d::find_closest_object (gdouble x, gdouble y)
247 Target *closest = 0;
248 Target *candidate;
249 float distance;
250 float best_distance = FLT_MAX;
251 CartesianVector c;
253 /* start with the position itself
256 position.position.cartesian (c);
257 cart_to_gtk (c);
258 best_distance = sqrt ((c.x - x) * (c.x - x) +
259 (c.y - y) * (c.y - y));
260 closest = &position;
262 for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i) {
263 candidate = *i;
265 candidate->position.cartesian (c);
266 cart_to_gtk (c);
268 distance = sqrt ((c.x - x) * (c.x - x) +
269 (c.y - y) * (c.y - y));
271 if (distance < best_distance) {
272 closest = candidate;
273 best_distance = distance;
277 if (best_distance > 20) { // arbitrary
278 return 0;
281 return closest;
284 bool
285 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
287 gint x, y;
288 GdkModifierType state;
290 if (ev->is_hint) {
291 gdk_window_get_pointer (ev->window, &x, &y, &state);
292 } else {
293 x = (int) floor (ev->x);
294 y = (int) floor (ev->y);
295 state = (GdkModifierType) ev->state;
298 return handle_motion (x, y, state);
301 bool
302 Panner2d::on_expose_event (GdkEventExpose *event)
304 CartesianVector c;
305 cairo_t* cr;
306 bool small;
308 cr = gdk_cairo_create (get_window()->gobj());
310 cairo_set_line_width (cr, 1.0);
312 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
313 if (!panner->bypassed()) {
314 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
315 } else {
316 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
318 cairo_fill_preserve (cr);
319 cairo_clip (cr);
321 small = !(height > 100);
323 if (!small) {
324 cairo_translate (cr, 10.0, 10.0);
327 /* horizontal line of "crosshairs" */
329 cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
330 cairo_move_to (cr, 0.5, height/2.0+0.5);
331 cairo_line_to (cr, width+0.5, height/2+0.5);
332 cairo_stroke (cr);
334 /* vertical line of "crosshairs" */
336 cairo_move_to (cr, width/2+0.5, 0.5);
337 cairo_line_to (cr, width/2+0.5, height+0.5);
338 cairo_stroke (cr);
340 /* the circle on which signals live */
342 cairo_set_line_width (cr, 2.0);
343 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
344 cairo_arc (cr, width/2, height/2, dimen/2, 0, 2.0 * M_PI);
345 cairo_stroke (cr);
347 /* 3 other circles of smaller diameter circle on which signals live */
349 cairo_set_line_width (cr, 1.0);
350 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
351 cairo_arc (cr, width/2, height/2, (dimen/2.0) * 0.75, 0, 2.0 * M_PI);
352 cairo_stroke (cr);
353 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
354 cairo_arc (cr, width/2, height/2, (dimen/2.0) * 0.50, 0, 2.0 * M_PI);
355 cairo_stroke (cr);
356 cairo_arc (cr, width/2, height/2, (dimen/2.0) * 0.25, 0, 2.0 * M_PI);
357 cairo_stroke (cr);
359 if (pucks.size() > 1) {
360 /* arc to show "diffusion" */
362 cairo_move_to (cr, width/2, height/2);
364 double max_angle = 0.0;
365 double min_angle = DBL_MAX;
367 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
368 max_angle = max ((*i)->position.azi, max_angle);
369 min_angle = min ((*i)->position.azi, min_angle);
372 /* if the angle between the max & min is bigger than 180, flip
373 them to use the opposite angle for the display. this
374 matches the psycho-acoustic perception of this condition
375 too, in almost all conditions that VBAP will handle.
378 if (fabs (max_angle - min_angle) > 180.0) {
379 swap (max_angle, min_angle);
382 /* convert to radians */
384 min_angle = (min_angle / 360.0) * 2.0 * M_PI;
385 max_angle = (max_angle / 360.0) * 2.0 * M_PI;
387 /* cairo has coordinates increasing in a clockwise direction */
389 max_angle = (2 * M_PI) - max_angle;
390 min_angle = (2 * M_PI) - min_angle;
392 /* the above transformation will have reversed the min/max relationship */
394 swap (min_angle, max_angle);
396 if (min_angle > max_angle) {
397 /* swapped because they span the zero position: draw two arc segments to span zero.
398 XXX there must be a way to get cairo to do this in a single call
400 cairo_arc_negative (cr, width/2, height/2, dimen/2.0, max_angle, 0.0);
401 cairo_arc_negative (cr, width/2, height/2, dimen/2.0, 0.0, min_angle);
402 } else {
403 cairo_arc (cr, width/2, height/2, dimen/2.0, min_angle, max_angle);
407 cairo_close_path (cr);
408 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.45);
409 cairo_fill (cr);
412 if (!panner->bypassed()) {
414 double arc_radius;
416 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
418 if (small) {
419 arc_radius = 4.0;
420 } else {
421 cairo_set_font_size (cr, 10);
422 arc_radius = 12.0;
425 /* signals */
427 if (pucks.size() > 1) {
428 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
430 Target* puck = *i;
432 if (puck->visible) {
434 puck->position.cartesian (c);
435 cart_to_gtk (c);
437 cairo_new_path (cr);
438 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
439 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 0.85);
440 cairo_fill_preserve (cr);
441 cairo_set_source_rgba (cr, 0.517, 0.772, 0.882, 1.0);
442 cairo_stroke (cr);
444 if (!small && !puck->text.empty()) {
445 cairo_set_source_rgb (cr, 0.517, 0.772, 0.882);
446 /* the +/- adjustments are a hack to try to center the text in the circle */
447 if (small) {
448 cairo_move_to (cr, c.x - 1, c.y + 1);
449 } else {
450 cairo_move_to (cr, c.x - 4, c.y + 4);
452 cairo_show_text (cr, puck->text.c_str());
458 /* speakers */
460 int n = 0;
462 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
463 Target *target = *i;
464 char buf[256];
465 ++n;
467 if (target->visible) {
469 CartesianVector c;
471 target->position.cartesian (c);
472 cart_to_gtk (c);
474 snprintf (buf, sizeof (buf), "%d", n);
476 /* stroke out a speaker shape */
478 cairo_move_to (cr, c.x, c.y);
479 cairo_save (cr);
480 cairo_rotate (cr, -(target->position.azi/360.0) * (2.0 * M_PI));
481 if (small) {
482 cairo_scale (cr, 0.8, 0.8);
483 } else {
484 cairo_scale (cr, 1.2, 1.2);
486 cairo_rel_line_to (cr, 4, -2);
487 cairo_rel_line_to (cr, 0, -7);
488 cairo_rel_line_to (cr, 5, +5);
489 cairo_rel_line_to (cr, 5, 0);
490 cairo_rel_line_to (cr, 0, 5);
491 cairo_rel_line_to (cr, -5, 0);
492 cairo_rel_line_to (cr, -5, +5);
493 cairo_rel_line_to (cr, 0, -7);
494 cairo_close_path (cr);
495 cairo_set_source_rgba (cr, 0.282, 0.517, 0.662, 1.0);
496 cairo_fill (cr);
497 cairo_restore (cr);
499 if (!small) {
500 cairo_set_font_size (cr, 16);
502 /* move the text in just a bit */
504 AngularVector textpos (target->position.azi, target->position.ele, 0.85);
505 textpos.cartesian (c);
506 cart_to_gtk (c);
507 cairo_move_to (cr, c.x, c.y);
508 cairo_show_text (cr, buf);
514 /* draw position puck */
516 position.position.cartesian (c);
517 cart_to_gtk (c);
519 cairo_new_path (cr);
520 cairo_arc (cr, c.x, c.y, arc_radius, 0, 2.0 * M_PI);
521 cairo_set_source_rgba (cr, 1.0, 0.419, 0.419, 0.85);
522 cairo_fill_preserve (cr);
523 cairo_set_source_rgba (cr, 1.0, 0.905, 0.905, 0.85);
524 cairo_stroke (cr);
527 cairo_destroy (cr);
529 return true;
532 bool
533 Panner2d::on_button_press_event (GdkEventButton *ev)
535 GdkModifierType state;
537 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
538 return false;
541 switch (ev->button) {
542 case 1:
543 case 2:
544 if ((drag_target = find_closest_object (ev->x, ev->y)) != 0) {
545 drag_target->set_selected (true);
548 drag_x = (int) floor (ev->x);
549 drag_y = (int) floor (ev->y);
550 state = (GdkModifierType) ev->state;
552 return handle_motion (drag_x, drag_y, state);
553 break;
555 default:
556 break;
559 return false;
562 bool
563 Panner2d::on_button_release_event (GdkEventButton *ev)
565 gint x, y;
566 GdkModifierType state;
567 bool ret = false;
569 switch (ev->button) {
570 case 1:
571 x = (int) floor (ev->x);
572 y = (int) floor (ev->y);
573 state = (GdkModifierType) ev->state;
575 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
577 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
578 //Target* puck = i->second;
579 /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
582 queue_draw ();
583 PuckMoved (-1);
584 ret = true;
586 } else {
587 ret = handle_motion (x, y, state);
590 drag_target = 0;
591 break;
593 case 2:
594 x = (int) floor (ev->x);
595 y = (int) floor (ev->y);
596 state = (GdkModifierType) ev->state;
598 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
599 toggle_bypass ();
600 ret = true;
601 } else {
602 ret = handle_motion (x, y, state);
605 drag_target = 0;
606 break;
608 case 3:
609 break;
613 return ret;
616 gint
617 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
619 if (drag_target == 0) {
620 return false;
623 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
624 return false;
628 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
629 CartesianVector c;
630 bool need_move = false;
632 drag_target->position.cartesian (c);
633 cart_to_gtk (c);
635 if ((evx != c.x) || (evy != c.y)) {
636 need_move = true;
639 if (need_move) {
640 CartesianVector cp (evx, evy, 0.0);
641 AngularVector av;
643 /* canonicalize position */
645 gtk_to_cart (cp);
647 // cerr << "Mouse at " << cp.x << ", " << cp.y << endl;
649 /* position actual signal on circle */
651 clamp_to_circle (cp.x, cp.y);
653 /* generate an angular representation of the current mouse position */
656 cp.angular (av);
658 if (drag_target == &position) {
659 // cerr << "Angle of mouse = " << av.azi << endl;
660 double degree_fract = av.azi / 360.0;
661 panner->set_position (degree_fract);
666 return true;
669 bool
670 Panner2d::on_scroll_event (GdkEventScroll* ev)
672 switch (ev->direction) {
673 case GDK_SCROLL_UP:
674 case GDK_SCROLL_RIGHT:
675 panner->set_position (panner->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
676 break;
678 case GDK_SCROLL_DOWN:
679 case GDK_SCROLL_LEFT:
680 panner->set_position (panner->pannable()->pan_azimuth_control->get_value() + 1.0/360.0);
681 break;
683 return true;
686 void
687 Panner2d::cart_to_gtk (CartesianVector& c) const
689 /* "c" uses a coordinate space that is:
691 center = 0.0
692 dimension = 2.0 * 2.0
693 so max values along each axis are -1..+1
695 GTK uses a coordinate space that is:
697 top left = 0.0
698 dimension = width * height
699 so max values along each axis are 0,width and
700 0,height
703 const uint32_t hoffset = (width - dimen)/2;
704 const uint32_t voffset = (height - dimen)/2;
706 c.x = hoffset + ((dimen / 2) * (c.x + 1));
707 c.y = voffset + ((dimen / 2) * (1 - c.y));
709 /* XXX z-axis not handled - 2D for now */
712 void
713 Panner2d::gtk_to_cart (CartesianVector& c) const
715 c.x = ((c.x / (dimen / 2.0)) - 1.0);
716 c.y = -((c.y / (dimen / 2.0)) - 1.0);
718 /* XXX z-axis not handled - 2D for now */
721 void
722 Panner2d::clamp_to_circle (double& x, double& y)
724 double azi, ele;
725 double z = 0.0;
726 double l;
728 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
729 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
732 void
733 Panner2d::toggle_bypass ()
735 panner->set_bypassed (!panner->bypassed());
738 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
739 : ArdourDialog (_("Panner (2D)"))
740 , widget (p, h)
741 , bypass_button (_("Bypass"))
743 widget.set_name ("MixerPanZone");
745 set_title (_("Panner"));
746 widget.set_size_request (h, h);
748 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
750 button_box.set_spacing (6);
751 button_box.pack_start (bypass_button, false, false);
753 spinner_box.set_spacing (6);
754 left_side.set_spacing (6);
756 left_side.pack_start (button_box, false, false);
757 left_side.pack_start (spinner_box, false, false);
759 bypass_button.show ();
760 button_box.show ();
761 spinner_box.show ();
762 left_side.show ();
764 hpacker.set_spacing (6);
765 hpacker.set_border_width (12);
766 hpacker.pack_start (widget, false, false);
767 hpacker.pack_start (left_side, false, false);
768 hpacker.show ();
770 get_vbox()->pack_start (hpacker);
771 reset (inputs);
772 widget.show ();
775 void
776 Panner2dWindow::reset (uint32_t n_inputs)
778 widget.reset (n_inputs);
780 #if 0
781 while (spinners.size() < n_inputs) {
782 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
783 //spinner_box.pack_start (*spinners.back(), false, false);
784 //spinners.back()->set_digits (4);
785 spinners.back()->show ();
788 while (spinners.size() > n_inputs) {
789 spinner_box.remove (*spinners.back());
790 delete spinners.back();
791 spinners.erase (--spinners.end());
793 #endif
796 void
797 Panner2dWindow::bypass_toggled ()
799 bool view = bypass_button.get_active ();
800 bool model = widget.get_panner()->bypassed ();
802 if (model != view) {
803 widget.get_panner()->set_bypassed (view);
807 bool
808 Panner2dWindow::on_key_press_event (GdkEventKey* event)
810 return relay_key_press (event, &PublicEditor::instance());
813 bool
814 Panner2dWindow::on_key_release_event (GdkEventKey *event)
816 return true;