2 Copyright (C) 2011 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 "pbd/cartesian.h"
22 #include "gtkmm2ext/keyboard.h"
24 #include "speaker_dialog.h"
25 #include "gui_thread.h"
29 using namespace ARDOUR;
33 using namespace Gtkmm2ext;
35 SpeakerDialog::SpeakerDialog ()
36 : ArdourWindow (_("Speaker Configuration"))
37 , aspect_sample ("", 0.5, 0.5, 1.5, false)
38 , azimuth_adjustment (0, 0.0, 360.0, 10.0, 1.0)
39 , azimuth_spinner (azimuth_adjustment)
40 , add_speaker_button (_("Add Speaker"))
41 , remove_speaker_button (_("Remove Speaker"))
42 /* initialize to 0 so that set_selected works below */
44 , ignore_speaker_position_change (false)
45 , ignore_azimuth_change (false)
47 side_vbox.set_homogeneous (false);
48 side_vbox.set_border_width (6);
49 side_vbox.set_spacing (6);
50 side_vbox.pack_start (add_speaker_button, false, false);
52 aspect_sample.set_size_request (300, 200);
53 aspect_sample.set_shadow_type (SHADOW_NONE);
54 aspect_sample.add (darea);
57 hbox.set_border_width (6);
58 hbox.pack_start (aspect_sample, true, true);
59 hbox.pack_start (side_vbox, false, false);
61 HBox* current_speaker_hbox = manage (new HBox);
62 current_speaker_hbox->set_spacing (4);
63 current_speaker_hbox->pack_start (*manage (new Label (_("Azimuth:"))), false, false);
64 current_speaker_hbox->pack_start (azimuth_spinner, true, true);
65 current_speaker_hbox->pack_start (remove_speaker_button, true, true);
67 VBox* vbox = manage (new VBox);
68 vbox->pack_start (hbox);
69 vbox->pack_start (*current_speaker_hbox, true, true);
73 darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
75 darea.signal_size_allocate().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_size_allocate));
76 darea.signal_expose_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_expose_event));
77 darea.signal_button_press_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_press_event));
78 darea.signal_button_release_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_release_event));
79 darea.signal_motion_notify_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_motion_notify_event));
81 add_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::add_speaker));
82 remove_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::remove_speaker));
83 azimuth_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &SpeakerDialog::azimuth_changed));
87 /* selected index initialised to 0 above; this will set `no selection' and
88 sensitize widgets accordingly.
94 SpeakerDialog::set_speakers (boost::shared_ptr<Speakers> s)
99 boost::shared_ptr<Speakers>
100 SpeakerDialog::get_speakers () const
102 return _speakers.lock ();
106 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
108 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
116 cr = gdk_cairo_create (darea.get_window()->gobj());
118 cairo_set_line_width (cr, 1.0);
120 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
121 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
122 cairo_fill_preserve (cr);
125 cairo_translate (cr, x_origin, y_origin);
127 /* horizontal line of "crosshairs" */
129 cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
130 cairo_move_to (cr, 0.5, height/2.0+0.5);
131 cairo_line_to (cr, width+0.5, height/2+0.5);
134 /* vertical line of "crosshairs" */
136 cairo_move_to (cr, width/2+0.5, 0.5);
137 cairo_line_to (cr, width/2+0.5, height+0.5);
140 /* the circle on which signals live */
142 cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
147 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
150 cairo_set_font_size (cr, 10);
153 cairo_set_font_size (cr, 16);
158 for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i) {
161 CartesianVector c (s.coords());
165 /* We have already moved our plotting origin to x_origin, y_origin,
166 so compensate for that.
171 x = (gint) floor (c.x);
172 y = (gint) floor (c.y);
174 /* XXX need to shift circles so that they are centered on the circle */
176 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
177 if (selected_index == n) {
178 cairo_set_source_rgb (cr, 0.8, 0.8, 0.2);
180 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
182 cairo_close_path (cr);
185 cairo_move_to (cr, x + 6, y + 6);
188 if (n == selected_index) {
189 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
191 snprintf (buf, sizeof (buf), "%d", n + 1);
193 cairo_show_text (cr, buf);
204 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
206 /* "c" uses a coordinate space that is:
209 dimension = 2.0 * 2.0
210 so max values along each axis are -1..+1
212 GTK uses a coordinate space that is:
215 dimension = width * height
216 so max values along each axis are 0,width and
220 c.x = (width / 2) * (c.x + 1) + x_origin;
221 c.y = (height / 2) * (1 - c.y) + y_origin;
223 /* XXX z-axis not handled - 2D for now */
227 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
229 c.x = ((c.x - x_origin) / (width / 2.0)) - 1.0;
230 c.y = -(((c.y - y_origin) / (height / 2.0)) - 1.0);
232 /* XXX z-axis not handled - 2D for now */
236 SpeakerDialog::clamp_to_circle (double& x, double& y)
242 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
243 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
247 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
249 width = alloc.get_width();
250 height = alloc.get_height();
252 /* The allocation will (should) be rectangualar, but make the basic
253 drawing square; space to the right of the square is for over-hanging
263 /* Put the x origin to the left of the rectangular allocation */
264 x_origin = (alloc.get_width() - width) / 3;
265 y_origin = (alloc.get_height() - height) / 2;
269 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
271 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
276 GdkModifierType state;
278 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
284 switch (ev->button) {
288 int const index = find_closest_object (ev->x, ev->y);
289 set_selected (index);
292 int const drag_x = (int) floor (ev->x);
293 int const drag_y = (int) floor (ev->y);
294 state = (GdkModifierType) ev->state;
296 if (drag_index >= 0) {
298 speakers->speakers()[drag_index].angles().cartesian (c);
300 drag_offset_x = drag_x - x_origin - c.x;
301 drag_offset_y = drag_y - y_origin - c.y;
304 return handle_motion (drag_x, drag_y, state);
316 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
318 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
324 GdkModifierType state;
327 switch (ev->button) {
329 x = (int) floor (ev->x);
330 y = (int) floor (ev->y);
331 state = (GdkModifierType) ev->state;
333 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
335 for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i) {
336 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
343 ret = handle_motion (x, y, state);
349 x = (int) floor (ev->x);
350 y = (int) floor (ev->y);
351 state = (GdkModifierType) ev->state;
353 ret = handle_motion (x, y, state);
367 SpeakerDialog::find_closest_object (gdouble x, gdouble y)
369 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
375 float best_distance = FLT_MAX;
379 for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i, ++n) {
381 Speaker& candidate (*i);
384 candidate.angles().cartesian (c);
387 distance = sqrt ((c.x - x) * (c.x - x) +
388 (c.y - y) * (c.y - y));
391 if (distance < best_distance) {
392 best_distance = distance;
397 if (best_distance > 20) { // arbitrary
405 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
408 GdkModifierType state;
411 gdk_window_get_pointer (ev->window, &x, &y, &state);
413 x = (int) floor (ev->x);
414 y = (int) floor (ev->y);
415 state = (GdkModifierType) ev->state;
418 return handle_motion (x, y, state);
422 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
424 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
429 if (drag_index < 0) {
433 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
437 /* correct event coordinates to have their origin at the corner of our graphic
438 rather than the corner of our allocation */
440 double obx = evx - x_origin;
441 double oby = evy - y_origin;
443 /* and compensate for any distance between the mouse pointer and the centre
444 of the object being dragged */
446 obx -= drag_offset_x;
447 oby -= drag_offset_y;
449 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
451 bool need_move = false;
452 Speaker& moving (speakers->speakers()[drag_index]);
454 moving.angles().cartesian (c);
457 if (obx != c.x || oby != c.y) {
462 CartesianVector cp (obx, oby, 0.0);
464 /* canonicalize position */
468 /* position actual signal on circle */
470 clamp_to_circle (cp.x, cp.y);
472 /* generate an angular representation and set drag target (GUI) position */
488 SpeakerDialog::add_speaker ()
490 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
495 speakers->add_speaker (PBD::AngularVector (0, 0, 0));
500 SpeakerDialog::set_selected (int i)
502 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
507 if (i == selected_index) {
514 selected_speaker_connection.disconnect ();
516 azimuth_spinner.set_sensitive (selected_index != -1);
517 remove_speaker_button.set_sensitive (selected_index != -1);
519 if (selected_index != -1) {
520 azimuth_adjustment.set_value (speakers->speakers()[selected_index].angles().azi);
521 speakers->speakers()[selected_index].PositionChanged.connect (
522 selected_speaker_connection, MISSING_INVALIDATOR,
523 boost::bind (&SpeakerDialog::speaker_position_changed, this),
530 SpeakerDialog::azimuth_changed ()
532 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
537 assert (selected_index != -1);
539 if (ignore_azimuth_change) {
543 ignore_speaker_position_change = true;
544 speakers->move_speaker (speakers->speakers()[selected_index].id, PBD::AngularVector (azimuth_adjustment.get_value (), 0, 0));
545 ignore_speaker_position_change = false;
551 SpeakerDialog::speaker_position_changed ()
553 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
558 assert (selected_index != -1);
560 if (ignore_speaker_position_change) {
564 ignore_azimuth_change = true;
565 azimuth_adjustment.set_value (speakers->speakers()[selected_index].angles().azi);
566 ignore_azimuth_change = false;
572 SpeakerDialog::remove_speaker ()
574 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
579 assert (selected_index != -1);
581 speakers->remove_speaker (speakers->speakers()[selected_index].id);