Back to index

extremetuxracer  0.5beta
ui_mgr.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 "ui_mgr.h"
00023 #include "textures.h"
00024 #include "ui_snow.h"
00025 #include "loop.h"
00026 #include "game_config.h"
00027 
00028 pp::UIManager UIMgr;
00029 
00030 namespace pp {
00031        
00032 UIManager::UIManager() 
00033        : cursorPos(0.0,0.0)
00034 {
00035        leftMouseButtonDown = false;
00036        middleMouseButtonDown = false;
00037        rightMouseButtonDown = false;
00038        dirty = true;
00039        focusWidget = NULL;
00040 }
00041 
00042 UIManager::~UIManager()
00043 {
00044 }
00045 
00046 void
00047 UIManager::setDirty()
00048 {
00049     dirty = true;
00050 }
00051        
00052 void
00053 UIManager::setupDisplay()
00054 {
00055     glMatrixMode( GL_PROJECTION );
00056     glLoadIdentity();
00057     glOrtho( 0, getparam_x_resolution(), 
00058               0, getparam_y_resolution(), -1.0, 1.0 );
00059     glMatrixMode( GL_MODELVIEW );
00060     glLoadIdentity();
00061     glTranslatef( 0.0, 0.0, -1.0 );
00062 
00063     glColor4f( 1.0, 1.0, 1.0, 1.0 );
00064 }
00065        
00066 void
00067 UIManager::draw()
00068 {
00069        setupDisplay();
00070        std::list<pp::Widget*>::iterator it;
00071        for(it=widgets.begin();it!=widgets.end();it++){
00072               (*it)->draw();              
00073        }
00074        drawCursor(); 
00075 }
00076 
00077 void
00078 UIManager::drawCursor()
00079 {
00080     GLuint texobj;
00081     char *binding;
00082 
00083     binding = "mouse_cursor";
00084     if ( !get_texture_binding( binding, &texobj ) ) {
00085        texobj = 0;
00086     }
00087 
00088     setupDisplay();
00089 
00090     glBindTexture( GL_TEXTURE_2D, texobj );
00091 
00092     glBegin( GL_QUADS );
00093     {
00094        glTexCoord2f( 0, 1 );
00095        glVertex2f( cursorPos.x, 
00096                   cursorPos.y );
00097 
00098        glTexCoord2f( 0, 0 );
00099        glVertex2f( cursorPos.x, 
00100                   cursorPos.y - 32 );
00101 
00102        glTexCoord2f( 1, 0 );
00103        glVertex2f( cursorPos.x + 32, 
00104                   cursorPos.y - 32 );
00105 
00106        glTexCoord2f( 1, 1 );
00107        glVertex2f( cursorPos.x + 32, 
00108                   cursorPos.y );
00109 
00110     }
00111     glEnd();
00112 }
00113 
00114 void 
00115 UIManager::mouseEvent( int button, int state, int x, int y )
00116 {
00117     if ( is_mode_change_pending() ) {
00118               // Don't process events until mode change occurs 
00119               return;
00120     }
00121 
00122     // Reverse y coordinate 
00123     y = getparam_y_resolution() - y;
00124        
00125        
00126        std::list<pp::Widget*>::iterator it;
00127        
00128        if ( state == SDL_PRESSED ) {
00129               for(it=widgets.begin();it!=widgets.end();it++){
00130                      (*it)->mouseDown(button,x,y);
00131               }
00132        } else {
00133               for(it=widgets.begin();it!=widgets.end();it++){
00134                      (*it)->mouseUp(button,x,y);
00135               }
00136        }
00137 
00138     if ( button == SDL_BUTTON_LEFT ) {
00139               leftMouseButtonDown = (bool) ( state == SDL_PRESSED );
00140     }
00141     if ( button == SDL_BUTTON_MIDDLE ) {
00142               middleMouseButtonDown = (bool) ( state == SDL_PRESSED );
00143     }
00144     if ( button == SDL_BUTTON_RIGHT ) {
00145               rightMouseButtonDown = (bool) ( state == SDL_PRESSED );
00146     }
00147 
00148     checkDirty();
00149 }
00150 
00151 void
00152 UIManager::checkDirty()
00153 {
00154     if( dirty){
00155               winsys_post_redisplay();
00156               dirty = false;
00157     }
00158 }
00159 
00160 pp::Vec2d
00161 UIManager::getMousePosition()
00162 {
00163     return cursorPos;
00164 }
00165 
00166 void
00167 UIManager::motionEvent( int x, int y )
00168 {
00169     if ( is_mode_change_pending() ) {
00170               // Don't process events until mode change occurs 
00171               return;
00172     }
00173 
00174     // Reverse y coordinate
00175     y = getparam_y_resolution() - y;
00176        
00177        std::list<pp::Widget*>::iterator it;
00178        
00179        for(it=widgets.begin();it!=widgets.end();it++){
00180               (*it)->mouseMotion(x,y);
00181        }
00182        
00183     pp::Vec2d oldPos = cursorPos;
00184     cursorPos = pp::Vec2d(x,y);
00185 
00186     if ( oldPos.x != x || oldPos.y != y ) {
00187               // Update UI snow 
00188               if ( getparam_ui_snow() ) {
00189                      if ( rightMouseButtonDown ) {
00190                             make_ui_snow( cursorPos );
00191                             reset_ui_snow_cursor_pos( cursorPos );
00192                      } else if ( middleMouseButtonDown ) {
00193                             make_ui_snow( cursorPos );
00194                             push_ui_snow( cursorPos );
00195                      } else {
00196                             push_ui_snow( cursorPos );
00197                      }
00198               }
00199 
00200               // Need to redraw cursor
00201               setDirty();
00202               checkDirty();
00203     }
00204 }
00205 
00206 void
00207 UIManager::add(pp::Widget *widget)
00208 {
00209        widgets.push_back(widget);  
00210 }
00211 
00212 void
00213 UIManager::remove(pp::Widget *widget)
00214 {
00215        if(focusWidget==widget){
00216               focusWidget=NULL;
00217        }      
00218        widgets.remove(widget);
00219 }
00220 
00221 bool
00222 UIManager::keyboardEvent(SDLKey key, SDLMod mod, bool release)
00223 {
00224        if(focusWidget!=NULL){
00225               return focusWidget->keyboardEvent(key, mod, release);   
00226        }
00227        return false;
00228 }
00229 
00230 void
00231 UIManager::grabFocus(pp::Widget* widget, bool focus)
00232 {
00233        if(focus){
00234               if(focusWidget!=NULL){
00235                      focusWidget->removeFocus();
00236               }
00237               focusWidget=widget;
00238        }else{
00239               if(focusWidget==widget){
00240                      focusWidget=NULL;
00241               }
00242        }
00243 }
00244 
00245 
00246 } //namepsace pp