Back to index

extremetuxracer  0.5beta
ssbutton.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "ssbutton.h"
00023 
00024 #include "textures.h"
00025 
00026 #include "render_util.h"
00027 
00028 #include "ui_mgr.h"
00029 
00030 
00031 namespace pp {
00032        
00033 SSButton::SSButton(pp::Vec2d pos, pp::Vec2d size,int num_states )
00034 {
00035     m_position=pos;
00036        m_size= size;
00037     m_sensitive = true;
00038     m_visible = false;
00039     m_active = false;
00040        m_numStates = num_states;
00041        m_regions = new texture_region_t[num_states];
00042 
00043     for (int i=0; i<num_states; i++) {
00044               m_regions[i].binding = NULL;
00045     }
00046 
00047     m_currentState = 0;
00048 }
00049 
00050 SSButton::~SSButton()
00051 {
00052     setVisible( false );
00053     setActive( false );
00054     delete m_regions;       
00055 }
00056 
00057 void
00058 SSButton::setState( int state )
00059 {
00060        m_currentState = state;
00061 
00062     if ( m_visible ) {
00063               UIMgr.setDirty();
00064     }
00065 }
00066 
00067 
00068 void
00069 SSButton::setStateImage( int state,
00070                             const char *binding,
00071                             pp::Vec2d ll, pp::Vec2d ur, 
00072                             pp::Color color )
00073 {
00074        m_regions[state].binding = binding;
00075     m_regions[state].color = color;
00076     m_regions[state].ll = ll;
00077     m_regions[state].ur = ur;
00078 }
00079 
00080 void
00081 SSButton::performClickAction()
00082 {
00083        m_currentState = ( m_currentState + 1 ) %m_numStates;
00084        signalClicked.Emit();
00085 }
00086 
00087 void
00088 SSButton::draw()
00089 {
00090        GLuint texobj;
00091        
00092        glEnable( GL_TEXTURE_2D );
00093        
00094        if ( !get_texture_binding( m_regions[m_currentState].binding, &texobj ) ) {
00095               print_warning( IMPORTANT_WARNING,
00096                         "Couldnt get texture object for binding %s",
00097                         m_regions[m_currentState].binding );
00098            texobj = 0;
00099        }
00100 
00101        glBindTexture( GL_TEXTURE_2D, texobj );
00102 
00103        glColor4dv( (double*) &m_regions[m_currentState].color );
00104 
00105        glBegin( GL_QUADS );
00106        {
00107            glTexCoord2f( m_regions[m_currentState].ll.x, m_regions[m_currentState].ll.y );
00108            glVertex3f( m_position.x, m_position.y, 0 );
00109 
00110            glTexCoord2f( m_regions[m_currentState].ur.x, m_regions[m_currentState].ll.y );
00111            glVertex3f( m_position.x + m_size.x, m_position.y, 0 );
00112 
00113            glTexCoord2f( m_regions[m_currentState].ur.x, m_regions[m_currentState].ur.y );
00114            glVertex3f( m_position.x + m_size.x, m_position.y + m_size.y, 0 );
00115 
00116            glTexCoord2f( m_regions[m_currentState].ll.x, m_regions[m_currentState].ur.y );
00117            glVertex3f( m_position.x, m_position.y + m_size.y, 0 );
00118        }
00119        glEnd();
00120 
00121 }
00122        
00123 } //namespace pp