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 "pbd/error.h"
28 #include "pbd/cartesian.h"
29 #include "ardour/panner.h"
30 #include <gtkmm2ext/gtk_ui.h>
34 #include "gui_thread.h"
40 using namespace ARDOUR;
42 using Gtkmm2ext::Keyboard;
44 Panner2d::Target::Target (const AngularVector& a, const char *txt)
51 Panner2d::Target::~Target ()
56 Panner2d::Target::set_text (const char* txt)
61 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
62 : panner (p), width (0), height (h)
64 panner->StateChanged.connect (state_connection, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
65 panner->Changed.connect (change_connection, invalidator (*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
68 set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
73 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
79 Panner2d::reset (uint32_t n_inputs)
81 uint32_t nouts = panner->out().n_audio();
85 while (pucks.size() < n_inputs) {
86 add_puck ("", AngularVector());
89 if (pucks.size() > n_inputs) {
90 for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
94 pucks.resize (n_inputs);
97 for (Targets::iterator x = pucks.begin(); x != pucks.end(); ++x) {
98 (*x)->visible = false;
106 pucks[0]->set_text ("");
110 pucks[0]->set_text ("R");
111 pucks[1]->set_text ("L");
115 for (uint32_t i = 0; i < n_inputs; ++i) {
117 snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
118 pucks[i]->set_text (buf);
124 for (uint32_t i = existing_pucks; i < n_inputs; ++i) {
125 pucks[i]->position = panner->streampanner (i).get_position ();
126 pucks[i]->visible = true;
130 /* add all outputs */
132 while (targets.size() < nouts) {
133 add_target (AngularVector());
136 if (targets.size() > nouts) {
137 for (uint32_t i = nouts; i < targets.size(); ++i) {
141 targets.resize (nouts);
144 for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
145 (*x)->visible = false;
148 for (uint32_t n = 0; n < nouts; ++n) {
151 snprintf (buf, sizeof (buf), "%d", n+1);
152 targets[n]->set_text (buf);
154 targets[n]->position = panner->output(n).position;
155 targets[n]->visible = true;
163 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
165 width = alloc.get_width();
166 height = alloc.get_height();
173 DrawingArea::on_size_allocate (alloc);
177 Panner2d::add_puck (const char* text, const AngularVector& a)
179 Target* puck = new Target (a, text);
180 pucks.push_back (puck);
181 puck->visible = true;
187 Panner2d::add_target (const AngularVector& a)
189 Target* target = new Target (a, "");
190 targets.push_back (target);
191 target->visible = true;
194 return targets.size() - 1;
198 Panner2d::handle_state_change ()
200 ENSURE_GUI_THREAD (*this, &Panner2d::handle_state_change)
206 Panner2d::handle_position_change ()
210 ENSURE_GUI_THREAD (*this, &Panner2d::handle_position_change)
212 for (n = 0; n < pucks.size(); ++n) {
213 pucks[n]->position = panner->streampanner(n).get_position ();
216 for (n = 0; n < targets.size(); ++n) {
217 targets[n]->position = panner->output(n).position;
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 cerr << "@ " << x << ", " << y << endl;
249 for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
254 candidate->position.cartesian (c);
257 distance = sqrt ((c.x - x) * (c.x - x) +
258 (c.y - y) * (c.y - y));
260 cerr << "\tConsider candiate " << candidate->text << " @ " << c.x << ", " << c.y << ", " << c.z << " distance = " << distance << endl;
262 if (distance < best_distance) {
264 best_distance = distance;
270 if (best_distance > 20) { // arbitrary
274 cerr << "the winner is " << closest->text << endl;
280 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
283 GdkModifierType state;
286 gdk_window_get_pointer (ev->window, &x, &y, &state);
288 x = (int) floor (ev->x);
289 y = (int) floor (ev->y);
290 state = (GdkModifierType) ev->state;
293 return handle_motion (x, y, state);
296 Panner2d::on_expose_event (GdkEventExpose *event)
301 cr = gdk_cairo_create (get_window()->gobj());
303 cairo_set_line_width (cr, 1.0);
305 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
306 if (!panner->bypassed()) {
307 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
309 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
311 cairo_fill_preserve (cr);
315 cairo_translate (cr, 10.0, 10.0);
318 /* horizontal line of "crosshairs" */
320 cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
321 cairo_move_to (cr, 0.5, height/2.0+0.5);
322 cairo_line_to (cr, width+0.5, height/2+0.5);
325 /* vertical line of "crosshairs" */
327 cairo_move_to (cr, width/2+0.5, 0.5);
328 cairo_line_to (cr, width/2+0.5, height+0.5);
331 /* the circle on which signals live */
333 cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
336 if (!panner->bypassed()) {
339 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
342 cairo_set_font_size (cr, 10);
345 cairo_set_font_size (cr, 16);
349 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
358 puck->position.cartesian (c);
361 x = (gint) floor (c.x);
362 y = (gint) floor (c.y);
364 /* XXX need to shift circles so that they are centered on the circle */
366 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
367 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
368 cairo_close_path (cr);
371 cairo_move_to (cr, x + 6, y + 6);
374 snprintf (buf, sizeof (buf), "%s:%d", puck->text.c_str(), (int) lrint (puck->position.azi));
375 cairo_show_text (cr, buf);
379 /* redraw any visible targets */
383 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
388 if (target->visible) {
392 target->position.cartesian (c);
395 x = (int) floor (c.x);
396 y = (int) floor (c.y);
398 snprintf (buf, sizeof (buf), "%d", n);
400 cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
401 cairo_rectangle (cr, x-2, y-2, 4, 4);
403 cairo_move_to (cr, x+6, y+6);
404 cairo_show_text (cr, buf);
416 Panner2d::on_button_press_event (GdkEventButton *ev)
418 GdkModifierType state;
420 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
424 switch (ev->button) {
427 if ((drag_target = find_closest_object (ev->x, ev->y, drag_index)) != 0) {
428 drag_target->set_selected (true);
431 drag_x = (int) floor (ev->x);
432 drag_y = (int) floor (ev->y);
433 state = (GdkModifierType) ev->state;
435 return handle_motion (drag_x, drag_y, state);
446 Panner2d::on_button_release_event (GdkEventButton *ev)
449 GdkModifierType state;
452 switch (ev->button) {
454 x = (int) floor (ev->x);
455 y = (int) floor (ev->y);
456 state = (GdkModifierType) ev->state;
458 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
460 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
461 //Target* puck = i->second;
462 /* XXX DO SOMETHING TO SET PUCK BACK TO "normal" */
470 ret = handle_motion (x, y, state);
477 x = (int) floor (ev->x);
478 y = (int) floor (ev->y);
479 state = (GdkModifierType) ev->state;
481 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
485 ret = handle_motion (x, y, state);
500 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
502 if (drag_target == 0) {
506 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
511 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
513 bool need_move = false;
515 drag_target->position.cartesian (c);
518 if ((evx != c.x) || (evy != c.y)) {
523 CartesianVector cp (evx, evy, 0.0);
525 /* canonicalize position */
529 /* position actual signal on circle */
531 clamp_to_circle (cp.x, cp.y);
533 /* generate an angular representation and set drag target (GUI) position */
535 cp.angular (drag_target->position); /* sets drag target position */
538 panner->streampanner (drag_index).set_position (drag_target->position);
549 Panner2d::cart_to_gtk (CartesianVector& c) const
551 /* "c" uses a coordinate space that is:
554 dimension = 2.0 * 2.0
555 so max values along each axis are -1..+1
557 GTK uses a coordinate space that is:
560 dimension = width * height
561 so max values along each axis are 0,width and
565 c.x = (width / 2) * (c.x + 1);
566 c.y = (height / 2) * (1 - c.y);
568 /* XXX z-axis not handled - 2D for now */
572 Panner2d::gtk_to_cart (CartesianVector& c) const
574 c.x = (c.x / (width / 2.0)) - 1.0;
575 c.y = -((c.y / (height / 2.0)) - 1.0);
577 /* XXX z-axis not handled - 2D for now */
581 Panner2d::clamp_to_circle (double& x, double& y)
586 PBD::cart_to_azi_ele (x, y, z, azi, ele);
587 PBD::azi_ele_to_cart (azi, ele, x, y, z);
591 Panner2d::toggle_bypass ()
593 panner->set_bypassed (!panner->bypassed());
596 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
598 , reset_button (_("Reset"))
599 , bypass_button (_("Bypass"))
600 , mute_button (_("Mute"))
602 widget.set_name ("MixerPanZone");
604 set_title (_("Panner"));
605 widget.set_size_request (h, h);
607 button_box.set_spacing (6);
608 button_box.pack_start (reset_button, false, false);
609 button_box.pack_start (bypass_button, false, false);
610 button_box.pack_start (mute_button, false, false);
612 spinner_box.set_spacing (6);
613 left_side.set_spacing (6);
615 left_side.pack_start (button_box, false, false);
616 left_side.pack_start (spinner_box, false, false);
618 reset_button.show ();
619 bypass_button.show ();
625 hpacker.set_spacing (6);
626 hpacker.set_border_width (12);
627 hpacker.pack_start (widget, false, false);
628 hpacker.pack_start (left_side, false, false);
637 Panner2dWindow::reset (uint32_t n_inputs)
639 widget.reset (n_inputs);
642 while (spinners.size() < n_inputs) {
643 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
644 //spinner_box.pack_start (*spinners.back(), false, false);
645 //spinners.back()->set_digits (4);
646 spinners.back()->show ();
649 while (spinners.size() > n_inputs) {
650 spinner_box.remove (*spinners.back());
651 delete spinners.back();
652 spinners.erase (--spinners.end());