draw pucks (signal positions) on vbap panner
[ardour.git] / gtk2_ardour / panner2d.cc
1 /*
2     Copyright (C) 2002 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 <cmath>
21 #include <climits>
22 #include <string.h>
23
24 #include <cairo.h>
25 #include <gtkmm/menu.h>
26
27 #include "gtkmm2ext/gtk_ui.h"
28
29 #include "pbd/error.h"
30 #include "pbd/cartesian.h"
31 #include "ardour/panner.h"
32
33 #include "panner2d.h"
34 #include "keyboard.h"
35 #include "gui_thread.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace ARDOUR;
42 using namespace PBD;
43 using Gtkmm2ext::Keyboard;
44
45 Panner2d::Target::Target (const AngularVector& a, const char *txt)
46         : position (a)
47         , text (txt)
48         , _selected (false)
49 {
50 }
51
52 Panner2d::Target::~Target ()
53 {
54 }
55
56 void
57 Panner2d::Target::set_text (const char* txt)
58 {
59         text = txt;
60 }
61
62 Panner2d::Panner2d (boost::shared_ptr<Panner> p, int32_t h)
63         : panner (p), width (0), height (h)
64 {
65         panner->StateChanged.connect (state_connection, invalidator (*this), boost::bind (&Panner2d::handle_state_change, this), gui_context());
66         panner->Changed.connect (change_connection, invalidator (*this), boost::bind (&Panner2d::handle_position_change, this), gui_context());
67
68         drag_target = 0;
69         set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
70 }
71
72 Panner2d::~Panner2d()
73 {
74         for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
75                 delete *i;
76         }
77 }
78
79 void
80 Panner2d::reset (uint32_t n_inputs)
81 {
82         uint32_t nouts = panner->out().n_audio();
83
84         /* pucks */
85
86         while (pucks.size() < n_inputs) {
87                 add_puck ("", AngularVector());
88         }
89
90         if (pucks.size() > n_inputs) {
91                 for (uint32_t i = pucks.size(); i < n_inputs; ++i) {
92                         delete pucks[i];
93                 }
94
95                 pucks.resize (n_inputs);
96         }
97                                                 
98         switch (n_inputs) {
99         case 0:
100                 break;
101
102         case 1:
103                 pucks[0]->set_text ("");
104                 break;
105
106         case 2:
107                 pucks[0]->set_text ("R");
108                 pucks[1]->set_text ("L");
109                 break;
110
111         default:
112                 for (uint32_t i = 0; i < n_inputs; ++i) {
113                         char buf[64];
114                         snprintf (buf, sizeof (buf), "%" PRIu32, i + 1);
115                         pucks[i]->set_text (buf);
116                 }
117                 break;
118         }
119
120         for (uint32_t i = 0; i < n_inputs; ++i) {
121                 pucks[i]->position = panner->signal_position (i);
122         }
123
124         /* add all outputs */
125
126         while (targets.size() < nouts) {
127                 add_target (AngularVector());
128         }
129
130         if (targets.size() > nouts) {
131                 for (uint32_t i = nouts; i < targets.size(); ++i) {
132                         delete targets[i];
133                 }
134
135                 targets.resize (nouts);
136         }
137
138         for (Targets::iterator x = targets.begin(); x != targets.end(); ++x) {
139                 (*x)->visible = false;
140         }
141
142         for (uint32_t n = 0; n < nouts; ++n) {
143                 char buf[16];
144
145                 snprintf (buf, sizeof (buf), "%d", n+1);
146                 targets[n]->set_text (buf);
147 #ifdef PANNER_HACKS
148                 targets[n]->position = panner->output(n).position;
149                 targets[n]->visible = true;
150 #endif
151         }
152
153         queue_draw ();
154 }
155
156 void
157 Panner2d::on_size_allocate (Gtk::Allocation& alloc)
158 {
159         width = alloc.get_width();
160         height = alloc.get_height();
161
162         /* our idea of our width/height must be "square
163          */
164
165         if (height > 100) {
166                 width -= 20;
167                 height -= 20;
168         }
169
170         dimen = min (width, height);
171         
172         DrawingArea::on_size_allocate (alloc);
173 }
174
175 int
176 Panner2d::add_puck (const char* text, const AngularVector& a)
177 {
178         Target* puck = new Target (a, text);
179         pucks.push_back (puck);
180         puck->visible = true;
181
182         return 0;
183 }
184
185 int
186 Panner2d::add_target (const AngularVector& a)
187 {
188         Target* target = new Target (a, "");
189         targets.push_back (target);
190         target->visible = true;
191         queue_draw ();
192
193         return targets.size() - 1;
194 }
195
196 void
197 Panner2d::handle_state_change ()
198 {
199         ENSURE_GUI_THREAD (*this, &Panner2d::handle_state_change)
200
201         queue_draw ();
202 }
203
204 void
205 Panner2d::handle_position_change ()
206 {
207 #ifdef PANNER_HACKS 
208         uint32_t n;
209         ENSURE_GUI_THREAD (*this, &Panner2d::handle_position_change)
210
211         for (n = 0; n < pucks.size(); ++n) {
212                 pucks[n]->position = panner->streampanner(n).get_position ();
213         }
214
215         for (n = 0; n < targets.size(); ++n) {
216                 targets[n]->position = panner->output(n).position;
217         }
218
219         queue_draw ();
220 #endif
221 }
222
223 void
224 Panner2d::move_puck (int which, const AngularVector& a)
225 {
226         if (which >= int (targets.size())) {
227                 return;
228         }
229         
230         targets[which]->position = a;
231         queue_draw ();
232 }
233
234 Panner2d::Target *
235 Panner2d::find_closest_object (gdouble x, gdouble y, int& which) const
236 {
237         Target *closest = 0;
238         Target *candidate;
239         float distance;
240         float best_distance = FLT_MAX;
241         int pwhich;
242
243         which = 0;
244         pwhich = 0;
245
246         for (Targets::const_iterator i = pucks.begin(); i != pucks.end(); ++i, ++pwhich) {
247                 candidate = *i;
248
249                 CartesianVector c;
250
251                 candidate->position.cartesian (c);
252                 cart_to_gtk (c);
253
254                 distance = sqrt ((c.x - x) * (c.x - x) +
255                                  (c.y - y) * (c.y - y));
256
257                 if (distance < best_distance) {
258                         closest = candidate;
259                         best_distance = distance;
260                         which = pwhich;
261                 }
262         }
263
264
265         if (best_distance > 20) { // arbitrary 
266                 return 0;
267         }
268
269         return closest;
270 }
271
272 bool
273 Panner2d::on_motion_notify_event (GdkEventMotion *ev)
274 {
275         gint x, y;
276         GdkModifierType state;
277
278         if (ev->is_hint) {
279                 gdk_window_get_pointer (ev->window, &x, &y, &state);
280         } else {
281                 x = (int) floor (ev->x);
282                 y = (int) floor (ev->y);
283                 state = (GdkModifierType) ev->state;
284         }
285
286         return handle_motion (x, y, state);
287 }
288 bool
289 Panner2d::on_expose_event (GdkEventExpose *event)
290 {
291         gint x, y;
292         cairo_t* cr;
293
294         cr = gdk_cairo_create (get_window()->gobj());
295
296         cairo_set_line_width (cr, 1.0);
297
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);
301         } else {
302                 cairo_set_source_rgba (cr, 0.1, 0.1, 0.1, 0.2);
303         }
304         cairo_fill_preserve (cr);
305         cairo_clip (cr);
306
307         if (height > 100) {
308                 cairo_translate (cr, 10.0, 10.0);
309         }
310
311         /* horizontal line of "crosshairs" */
312
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);
316         cairo_stroke (cr);
317
318         /* vertical line of "crosshairs" */
319         
320         cairo_move_to (cr, width/2+0.5, 0.5);
321         cairo_line_to (cr, width/2+0.5, height+0.5);
322         cairo_stroke (cr);
323
324         /* the circle on which signals live */
325
326         cairo_arc (cr, width/2, height/2, dimen/2, 0, 2.0 * M_PI);
327         cairo_stroke (cr);
328
329         if (!panner->bypassed()) {
330                 float arc_radius;
331
332                 cairo_select_font_face (cr, "sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
333
334                 if (height < 100) {
335                         cairo_set_font_size (cr, 10);
336                         arc_radius = 2.0;
337                 } else {
338                         cairo_set_font_size (cr, 16);
339                         arc_radius = 4.0;
340                 }
341
342                 for (Targets::iterator i = pucks.begin(); i != pucks.end(); ++i) {
343
344                         Target* puck = *i;
345
346                         if (puck->visible) {
347                                 /* redraw puck */
348
349                                 CartesianVector c;
350                                 
351                                 puck->position.cartesian (c);
352                                 cart_to_gtk (c);
353
354                                 x = (gint) floor (c.x);
355                                 y = (gint) floor (c.y);
356
357                                 /* XXX need to shift circles so that they are centered on the circle */
358                                         
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);
362                                 cairo_fill (cr);
363
364                                 cairo_move_to (cr, x + 6, y + 6);
365
366                                 char buf[256];
367                                 snprintf (buf, sizeof (buf), "%s:%d", puck->text.c_str(), (int) lrint (puck->position.azi));
368                                 cairo_show_text (cr, buf);
369                         }
370                 }
371
372                 /* redraw any visible targets */
373
374                 int n = 0;
375
376                 for (Targets::iterator i = targets.begin(); i != targets.end(); ++i) {
377                         Target *target = *i;
378                         char buf[256];
379                         ++n;
380
381                         if (target->visible) {
382
383                                 CartesianVector c;
384                                 
385                                 target->position.cartesian (c);
386                                 cart_to_gtk (c);
387
388                                 x = (int) floor (c.x);
389                                 y = (int) floor (c.y);
390                                 
391                                 snprintf (buf, sizeof (buf), "%d", n);
392
393                                 cairo_set_source_rgb (cr, 0.0, 0.8, 0.1);
394                                 cairo_rectangle (cr, x-2, y-2, 4, 4);
395                                 cairo_fill (cr);
396                                 cairo_move_to (cr, x+6, y+6);
397                                 cairo_show_text (cr, buf);
398
399                         }
400                 }
401         }
402
403         cairo_destroy (cr);
404
405         return true;
406 }
407
408 bool
409 Panner2d::on_button_press_event (GdkEventButton *ev)
410 {
411         GdkModifierType state;
412
413         if (ev->type == GDK_2BUTTON_PRESS && ev->button == 1) {
414                 return false;
415         }
416
417         switch (ev->button) {
418         case 1:
419         case 2:
420                 if ((drag_target = find_closest_object (ev->x, ev->y, drag_index)) != 0) {
421                         drag_target->set_selected (true);
422                 }
423
424                 drag_x = (int) floor (ev->x);
425                 drag_y = (int) floor (ev->y);
426                 state = (GdkModifierType) ev->state;
427
428                 return handle_motion (drag_x, drag_y, state);
429                 break;
430
431         default:
432                 break;
433         }
434
435         return false;
436 }
437
438 bool
439 Panner2d::on_button_release_event (GdkEventButton *ev)
440 {
441         gint x, y;
442         GdkModifierType state;
443         bool ret = false;
444
445         switch (ev->button) {
446         case 1:
447                 x = (int) floor (ev->x);
448                 y = (int) floor (ev->y);
449                 state = (GdkModifierType) ev->state;
450
451                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
452                         
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" */
456                         }
457
458                         queue_draw ();
459                         PuckMoved (-1);
460                         ret = true;
461
462                 } else {
463                         ret = handle_motion (x, y, state);
464                 }
465
466                 drag_target = 0;
467                 break;
468
469         case 2:
470                 x = (int) floor (ev->x);
471                 y = (int) floor (ev->y);
472                 state = (GdkModifierType) ev->state;
473
474                 if (Keyboard::modifier_state_contains (state, Keyboard::TertiaryModifier)) {
475                         toggle_bypass ();
476                         ret = true;
477                 } else {
478                         ret = handle_motion (x, y, state);
479                 }
480
481                 drag_target = 0;
482                 break;
483
484         case 3:
485                 break;
486
487         }
488
489         return ret;
490 }
491
492 gint
493 Panner2d::handle_motion (gint evx, gint evy, GdkModifierType state)
494 {
495         if (drag_target == 0) {
496                 return false;
497         }
498
499         if ((state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK)) == 0) {
500                 return false;
501         }
502
503
504         if (state & GDK_BUTTON1_MASK && !(state & GDK_BUTTON2_MASK)) {
505                 CartesianVector c;
506                 bool need_move = false;
507                 
508                 drag_target->position.cartesian (c);
509                 cart_to_gtk (c);
510
511                 if ((evx != c.x) || (evy != c.y)) {
512                         need_move = true;
513                 }
514
515                 if (need_move) {
516                         CartesianVector cp (evx, evy, 0.0);
517
518                         /* canonicalize position */
519
520                         gtk_to_cart (cp);
521
522                         /* position actual signal on circle */
523
524                         clamp_to_circle (cp.x, cp.y);
525                         
526                         /* generate an angular representation and set drag target (GUI) position */
527
528                         cp.angular (drag_target->position); /* sets drag target position */
529
530 #ifdef PANNER_HACKS
531                         panner->streampanner (drag_index).set_position (drag_target->position);
532 #endif 
533                         
534                         queue_draw ();
535                 }
536         } 
537
538         return true;
539 }
540
541 void
542 Panner2d::cart_to_gtk (CartesianVector& c) const
543 {
544         /* "c" uses a coordinate space that is:
545             
546            center = 0.0
547            dimension = 2.0 * 2.0
548            so max values along each axis are -1..+1
549
550            GTK uses a coordinate space that is:
551
552            top left = 0.0
553            dimension = width * height
554            so max values along each axis are 0,width and
555            0,height
556         */
557         
558         const uint32_t hoffset = (width - dimen)/2;
559         const uint32_t voffset = (height - dimen)/2;
560
561         c.x = hoffset + ((dimen / 2) * (c.x + 1));
562         c.y = voffset + ((dimen / 2) * (1 - c.y));
563
564         /* XXX z-axis not handled - 2D for now */
565 }
566
567 void
568 Panner2d::gtk_to_cart (CartesianVector& c) const
569 {
570         c.x = ((c.x / (dimen / 2.0)) - 1.0);
571         c.y = -((c.y / (dimen / 2.0)) - 1.0);
572
573         /* XXX z-axis not handled - 2D for now */
574 }
575
576 void
577 Panner2d::clamp_to_circle (double& x, double& y)
578 {
579         double azi, ele;
580         double z = 0.0;
581         
582         PBD::cart_to_azi_ele (x, y, z, azi, ele);
583         PBD::azi_ele_to_cart (azi, ele, x, y, z);
584 }
585
586 void
587 Panner2d::toggle_bypass ()
588 {
589         panner->set_bypassed (!panner->bypassed());
590 }
591
592 Panner2dWindow::Panner2dWindow (boost::shared_ptr<Panner> p, int32_t h, uint32_t inputs)
593         : widget (p, h)
594         , reset_button (_("Reset"))
595         , bypass_button (_("Bypass"))
596         , mute_button (_("Mute"))
597 {
598         widget.set_name ("MixerPanZone");
599
600         set_title (_("Panner"));
601         widget.set_size_request (h, h);
602
603         button_box.set_spacing (6);
604         button_box.pack_start (reset_button, false, false);
605         button_box.pack_start (bypass_button, false, false);
606         button_box.pack_start (mute_button, false, false);
607
608         spinner_box.set_spacing (6);
609         left_side.set_spacing (6);
610
611         left_side.pack_start (button_box, false, false);
612         left_side.pack_start (spinner_box, false, false);
613
614         reset_button.show ();
615         bypass_button.show ();
616         mute_button.show ();
617         button_box.show ();
618         spinner_box.show ();
619         left_side.show ();
620
621         hpacker.set_spacing (6);
622         hpacker.set_border_width (12);
623         hpacker.pack_start (widget, false, false);
624         hpacker.pack_start (left_side, false, false);
625         hpacker.show ();
626
627         add (hpacker);
628         reset (inputs);
629         widget.show ();
630 }
631
632 void
633 Panner2dWindow::reset (uint32_t n_inputs)
634 {
635         widget.reset (n_inputs);
636
637 #if 0
638         while (spinners.size() < n_inputs) {
639                 // spinners.push_back (new Gtk::SpinButton (widget.azimuth (spinners.size())));
640                 //spinner_box.pack_start (*spinners.back(), false, false);
641                 //spinners.back()->set_digits (4);
642                 spinners.back()->show ();
643         }
644
645         while (spinners.size() > n_inputs) {
646                 spinner_box.remove (*spinners.back());
647                 delete spinners.back();
648                 spinners.erase (--spinners.end());
649         }
650 #endif
651 }