Back to index

extremetuxracer  0.5beta
joystickconfig.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019  
00020 #include "joystickconfig.h"
00021 
00022 #include "joystick.h"
00023 #include "ppgltk/ui_mgr.h"
00024 
00025 #define MAX_JOY_BUTTON 8
00026 
00027 JoystickConfig::JoystickConfig()
00028 {      
00029        setTitle(_("Joystick Configuration"));
00030        pp::Vec2d pos(0,0);
00031        
00032        mp_enableJoystickLbl = new pp::Label(pos, "button_label",
00033                                                                        _("Enable Joystick"));
00034        
00035        mp_enableJoystickBox = new pp::CheckBox(pos, pp::Vec2d(32, 32) );
00036        mp_enableJoystickBox->setState( !getparam_disable_joystick());
00037     mp_enableJoystickBox->signalClicked.Connect(pp::CreateSlot(this,&JoystickConfig::checkboxClicked));
00038        
00039        
00040        //paddle button
00041        mp_paddleLbl = new pp::Label(pos,"button_label",_("Paddle:"));
00042        mp_paddleLbl->setInsensitiveFont("button_label_disabled");
00043        
00044        createButtonList(m_paddleList);
00045        mp_paddleListbox = new pp::Listbox<button_t>(pos,
00046                                pp::Vec2d(96, 32),
00047                                "listbox_item",
00048                                m_paddleList);
00049        mp_paddleListbox->setInsensitiveFont("listbox_item_insensitive");
00050        mp_paddleListbox->signalClicked.Connect(pp::CreateSlot(this,&JoystickConfig::paddleClicked));
00051        setButton(getparam_joystick_paddle_button(), mp_paddleListbox);
00052               
00053        //brake button
00054        mp_brakeLbl = new pp::Label(pos,"button_label",_("Brake:"));
00055        mp_brakeLbl->setInsensitiveFont("button_label_disabled");
00056 
00057        createButtonList(m_brakeList);
00058        mp_brakeListbox = new pp::Listbox<button_t>(pos,
00059                                pp::Vec2d(96, 32),
00060                                "listbox_item",
00061                                m_brakeList);
00062        mp_brakeListbox->setInsensitiveFont("listbox_item_insensitive");
00063        mp_brakeListbox->signalClicked.Connect(pp::CreateSlot(this,&JoystickConfig::brakeClicked));
00064        setButton(getparam_joystick_brake_button(), mp_brakeListbox);
00065        
00066        //jump button
00067        mp_jumpLbl = new pp::Label(pos,"button_label",_("Jump:"));
00068        mp_jumpLbl->setInsensitiveFont("button_label_disabled");
00069 
00070        createButtonList(m_jumpList);
00071        mp_jumpListbox = new pp::Listbox<button_t>(pos,
00072                                pp::Vec2d(96, 32),
00073                                "listbox_item",
00074                                m_jumpList);
00075        mp_jumpListbox->setInsensitiveFont("listbox_item_insensitive");
00076        mp_jumpListbox->signalClicked.Connect(pp::CreateSlot(this,&JoystickConfig::jumpClicked));
00077        setButton(getparam_joystick_jump_button(), mp_jumpListbox);
00078        
00079        //trick button
00080        mp_trickLbl = new pp::Label(pos,"button_label",_("Trick:"));
00081        mp_trickLbl->setInsensitiveFont("button_label_disabled");
00082        
00083        createButtonList(m_trickList);
00084        mp_trickListbox = new pp::Listbox<button_t>(pos,
00085                                pp::Vec2d(96, 32),
00086                                "listbox_item",
00087                                m_trickList);
00088        mp_trickListbox->setInsensitiveFont("listbox_item_insensitive");
00089        mp_trickListbox->signalClicked.Connect(pp::CreateSlot(this,&JoystickConfig::trickClicked));
00090        setButton(getparam_joystick_trick_button(), mp_trickListbox);
00091        
00092        updateWidgetsEnabledStates();
00093 }
00094 
00095 JoystickConfig::~JoystickConfig()
00096 {
00097        delete mp_enableJoystickLbl;
00098        delete mp_enableJoystickBox;
00099        
00100        delete mp_paddleLbl;
00101        delete mp_paddleListbox;
00102               
00103        delete mp_brakeLbl;
00104        delete mp_brakeListbox;
00105                      
00106        delete mp_jumpLbl;
00107        delete mp_jumpListbox;
00108               
00109        delete mp_trickLbl;
00110        delete mp_trickListbox;
00111 }
00112 
00113 void
00114 JoystickConfig::createButtonList(std::list<button_t> &list)
00115 {
00116        button_t button;
00117        char buff[8];
00118        
00119        for(int i=0; i<MAX_JOY_BUTTON; i++){
00120               sprintf(buff,"%d",i);
00121               button.name=buff;
00122               button.data=i;              
00123               list.push_back(button);                   
00124        }      
00125 }
00126 
00127 void
00128 JoystickConfig::setButton(int button, pp::Listbox<button_t> *listbox)
00129 {
00130        std::list<button_t>::iterator it;
00131        std::list<button_t> &list = listbox->getItemList();
00132        
00133        
00134        for(it=list.begin(); it!=list.end(); it++){
00135               if((*it).data==button){
00136                      listbox->setCurrentItem(it);
00137                      break;
00138               }             
00139        }      
00140 }
00141 
00142 void
00143 JoystickConfig::updateWidgetsEnabledStates()
00144 {
00145        bool state = mp_enableJoystickBox->getState();
00146        
00147        mp_paddleLbl->setSensitive(state);
00148        mp_paddleListbox->setSensitive(state);
00149        
00150        mp_brakeLbl->setSensitive(state);
00151        mp_brakeListbox->setSensitive(state);
00152        
00153        mp_jumpLbl->setSensitive(state);
00154        mp_jumpListbox->setSensitive(state);
00155        
00156        mp_trickLbl->setSensitive(state);
00157        mp_trickListbox->setSensitive(state);
00158 }
00159 
00160 void
00161 JoystickConfig::checkboxClicked()
00162 {
00163        updateWidgetsEnabledStates();
00164 }
00165 
00166 void
00167 JoystickConfig::customLoop(double TimeStep)
00168 {
00169        // Check joystick
00170     if ( is_joystick_active() ){
00171               update_joystick();
00172 
00173               int button = get_joystick_down_button();
00174               if(button>-1){
00175                      if(mp_paddleListbox->hasFocus()){
00176                             setButton(button, mp_paddleListbox);
00177                      }else if(mp_brakeListbox->hasFocus()){
00178                             setButton(button, mp_brakeListbox);
00179                      }else if(mp_jumpListbox->hasFocus()){
00180                             setButton(button, mp_jumpListbox);
00181                      }else if(mp_trickListbox->hasFocus()){
00182                             setButton(button, mp_trickListbox);
00183                      }
00184               }
00185     }
00186 }
00187 
00188 void
00189 JoystickConfig::setWidgetPositions()
00190 {
00191        int width = 500;
00192        int height = 240;
00193 
00194        pp::Vec2d pos(getparam_x_resolution()/2 - width/2,
00195                               getparam_y_resolution()/2 + height/2);
00196        
00197        mp_enableJoystickLbl->setPosition(pos);
00198        mp_enableJoystickBox->setPosition(pp::Vec2d(pos.x+width-32,pos.y));
00199        
00200        pos.y-=100;
00201        mp_paddleLbl->setPosition(pos);
00202        mp_paddleListbox->setPosition(pp::Vec2d(pos.x+width-96,pos.y));
00203               
00204        pos.y-=40;
00205        mp_brakeLbl->setPosition(pos);
00206        mp_brakeListbox->setPosition(pp::Vec2d(pos.x+width-96,pos.y));
00207        
00208        pos.y-=40;
00209        mp_jumpLbl->setPosition(pos);
00210        mp_jumpListbox->setPosition(pp::Vec2d(pos.x+width-96,pos.y));
00211        
00212        pos.y-=40;
00213        mp_trickLbl->setPosition(pos);
00214        mp_trickListbox->setPosition(pp::Vec2d(pos.x+width-96,pos.y));
00215 }
00216 
00217 
00218 void
00219 JoystickConfig::apply()
00220 {
00221        setparam_disable_joystick(!bool(mp_enableJoystickBox->getState() ));
00222               
00223        std::list<button_t>::iterator it;
00224        
00225        it = mp_paddleListbox->getCurrentItem();
00226        setparam_joystick_paddle_button((*it).data);
00227        
00228        it = mp_brakeListbox->getCurrentItem();
00229        setparam_joystick_brake_button((*it).data);
00230        
00231        it = mp_jumpListbox->getCurrentItem();
00232        setparam_joystick_jump_button((*it).data);
00233        
00234        it = mp_trickListbox->getCurrentItem();
00235        setparam_joystick_trick_button((*it).data);      
00236        
00237        write_config_file();
00238        set_game_mode( GameMode::prevmode );
00239     UIMgr.setDirty();       
00240 }
00241 
00242 void
00243 JoystickConfig::paddleClicked()
00244 {
00245        mp_paddleListbox->setFocus();
00246 }
00247 
00248 void
00249 JoystickConfig::brakeClicked()
00250 {
00251        mp_brakeListbox->setFocus();
00252 }
00253 
00254 void
00255 JoystickConfig::jumpClicked()
00256 {
00257        mp_jumpListbox->setFocus();
00258 }
00259 
00260 void
00261 JoystickConfig::trickClicked()
00262 {
00263        mp_trickListbox->setFocus();
00264 }