Stop speaker positions jumping around when clicked on.
[ardour.git] / gtk2_ardour / speaker_dialog.cc
1 /*
2     Copyright (C) 2011 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "pbd/cartesian.h"
21
22 #include "gtkmm2ext/keyboard.h"
23
24 #include "speaker_dialog.h"
25
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29 using namespace PBD;
30 using namespace std;
31 using namespace Gtk;
32 using namespace Gtkmm2ext;
33
34 SpeakerDialog::SpeakerDialog ()
35         : ArdourDialog (_("Speaker Configuration"))
36         , aspect_frame ("", 0.5, 0.5, 1.0, false)
37         , azimuth_adjustment (0, 0.0, 360.0, 10.0, 1.0)
38         , azimuth_spinner (azimuth_adjustment)
39         , add_speaker_button (_("Add Speaker"))
40         , use_system_button (_("Use System"))
41                               
42 {
43         side_vbox.set_homogeneous (false);
44         side_vbox.set_border_width (9);
45         side_vbox.set_spacing (6);
46         side_vbox.pack_start (azimuth_spinner, false, false);
47         side_vbox.pack_start (add_speaker_button, false, false);
48         side_vbox.pack_start (use_system_button, false, false);
49
50         aspect_frame.set_size_request (200, 200);
51         aspect_frame.set_shadow_type (SHADOW_NONE);
52         aspect_frame.add (darea);
53
54         hbox.set_spacing (6);
55         hbox.set_border_width (6);
56         hbox.pack_start (aspect_frame, true, true);
57         hbox.pack_start (side_vbox, false, false);
58
59         get_vbox()->pack_start (hbox);
60         get_vbox()->show_all ();
61
62         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
63
64         darea.signal_size_allocate().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_size_allocate));
65         darea.signal_expose_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_expose_event));
66         darea.signal_button_press_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_press_event));
67         darea.signal_button_release_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_button_release_event));
68         darea.signal_motion_notify_event().connect (sigc::mem_fun (*this, &SpeakerDialog::darea_motion_notify_event));
69
70         add_speaker_button.signal_clicked().connect (sigc::mem_fun (*this, &SpeakerDialog::add_speaker));
71
72         drag_index = -1;
73 }
74
75 void
76 SpeakerDialog::set_speakers (boost::shared_ptr<Speakers> s) 
77 {
78         speakers = *s;
79 }
80
81 Speakers
82 SpeakerDialog::get_speakers () const
83 {
84         return speakers;
85 }
86
87 bool
88 SpeakerDialog::darea_expose_event (GdkEventExpose* event)
89 {
90         gint x, y;
91         cairo_t* cr;
92
93         cr = gdk_cairo_create (darea.get_window()->gobj());
94
95         cairo_set_line_width (cr, 1.0);
96
97         cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
98         cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 1.0);
99         cairo_fill_preserve (cr);
100         cairo_clip (cr);
101
102         cairo_translate (cr, x_origin, y_origin);
103
104         /* horizontal line of "crosshairs" */
105
106         cairo_set_source_rgb (cr, 0.0, 0.1, 0.7);
107         cairo_move_to (cr, 0.5, height/2.0+0.5);
108         cairo_line_to (cr, width+0.5, height/2+0.5);
109         cairo_stroke (cr);
110
111         /* vertical line of "crosshairs" */
112         
113         cairo_move_to (cr, width/2+0.5, 0.5);
114         cairo_line_to (cr, width/2+0.5, height+0.5);
115         cairo_stroke (cr);
116
117         /* the circle on which signals live */
118
119         cairo_arc (cr, width/2, height/2, height/2, 0, 2.0 * M_PI);
120         cairo_stroke (cr);
121
122         float arc_radius;
123         
124         cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
125         
126         if (height < 100) {
127                 cairo_set_font_size (cr, 10);
128                 arc_radius = 2.0;
129         } else {
130                 cairo_set_font_size (cr, 16);
131                 arc_radius = 4.0;
132         }
133
134         uint32_t n = 0;
135         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
136                 
137                 Speaker& s (*i);
138                 CartesianVector c (s.coords());
139                 
140                 cart_to_gtk (c);
141                 
142                 x = (gint) floor (c.x);
143                 y = (gint) floor (c.y);
144                 
145                 /* XXX need to shift circles so that they are centered on the circle */
146                 
147                 cairo_arc (cr, x, y, arc_radius, 0, 2.0 * M_PI);
148                 cairo_set_source_rgb (cr, 0.8, 0.2, 0.1);
149                 cairo_close_path (cr);
150                 cairo_fill (cr);
151                 
152                 cairo_move_to (cr, x + 6, y + 6);
153                 
154                 char buf[256];
155                 snprintf (buf, sizeof (buf), "%d:%d", n+1, (int) lrint (s.angles().azi));
156                 cairo_show_text (cr, buf);
157                 ++n;
158         }
159
160         cairo_destroy (cr);
161
162         return true;
163         
164 }
165
166 void
167 SpeakerDialog::cart_to_gtk (CartesianVector& c) const
168 {
169         /* "c" uses a coordinate space that is:
170             
171            center = 0.0
172            dimension = 2.0 * 2.0
173            so max values along each axis are -1..+1
174
175            GTK uses a coordinate space that is:
176
177            top left = 0.0
178            dimension = width * height
179            so max values along each axis are 0,width and
180            0,height
181         */
182
183         c.x = (width / 2) * (c.x + 1);
184         c.y = (height / 2) * (1 - c.y);
185
186         /* XXX z-axis not handled - 2D for now */
187 }
188
189 void
190 SpeakerDialog::gtk_to_cart (CartesianVector& c) const
191 {
192         c.x = (c.x / (width / 2.0)) - 1.0;
193         c.y = -((c.y / (height / 2.0)) - 1.0);
194
195         /* XXX z-axis not handled - 2D for now */
196 }
197
198 void
199 SpeakerDialog::clamp_to_circle (double& x, double& y)
200 {
201         double azi, ele;
202         double z = 0.0;
203         double l;
204
205         PBD::cartesian_to_spherical (x, y, z, azi, ele, l);
206         PBD::spherical_to_cartesian (azi, ele, 1.0, x, y, z);
207 }
208
209 void
210 SpeakerDialog::darea_size_allocate (Gtk::Allocation& alloc)
211 {
212         width = alloc.get_width();
213         height = alloc.get_height();
214
215         if (height > 100) {
216                 width -= 20;
217                 height -= 20;
218         }
219
220         x_origin = (alloc.get_width() - width) / 2;
221         y_origin = (alloc.get_height() - height) / 2;
222 }
223
224 bool
225 SpeakerDialog::darea_button_press_event (GdkEventButton *ev)
226 {
227         GdkModifierType state;
228
229         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
230                 return false;
231         }
232
233         drag_index = -1;
234
235         switch (ev->button) {
236         case 1:
237         case 2:
238         {
239                 drag_index = find_closest_object (ev->x, ev->y);
240                 int const drag_x = (int) floor (ev->x);
241                 int const drag_y = (int) floor (ev->y);
242                 state = (GdkModifierType) ev->state;
243
244                 if (drag_index >= 0) {
245                         CartesianVector c;
246                         speakers.speakers()[drag_index].angles().cartesian (c);
247                         cart_to_gtk (c);
248                         drag_offset_x = drag_x - x_origin - c.x;
249                         drag_offset_y = drag_y - y_origin - c.y;
250                 }
251
252                 return handle_motion (drag_x, drag_y, state);
253                 break;
254         }
255
256         default:
257                 break;
258         }
259
260         return false;
261 }
262
263 bool
264 SpeakerDialog::darea_button_release_event (GdkEventButton *ev)
265 {
266         gint x, y;
267         GdkModifierType state;
268         bool ret = false;
269
270         switch (ev->button) {
271         case 1:
272                 x = (int) floor (ev->x);
273                 y = (int) floor (ev->y);
274                 state = (GdkModifierType) ev->state;
275
276                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
277                         
278                         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i) {
279                                 /* XXX DO SOMETHING TO SET SPEAKER BACK TO "normal" */
280                         }
281
282                         queue_draw ();
283                         ret = true;
284
285                 } else {
286                         ret = handle_motion (x, y, state);
287                 }
288
289                 break;
290
291         case 2:
292                 x = (int) floor (ev->x);
293                 y = (int) floor (ev->y);
294                 state = (GdkModifierType) ev->state;
295
296                 ret = handle_motion (x, y, state);
297                 break;
298
299         case 3:
300                 break;
301
302         }
303         
304         drag_index = -1;
305
306         return ret;
307 }
308
309 int
310 SpeakerDialog::find_closest_object (gdouble x, gdouble y) 
311 {
312         float distance;
313         float best_distance = FLT_MAX;
314         int n = 0;
315         int which = -1;
316
317         for (vector<Speaker>::iterator i = speakers.speakers().begin(); i != speakers.speakers().end(); ++i, ++n) {
318
319                 Speaker& candidate (*i);
320                 CartesianVector c;
321         
322                 candidate.angles().cartesian (c);
323                 cart_to_gtk (c);
324
325                 distance = sqrt ((c.x - x) * (c.x - x) +
326                                  (c.y - y) * (c.y - y));
327
328
329                 if (distance < best_distance) {
330                         best_distance = distance;
331                         which = n;
332                 }
333         }
334
335         if (best_distance > 20) { // arbitrary 
336                 return -1;
337         }
338
339         return which;
340 }
341
342 bool
343 SpeakerDialog::darea_motion_notify_event (GdkEventMotion *ev)
344 {
345         gint x, y;
346         GdkModifierType state;
347
348         if (ev->is_hint) {
349                 gdk_window_get_pointer (ev->window, &x, &y, &state);
350         } else {
351                 x = (int) floor (ev->x);
352                 y = (int) floor (ev->y);
353                 state = (GdkModifierType) ev->state;
354         }
355
356         return handle_motion (x, y, state);
357 }
358
359 bool
360 SpeakerDialog::handle_motion (gint evx, gint evy, GdkModifierType state)
361 {
362         if (drag_index < 0) {
363                 return false;
364         }
365
366         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
367                 return false;
368         }
369
370         /* correct event coordinates to have their origin at the corner of our graphic
371            rather than the corner of our allocation */
372
373         double obx = evx - x_origin;
374         double oby = evy - y_origin;
375
376         /* and compensate for any distance between the mouse pointer and the centre
377            of the object being dragged */
378
379         obx -= drag_offset_x;
380         oby -= drag_offset_y;
381
382         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
383                 CartesianVector c;
384                 bool need_move = false;
385                 Speaker& moving (speakers.speakers()[drag_index]);
386
387                 moving.angles().cartesian (c);
388                 cart_to_gtk (c);
389
390                 if (obx != c.x || oby != c.y) {
391                         need_move = true;
392                 }
393
394                 if (need_move) {
395                         CartesianVector cp (obx, oby, 0.0);
396
397                         /* canonicalize position */
398
399                         gtk_to_cart (cp);
400
401                         /* position actual signal on circle */
402
403                         clamp_to_circle (cp.x, cp.y);
404                         
405                         /* generate an angular representation and set drag target (GUI) position */
406
407                         AngularVector a;
408
409                         cp.angular (a);
410
411                         moving.move (a);
412
413                         queue_draw ();
414                 }
415         } 
416
417         return true;
418 }
419
420 void
421 SpeakerDialog::add_speaker ()
422 {
423         speakers.add_speaker (PBD::AngularVector (0, 0, 0));
424         queue_draw ();
425 }