Update NTK.
[nondaw.git] / mixer / src / Panner.H
blob08943c04d9784d90754f2d7aae9f835b4a584b02
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
5 /* This program is free software; you can redistribute it and/or modify it     */
6 /* under the terms of the GNU General Public License as published by the       */
7 /* Free Software Foundation; either version 2 of the License, or (at your      */
8 /* option) any later version.                                                  */
9 /*                                                                             */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       */
12 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   */
13 /* more details.                                                               */
14 /*                                                                             */
15 /* You should have received a copy of the GNU General Public License along     */
16 /* with This program; see the file COPYING.  If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 /*******************************************************************************/
20 #pragma once
22 #include <FL/Fl_Widget.H>
23 #include <FL/fl_draw.H>
24 #include <FL/Fl.H>
26 #include <math.h>
28 #include <vector>
29 using namespace std;
31 class Panner : public Fl_Widget
34     void draw_the_box( int, int, int, int );
36     struct Point
37     {
38         /* axes */
40         /* distance from center (from 0 to 1) */
41         float d;
42         /* angle */
43         float a;
45         Point ( ) : d( 0.0f ), a( 0.0f ) { }
46         Point ( float D, float A ) : d( D ), a( A ) { }
48         /** translate angle /a/ into x/y coords and place the result in /X/ and /Y/ */
49         void
50         axes ( float *X, float *Y ) const
51             {
52                 /* rotate */
53                 float A = ( 270 - a ) * ( M_PI / 180 );
55                 *X = -d * cosf( A );
56                 *Y = d * sinf( A );
57             }
59         float azimuth ( void ) const
60             {
61                 return a > 180.0f ? a - 360.0f : a;
62             }
65         float elevation ( void ) const
66             {
67                 return ( 1.0f - d ) * 90.0f;
68             }
70          void azimuth (  float v )
71             {
72                 a = v < 0.0f ? v + 360.0f : v;
73                 a = a < 0.0f ? 0.0f : a > 360.0f ? 360.0f : a;
74             }
76          void elevation ( float v )
77             {
78                 d = 1.0f - ( v / 90.0f );
79                 d = d < 0.0f ? 0.0f : d > 1.0f ? 1.0f : d;
80             }
82         /** set point position in X, Y coordinates (0.0 to 1.0) */
83         void
84         angle ( float X1, float Y1 )
85             {
87                 float X2, Y2;
89                 Y2 = X2 = 0;
91                 float t;
93                 t = atan2( X2 - X1, Y2 - Y1 );
95                 a = t * (180 / M_PI);
97                 if ( a < 0 )
98                     a = 360 + a;
100                 a = 360 - a;
102                 /* standard distance calculation */
103                 d = sqrt( pow( X2 - X1, 2 ) + pow( Y2 - Y1, 2 ) );
105                 if ( d > 1.0f )
106                     d = 1.0f;
107             }
109         /** return the distance between the point and that referenced by /p/ */
110         float
111         distance ( const Point &p )
112             {
113                 /* first, transform point coords */
115                 float x1, y1, x2, y2;
117                 axes( &x1, &y1 );
118                 p.axes( &x2, &y2 );
120                 /* standard distance calculation */
121                 return sqrt( pow( x1 - x2, 2 ) + pow( y1 - y2, 2 ) );
122             }
124     };
127     /* channel configuration */
128     int _ins,
129         _outs;
131     bool _bypassed;
133     vector <Point> _points;
135     static int pw ( void ) { return 12; }
136     static int ph ( void ) { return 12; }
138     static int _configs[][12];
140     void bbox ( int &X, int &Y, int &W, int &H ) const
141         {
142             W = w() - Fl::box_dw( box() );
143             H = h() - Fl::box_dh( box() );
144             X = x() + Fl::box_dx( box() );
145             Y = y() + Fl::box_dy( box() );
146         }
148     void point_bbox ( const Point *p, int *X, int *Y, int *W, int *H ) const;
150     Point * event_point ( void );
151     Point angle_to_axes ( float a );
153     enum {
154         NONE = -1,
155         R    = 90,
156         L    = 270,
157         C   = 0,
158         FL   = 315,
159         FR   = 45,
160         RL   = 225,
161         RR   = 135,
162     };
164     static Point * drag;
166 protected:
168     virtual void draw ( void );
169     virtual int handle ( int );
171 public:
174     Panner ( int X, int Y, int W, int H, const char *L = 0 ) :
175         Fl_Widget( X, Y, W, H, L )
176         {
177             _bypassed = false;
179             _ins = 1;
181             _outs = 1;
183 //            _ins = _outs = 4;
185 //            _points.push_back( Point( 1, FL ) );
186             _points.push_back( Point( 1, C ) );
188 /*             _points.push_back( Point( 1, FR ) ); */
189 /*             _points.push_back( Point( 1, RL ) ); */
190 /*             _points.push_back( Point( 1, RR ) ); */
193         }
195     virtual ~Panner ( ) { }
197     Panner::Point *point ( int i );