Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

CAUGuiKnob.cpp

Go to the documentation of this file.
00001 /*
00002  *  CAUGuiKnob.cpp
00003  *  CoreGraphics AudioUnit GUI framework
00004  *
00005  *  Created by Urs Heckmann on Sun Oct 20 2002.
00006  *  Copyright (c) 2002 __MyCompanyName__. All rights reserved.
00007  *
00008  */
00009 
00010 #include "CAUGuiKnob.h"
00011 #include "CAUGuiMoreImages.h"
00012 
00013 CAUGuiKnob::CAUGuiKnob (    CAUGuiMan*          theChief,
00014                             CAAUParameter       &theAuvp, 
00015                             eRect*              theWhere,
00016                             CAUGuiResolution    theRes, 
00017                             CAUGuiGraphic*      theForeGround, 
00018                             CAUGuiGraphic*      theBackground)
00019                         
00020                         :   CAUGuiCtrl ( theChief, theAuvp, theWhere, theRes )
00021                             
00022 {
00023         
00024     ForeGround = theForeGround;
00025     BackGround = theBackground;
00026     
00027     if ( theWhere->w == 0 )
00028     {
00029         if ( BackGround != NULL )
00030         {
00031             theWhere->w = BackGround->getWidth();
00032             setBounds(theWhere); 
00033         }
00034     
00035     }
00036     
00037     if ( theWhere->h == 0 )
00038     {
00039         if ( BackGround != NULL )
00040         {
00041             theWhere->h = BackGround->getHeight();
00042             setBounds(theWhere);
00043         }
00044     
00045     }
00046     
00047     centerX = theWhere->w / 2;
00048     centerY = theWhere->h / 2;
00049     
00050     setType( kCAUGui_Knob );
00051     
00052     setTolerance ( (SInt32) theRes );
00053     
00054     //printf ("ctor CAUGuiKnob\n");
00055 
00056 }
00057 
00058 CAUGuiKnob::~CAUGuiKnob ()
00059 {
00060 
00061     ForeGround = NULL;
00062     BackGround = NULL;
00063 
00064 
00065     //printf ("dtor CAUGuiKnob\n");
00066 
00067 
00068 }
00069 
00070 void CAUGuiKnob::draw(CGContextRef context, UInt32 portHeight )
00071 {
00072     ControlRef carbonControl = getCarbonControl();
00073     
00074     UInt32 max = GetControl32BitMaximum(carbonControl);
00075     UInt32 val = GetControl32BitValue( carbonControl );
00076     
00077     CGImageRef theBack = NULL;
00078     CGImageRef theKnob = NULL;
00079     
00080     CGRect bounds;
00081     
00082     getBounds()->to( &bounds, portHeight );
00083     
00084     if ( BackGround != NULL )
00085         theBack = BackGround->getImage();
00086     
00087                 
00088     if ( theBack != NULL )
00089         CGContextDrawImage( context, bounds, theBack );
00090     
00091     SInt32 pivotX = centerX - (getForeBounds()->x - getBounds()->x + getForeBounds()->w/2 );
00092     SInt32 pivotY = centerY - (getForeBounds()->y - getBounds()->y + getForeBounds()->h/2 );
00093     
00094     //printf ( "x %d  y %d \n", pivotX, pivotY );
00095     
00096     if ( ForeGround != NULL )
00097     {
00098         theKnob = ForeGround->getImage();
00099     
00100         if ( theKnob != NULL )
00101         {
00102             float valNorm = (float) val / (float) max;
00103             
00104             if ( ForeGround->getType() == kCAUGui_SpinImage )
00105                 ((CAUGuiSpinImage*)ForeGround)->draw ( context, portHeight, getForeBounds(), valNorm, pivotX, pivotY );
00106             else
00107                 ForeGround->draw ( context, portHeight, getForeBounds(), valNorm );
00108         }
00109     }
00110 }
00111 
00112 SInt32 cent_X;
00113 SInt32 cent_Y;
00114 SInt32 tmp;
00115 SInt32 val;
00116 
00117 void CAUGuiKnob::mouseDown(Point *P, bool with_option, bool with_shift)
00118 {
00119     ControlRef carbonControl = getCarbonControl();
00120 
00121     if ( !with_option )
00122         mouseTrack ( P, with_option, with_shift );
00123     else
00124     {
00125         val = tmp = GetControl32BitValue( carbonControl );
00126         cent_X = P->h;
00127         cent_Y = 0;
00128     }
00129 
00130 }
00131 
00132 void CAUGuiKnob::mouseTrack(Point *P, bool with_option, bool with_shift)
00133 {
00134 
00135     ControlRef carbonControl = getCarbonControl();
00136 
00137     SInt32 max = (SInt32) GetControl32BitMaximum(carbonControl);
00138 
00139     if ( with_option ) // linear mode 
00140     {               
00141         if ( cent_Y != (cent_X - P->h) )
00142         {
00143             cent_Y = cent_X - P->h;
00144             if ( with_shift ) val = tmp - (cent_Y/2);
00145             else val = tmp - (UInt32)getResolution () * (cent_Y/2);
00146         }
00147     }
00148     else
00149     {
00150         if ( with_shift ) val = (SInt32)( getValueFromPoint(P) * (float) max );
00151         else val = (SInt32)getResolution () * (SInt32)( getValueFromPoint(P) * (float) getRange() );
00152     }
00153             
00154     if ( val > max ) val = max;
00155     if ( val < 0 ) val = 0;
00156     SetControl32BitValue ( carbonControl, (UInt32) val );
00157 }
00158 
00159 void CAUGuiKnob::mouseUp(Point *P, bool with_option, bool with_shift)
00160 {
00161     mouseTrack ( P, with_option, with_shift );
00162 }
00163 
00164 float CAUGuiKnob::getValueFromPoint(Point* P)
00165 {
00166     
00167     //printf ( "X= %d - Y= %d\n", P->h, P->v ); 
00168     
00169     float deltaX = (float)( P->h - centerX );
00170     float deltaY = (float)( P->v - centerY ); // bigger if upwards
00171 
00172     //printf ( "X= %f - Y= %f\n", deltaX, deltaY );
00173 
00174     float PIh = PI / 2.0;
00175     float angle;
00176 
00177     if ( deltaY == 0.f )
00178     {
00179         if ( deltaX < 0.f ) angle = -PIh;
00180         else angle = PIh;
00181     }
00182     else
00183     {
00184         if ( deltaY > 0.f )
00185         {
00186             angle = atan ( deltaX / deltaY );
00187             if ( angle > 0.f ) angle = PI - angle;
00188             else angle = -PI - angle;
00189         }
00190         else
00191         {
00192             angle = - atan ( deltaX / deltaY );
00193         }
00194     }
00195 
00196     angle += PI * 3. / 4.;
00197     return angle / ( PI * 1.5 );
00198 
00199 
00200 }
00201 
00202 
00203 

(c) 2002 - 2003 by Urs Heckmann www.u-he.com
Generated on Thu May 6 15:13:14 2004 for CAUGui by doxygen 1.3.6