core/vgui/vgui_slider_tableau.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \author Amitha Perera
00004 // \date   Feb 2005
00005 
00006 #include "vgui_slider_tableau.h"
00007 
00008 #include <vgui/vgui_gl.h>
00009 
00010 vgui_slider_tableau::vgui_slider_tableau( slider_type type )
00011   : loc_( 0.0f ),
00012     horiz_( type == horiz ? true : false ),
00013     down_( false )
00014 {
00015 }
00016 
00017 vgui_slider_tableau::~vgui_slider_tableau()
00018 {
00019 }
00020 
00021 
00022 bool
00023 vgui_slider_tableau::handle(const vgui_event& e)
00024 {
00025   if( e.type == vgui_DRAW ) {
00026     draw_bar();
00027     return true;
00028   } else if( ! down_ && e.type == vgui_MOUSE_DOWN ) {
00029     last_x_ = e.wx;
00030     last_y_ = e.wy;
00031     last_loc_ = loc_;
00032     down_ = true;
00033     return true;
00034   } else if( down_ && e.type == vgui_MOTION ) {
00035     update_location( e.wx, e.wy );
00036     // call the motion callbacks
00037     call_callbacks( motion_callbacks_ );
00038     return true;
00039   } else if( down_ && e.type == vgui_MOUSE_UP ) {
00040     down_ = false;
00041     update_location(  e.wx, e.wy );
00042     // call the final & motion callbacks
00043     call_callbacks( motion_callbacks_ );
00044     call_callbacks( final_callbacks_ );
00045     return true;
00046   } else {
00047     return false;
00048   }
00049 }
00050 
00051 
00052 void
00053 vgui_slider_tableau::draw_bar() const
00054 {
00055   float pos = loc_*2.0f - 1.0f;
00056 
00057   glColor3f(1.0f,1.0f,1.0f);
00058   if( horiz_ ) {
00059     glBegin( GL_POLYGON );
00060     glVertex2f( pos-0.2f,  0.7f );
00061     glVertex2f( pos+0.2f,  0.7f );
00062     glVertex2f( pos+0.2f, -0.7f );
00063     glVertex2f( pos-0.2f, -0.7f );
00064     glEnd();
00065 
00066     glBegin( GL_LINES );
00067     glVertex2f( pos, -1.0f );
00068     glVertex2f( pos,  1.0f );
00069     glEnd();
00070   } else {
00071     glBegin( GL_POLYGON );
00072     glVertex2f(  0.7f, pos-0.2f );
00073     glVertex2f(  0.7f, pos+0.2f );
00074     glVertex2f( -0.7f, pos+0.2f );
00075     glVertex2f( -0.7f, pos-0.2f );
00076     glEnd();
00077 
00078     glBegin( GL_LINES );
00079     glVertex2f( -1.0f, pos );
00080     glVertex2f(  1.0f, pos );
00081     glEnd();
00082   }
00083 }
00084 
00085 
00086 vgui_slider_tableau::cb_handle
00087 vgui_slider_tableau::add_motion_callback( callback cb, void* data )
00088 {
00089   return motion_callbacks_.insert( motion_callbacks_.end(), callback_info( cb, data ) );
00090 }
00091 
00092 
00093 vgui_slider_tableau::cb_handle
00094 vgui_slider_tableau::add_final_callback( callback cb, void* data )
00095 {
00096   return final_callbacks_.insert( final_callbacks_.end(), callback_info( cb, data ) );
00097 }
00098 
00099 
00100 void
00101 vgui_slider_tableau::remove_motion_callback( cb_handle cbh )
00102 {
00103   motion_callbacks_.erase( cbh );
00104 }
00105 
00106 
00107 void
00108 vgui_slider_tableau::remove_final_callback( cb_handle cbh )
00109 {
00110   final_callbacks_.erase( cbh );
00111 }
00112 
00113 
00114 void
00115 vgui_slider_tableau::set_value( float v )
00116 {
00117   set_value_no_callbacks( v );
00118   call_callbacks( motion_callbacks_ );
00119   call_callbacks( final_callbacks_ );
00120 }
00121 
00122 void
00123 vgui_slider_tableau::set_value_no_callbacks( float v )
00124 {
00125   if( v < 0.0f ) {
00126     v = 0.0f;
00127   } else if( v > 1.0f ) {
00128     v = 1.0f;
00129   }
00130   loc_ = v;
00131   post_redraw();
00132 }
00133 
00134 
00135 void
00136 vgui_slider_tableau::call_callbacks( vcl_list< callback_info > const& cbs )
00137 {
00138   if( ! cbs.empty() ) {
00139     vcl_list< callback_info >::const_iterator it = cbs.begin();
00140     vcl_list< callback_info >::const_iterator end = cbs.end();
00141     for( ; it != end; ++it ) {
00142       (it->func_)( this, it->data_ );
00143     }
00144   }
00145 }
00146 
00147 
00148 void
00149 vgui_slider_tableau::update_location( int newx, int newy )
00150 {
00151   GLfloat vp[4];
00152   glGetFloatv(GL_VIEWPORT, vp);
00153   if( horiz_ ) {
00154     loc_ = last_loc_ + (newx - last_x_) / vp[2];
00155   } else {
00156     loc_ = last_loc_ + (newy - last_y_) / vp[3];
00157   }    
00158   if( loc_ < 0.0f ) {
00159     loc_ = 0.0f;
00160   } else if( loc_ > 1.0f ) {
00161     loc_ = 1.0f;
00162   }
00163   post_redraw();
00164 }