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.
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"
37 #include "gui_thread.h"
39 #include "public_editor.h"
45 using namespace ARDOUR;
47 using Gtkmm2ext::Keyboard;
49 Panner2d::Target::Target (const AngularVector& a, const char *txt)
56 Panner2d::Target::~Target ()
61 Panner2d::Target::set_text (const char* txt)
66 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
68 , position (AngularVector (0.0, 0.0), "")
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());
78 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
80 handle_position_change ();
85 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
91 Panner2d::reset (uint32_t n_inputs)
93 uint32_t nouts = panner->out().n_audio();
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) {
106 pucks.resize (n_inputs);
114 pucks[0]->set_text ("");
118 pucks[0]->set_text ("R");
119 pucks[1]->set_text ("L");
123 for (uint32_t i = 0; i < n_inputs; ++i) {
125 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
126 pucks[i]->set_text (buf);
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) {
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) {
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;
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
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;
197 Panner2d::add_target (const AngularVector& a)
199 Target* target = new Target (a, "");
200 targets.push_back (target);
201 target->visible = true;
204 return targets.size() - 1;
208 Panner2d::handle_state_change ()
214 Panner2d::handle_position_change ()
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();
234 Panner2d::move_puck (int which, const AngularVector& a)
236 if (which >= int (targets.size())) {
240 targets[which]->position = a;
245 Panner2d::find_closest_object (gdouble x, gdouble y)
250 float best_distance = FLT_MAX;
253 /* start with the position itself
256 position.position.cartesian (c);
258 best_distance = sqrt ((c.x - x) * (c.x - x) +
259 (c.y - y) * (c.y - y));
262 for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i) {
265 candidate->position.cartesian (c);
268 distance = sqrt ((c.x - x) * (c.x - x) +
269 (c.y - y) * (c.y - y));
271 if (distance < best_distance) {
273 best_distance = distance;
277 if (best_distance > 20) { // arbitrary
285 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
288 GdkModifierType state;
291 gdk_window_get_pointer (ev->window, &x, &y, &state);
293 x = (int) floor (ev->x);
294 y = (int) floor (ev->y);
295 state = (GdkModifierType) ev->state;
298 return handle_motion (x, y, state);
302 Panner2d::on_expose_event (GdkEventExpose *event)
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);
316 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
318 cairo_fill_preserve (cr);
321 small = !(height > 100);
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);
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);
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);
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);
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);
356 cairo_arc (cr, width/2, height/2, (dimen/2.0) * 0.25, 0, 2.0 * M_PI);
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);
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);
412 if (!panner->bypassed()) {
416 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
421 cairo_set_font_size (cr, 10);
427 if (pucks.size() > 1) {
428 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
434 puck->position.cartesian (c);
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);
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 */
448 cairo_move_to (cr, c.x - 1, c.y + 1);
450 cairo_move_to (cr, c.x - 4, c.y + 4);
452 cairo_show_text (cr, puck->text.c_str());
462 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
467 if (target->visible) {
471 target->position.cartesian (c);
474 snprintf (buf, sizeof (buf), "%d", n);
476 /* stroke out a speaker shape */
478 cairo_move_to (cr, c.x, c.y);
480 cairo_rotate (cr, -(target->position.azi/360.0) * (2.0 * M_PI));
482 cairo_scale (cr, 0.8, 0.8);
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);
499 /* move the text in just a bit */
501 AngularVector textpos (target->position.azi, 0.75);
502 textpos.cartesian (c);
506 cairo_move_to (cr, c.x, c.y);
507 cairo_set_font_size (cr, 10);
508 cairo_show_text (cr, buf);
514 /* draw position puck */
516 position.position.cartesian (c);
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);
533 Panner2d::on_button_press_event (GdkEventButton *ev)
535 GdkModifierType state;
537 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
541 switch (ev->button) {
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);
563 Panner2d::on_button_release_event (GdkEventButton *ev)
566 GdkModifierType state;
569 switch (ev->button) {
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" */
587 ret = handle_motion (x, y, state);
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)) {
602 ret = handle_motion (x, y, state);
617 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
619 if (drag_target == 0) {
623 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
628 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
630 bool need_move = false;
632 drag_target->position.cartesian (c);
635 if ((evx != c.x) || (evy != c.y)) {
640 CartesianVector cp (evx, evy, 0.0);
643 /* canonicalize position */
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 */
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);
670 Panner2d::on_scroll_event (GdkEventScroll* ev)
672 switch (ev->direction) {
674 case GDK_SCROLL_RIGHT:
675 panner->set_position (panner->pannable()->pan_azimuth_control->get_value() - 1.0/360.0);
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);
687 Panner2d::cart_to_gtk (CartesianVector& c) const
689 /* "c" uses a coordinate space that is:
692 dimension = 2.0 * 2.0
693 so max values along each axis are -1..+1
695 GTK uses a coordinate space that is:
698 dimension = width * height
699 so max values along each axis are 0,width and
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 */
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 */
722 Panner2d::clamp_to_circle (double& x, double& y)
727 PBD::cart_to_azi_ele (x, y, z, azi, ele);
728 PBD::azi_ele_to_cart (azi, ele, x, y, z);
732 Panner2d::toggle_bypass ()
734 panner->set_bypassed (!panner->bypassed());
737 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
738 : ArdourDialog (_("Panner (2D)"))
740 , bypass_button (_("Bypass"))
742 widget.set_name ("MixerPanZone");
744 set_title (_("Panner"));
745 widget.set_size_request (h, h);
747 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
749 button_box.set_spacing (6);
750 button_box.pack_start (bypass_button, false, false);
752 spinner_box.set_spacing (6);
753 left_side.set_spacing (6);
755 left_side.pack_start (button_box, false, false);
756 left_side.pack_start (spinner_box, false, false);
758 bypass_button.show ();
763 hpacker.set_spacing (6);
764 hpacker.set_border_width (12);
765 hpacker.pack_start (widget, false, false);
766 hpacker.pack_start (left_side, false, false);
769 get_vbox()->pack_start (hpacker);
775 Panner2dWindow::reset (uint32_t n_inputs)
777 widget.reset (n_inputs);
780 while (spinners.size() < n_inputs) {
781 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
782 //spinner_box.pack_start (*spinners.back(), false, false);
783 //spinners.back()->set_digits (4);
784 spinners.back()->show ();
787 while (spinners.size() > n_inputs) {
788 spinner_box.remove (*spinners.back());
789 delete spinners.back();
790 spinners.erase (--spinners.end());
796 Panner2dWindow::bypass_toggled ()
798 bool view = bypass_button.get_active ();
799 bool model = widget.get_panner()->bypassed ();
802 widget.get_panner()->set_bypassed (view);
807 Panner2dWindow::on_key_press_event (GdkEventKey* event)
809 return relay_key_press (event, &PublicEditor::instance());
813 Panner2dWindow::on_key_release_event (GdkEventKey *event)