Back to index

extremetuxracer  0.5beta
keyboardconfig.cpp
Go to the documentation of this file.
00001 /* 
00002  * ETRacer
00003  * Copyright (C) 2007-2008 The ETRacer Team <www.extremetuxracer.com>
00004  *
00005  * PPRacer 
00006  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00007  * 
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  * 
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  */
00022  
00023 #include "keyboardconfig.h"
00024 
00025 #include "game_config.h"
00026 #include "game_mgr.h"
00027 #include "ppgltk/ui_mgr.h"
00028 
00029 // Definition of Key
00030 
00031 KeyEntry::KeyEntry(pp::Vec2d pos, pp::Vec2d size, const char *binding, const char *content) : pp::Entry(pos, size, binding, content)
00032 {
00033 keysym = SDLK_BACKSPACE; //Well, just to reserve the memory
00034 }
00035 
00036 KeyEntry::~KeyEntry()
00037 {
00038 }
00039 
00040 void KeyEntry::setKeySym(SDLKey key)
00041 {
00042 keysym = key;
00043 }
00044 
00045 SDLKey KeyEntry::getKeySym()
00046 {
00047 return keysym;
00048 }
00049 
00050 
00051 // Definition of KeyboardConfig
00052 
00053 KeyboardConfig::KeyboardConfig()
00054 {
00055        setTitle(_("Keyboard Configuration"));
00056        
00057        pp::Vec2d pos(0,0);
00058        pp::Vec2d size(150,32);
00059        
00060        mp_left = new KeyEntry(pos, size, "listbox_item");
00061        mp_left->setEditable(false);
00062        mp_left->signalKeyPressed.Connect(pp::CreateSlot(this, &KeyboardConfig::setKey));
00063        setKey( mp_left, SDLKey(getparam_turn_left_key()) );
00064        
00065        mp_right = new KeyEntry(pos, size,
00066                                  "listbox_item");
00067        mp_right->setEditable(false);
00068        mp_right->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
00069        setKey( mp_right, SDLKey(getparam_turn_right_key()) );
00070        
00071        mp_paddle = new KeyEntry(pos, size,
00072                                  "listbox_item");
00073        mp_paddle->setEditable(false);
00074        mp_paddle->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
00075        setKey( mp_paddle, SDLKey(getparam_paddle_key()) );
00076               
00077        mp_brake = new KeyEntry(pos, size,
00078                                  "listbox_item");
00079        mp_brake->setEditable(false);
00080        mp_brake->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
00081        setKey( mp_brake, SDLKey(getparam_brake_key()) );
00082        
00083        mp_jump = new KeyEntry(pos, size,
00084                                  "listbox_item");
00085        mp_jump->setEditable(false);
00086        mp_jump->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
00087        setKey( mp_jump, SDLKey(getparam_jump_key()) );
00088        
00089        mp_trick = new KeyEntry(pos, size,
00090                                  "listbox_item");
00091        mp_trick->setEditable(false);
00092        mp_trick->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
00093        setKey( mp_trick, SDLKey(getparam_trick_modifier_key()) );
00094        
00095        mp_reset = new KeyEntry(pos, size,
00096                                  "listbox_item");
00097        mp_reset->setEditable(false);
00098        mp_reset->signalKeyPressed.Connect(pp::CreateSlot(this,&KeyboardConfig::setKey));
00099        setKey( mp_reset, SDLKey(getparam_reset_key()) );
00100 }
00101 
00102 KeyboardConfig::~KeyboardConfig()
00103 {
00104        delete mp_left;
00105        delete mp_right;
00106        delete mp_paddle;
00107        delete mp_brake;
00108        delete mp_jump;
00109        delete mp_trick;
00110        delete mp_reset;
00111 }
00112 
00113 void
00114 KeyboardConfig::setWidgetPositions()
00115 {
00116        int width = 500;
00117        int height = 240;
00118               
00119        pp::Vec2d pos(getparam_x_resolution()/2 - width/2,
00120                               getparam_y_resolution()/2 + height/2);
00121        
00122        pp::Font *font = pp::Font::get("button_label");
00123        
00124        font->draw(_("Turn left:"),pos);
00125        mp_left->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00126               
00127        pos.y-=40;
00128        font->draw(_("Turn right:"),pos);
00129        mp_right->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00130        
00131        pos.y-=40;
00132        font->draw(_("Paddle:"),pos);
00133        mp_paddle->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00134        
00135        pos.y-=40;
00136        font->draw(_("Brake:"),pos);
00137        mp_brake->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00138        
00139        pos.y-=40;
00140        font->draw(_("Jump:"),pos);
00141        mp_jump->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00142        
00143        pos.y-=40;
00144        font->draw(_("Trick:"),pos);
00145        mp_trick->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00146 
00147        pos.y-=40;
00148        font->draw(_("Reset:"),pos);
00149        mp_reset->setPosition(pp::Vec2d(pos.x+width-150,pos.y));
00150 }
00151 
00152 void
00153 KeyboardConfig::apply()
00154 {
00155        setparam_turn_left_key( mp_left->getKeySym() );
00156        setparam_turn_right_key( mp_right->getKeySym() );              
00157        setparam_paddle_key( mp_paddle->getKeySym() );          
00158        setparam_brake_key( mp_brake->getKeySym() );            
00159        setparam_jump_key( mp_jump->getKeySym() );              
00160        setparam_trick_modifier_key( mp_trick->getKeySym() );          
00161        setparam_reset_key( mp_reset->getKeySym() );            
00162        
00163        write_config_file();
00164        set_game_mode( GameMode::prevmode );
00165     UIMgr.setDirty();
00166 }
00167 
00168 void
00169 KeyboardConfig::setKey(KeyEntry* widget, SDLKey key)
00170 {
00171        std::string content="";
00172        
00173        if(key==SDLK_BACKSPACE){
00174               widget->setContent(content);
00175               return;
00176        }else{
00177               content = SDL_GetKeyName(key);
00178        }
00179        
00180        if (content!=""){
00181               widget->setContent(content);
00182               widget->setKeySym(key);
00183        }
00184 }
00185