JoystickKeyboardController Class Reference

#include <joystickkeyboardcontroller.hpp>

List of all members.

Public Member Functions

 JoystickKeyboardController ()
virtual ~JoystickKeyboardController ()
void process_event (const SDL_Event &event)
 Process an SDL Event and return true if the event has been used.
void write (Writer &writer)
void read (const Reader &lisp)
void update ()
void reset ()
void updateAvailableJoysticks ()
Controllerget_main_controller ()

Private Types

typedef Controller::Control Control
typedef Uint8 JoyId
typedef std::map< SDLKey,
Control
KeyMap
typedef std::map< std::pair<
JoyId, int >, Control
ButtonMap
typedef std::map< std::pair<
JoyId, int >, Control
AxisMap
typedef std::map< std::pair<
JoyId, int >, Control
HatMap

Private Member Functions

void process_key_event (const SDL_KeyboardEvent &event)
void process_hat_event (const SDL_JoyHatEvent &jhat)
void process_axis_event (const SDL_JoyAxisEvent &jaxis)
void process_button_event (const SDL_JoyButtonEvent &jbutton)
void process_console_key_event (const SDL_KeyboardEvent &event)
void process_menu_key_event (const SDL_KeyboardEvent &event)
void print_joystick_mappings ()
SDLKey reversemap_key (Control c)
int reversemap_joybutton (Control c)
int reversemap_joyaxis (Control c)
int reversemap_joyhat (Control c)
void unbind_joystick_control (Control c)
void bind_joybutton (JoyId joy_id, int button, Control c)
void bind_joyaxis (JoyId joy_id, int axis, Control c)
void bind_joyhat (JoyId joy_id, int dir, Control c)
void bind_key (SDLKey key, Control c)
void set_joy_controls (Control id, bool value)
 JoystickKeyboardController (const JoystickKeyboardController &)
JoystickKeyboardControlleroperator= (const JoystickKeyboardController &)

Private Attributes

Controllercontroller
KeyMap keymap
ButtonMap joy_button_map
AxisMap joy_axis_map
HatMap joy_hat_map
std::vector< SDL_Joystick * > joysticks
std::string name
int dead_zone
int min_joybuttons
 the number of buttons all joysticks have
int max_joybuttons
 the max number of buttons a joystick has
int max_joyaxis
int max_joyhats
Uint8 hat_state
bool jump_with_up_joy
bool jump_with_up_kbd
int wait_for_key
int wait_for_joystick

Friends

class KeyboardMenu
class JoystickMenu


Detailed Description

Definition at line 36 of file joystickkeyboardcontroller.hpp.


Member Typedef Documentation

typedef Controller::Control JoystickKeyboardController::Control [private]

Definition at line 42 of file joystickkeyboardcontroller.hpp.

typedef Uint8 JoystickKeyboardController::JoyId [private]

Definition at line 43 of file joystickkeyboardcontroller.hpp.

typedef std::map<SDLKey, Control> JoystickKeyboardController::KeyMap [private]

Definition at line 45 of file joystickkeyboardcontroller.hpp.

typedef std::map<std::pair<JoyId, int>, Control> JoystickKeyboardController::ButtonMap [private]

Definition at line 46 of file joystickkeyboardcontroller.hpp.

typedef std::map<std::pair<JoyId, int>, Control> JoystickKeyboardController::AxisMap [private]

Definition at line 47 of file joystickkeyboardcontroller.hpp.

typedef std::map<std::pair<JoyId, int>, Control> JoystickKeyboardController::HatMap [private]

Definition at line 48 of file joystickkeyboardcontroller.hpp.


Constructor & Destructor Documentation

JoystickKeyboardController::JoystickKeyboardController (  ) 

Definition at line 32 of file joystickkeyboardcontroller.cpp.

References Controller::ACTION, bind_joyaxis(), bind_joybutton(), Controller::CONSOLE, controller, dead_zone, Controller::DOWN, Controller::JUMP, jump_with_up_joy, jump_with_up_kbd, keymap, Controller::LEFT, max_joybuttons, Controller::MENU_SELECT, min_joybuttons, Controller::PAUSE_MENU, Controller::PEEK_DOWN, Controller::PEEK_LEFT, Controller::PEEK_RIGHT, Controller::PEEK_UP, Controller::RIGHT, Controller::UP, and updateAvailableJoysticks().

00032                                                        :
00033   controller(),
00034   keymap(),
00035   joy_button_map(),
00036   joy_axis_map(),
00037   joy_hat_map(),
00038   joysticks(),
00039   name(),
00040   dead_zone(),
00041   min_joybuttons(),
00042   max_joybuttons(),
00043   max_joyaxis(),
00044   max_joyhats(),
00045   hat_state(0),
00046   jump_with_up_joy(),
00047   jump_with_up_kbd(),
00048   wait_for_key(-1), 
00049   wait_for_joystick(-1)
00050 {
00051   controller = new Controller;
00052 
00053   // initialize default keyboard map
00054   keymap[SDLK_LEFT]     = Controller::LEFT;
00055   keymap[SDLK_RIGHT]    = Controller::RIGHT;
00056   keymap[SDLK_UP]       = Controller::UP;
00057   keymap[SDLK_DOWN]     = Controller::DOWN;
00058   keymap[SDLK_SPACE]    = Controller::JUMP;
00059   keymap[SDLK_LCTRL]    = Controller::ACTION;
00060   keymap[SDLK_LALT]     = Controller::ACTION;
00061   keymap[SDLK_ESCAPE]   = Controller::PAUSE_MENU;
00062   keymap[SDLK_p]        = Controller::PAUSE_MENU;
00063   keymap[SDLK_PAUSE]    = Controller::PAUSE_MENU;
00064   keymap[SDLK_RETURN]   = Controller::MENU_SELECT;
00065   keymap[SDLK_KP_ENTER] = Controller::MENU_SELECT;
00066   keymap[SDLK_CARET]    = Controller::CONSOLE;
00067   keymap[SDLK_DELETE]   = Controller::PEEK_LEFT;
00068   keymap[SDLK_PAGEDOWN] = Controller::PEEK_RIGHT;
00069   keymap[SDLK_HOME]     = Controller::PEEK_UP;
00070   keymap[SDLK_END]      = Controller::PEEK_DOWN;
00071 
00072   jump_with_up_joy = false;
00073   jump_with_up_kbd = false;
00074 
00075   updateAvailableJoysticks();
00076 
00077   dead_zone = 1000;
00078 
00079   // Default joystick button configuration
00080   bind_joybutton(0, 0, Controller::JUMP);
00081   bind_joybutton(0, 1, Controller::ACTION);
00082   // 6 or more Buttons
00083   if( min_joybuttons > 5 ){
00084     bind_joybutton(0, 4, Controller::PEEK_LEFT);
00085     bind_joybutton(0, 5, Controller::PEEK_RIGHT);
00086     // 8 or more
00087     if(min_joybuttons > 7)
00088       bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU);
00089   } else {
00090     // map the last 2 buttons to menu and pause
00091     if(min_joybuttons > 2)
00092       bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU);
00093     // map all remaining joystick buttons to MENU_SELECT
00094     for(int i = 2; i < max_joybuttons; ++i) {
00095       if(i != min_joybuttons-1)
00096         bind_joybutton(0, i, Controller::MENU_SELECT);
00097     }
00098   }
00099 
00100   // Default joystick axis configuration
00101   bind_joyaxis(0, -1, Controller::LEFT);
00102   bind_joyaxis(0, 1, Controller::RIGHT);
00103   bind_joyaxis(0, -2, Controller::UP);
00104   bind_joyaxis(0, 2, Controller::DOWN);
00105 }

JoystickKeyboardController::~JoystickKeyboardController (  )  [virtual]

Definition at line 107 of file joystickkeyboardcontroller.cpp.

References controller, and joysticks.

00108 {
00109   for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
00110       i != joysticks.end(); ++i) {
00111     if(*i != 0)
00112       SDL_JoystickClose(*i);
00113   }
00114   delete controller;
00115 }

JoystickKeyboardController::JoystickKeyboardController ( const JoystickKeyboardController  )  [private]


Member Function Documentation

void JoystickKeyboardController::process_event ( const SDL_Event &  event  ) 

Process an SDL Event and return true if the event has been used.

Definition at line 338 of file joystickkeyboardcontroller.cpp.

References process_axis_event(), process_button_event(), process_hat_event(), and process_key_event().

Referenced by ScreenManager::process_events().

00339 {
00340   switch(event.type) {
00341     case SDL_KEYUP:
00342     case SDL_KEYDOWN:
00343       process_key_event(event.key);
00344       break;
00345 
00346     case SDL_JOYAXISMOTION:
00347       process_axis_event(event.jaxis);
00348       break;
00349 
00350     case SDL_JOYHATMOTION:
00351       process_hat_event(event.jhat);
00352       break;
00353 
00354     case SDL_JOYBUTTONDOWN:
00355     case SDL_JOYBUTTONUP:
00356       process_button_event(event.jbutton);
00357       break;
00358 
00359     default:
00360       break;
00361   }
00362 }

void JoystickKeyboardController::write ( Writer writer  ) 

Definition at line 275 of file joystickkeyboardcontroller.cpp.

References Controller::controlNames, dead_zone, lisp::Writer::end_list(), joy_axis_map, joy_button_map, joy_hat_map, jump_with_up_joy, jump_with_up_kbd, keymap, lisp::Writer::start_list(), and lisp::Writer::write().

Referenced by Config::save().

00276 {
00277   writer.start_list("keymap");
00278   writer.write("jump-with-up", jump_with_up_kbd);
00279   for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
00280     writer.start_list("map");
00281     writer.write("key", (int) i->first);
00282     writer.write("control", Controller::controlNames[i->second]);
00283     writer.end_list("map");
00284   }
00285   writer.end_list("keymap");
00286 
00287   writer.start_list("joystick");
00288   writer.write("dead-zone", dead_zone);
00289   writer.write("jump-with-up", jump_with_up_joy);
00290 
00291   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end();
00292       ++i) {
00293     writer.start_list("map");
00294     writer.write("button", i->first.second);
00295     writer.write("control", Controller::controlNames[i->second]);
00296     writer.end_list("map");
00297   }
00298 
00299   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
00300     writer.start_list("map");
00301     writer.write("hat", i->first.second);
00302     writer.write("control", Controller::controlNames[i->second]);
00303     writer.end_list("map");
00304   }
00305 
00306   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
00307     writer.start_list("map");
00308     writer.write("axis", i->first.second);
00309     writer.write("control", Controller::controlNames[i->second]);
00310     writer.end_list("map");
00311   }
00312 
00313   writer.end_list("joystick");
00314 }

void JoystickKeyboardController::read ( const Reader lisp  ) 

Definition at line 185 of file joystickkeyboardcontroller.cpp.

References bind_joyaxis(), bind_joybutton(), bind_joyhat(), Controller::controlNames, dead_zone, lisp::Lisp::get(), lisp::Lisp::get_lisp(), lisp::ListIterator::item(), jump_with_up_joy, jump_with_up_kbd, keymap, lisp::ListIterator::lisp(), log_info, max_joyaxis, max_joybuttons, and lisp::ListIterator::next().

Referenced by Config::load().

00186 {
00187   const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
00188   if(keymap_lisp) {
00189     keymap.clear();
00190     keymap_lisp->get("jump-with-up", jump_with_up_kbd);
00191     lisp::ListIterator iter(keymap_lisp);
00192     while(iter.next()) {
00193       if(iter.item() == "map") {
00194         int key = -1;
00195         std::string control;
00196         const lisp::Lisp* map = iter.lisp();
00197         map->get("key", key);
00198         map->get("control", control);
00199         if(key < SDLK_FIRST || key >= SDLK_LAST) {
00200           log_info << "Invalid key '" << key << "' in keymap" << std::endl;
00201           continue;
00202         }
00203 
00204         int i = 0;
00205         for(i = 0; Controller::controlNames[i] != 0; ++i) {
00206           if(control == Controller::controlNames[i])
00207             break;
00208         }
00209         if(Controller::controlNames[i] == 0) {
00210           log_info << "Invalid control '" << control << "' in keymap" << std::endl;
00211           continue;
00212         }
00213         keymap[SDLKey(key)] = Control(i);
00214       }
00215     }
00216   }
00217 
00218   const lisp::Lisp* joystick_lisp = lisp.get_lisp("joystick");
00219   if(joystick_lisp) {
00220     joystick_lisp->get("dead-zone", dead_zone);
00221     joystick_lisp->get("jump-with-up", jump_with_up_joy);
00222     lisp::ListIterator iter(joystick_lisp);
00223     while(iter.next()) {
00224       if(iter.item() == "map") {
00225         int button = -1;
00226         int axis   = 0;
00227         int hat    = -1;
00228         std::string control;
00229         const lisp::Lisp* map = iter.lisp();
00230 
00231         map->get("control", control);
00232         int i = 0;
00233         for(i = 0; Controller::controlNames[i] != 0; ++i) {
00234           if(control == Controller::controlNames[i])
00235             break;
00236         }
00237         if(Controller::controlNames[i] == 0) {
00238           log_info << "Invalid control '" << control << "' in buttonmap" << std::endl;
00239           continue;
00240         }
00241 
00242         if (map->get("button", button)) {
00243           if(button < 0 || button >= max_joybuttons) {
00244             log_info << "Invalid button '" << button << "' in buttonmap" << std::endl;
00245             continue;
00246           }
00247           bind_joybutton(0, button, Control(i));
00248         }
00249 
00250         if (map->get("axis",   axis)) {
00251           if (axis == 0 || abs(axis) > max_joyaxis) {
00252             log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
00253             continue;
00254           }
00255           bind_joyaxis(0, axis, Control(i));
00256         }
00257 
00258         if (map->get("hat",   hat)) {
00259           if (hat != SDL_HAT_UP   &&
00260               hat != SDL_HAT_DOWN &&
00261               hat != SDL_HAT_LEFT &&
00262               hat != SDL_HAT_RIGHT) {
00263             log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
00264             continue;
00265           } else {
00266             bind_joyhat(0, hat, Control(i));
00267           }
00268         }
00269       }
00270     }
00271   }
00272 }

void JoystickKeyboardController::update (  ) 

Definition at line 317 of file joystickkeyboardcontroller.cpp.

References controller, and Controller::update().

Referenced by ScreenManager::process_events().

00318 {
00319   controller->update();
00320 }

void JoystickKeyboardController::reset (  ) 

Definition at line 323 of file joystickkeyboardcontroller.cpp.

References controller, and Controller::reset().

Referenced by process_button_event(), process_menu_key_event(), GameSession::restart_level(), and MenuManager::set_current().

00324 {
00325   controller->reset();
00326 }

void JoystickKeyboardController::updateAvailableJoysticks (  ) 

Definition at line 118 of file joystickkeyboardcontroller.cpp.

References joysticks, log_info, max_joyaxis, max_joybuttons, max_joyhats, and min_joybuttons.

Referenced by JoystickKeyboardController(), and JoystickMenu::menu_action().

00119 {
00120   for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
00121       i != joysticks.end(); ++i) {
00122     if(*i != 0)
00123       SDL_JoystickClose(*i);
00124   }
00125   joysticks.clear();
00126   
00127   SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
00128   SDL_InitSubSystem(SDL_INIT_JOYSTICK);
00129 
00130   int joystick_count = SDL_NumJoysticks();
00131   min_joybuttons = -1;
00132   max_joybuttons = -1;
00133   max_joyaxis    = -1;
00134   max_joyhats    = -1;
00135 
00136   if( joystick_count > 0 ){
00137     for(int i = 0; i < joystick_count; ++i) {
00138       SDL_Joystick* joystick = SDL_JoystickOpen(i);
00139       bool good = true;
00140       if(SDL_JoystickNumButtons(joystick) < 2) {
00141         log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 buttons" << std::endl;
00142         good = false;
00143       }
00144       if(SDL_JoystickNumAxes(joystick) < 2
00145          && SDL_JoystickNumHats(joystick) == 0) {
00146         log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 axes and no hat" << std::endl;
00147         good = false;
00148       }
00149       if(!good) {
00150         SDL_JoystickClose(joystick);
00151         continue;
00152       }
00153 
00154       if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
00155         min_joybuttons = SDL_JoystickNumButtons(joystick);
00156 
00157       if(SDL_JoystickNumButtons(joystick) > max_joybuttons)
00158         max_joybuttons = SDL_JoystickNumButtons(joystick);
00159 
00160       if(SDL_JoystickNumAxes(joystick) > max_joyaxis)
00161         max_joyaxis = SDL_JoystickNumAxes(joystick);
00162 
00163       if(SDL_JoystickNumHats(joystick) > max_joyhats)
00164         max_joyhats = SDL_JoystickNumHats(joystick);
00165 
00166       joysticks.push_back(joystick);
00167     }
00168   }
00169 
00170   // some joysticks or SDL seem to produce some bogus events after being opened
00171   Uint32 ticks = SDL_GetTicks();
00172   while(SDL_GetTicks() - ticks < 200) {
00173     SDL_Event event;
00174     SDL_PollEvent(&event);
00175   }
00176 }

Controller * JoystickKeyboardController::get_main_controller (  ) 

Definition at line 179 of file joystickkeyboardcontroller.cpp.

References controller.

Referenced by Player::Player(), GameSession::process_events(), worldmap::WorldMap::update(), TextScroller::update(), LevelIntro::update(), GameSession::update(), Menu::update(), and worldmap::Tux::updateInputDirection().

00180 {
00181   return controller;
00182 }

void JoystickKeyboardController::process_key_event ( const SDL_KeyboardEvent &  event  )  [private]

Definition at line 493 of file joystickkeyboardcontroller.cpp.

References Controller::CONSOLE, controller, MenuManager::current(), Console::instance, Controller::JUMP, jump_with_up_kbd, keymap, process_console_key_event(), process_menu_key_event(), Controller::set_control(), Console::toggle(), and Controller::UP.

Referenced by process_event().

00494 {
00495   KeyMap::iterator key_mapping = keymap.find(event.keysym.sym);
00496 
00497   // if console key was pressed: toggle console
00498   if ((key_mapping != keymap.end()) && (key_mapping->second == Controller::CONSOLE)) {
00499     if (event.type == SDL_KEYDOWN) 
00500       Console::instance->toggle();
00501   } else {
00502     if (Console::instance->hasFocus()) {
00503       // if console is open: send key there
00504       process_console_key_event(event);
00505     } else if (MenuManager::current()) {
00506       // if menu mode: send key there
00507       process_menu_key_event(event);
00508     } else if(key_mapping == keymap.end()) {
00509       // default action: update controls
00510       //log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl;
00511     } else {
00512       Control control = key_mapping->second;
00513       bool value = (event.type == SDL_KEYDOWN);
00514       controller->set_control(control, value);
00515       if (jump_with_up_kbd && control == Controller::UP){
00516         controller->set_control(Controller::JUMP, value);
00517       }
00518     }
00519   }
00520 }

void JoystickKeyboardController::process_hat_event ( const SDL_JoyHatEvent &  jhat  )  [private]

Definition at line 437 of file joystickkeyboardcontroller.cpp.

References bind_joyhat(), MenuStorage::get_joystick_options_menu(), hat_state, joy_hat_map, set_joy_controls(), JoystickMenu::update(), and wait_for_joystick.

Referenced by process_event().

00438 {
00439   Uint8 changed = hat_state ^ jhat.value;
00440 
00441   if (wait_for_joystick >= 0)
00442   {
00443     if (changed & SDL_HAT_UP && jhat.value & SDL_HAT_UP)
00444       bind_joyhat(jhat.which, SDL_HAT_UP, Control(wait_for_joystick));
00445 
00446     if (changed & SDL_HAT_DOWN && jhat.value & SDL_HAT_DOWN)
00447       bind_joyhat(jhat.which, SDL_HAT_DOWN, Control(wait_for_joystick));
00448 
00449     if (changed & SDL_HAT_LEFT && jhat.value & SDL_HAT_LEFT)
00450       bind_joyhat(jhat.which, SDL_HAT_LEFT, Control(wait_for_joystick));
00451 
00452     if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT)
00453       bind_joyhat(jhat.which, SDL_HAT_RIGHT, Control(wait_for_joystick));
00454 
00455     MenuStorage::get_joystick_options_menu()->update();
00456     wait_for_joystick = -1;
00457   }
00458   else
00459   {
00460     if (changed & SDL_HAT_UP)
00461     {
00462       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_UP));
00463       if (it != joy_hat_map.end())
00464         set_joy_controls(it->second, jhat.value & SDL_HAT_UP);
00465     }
00466 
00467     if (changed & SDL_HAT_DOWN)
00468     {
00469       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_DOWN));
00470       if (it != joy_hat_map.end())
00471         set_joy_controls(it->second, jhat.value & SDL_HAT_DOWN);
00472     }
00473 
00474     if (changed & SDL_HAT_LEFT)
00475     {
00476       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_LEFT));
00477       if (it != joy_hat_map.end())
00478         set_joy_controls(it->second, jhat.value & SDL_HAT_LEFT);
00479     }
00480 
00481     if (changed & SDL_HAT_RIGHT)
00482     {
00483       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_RIGHT));
00484       if (it != joy_hat_map.end())
00485         set_joy_controls(it->second, jhat.value & SDL_HAT_RIGHT);
00486     }
00487   }
00488 
00489   hat_state = jhat.value;
00490 }

void JoystickKeyboardController::process_axis_event ( const SDL_JoyAxisEvent &  jaxis  )  [private]

Definition at line 389 of file joystickkeyboardcontroller.cpp.

References bind_joyaxis(), dead_zone, MenuStorage::get_joystick_options_menu(), joy_axis_map, set_joy_controls(), JoystickMenu::update(), and wait_for_joystick.

Referenced by process_event().

00390 {
00391   if (wait_for_joystick >= 0)
00392   {
00393     if (abs(jaxis.value) > dead_zone) {
00394       if (jaxis.value < 0)
00395         bind_joyaxis(jaxis.which, -(jaxis.axis + 1), Control(wait_for_joystick));
00396       else
00397         bind_joyaxis(jaxis.which, jaxis.axis + 1, Control(wait_for_joystick));
00398 
00399       MenuStorage::get_joystick_options_menu()->update();
00400       wait_for_joystick = -1;
00401     }
00402   }
00403   else
00404   {
00405     // Split the axis into left and right, so that both can be
00406     // mapped separately (needed for jump/down vs up/down)
00407     int axis = jaxis.axis + 1;
00408 
00409     AxisMap::iterator left  = joy_axis_map.find(std::make_pair(jaxis.which, -axis));
00410     AxisMap::iterator right = joy_axis_map.find(std::make_pair(jaxis.which, axis));
00411 
00412     if(left == joy_axis_map.end()) {
00413       // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl;
00414     } else {
00415       if (jaxis.value < -dead_zone)
00416         set_joy_controls(left->second,  true);
00417       else if (jaxis.value > dead_zone)
00418         set_joy_controls(left->second, false);
00419       else
00420         set_joy_controls(left->second, false);
00421     }
00422 
00423     if(right == joy_axis_map.end()) {
00424       // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl;
00425     } else {
00426       if (jaxis.value < -dead_zone)
00427         set_joy_controls(right->second, false);
00428       else if (jaxis.value > dead_zone)
00429         set_joy_controls(right->second, true);
00430       else
00431         set_joy_controls(right->second, false);
00432     }
00433   }
00434 }

void JoystickKeyboardController::process_button_event ( const SDL_JoyButtonEvent &  jbutton  )  [private]

Definition at line 365 of file joystickkeyboardcontroller.cpp.

References bind_joybutton(), MenuStorage::get_joystick_options_menu(), joy_button_map, log_debug, reset(), set_joy_controls(), JoystickMenu::update(), and wait_for_joystick.

Referenced by process_event().

00366 {
00367   if(wait_for_joystick >= 0) 
00368   {
00369     if(jbutton.state == SDL_PRESSED)
00370     {
00371       bind_joybutton(jbutton.which, jbutton.button, (Control)wait_for_joystick);
00372       MenuStorage::get_joystick_options_menu()->update();
00373       reset();
00374       wait_for_joystick = -1;
00375     }
00376   } 
00377   else 
00378   {
00379     ButtonMap::iterator i = joy_button_map.find(std::make_pair(jbutton.which, jbutton.button));
00380     if(i == joy_button_map.end()) {
00381       log_debug << "Unmapped joybutton " << (int)jbutton.button << " pressed" << std::endl;
00382     } else {
00383       set_joy_controls(i->second, (jbutton.state == SDL_PRESSED));
00384     }
00385   }
00386 }

void JoystickKeyboardController::process_console_key_event ( const SDL_KeyboardEvent &  event  )  [private]

Definition at line 523 of file joystickkeyboardcontroller.cpp.

References Console::autocomplete(), Console::backspace(), Console::enter(), Console::input(), Console::instance, Console::move_cursor(), Console::scroll(), and Console::show_history().

Referenced by process_key_event().

00524 {
00525   if (event.type != SDL_KEYDOWN) return;
00526 
00527   switch (event.keysym.sym) {
00528     case SDLK_RETURN:
00529       Console::instance->enter();
00530       break;
00531     case SDLK_BACKSPACE:
00532       Console::instance->backspace();
00533       break;
00534     case SDLK_TAB:
00535       Console::instance->autocomplete();
00536       break;
00537     case SDLK_PAGEUP:
00538       Console::instance->scroll(-1);
00539       break;
00540     case SDLK_PAGEDOWN:
00541       Console::instance->scroll(+1);
00542       break;
00543     case SDLK_HOME:
00544       Console::instance->move_cursor(-65535);
00545       break;
00546     case SDLK_END:
00547       Console::instance->move_cursor(+65535);
00548       break;
00549     case SDLK_UP:
00550       Console::instance->show_history(-1);
00551       break;
00552     case SDLK_DOWN:
00553       Console::instance->show_history(+1);
00554       break;
00555     case SDLK_LEFT:
00556       Console::instance->move_cursor(-1);
00557       break;
00558     case SDLK_RIGHT:
00559       Console::instance->move_cursor(+1);
00560       break;
00561     default:
00562       int c = event.keysym.unicode;
00563       if ((c >= 32) && (c <= 126)) {
00564         Console::instance->input((char)c);
00565       }
00566       break;
00567   }
00568 }

void JoystickKeyboardController::process_menu_key_event ( const SDL_KeyboardEvent &  event  )  [private]

Definition at line 571 of file joystickkeyboardcontroller.cpp.

References bind_key(), controller, Controller::DOWN, MenuStorage::get_joystick_options_menu(), MenuStorage::get_key_options_menu(), Controller::LEFT, Controller::MENU_SELECT, Controller::PAUSE_MENU, reset(), Controller::RIGHT, Controller::set_control(), Controller::UP, JoystickMenu::update(), KeyboardMenu::update(), wait_for_joystick, and wait_for_key.

Referenced by process_key_event().

00572 {
00573   // wait for key mode?
00574   if(wait_for_key >= 0) {
00575     if(event.type == SDL_KEYUP)
00576       return;
00577 
00578     if(event.keysym.sym != SDLK_ESCAPE
00579        && event.keysym.sym != SDLK_PAUSE) {
00580       bind_key(event.keysym.sym, Control(wait_for_key));
00581     }
00582     reset();
00583     MenuStorage::get_key_options_menu()->update();
00584     wait_for_key = -1;
00585     return;
00586   }
00587   if(wait_for_joystick >= 0) {
00588     if(event.keysym.sym == SDLK_ESCAPE) {
00589       reset();
00590       MenuStorage::get_joystick_options_menu()->update();
00591       wait_for_joystick = -1;
00592     }
00593     return;
00594   }
00595 
00596   Control control;
00597   /* we use default keys when the menu is open (to avoid problems when
00598    * redefining keys to invalid settings
00599    */
00600   switch(event.keysym.sym) {
00601     case SDLK_UP:
00602       control = Controller::UP;
00603       break;
00604     case SDLK_DOWN:
00605       control = Controller::DOWN;
00606       break;
00607     case SDLK_LEFT:
00608       control = Controller::LEFT;
00609       break;
00610     case SDLK_RIGHT:
00611       control = Controller::RIGHT;
00612       break;
00613     case SDLK_SPACE:
00614     case SDLK_RETURN:
00615     case SDLK_KP_ENTER:
00616       control = Controller::MENU_SELECT;
00617       break;
00618     case SDLK_ESCAPE:
00619     case SDLK_PAUSE:
00620       control = Controller::PAUSE_MENU;
00621       break;
00622     default:
00623       return;
00624       break;
00625   }
00626 
00627   controller->set_control(control, (event.type == SDL_KEYDOWN));
00628 }

void JoystickKeyboardController::print_joystick_mappings (  )  [private]

Definition at line 712 of file joystickkeyboardcontroller.cpp.

References joy_axis_map, joy_button_map, and joy_hat_map.

00713 {
00714   std::cout << "Joystick Mappings" << std::endl;
00715   std::cout << "-----------------" << std::endl;
00716   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
00717     std::cout << "Axis: " << i->first.second << " -> " << i->second << std::endl;
00718   }
00719 
00720   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) {
00721     std::cout << "Button: " << i->first.second << " -> " << i->second << std::endl;
00722   }
00723 
00724   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
00725     std::cout << "Hat: " << i->first.second << " -> " << i->second << std::endl;
00726   }
00727   std::cout << std::endl;
00728 }

SDLKey JoystickKeyboardController::reversemap_key ( Control  c  )  [private]

Definition at line 731 of file joystickkeyboardcontroller.cpp.

References keymap.

Referenced by KeyboardMenu::update().

00732 {
00733   for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
00734     if(i->second == c)
00735       return i->first;
00736   }
00737 
00738   return SDLK_UNKNOWN;
00739 }

int JoystickKeyboardController::reversemap_joybutton ( Control  c  )  [private]

Definition at line 753 of file joystickkeyboardcontroller.cpp.

References joy_button_map.

Referenced by JoystickMenu::update_menu_item().

00754 {
00755   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) {
00756     if(i->second == c)
00757       return i->first.second;
00758   }
00759 
00760   return -1;
00761 }

int JoystickKeyboardController::reversemap_joyaxis ( Control  c  )  [private]

Definition at line 742 of file joystickkeyboardcontroller.cpp.

References joy_axis_map.

Referenced by JoystickMenu::update_menu_item().

00743 {
00744   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
00745     if(i->second == c)
00746       return i->first.second;
00747   }
00748 
00749   return 0;
00750 }

int JoystickKeyboardController::reversemap_joyhat ( Control  c  )  [private]

Definition at line 764 of file joystickkeyboardcontroller.cpp.

References joy_hat_map.

Referenced by JoystickMenu::update_menu_item().

00765 {
00766   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
00767     if(i->second == c)
00768       return i->first.second;
00769   }
00770 
00771   return -1;
00772 }

void JoystickKeyboardController::unbind_joystick_control ( Control  c  )  [private]

Definition at line 631 of file joystickkeyboardcontroller.cpp.

References joy_axis_map, joy_button_map, and joy_hat_map.

Referenced by bind_joyaxis(), bind_joybutton(), and bind_joyhat().

00632 {
00633   // remove all previous mappings for that control
00634   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); /* no ++i */) {
00635     if(i->second == control)
00636       joy_axis_map.erase(i++);
00637     else
00638       ++i;
00639   }
00640 
00641   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); /* no ++i */) {
00642     if(i->second == control)
00643       joy_button_map.erase(i++);
00644     else
00645       ++i;
00646   }
00647 
00648   for(HatMap::iterator i = joy_hat_map.begin();  i != joy_hat_map.end(); /* no ++i */) {
00649     if(i->second == control)
00650       joy_hat_map.erase(i++);
00651     else
00652       ++i;
00653   }
00654 }

void JoystickKeyboardController::bind_joybutton ( JoyId  joy_id,
int  button,
Control  c 
) [private]

Definition at line 680 of file joystickkeyboardcontroller.cpp.

References joy_button_map, and unbind_joystick_control().

Referenced by JoystickKeyboardController(), process_button_event(), and read().

00681 {
00682   unbind_joystick_control(control);
00683 
00684   // add new mapping
00685   joy_button_map[std::make_pair(joy_id, button)] = control;
00686 }

void JoystickKeyboardController::bind_joyaxis ( JoyId  joy_id,
int  axis,
Control  c 
) [private]

Definition at line 657 of file joystickkeyboardcontroller.cpp.

References joy_axis_map, and unbind_joystick_control().

Referenced by JoystickKeyboardController(), process_axis_event(), and read().

00658 {
00659   // axis isn't the SDL axis number, but axisnumber + 1 with sign
00660   // changed depending on if the positive or negative end is to be
00661   // used (negative axis 0 becomes -1, positive axis 2 becomes +3,
00662   // etc.)
00663 
00664   unbind_joystick_control(control);
00665 
00666   // add new mapping
00667   joy_axis_map[std::make_pair(joy_id, axis)] = control;
00668 }

void JoystickKeyboardController::bind_joyhat ( JoyId  joy_id,
int  dir,
Control  c 
) [private]

Definition at line 671 of file joystickkeyboardcontroller.cpp.

References joy_hat_map, and unbind_joystick_control().

Referenced by process_hat_event(), and read().

00672 {
00673   unbind_joystick_control(c);
00674 
00675   // add new mapping
00676   joy_hat_map[std::make_pair(joy_id, dir)] = c;
00677 }

void JoystickKeyboardController::bind_key ( SDLKey  key,
Control  c 
) [private]

Definition at line 689 of file joystickkeyboardcontroller.cpp.

References keymap.

Referenced by process_menu_key_event().

00690 {
00691   // remove all previous mappings for that control and for that key
00692   for(KeyMap::iterator i = keymap.begin();
00693       i != keymap.end(); /* no ++i */) {
00694     if(i->second == control) {
00695       KeyMap::iterator e = i;
00696       ++i;
00697       keymap.erase(e);
00698     } else {
00699       ++i;
00700     }
00701   }
00702 
00703   KeyMap::iterator i = keymap.find(key);
00704   if(i != keymap.end())
00705     keymap.erase(i);
00706 
00707   // add new mapping
00708   keymap[key] = control;
00709 }

void JoystickKeyboardController::set_joy_controls ( Control  id,
bool  value 
) [private]

Definition at line 329 of file joystickkeyboardcontroller.cpp.

References controller, Controller::JUMP, jump_with_up_joy, Controller::set_control(), and Controller::UP.

Referenced by process_axis_event(), process_button_event(), and process_hat_event().

00330 {
00331   if (jump_with_up_joy && id == Controller::UP)
00332     controller->set_control(Controller::JUMP, value);
00333 
00334   controller->set_control(id, value);
00335 }

JoystickKeyboardController& JoystickKeyboardController::operator= ( const JoystickKeyboardController  )  [private]


Friends And Related Function Documentation

friend class KeyboardMenu [friend]

Definition at line 39 of file joystickkeyboardcontroller.hpp.

friend class JoystickMenu [friend]

Definition at line 40 of file joystickkeyboardcontroller.hpp.


Member Data Documentation

Controller* JoystickKeyboardController::controller [private]

Definition at line 92 of file joystickkeyboardcontroller.hpp.

Referenced by get_main_controller(), JoystickKeyboardController(), process_key_event(), process_menu_key_event(), reset(), set_joy_controls(), update(), and ~JoystickKeyboardController().

KeyMap JoystickKeyboardController::keymap [private]

Definition at line 94 of file joystickkeyboardcontroller.hpp.

Referenced by bind_key(), JoystickKeyboardController(), process_key_event(), read(), reversemap_key(), and write().

ButtonMap JoystickKeyboardController::joy_button_map [private]

Definition at line 96 of file joystickkeyboardcontroller.hpp.

Referenced by bind_joybutton(), print_joystick_mappings(), process_button_event(), reversemap_joybutton(), unbind_joystick_control(), and write().

AxisMap JoystickKeyboardController::joy_axis_map [private]

Definition at line 98 of file joystickkeyboardcontroller.hpp.

Referenced by bind_joyaxis(), print_joystick_mappings(), process_axis_event(), reversemap_joyaxis(), unbind_joystick_control(), and write().

HatMap JoystickKeyboardController::joy_hat_map [private]

Definition at line 100 of file joystickkeyboardcontroller.hpp.

Referenced by bind_joyhat(), print_joystick_mappings(), process_hat_event(), reversemap_joyhat(), unbind_joystick_control(), and write().

std::vector<SDL_Joystick*> JoystickKeyboardController::joysticks [private]

Definition at line 102 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickMenu::recreateMenu(), JoystickMenu::update(), updateAvailableJoysticks(), and ~JoystickKeyboardController().

std::string JoystickKeyboardController::name [private]

Definition at line 104 of file joystickkeyboardcontroller.hpp.

int JoystickKeyboardController::dead_zone [private]

Definition at line 106 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickKeyboardController(), process_axis_event(), read(), and write().

int JoystickKeyboardController::min_joybuttons [private]

the number of buttons all joysticks have

Definition at line 108 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickKeyboardController(), and updateAvailableJoysticks().

int JoystickKeyboardController::max_joybuttons [private]

the max number of buttons a joystick has

Definition at line 110 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickKeyboardController(), read(), and updateAvailableJoysticks().

int JoystickKeyboardController::max_joyaxis [private]

Definition at line 112 of file joystickkeyboardcontroller.hpp.

Referenced by read(), and updateAvailableJoysticks().

int JoystickKeyboardController::max_joyhats [private]

Definition at line 114 of file joystickkeyboardcontroller.hpp.

Referenced by updateAvailableJoysticks().

Uint8 JoystickKeyboardController::hat_state [private]

Definition at line 116 of file joystickkeyboardcontroller.hpp.

Referenced by process_hat_event().

bool JoystickKeyboardController::jump_with_up_joy [private]

Definition at line 118 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickKeyboardController(), JoystickMenu::menu_action(), read(), JoystickMenu::recreateMenu(), set_joy_controls(), JoystickMenu::update(), and write().

bool JoystickKeyboardController::jump_with_up_kbd [private]

Definition at line 119 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickKeyboardController(), KeyboardMenu::KeyboardMenu(), KeyboardMenu::menu_action(), process_key_event(), read(), KeyboardMenu::update(), and write().

int JoystickKeyboardController::wait_for_key [private]

Definition at line 121 of file joystickkeyboardcontroller.hpp.

Referenced by KeyboardMenu::menu_action(), and process_menu_key_event().

int JoystickKeyboardController::wait_for_joystick [private]

Definition at line 122 of file joystickkeyboardcontroller.hpp.

Referenced by JoystickMenu::menu_action(), process_axis_event(), process_button_event(), process_hat_event(), and process_menu_key_event().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:31 2014 for SuperTux by  doxygen 1.5.1