2 * Copyright (C) 2011-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2011 David Robillard <d@drobilla.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "pbd/cartesian.h"
23 #include "gtkmm2ext/keyboard.h"
25 #include "speaker_dialog.h"
26 #include "gui_thread.h"
30 using namespace ARDOUR;
34 using namespace Gtkmm2ext;
36 SpeakerDialog::SpeakerDialog ()
37 : ArdourWindow (_("Speaker Configuration"))
38 , aspect_frame ("", 0.5, 0.5, 1.5, false)
39 , azimuth_adjustment (0, 0.0, 360.0, 10.0, 1.0)
40 , azimuth_spinner (azimuth_adjustment)
41 , add_speaker_button (_("Add Speaker"))
42 , remove_speaker_button (_("Remove Speaker"))
43 /* initialize to 0 so that set_selected works below */
45 , ignore_speaker_position_change (false)
46 , ignore_azimuth_change (false)
48 side_vbox.set_homogeneous (false);
49 side_vbox.set_border_width (6);
50 side_vbox.set_spacing (6);
51 side_vbox.pack_start (add_speaker_button, false, false);
53 aspect_frame.set_size_request (300, 200);
54 aspect_frame.set_shadow_type (SHADOW_NONE);
55 aspect_frame.add (darea);
58 hbox.set_border_width (6);
59 hbox.pack_start (aspect_frame, true, true);
60 hbox.pack_start (side_vbox, false, false);
62 HBox* current_speaker_hbox = manage (new HBox);
63 current_speaker_hbox->set_spacing (4);
64 current_speaker_hbox->pack_start (*manage (new Label (_("Azimuth:"))), false, false);
65 current_speaker_hbox->pack_start (azimuth_spinner, true, true);
66 current_speaker_hbox->pack_start (remove_speaker_button, true, true);
68 VBox* vbox = manage (new VBox);
69 vbox->pack_start (hbox);
70 vbox->pack_start (*current_speaker_hbox, true, true);
74 darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
76 darea.signal_size_allocate().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_size_allocate));
77 darea.signal_expose_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_expose_event));
78 darea.signal_button_press_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_press_event));
79 darea.signal_button_release_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_release_event));
80 darea.signal_motion_notify_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_motion_notify_event));
82 add_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::add_speaker));
83 remove_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::remove_speaker));
84 azimuth_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &SpeakerDialog::azimuth_changed));
88 /* selected index initialised to 0 above; this will set `no selection' and
89 sensitize widgets accordingly.
95 SpeakerDialog::set_speakers (boost::shared_ptr<Speakers> s)
100 boost::shared_ptr<Speakers>
101 SpeakerDialog::get_speakers () const
103 return _speakers.lock ();
107 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
109 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
117 cr = gdk_cairo_create (darea.get_window()->gobj());
119 cairo_set_line_width (cr, 1.0);
121 cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
122 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
123 cairo_fill_preserve (cr);
126 cairo_translate (cr, x_origin, y_origin);
128 /* horizontal line of "crosshairs" */
130 cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
131 cairo_move_to (cr, 0.5, height/2.0+0.5);
132 cairo_line_to (cr, width+0.5, height/2+0.5);
135 /* vertical line of "crosshairs" */
137 cairo_move_to (cr, width/2+0.5, 0.5);
138 cairo_line_to (cr, width/2+0.5, height+0.5);
141 /* the circle on which signals live */
143 cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
148 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
151 cairo_set_font_size (cr, 10);
154 cairo_set_font_size (cr, 16);
159 for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i) {
162 CartesianVector c (s.coords());
166 /* We have already moved our plotting origin to x_origin, y_origin,
167 so compensate for that.
172 x = (gint) floor (c.x);
173 y = (gint) floor (c.y);
175 /* XXX need to shift circles so that they are centered on the circle */
177 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
178 if (selected_index == n) {
179 cairo_set_source_rgb (cr, 0.8, 0.8, 0.2);
181 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
183 cairo_close_path (cr);
186 cairo_move_to (cr, x + 6, y + 6);
189 if (n == selected_index) {
190 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
192 snprintf (buf, sizeof (buf), "%d", n + 1);
194 cairo_show_text (cr, buf);
205 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
207 /* "c" uses a coordinate space that is:
210 dimension = 2.0 * 2.0
211 so max values along each axis are -1..+1
213 GTK uses a coordinate space that is:
216 dimension = width * height
217 so max values along each axis are 0,width and
221 c.x = (width / 2) * (c.x + 1) + x_origin;
222 c.y = (height / 2) * (1 - c.y) + y_origin;
224 /* XXX z-axis not handled - 2D for now */
228 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
230 c.x = ((c.x - x_origin) / (width / 2.0)) - 1.0;
231 c.y = -(((c.y - y_origin) / (height / 2.0)) - 1.0);
233 /* XXX z-axis not handled - 2D for now */
237 SpeakerDialog::clamp_to_circle (double& x, double& y)
243 PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
244 PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
248 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
250 width = alloc.get_width();
251 height = alloc.get_height();
253 /* The allocation will (should) be rectangualar, but make the basic
254 * drawing square; space to the right of the square is for over-hanging
264 /* Put the x origin to the left of the rectangular allocation */
265 x_origin = (alloc.get_width() - width) / 3;
266 y_origin = (alloc.get_height() - height) / 2;
270 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
272 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
277 GdkModifierType state;
279 if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
285 switch (ev->button) {
289 int const index = find_closest_object (ev->x, ev->y);
290 set_selected (index);
293 int const drag_x = (int) floor (ev->x);
294 int const drag_y = (int) floor (ev->y);
295 state = (GdkModifierType) ev->state;
297 if (drag_index >= 0) {
299 speakers->speakers()[drag_index].angles().cartesian (c);
301 drag_offset_x = drag_x - x_origin - c.x;
302 drag_offset_y = drag_y - y_origin - c.y;
305 return handle_motion (drag_x, drag_y, state);
317 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
319 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
325 GdkModifierType state;
328 switch (ev->button) {
330 x = (int) floor (ev->x);
331 y = (int) floor (ev->y);
332 state = (GdkModifierType) ev->state;
334 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
336 for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i) {
337 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
344 ret = handle_motion (x, y, state);
350 x = (int) floor (ev->x);
351 y = (int) floor (ev->y);
352 state = (GdkModifierType) ev->state;
354 ret = handle_motion (x, y, state);
368 SpeakerDialog::find_closest_object (gdouble x, gdouble y)
370 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
376 float best_distance = FLT_MAX;
380 for (vector<Speaker>::iterator i = speakers->speakers().begin(); i != speakers->speakers().end(); ++i, ++n) {
382 Speaker& candidate (*i);
385 candidate.angles().cartesian (c);
388 distance = sqrt ((c.x - x) * (c.x - x) +
389 (c.y - y) * (c.y - y));
392 if (distance < best_distance) {
393 best_distance = distance;
398 if (best_distance > 20) { // arbitrary
406 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
409 GdkModifierType state;
412 gdk_window_get_pointer (ev->window, &x, &y, &state);
414 x = (int) floor (ev->x);
415 y = (int) floor (ev->y);
416 state = (GdkModifierType) ev->state;
419 return handle_motion (x, y, state);
423 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
425 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
430 if (drag_index < 0) {
434 if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
438 /* correct event coordinates to have their origin at the corner of our graphic
439 rather than the corner of our allocation */
441 double obx = evx - x_origin;
442 double oby = evy - y_origin;
444 /* and compensate for any distance between the mouse pointer and the centre
445 of the object being dragged */
447 obx -= drag_offset_x;
448 oby -= drag_offset_y;
450 if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
452 bool need_move = false;
453 Speaker& moving (speakers->speakers()[drag_index]);
455 moving.angles().cartesian (c);
458 if (obx != c.x || oby != c.y) {
463 CartesianVector cp (obx, oby, 0.0);
465 /* canonicalize position */
469 /* position actual signal on circle */
471 clamp_to_circle (cp.x, cp.y);
473 /* generate an angular representation and set drag target (GUI) position */
489 SpeakerDialog::add_speaker ()
491 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
496 speakers->add_speaker (PBD::AngularVector (0, 0, 0));
501 SpeakerDialog::set_selected (int i)
503 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
508 if (i == selected_index) {
515 selected_speaker_connection.disconnect ();
517 azimuth_spinner.set_sensitive (selected_index != -1);
518 remove_speaker_button.set_sensitive (selected_index != -1);
520 if (selected_index != -1) {
521 azimuth_adjustment.set_value (speakers->speakers()[selected_index].angles().azi);
522 speakers->speakers()[selected_index].PositionChanged.connect (
523 selected_speaker_connection, MISSING_INVALIDATOR,
524 boost::bind (&SpeakerDialog::speaker_position_changed, this),
531 SpeakerDialog::azimuth_changed ()
533 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
538 assert (selected_index != -1);
540 if (ignore_azimuth_change) {
544 ignore_speaker_position_change = true;
545 speakers->move_speaker (speakers->speakers()[selected_index].id, PBD::AngularVector (azimuth_adjustment.get_value (), 0, 0));
546 ignore_speaker_position_change = false;
552 SpeakerDialog::speaker_position_changed ()
554 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
559 assert (selected_index != -1);
561 if (ignore_speaker_position_change) {
565 ignore_azimuth_change = true;
566 azimuth_adjustment.set_value (speakers->speakers()[selected_index].angles().azi);
567 ignore_azimuth_change = false;
573 SpeakerDialog::remove_speaker ()
575 boost::shared_ptr<Speakers> speakers = _speakers.lock ();
580 assert (selected_index != -1);
582 speakers->remove_speaker (speakers->speakers()[selected_index].id);