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"
43 using namespace ARDOUR;
45 using Gtkmm2ext::Keyboard;
47 Panner2d::Target::Target (const AngularVector& a, const char *txt)
54 Panner2d::Target::~Target ()
59 Panner2d::Target::set_text (const char* txt)
64 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
65 : panner (p), width (0), height (h)
67 panner->StateChanged.connect (connections, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
69 panner->pannable()->pan_azimuth_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
70 panner->pannable()->pan_width_control->Changed.connect (connections, invalidator(*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
73 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
78 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
84 Panner2d::reset (uint32_t n_inputs)
86 uint32_t nouts = panner->out().n_audio();
90 while (pucks.size() < n_inputs) {
91 add_puck ("", AngularVector());
94 if (pucks.size() > n_inputs) {
95 for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
99 pucks.resize (n_inputs);
107 pucks[0]->set_text ("");
111 pucks[0]->set_text ("R");
112 pucks[1]->set_text ("L");
116 for (uint32_t i = 0; i < n_inputs; ++i) {
118 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
119 pucks[i]->set_text (buf);
124 for (uint32_t i = 0; i < n_inputs; ++i) {
125 pucks[i]->position = panner->signal_position (i);
128 /* add all outputs */
130 while (targets.size() < nouts) {
131 add_target (AngularVector());
134 if (targets.size() > nouts) {
135 for (uint32_t i = nouts; i < targets.size(); ++i) {
139 targets.resize (nouts);
142 for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
143 (*x)->visible = false;
146 vector<Speaker>& speakers (panner->get_speakers()->speakers());
148 for (uint32_t n = 0; n < nouts; ++n) {
151 snprintf (buf, sizeof (buf), "%d", n+1);
152 targets[n]->set_text (buf);
153 targets[n]->position = speakers[n].angles();
154 targets[n]->visible = true;
161 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
163 width = alloc.get_width();
164 height = alloc.get_height();
166 /* our idea of our width/height must be "square
174 dimen = min (width, height);
176 DrawingArea::on_size_allocate (alloc);
180 Panner2d::add_puck (const char* text, const AngularVector& a)
182 Target* puck = new Target (a, text);
183 pucks.push_back (puck);
184 puck->visible = true;
190 Panner2d::add_target (const AngularVector& a)
192 Target* target = new Target (a, "");
193 targets.push_back (target);
194 target->visible = true;
197 return targets.size() - 1;
201 Panner2d::handle_state_change ()
207 Panner2d::handle_position_change ()
211 for (uint32_t i = 0; i < pucks.size(); ++i) {
212 pucks[i]->position = panner->signal_position (i);
215 vector<Speaker>& speakers (panner->get_speakers()->speakers());
217 for (n = 0; n < targets.size(); ++n) {
218 targets[n]->position = speakers[n].angles();
225 Panner2d::move_puck (int which, const AngularVector& a)
227 if (which >= int (targets.size())) {
231 targets[which]->position = a;
236 Panner2d::find_closest_object (gdouble x, gdouble y, int& which) const
241 float best_distance = FLT_MAX;
247 for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
252 candidate->position.cartesian (c);
255 distance = sqrt ((c.x - x) * (c.x - x) +
256 (c.y - y) * (c.y - y));
258 if (distance < best_distance) {
260 best_distance = distance;
265 if (best_distance > 20) { // arbitrary
273 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
276 GdkModifierType state;
279 gdk_window_get_pointer (ev->window, &x, &y, &state);
281 x = (int) floor (ev->x);
282 y = (int) floor (ev->y);
283 state = (GdkModifierType) ev->state;
286 return handle_motion (x, y, state);
289 Panner2d::on_expose_event (GdkEventExpose *event)
294 cr = gdk_cairo_create (get_window()->gobj());
296 cairo_set_line_width (cr, 1.0);
298 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
299 if (!panner->bypassed()) {
300 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
302 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
304 cairo_fill_preserve (cr);
308 cairo_translate (cr, 10.0, 10.0);
311 /* horizontal line of "crosshairs" */
313 cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
314 cairo_move_to (cr, 0.5, height/2.0+0.5);
315 cairo_line_to (cr, width+0.5, height/2+0.5);
318 /* vertical line of "crosshairs" */
320 cairo_move_to (cr, width/2+0.5, 0.5);
321 cairo_line_to (cr, width/2+0.5, height+0.5);
324 /* the circle on which signals live */
326 cairo_arc (cr, width/2, height/2, dimen/2, 0, 2.0 * M_PI);
329 if (!panner->bypassed()) {
332 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
335 cairo_set_font_size (cr, 10);
338 cairo_set_font_size (cr, 16);
342 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
351 puck->position.cartesian (c);
354 x = (gint) floor (c.x);
355 y = (gint) floor (c.y);
357 /* XXX need to shift circles so that they are centered on the circle */
359 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
360 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
361 cairo_close_path (cr);
364 cairo_move_to (cr, x + 6, y + 6);
367 snprintf (buf, sizeof (buf), "%s:%d", puck->text.c_str(), (int) lrint (puck->position.azi));
368 cairo_show_text (cr, buf);
372 /* redraw any visible targets */
376 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
381 if (target->visible) {
385 target->position.cartesian (c);
388 x = (int) floor (c.x);
389 y = (int) floor (c.y);
391 snprintf (buf, sizeof (buf), "%d", n);
393 cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
394 cairo_rectangle (cr, x-2, y-2, 4, 4);
396 cairo_move_to (cr, x+6, y+6);
397 cairo_show_text (cr, buf);
409 Panner2d::on_button_press_event (GdkEventButton *ev)
411 GdkModifierType state;
413 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
417 switch (ev->button) {
420 if ((drag_target = find_closest_object (ev->x, ev->y, drag_index)) != 0) {
421 drag_target->set_selected (true);
424 drag_x = (int) floor (ev->x);
425 drag_y = (int) floor (ev->y);
426 state = (GdkModifierType) ev->state;
428 return handle_motion (drag_x, drag_y, state);
439 Panner2d::on_button_release_event (GdkEventButton *ev)
442 GdkModifierType state;
445 switch (ev->button) {
447 x = (int) floor (ev->x);
448 y = (int) floor (ev->y);
449 state = (GdkModifierType) ev->state;
451 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
453 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
454 //Target* puck = i->second;
455 /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
463 ret = handle_motion (x, y, state);
470 x = (int) floor (ev->x);
471 y = (int) floor (ev->y);
472 state = (GdkModifierType) ev->state;
474 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
478 ret = handle_motion (x, y, state);
493 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
495 if (drag_target == 0) {
499 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
504 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
506 bool need_move = false;
508 drag_target->position.cartesian (c);
511 if ((evx != c.x) || (evy != c.y)) {
516 CartesianVector cp (evx, evy, 0.0);
518 /* canonicalize position */
522 /* position actual signal on circle */
524 clamp_to_circle (cp.x, cp.y);
526 /* generate an angular representation and set drag target (GUI) position */
528 cp.angular (drag_target->position); /* sets drag target position */
530 double degree_fract = drag_target->position.azi / 360.0;
532 panner->set_position (degree_fract);
540 Panner2d::cart_to_gtk (CartesianVector& c) const
542 /* "c" uses a coordinate space that is:
545 dimension = 2.0 * 2.0
546 so max values along each axis are -1..+1
548 GTK uses a coordinate space that is:
551 dimension = width * height
552 so max values along each axis are 0,width and
556 const uint32_t hoffset = (width - dimen)/2;
557 const uint32_t voffset = (height - dimen)/2;
559 c.x = hoffset + ((dimen / 2) * (c.x + 1));
560 c.y = voffset + ((dimen / 2) * (1 - c.y));
562 /* XXX z-axis not handled - 2D for now */
566 Panner2d::gtk_to_cart (CartesianVector& c) const
568 c.x = ((c.x / (dimen / 2.0)) - 1.0);
569 c.y = -((c.y / (dimen / 2.0)) - 1.0);
571 /* XXX z-axis not handled - 2D for now */
575 Panner2d::clamp_to_circle (double& x, double& y)
580 PBD::cart_to_azi_ele (x, y, z, azi, ele);
581 PBD::azi_ele_to_cart (azi, ele, x, y, z);
585 Panner2d::toggle_bypass ()
587 panner->set_bypassed (!panner->bypassed());
590 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
591 : ArdourDialog (_("Panner (2D)"))
593 , bypass_button (_("Bypass"))
595 widget.set_name ("MixerPanZone");
597 set_title (_("Panner"));
598 widget.set_size_request (h, h);
600 bypass_button.signal_toggled().connect (sigc::mem_fun (*this, &Panner2dWindow::bypass_toggled));
602 button_box.set_spacing (6);
603 button_box.pack_start (bypass_button, false, false);
605 spinner_box.set_spacing (6);
606 left_side.set_spacing (6);
608 left_side.pack_start (button_box, false, false);
609 left_side.pack_start (spinner_box, false, false);
611 bypass_button.show ();
616 hpacker.set_spacing (6);
617 hpacker.set_border_width (12);
618 hpacker.pack_start (widget, false, false);
619 hpacker.pack_start (left_side, false, false);
622 get_vbox()->pack_start (hpacker);
628 Panner2dWindow::reset (uint32_t n_inputs)
630 widget.reset (n_inputs);
633 while (spinners.size() < n_inputs) {
634 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
635 //spinner_box.pack_start (*spinners.back(), false, false);
636 //spinners.back()->set_digits (4);
637 spinners.back()->show ();
640 while (spinners.size() > n_inputs) {
641 spinner_box.remove (*spinners.back());
642 delete spinners.back();
643 spinners.erase (--spinners.end());
649 Panner2dWindow::bypass_toggled ()
651 bool view = bypass_button.get_active ();
652 bool model = widget.get_panner()->bypassed ();
655 widget.get_panner()->set_bypassed (view);