Back to index

extremetuxracer  0.5beta
button.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00003  * 
00004  * Copyright (C) 1999-2001 Jasmin F. Patry
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 #include "button.h"
00022 
00023 #include "textures.h"
00024 #include "ui_mgr.h"
00025 
00026 
00027 namespace pp {
00028        
00029 Button::Button( pp::Vec2d pos, pp::Vec2d size,
00030                       const char *binding, const char *label )
00031  :     mp_font(NULL),
00032        mp_hilitFont(NULL),
00033        mp_disabledFont(NULL)
00034 {
00035     m_position = pos;
00036     m_size = size;
00037 
00038     m_tex.binding = NULL;
00039     m_hilitTex.binding = NULL;
00040     m_clickedTex.binding = NULL;
00041     m_disabledTex.binding = NULL;
00042        
00043        if(binding!=NULL){
00044               mp_font = pp::Font::get(binding);
00045        }
00046        
00047     mp_label = label;
00048 
00049     m_clicked = false;
00050     m_focused = false;
00051     m_sensitive = true;
00052     m_visible = false;
00053     m_active = false;
00054 }
00055 
00056 void
00057 Button::setHilitFontBinding( const char *binding )
00058 {
00059        mp_hilitFont = pp::Font::get(binding);
00060 }
00061 
00062 void
00063 Button::setImage( const char *binding,
00064                      pp::Vec2d p0, pp::Vec2d p1, pp::Color color )
00065 {
00066     m_tex.binding = binding;
00067     m_tex.ll = p0;
00068     m_tex.ur = p1;
00069     m_tex.color = color;
00070 }
00071 
00072 void
00073 Button::setDisabledImage( const char *binding,
00074                             pp::Vec2d p0, pp::Vec2d p1, pp::Color color )
00075 {
00076        m_disabledTex.binding = binding;
00077     m_disabledTex.ll = p0;
00078     m_disabledTex.ur = p1;
00079     m_disabledTex.color = color;
00080 }
00081 
00082 void
00083 Button::setHighlight(bool highlight)
00084 {
00085     if (!(m_focused == highlight)) {
00086               UIMgr.setDirty();
00087     }
00088     m_focused = highlight;
00089 }
00090 
00091 void
00092 Button::setHilitImage( const char *binding,
00093                           pp::Vec2d p0, pp::Vec2d p1, pp::Color color )
00094 {
00095     m_hilitTex.binding = binding;
00096     m_hilitTex.ll = p0;
00097     m_hilitTex.ur = p1;
00098     m_hilitTex.color = color;
00099 }
00100 
00101 void
00102 Button::setClickedImage( const char *binding,
00103                             pp::Vec2d p0, pp::Vec2d p1, pp::Color color )
00104 {
00105     m_clickedTex.binding = binding;
00106     m_clickedTex.ll = p0;
00107     m_clickedTex.ur = p1;
00108     m_clickedTex.color = color;
00109 }
00110 
00111 void
00112 Button::setDisabledFontBinding( const char *binding )
00113 {
00114        mp_disabledFont = pp::Font::get(binding);
00115 }
00116 
00117 void
00118 Button::draw()
00119 {
00120     GLuint texobj;
00121     texture_region_t *tex;
00122        pp::Font *font=mp_font;
00123 
00124     glEnable( GL_TEXTURE_2D );
00125 
00126     tex = NULL;
00127     
00128     if ( !m_sensitive ) {
00129               if ( m_disabledTex.binding ) {
00130                      tex = &m_disabledTex;
00131               } else if ( m_tex.binding ) {
00132                      tex = &m_tex;
00133               }
00134 
00135               if ( mp_disabledFont ) {
00136                      font = mp_disabledFont;
00137               }
00138     } else if ( m_clicked ) {
00139               if ( m_clickedTex.binding ) {
00140                      tex = &m_clickedTex;
00141               } else if ( m_hilitTex.binding ) {
00142                      tex = &m_hilitTex;
00143               } else if ( m_tex.binding ) {
00144                      tex = &m_tex;
00145               } 
00146 
00147               if ( mp_hilitFont) {
00148                      font = mp_hilitFont;
00149               } 
00150     } else if ( m_focused ) {
00151               if ( m_hilitTex.binding ) {
00152                      tex = &m_hilitTex;
00153               } else if ( m_tex.binding ) {
00154                      tex = &m_tex;
00155               } 
00156 
00157               if ( mp_hilitFont ) {
00158                      font = mp_hilitFont;
00159               }
00160     } else {
00161               if ( m_tex.binding ) {
00162                      tex = &m_tex;
00163               }
00164     }
00165 
00166     if ( tex != NULL ) {
00167        if ( !get_texture_binding( tex->binding, &texobj ) ) {
00168            print_warning( IMPORTANT_WARNING,
00169                         "Couldnt get texture object for binding %s",
00170                         tex->binding );
00171            texobj = 0;
00172        }
00173 
00174        glBindTexture( GL_TEXTURE_2D, texobj );
00175 
00176        glColor4dv( (double*) &tex->color );
00177 
00178        glBegin( GL_QUADS );
00179        {
00180            glTexCoord2f( tex->ll.x, tex->ll.y );
00181            glVertex3f( m_position.x, m_position.y, 0 );
00182 
00183            glTexCoord2f( tex->ur.x, tex->ll.y );
00184            glVertex3f( m_position.x + m_size.x, m_position.y, 0 );
00185 
00186            glTexCoord2f( tex->ur.x, tex->ur.y );
00187            glVertex3f( m_position.x + m_size.x, m_position.y + m_size.y, 0 );
00188 
00189            glTexCoord2f( tex->ll.x, tex->ur.y );
00190            glVertex3f( m_position.x, m_position.y + m_size.y, 0 );
00191        }
00192        glEnd();
00193     }
00194 
00195        if(mp_font!=NULL){
00196        
00197               float width = font->advance(mp_label);
00198               float asc = font->ascender();
00199               float desc = font->descender();
00200        
00201               font->draw(mp_label,
00202                             m_position.x + m_size.x/2.0 - width/2.0,
00203                             m_position.y + m_size.y/2.0 - asc/2.0 - desc/2.0);
00204        }    
00205 }
00206 
00207 } //namespace pp