From a1c402c92fa5976481e9da8750c29418cef00f12 Mon Sep 17 00:00:00 2001 From: grumbel Date: Mon, 8 Mar 2010 21:37:32 +0000 Subject: [PATCH] Patch for multiple joysticks from const86 git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6581 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/control/controller.cpp | 6 + src/control/controller.hpp | 1 + src/control/joystickkeyboardcontroller.cpp | 213 ++++++++++++++++------------- src/control/joystickkeyboardcontroller.hpp | 41 +++--- src/gui/menu.cpp | 23 ++-- src/gui/menu_manager.cpp | 2 +- src/object/player.cpp | 2 +- src/supertux/game_session.cpp | 17 +-- src/supertux/gameconfig.cpp | 8 +- src/supertux/globals.cpp | 2 +- src/supertux/globals.hpp | 2 +- src/supertux/levelintro.cpp | 9 +- src/supertux/main.cpp | 6 +- src/supertux/menu/menu_storage.cpp | 4 +- src/supertux/screen_manager.cpp | 4 +- src/supertux/textscroller.cpp | 13 +- src/worldmap/tux.cpp | 9 +- src/worldmap/worldmap.cpp | 11 +- 18 files changed, 205 insertions(+), 168 deletions(-) diff --git a/src/control/controller.cpp b/src/control/controller.cpp index b936ee81f..93d6536e4 100644 --- a/src/control/controller.cpp +++ b/src/control/controller.cpp @@ -50,6 +50,12 @@ Controller::reset() } } +void +Controller::set_control(Control control, bool value) +{ + controls[control] = value; +} + bool Controller::hold(Control control) { diff --git a/src/control/controller.hpp b/src/control/controller.hpp index 2a8d5928b..5bec7581c 100644 --- a/src/control/controller.hpp +++ b/src/control/controller.hpp @@ -47,6 +47,7 @@ public: Controller(); virtual ~Controller(); + void set_control(Control control, bool value); /** returns true if the control is pressed down */ bool hold(Control control); /** returns true if the control has just been pressed down this frame */ diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index 761c6a074..ecf99fe3f 100644 --- a/src/control/joystickkeyboardcontroller.cpp +++ b/src/control/joystickkeyboardcontroller.cpp @@ -30,6 +30,7 @@ #include "util/writer.hpp" JoystickKeyboardController::JoystickKeyboardController() : + controller(), keymap(), joy_button_map(), joy_axis_map(), @@ -47,24 +48,26 @@ JoystickKeyboardController::JoystickKeyboardController() : wait_for_key(-1), wait_for_joystick(-1) { + controller = new Controller; + // initialize default keyboard map - keymap[SDLK_LEFT] = LEFT; - keymap[SDLK_RIGHT] = RIGHT; - keymap[SDLK_UP] = UP; - keymap[SDLK_DOWN] = DOWN; - keymap[SDLK_SPACE] = JUMP; - keymap[SDLK_LCTRL] = ACTION; - keymap[SDLK_LALT] = ACTION; - keymap[SDLK_ESCAPE] = PAUSE_MENU; - keymap[SDLK_p] = PAUSE_MENU; - keymap[SDLK_PAUSE] = PAUSE_MENU; - keymap[SDLK_RETURN] = MENU_SELECT; - keymap[SDLK_KP_ENTER] = MENU_SELECT; - keymap[SDLK_CARET] = CONSOLE; - keymap[SDLK_DELETE] = PEEK_LEFT; - keymap[SDLK_PAGEDOWN] = PEEK_RIGHT; - keymap[SDLK_HOME] = PEEK_UP; - keymap[SDLK_END] = PEEK_DOWN; + keymap[SDLK_LEFT] = Controller::LEFT; + keymap[SDLK_RIGHT] = Controller::RIGHT; + keymap[SDLK_UP] = Controller::UP; + keymap[SDLK_DOWN] = Controller::DOWN; + keymap[SDLK_SPACE] = Controller::JUMP; + keymap[SDLK_LCTRL] = Controller::ACTION; + keymap[SDLK_LALT] = Controller::ACTION; + keymap[SDLK_ESCAPE] = Controller::PAUSE_MENU; + keymap[SDLK_p] = Controller::PAUSE_MENU; + keymap[SDLK_PAUSE] = Controller::PAUSE_MENU; + keymap[SDLK_RETURN] = Controller::MENU_SELECT; + keymap[SDLK_KP_ENTER] = Controller::MENU_SELECT; + keymap[SDLK_CARET] = Controller::CONSOLE; + keymap[SDLK_DELETE] = Controller::PEEK_LEFT; + keymap[SDLK_PAGEDOWN] = Controller::PEEK_RIGHT; + keymap[SDLK_HOME] = Controller::PEEK_UP; + keymap[SDLK_END] = Controller::PEEK_DOWN; jump_with_up_joy = false; jump_with_up_kbd = false; @@ -74,31 +77,31 @@ JoystickKeyboardController::JoystickKeyboardController() : dead_zone = 1000; // Default joystick button configuration - joy_button_map[0] = JUMP; - joy_button_map[1] = ACTION; + bind_joybutton(0, 0, Controller::JUMP); + bind_joybutton(0, 1, Controller::ACTION); // 6 or more Buttons if( min_joybuttons > 5 ){ - joy_button_map[4] = PEEK_LEFT; - joy_button_map[5] = PEEK_RIGHT; + bind_joybutton(0, 4, Controller::PEEK_LEFT); + bind_joybutton(0, 5, Controller::PEEK_RIGHT); // 8 or more if(min_joybuttons > 7) - joy_button_map[min_joybuttons-1] = PAUSE_MENU; + bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU); } else { // map the last 2 buttons to menu and pause if(min_joybuttons > 2) - joy_button_map[min_joybuttons-1] = PAUSE_MENU; + bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU); // map all remaining joystick buttons to MENU_SELECT for(int i = 2; i < max_joybuttons; ++i) { if(i != min_joybuttons-1) - joy_button_map[i] = MENU_SELECT; + bind_joybutton(0, i, Controller::MENU_SELECT); } } // Default joystick axis configuration - joy_axis_map[-1] = LEFT; - joy_axis_map[ 1] = RIGHT; - joy_axis_map[-2] = UP; - joy_axis_map[ 2] = DOWN; + bind_joyaxis(0, -1, Controller::LEFT); + bind_joyaxis(0, 1, Controller::RIGHT); + bind_joyaxis(0, -2, Controller::UP); + bind_joyaxis(0, 2, Controller::DOWN); } JoystickKeyboardController::~JoystickKeyboardController() @@ -108,6 +111,7 @@ JoystickKeyboardController::~JoystickKeyboardController() if(*i != 0) SDL_JoystickClose(*i); } + delete controller; } void @@ -171,6 +175,12 @@ JoystickKeyboardController::updateAvailableJoysticks() } } +Controller* +JoystickKeyboardController::get_main_controller() +{ + return controller; +} + void JoystickKeyboardController::read(const Reader& lisp) { @@ -192,15 +202,15 @@ JoystickKeyboardController::read(const Reader& lisp) } int i = 0; - for(i = 0; controlNames[i] != 0; ++i) { - if(control == controlNames[i]) + for(i = 0; Controller::controlNames[i] != 0; ++i) { + if(control == Controller::controlNames[i]) break; } - if(controlNames[i] == 0) { + if(Controller::controlNames[i] == 0) { log_info << "Invalid control '" << control << "' in keymap" << std::endl; continue; } - keymap[(SDLKey) key] = (Control)i; + keymap[SDLKey(key)] = Control(i); } } } @@ -220,11 +230,11 @@ JoystickKeyboardController::read(const Reader& lisp) map->get("control", control); int i = 0; - for(i = 0; controlNames[i] != 0; ++i) { - if(control == controlNames[i]) + for(i = 0; Controller::controlNames[i] != 0; ++i) { + if(control == Controller::controlNames[i]) break; } - if(controlNames[i] == 0) { + if(Controller::controlNames[i] == 0) { log_info << "Invalid control '" << control << "' in buttonmap" << std::endl; continue; } @@ -234,7 +244,7 @@ JoystickKeyboardController::read(const Reader& lisp) log_info << "Invalid button '" << button << "' in buttonmap" << std::endl; continue; } - bind_joybutton(button, (Control) i); + bind_joybutton(0, button, Control(i)); } if (map->get("axis", axis)) { @@ -242,7 +252,7 @@ JoystickKeyboardController::read(const Reader& lisp) log_info << "Invalid axis '" << axis << "' in axismap" << std::endl; continue; } - bind_joyaxis(axis, (Control) i); + bind_joyaxis(0, axis, Control(i)); } if (map->get("hat", hat)) { @@ -253,7 +263,7 @@ JoystickKeyboardController::read(const Reader& lisp) log_info << "Invalid axis '" << axis << "' in axismap" << std::endl; continue; } else { - bind_joyhat(hat, (Control) i); + bind_joyhat(0, hat, Control(i)); } } } @@ -269,7 +279,7 @@ JoystickKeyboardController::write(Writer& writer) for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) { writer.start_list("map"); writer.write("key", (int) i->first); - writer.write("control", controlNames[i->second]); + writer.write("control", Controller::controlNames[i->second]); writer.end_list("map"); } writer.end_list("keymap"); @@ -281,22 +291,22 @@ JoystickKeyboardController::write(Writer& writer) for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) { writer.start_list("map"); - writer.write("button", i->first); - writer.write("control", controlNames[i->second]); + writer.write("button", i->first.second); + writer.write("control", Controller::controlNames[i->second]); writer.end_list("map"); } for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) { writer.start_list("map"); - writer.write("hat", i->first); - writer.write("control", controlNames[i->second]); + writer.write("hat", i->first.second); + writer.write("control", Controller::controlNames[i->second]); writer.end_list("map"); } for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) { writer.start_list("map"); - writer.write("axis", i->first); - writer.write("control", controlNames[i->second]); + writer.write("axis", i->first.second); + writer.write("control", Controller::controlNames[i->second]); writer.end_list("map"); } @@ -304,18 +314,24 @@ JoystickKeyboardController::write(Writer& writer) } void +JoystickKeyboardController::update() +{ + controller->update(); +} + +void JoystickKeyboardController::reset() { - Controller::reset(); + controller->reset(); } void JoystickKeyboardController::set_joy_controls(Control id, bool value) { if (jump_with_up_joy && id == Controller::UP) - controls[Controller::JUMP] = value; + controller->set_control(Controller::JUMP, value); - controls[(Control)id] = value; + controller->set_control(id, value); } void @@ -324,7 +340,7 @@ JoystickKeyboardController::process_event(const SDL_Event& event) switch(event.type) { case SDL_KEYUP: case SDL_KEYDOWN: - process_key_event(event); + process_key_event(event.key); break; case SDL_JOYAXISMOTION: @@ -352,7 +368,7 @@ JoystickKeyboardController::process_button_event(const SDL_JoyButtonEvent& jbutt { if(jbutton.state == SDL_PRESSED) { - bind_joybutton(jbutton.button, (Control)wait_for_joystick); + bind_joybutton(jbutton.which, jbutton.button, (Control)wait_for_joystick); MenuStorage::get_joystick_options_menu()->update(); reset(); wait_for_joystick = -1; @@ -360,7 +376,7 @@ JoystickKeyboardController::process_button_event(const SDL_JoyButtonEvent& jbutt } else { - ButtonMap::iterator i = joy_button_map.find(jbutton.button); + ButtonMap::iterator i = joy_button_map.find(std::make_pair(jbutton.which, jbutton.button)); if(i == joy_button_map.end()) { log_debug << "Unmapped joybutton " << (int)jbutton.button << " pressed" << std::endl; } else { @@ -376,9 +392,9 @@ JoystickKeyboardController::process_axis_event(const SDL_JoyAxisEvent& jaxis) { if (abs(jaxis.value) > dead_zone) { if (jaxis.value < 0) - bind_joyaxis(-(jaxis.axis + 1), Control(wait_for_joystick)); + bind_joyaxis(jaxis.which, -(jaxis.axis + 1), Control(wait_for_joystick)); else - bind_joyaxis(jaxis.axis + 1, Control(wait_for_joystick)); + bind_joyaxis(jaxis.which, jaxis.axis + 1, Control(wait_for_joystick)); MenuStorage::get_joystick_options_menu()->update(); wait_for_joystick = -1; @@ -390,8 +406,8 @@ JoystickKeyboardController::process_axis_event(const SDL_JoyAxisEvent& jaxis) // mapped separately (needed for jump/down vs up/down) int axis = jaxis.axis + 1; - AxisMap::iterator left = joy_axis_map.find(-axis); - AxisMap::iterator right = joy_axis_map.find(axis); + AxisMap::iterator left = joy_axis_map.find(std::make_pair(jaxis.which, -axis)); + AxisMap::iterator right = joy_axis_map.find(std::make_pair(jaxis.which, axis)); if(left == joy_axis_map.end()) { std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl; @@ -425,16 +441,16 @@ JoystickKeyboardController::process_hat_event(const SDL_JoyHatEvent& jhat) if (wait_for_joystick >= 0) { if (changed & SDL_HAT_UP && jhat.value & SDL_HAT_UP) - bind_joyhat(SDL_HAT_UP, (Control)wait_for_joystick); + bind_joyhat(jhat.which, SDL_HAT_UP, Control(wait_for_joystick)); if (changed & SDL_HAT_DOWN && jhat.value & SDL_HAT_DOWN) - bind_joyhat(SDL_HAT_DOWN, (Control)wait_for_joystick); + bind_joyhat(jhat.which, SDL_HAT_DOWN, Control(wait_for_joystick)); if (changed & SDL_HAT_LEFT && jhat.value & SDL_HAT_LEFT) - bind_joyhat(SDL_HAT_LEFT, (Control)wait_for_joystick); + bind_joyhat(jhat.which, SDL_HAT_LEFT, Control(wait_for_joystick)); if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT) - bind_joyhat(SDL_HAT_RIGHT, (Control)wait_for_joystick); + bind_joyhat(jhat.which, SDL_HAT_RIGHT, Control(wait_for_joystick)); MenuStorage::get_joystick_options_menu()->update(); wait_for_joystick = -1; @@ -443,28 +459,28 @@ JoystickKeyboardController::process_hat_event(const SDL_JoyHatEvent& jhat) { if (changed & SDL_HAT_UP) { - HatMap::iterator it = joy_hat_map.find(SDL_HAT_UP); + HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_UP)); if (it != joy_hat_map.end()) set_joy_controls(it->second, jhat.value & SDL_HAT_UP); } if (changed & SDL_HAT_DOWN) { - HatMap::iterator it = joy_hat_map.find(SDL_HAT_DOWN); + HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_DOWN)); if (it != joy_hat_map.end()) set_joy_controls(it->second, jhat.value & SDL_HAT_DOWN); } if (changed & SDL_HAT_LEFT) { - HatMap::iterator it = joy_hat_map.find(SDL_HAT_LEFT); + HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_LEFT)); if (it != joy_hat_map.end()) set_joy_controls(it->second, jhat.value & SDL_HAT_LEFT); } if (changed & SDL_HAT_RIGHT) { - HatMap::iterator it = joy_hat_map.find(SDL_HAT_RIGHT); + HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_RIGHT)); if (it != joy_hat_map.end()) set_joy_controls(it->second, jhat.value & SDL_HAT_RIGHT); } @@ -474,12 +490,12 @@ JoystickKeyboardController::process_hat_event(const SDL_JoyHatEvent& jhat) } void -JoystickKeyboardController::process_key_event(const SDL_Event& event) +JoystickKeyboardController::process_key_event(const SDL_KeyboardEvent& event) { - KeyMap::iterator key_mapping = keymap.find(event.key.keysym.sym); + KeyMap::iterator key_mapping = keymap.find(event.keysym.sym); // if console key was pressed: toggle console - if ((key_mapping != keymap.end()) && (key_mapping->second == CONSOLE)) { + if ((key_mapping != keymap.end()) && (key_mapping->second == Controller::CONSOLE)) { if (event.type == SDL_KEYDOWN) Console::instance->toggle(); } else { @@ -494,20 +510,21 @@ JoystickKeyboardController::process_key_event(const SDL_Event& event) //log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl; } else { Control control = key_mapping->second; - controls[control] = (event.type == SDL_KEYDOWN); - if (jump_with_up_kbd && control == UP){ - controls[JUMP] = (event.type == SDL_KEYDOWN); + bool value = (event.type == SDL_KEYDOWN); + controller->set_control(control, value); + if (jump_with_up_kbd && control == Controller::UP){ + controller->set_control(Controller::JUMP, value); } } } } void -JoystickKeyboardController::process_console_key_event(const SDL_Event& event) +JoystickKeyboardController::process_console_key_event(const SDL_KeyboardEvent& event) { if (event.type != SDL_KEYDOWN) return; - switch (event.key.keysym.sym) { + switch (event.keysym.sym) { case SDLK_RETURN: Console::instance->enter(); break; @@ -542,7 +559,7 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) Console::instance->move_cursor(+1); break; default: - int c = event.key.keysym.unicode; + int c = event.keysym.unicode; if ((c >= 32) && (c <= 126)) { Console::instance->input((char)c); } @@ -551,16 +568,16 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event) } void -JoystickKeyboardController::process_menu_key_event(const SDL_Event& event) +JoystickKeyboardController::process_menu_key_event(const SDL_KeyboardEvent& event) { // wait for key mode? if(wait_for_key >= 0) { if(event.type == SDL_KEYUP) return; - if(event.key.keysym.sym != SDLK_ESCAPE - && event.key.keysym.sym != SDLK_PAUSE) { - bind_key(event.key.keysym.sym, (Control) wait_for_key); + if(event.keysym.sym != SDLK_ESCAPE + && event.keysym.sym != SDLK_PAUSE) { + bind_key(event.keysym.sym, Control(wait_for_key)); } reset(); MenuStorage::get_key_options_menu()->update(); @@ -568,7 +585,7 @@ JoystickKeyboardController::process_menu_key_event(const SDL_Event& event) return; } if(wait_for_joystick >= 0) { - if(event.key.keysym.sym == SDLK_ESCAPE) { + if(event.keysym.sym == SDLK_ESCAPE) { reset(); MenuStorage::get_joystick_options_menu()->update(); wait_for_joystick = -1; @@ -580,34 +597,34 @@ JoystickKeyboardController::process_menu_key_event(const SDL_Event& event) /* we use default keys when the menu is open (to avoid problems when * redefining keys to invalid settings */ - switch(event.key.keysym.sym) { + switch(event.keysym.sym) { case SDLK_UP: - control = UP; + control = Controller::UP; break; case SDLK_DOWN: - control = DOWN; + control = Controller::DOWN; break; case SDLK_LEFT: - control = LEFT; + control = Controller::LEFT; break; case SDLK_RIGHT: - control = RIGHT; + control = Controller::RIGHT; break; case SDLK_SPACE: case SDLK_RETURN: case SDLK_KP_ENTER: - control = MENU_SELECT; + control = Controller::MENU_SELECT; break; case SDLK_ESCAPE: case SDLK_PAUSE: - control = PAUSE_MENU; + control = Controller::PAUSE_MENU; break; default: return; break; } - controls[control] = (event.type == SDL_KEYDOWN); + controller->set_control(control, (event.type == SDL_KEYDOWN)); } void @@ -637,7 +654,7 @@ JoystickKeyboardController::unbind_joystick_control(Control control) } void -JoystickKeyboardController::bind_joyaxis(int axis, Control control) +JoystickKeyboardController::bind_joyaxis(JoyId joy_id, int axis, Control control) { // axis isn't the SDL axis number, but axisnumber + 1 with sign // changed depending on if the positive or negative end is to be @@ -647,25 +664,25 @@ JoystickKeyboardController::bind_joyaxis(int axis, Control control) unbind_joystick_control(control); // add new mapping - joy_axis_map[axis] = control; + joy_axis_map[std::make_pair(joy_id, axis)] = control; } void -JoystickKeyboardController::bind_joyhat(int dir, Control c) +JoystickKeyboardController::bind_joyhat(JoyId joy_id, int dir, Control c) { unbind_joystick_control(c); // add new mapping - joy_hat_map[dir] = c; + joy_hat_map[std::make_pair(joy_id, dir)] = c; } void -JoystickKeyboardController::bind_joybutton(int button, Control control) +JoystickKeyboardController::bind_joybutton(JoyId joy_id, int button, Control control) { unbind_joystick_control(control); // add new mapping - joy_button_map[button] = control; + joy_button_map[std::make_pair(joy_id, button)] = control; } void @@ -688,7 +705,7 @@ JoystickKeyboardController::bind_key(SDLKey key, Control control) keymap.erase(i); // add new mapping - keymap[key]= control; + keymap[key] = control; } void @@ -697,15 +714,15 @@ JoystickKeyboardController::print_joystick_mappings() std::cout << "Joystick Mappings" << std::endl; std::cout << "-----------------" << std::endl; for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) { - std::cout << "Axis: " << i->first << " -> " << i->second << std::endl; + std::cout << "Axis: " << i->first.second << " -> " << i->second << std::endl; } for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) { - std::cout << "Button: " << i->first << " -> " << i->second << std::endl; + std::cout << "Button: " << i->first.second << " -> " << i->second << std::endl; } for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) { - std::cout << "Hat: " << i->first << " -> " << i->second << std::endl; + std::cout << "Hat: " << i->first.second << " -> " << i->second << std::endl; } std::cout << std::endl; } @@ -726,7 +743,7 @@ JoystickKeyboardController::reversemap_joyaxis(Control c) { for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) { if(i->second == c) - return i->first; + return i->first.second; } return 0; @@ -737,7 +754,7 @@ JoystickKeyboardController::reversemap_joybutton(Control c) { for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) { if(i->second == c) - return i->first; + return i->first.second; } return -1; @@ -748,7 +765,7 @@ JoystickKeyboardController::reversemap_joyhat(Control c) { for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) { if(i->second == c) - return i->first; + return i->first.second; } return -1; diff --git a/src/control/joystickkeyboardcontroller.hpp b/src/control/joystickkeyboardcontroller.hpp index 33d6020b6..00c4c2f40 100644 --- a/src/control/joystickkeyboardcontroller.hpp +++ b/src/control/joystickkeyboardcontroller.hpp @@ -31,9 +31,22 @@ class Menu; class KeyboardMenu; class JoystickMenu; +class Controller; -class JoystickKeyboardController : public Controller +class JoystickKeyboardController { +private: + friend class KeyboardMenu; + friend class JoystickMenu; + + typedef Controller::Control Control; + typedef Uint8 JoyId; + + typedef std::map KeyMap; + typedef std::map, Control> ButtonMap; + typedef std::map, Control> AxisMap; + typedef std::map, Control> HatMap; + public: JoystickKeyboardController(); virtual ~JoystickKeyboardController(); @@ -44,19 +57,20 @@ public: void write(Writer& writer); void read(const Reader& lisp); + void update(); void reset(); - Menu* get_key_options_menu(); - Menu* get_joystick_options_menu(); void updateAvailableJoysticks(); + Controller *get_main_controller(); + private: - void process_key_event(const SDL_Event& event); + 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_Event& event); - void process_menu_key_event(const SDL_Event& event); + void process_console_key_event(const SDL_KeyboardEvent& event); + void process_menu_key_event(const SDL_KeyboardEvent& event); void print_joystick_mappings(); @@ -67,23 +81,16 @@ private: void unbind_joystick_control(Control c); - void bind_joybutton(int button, Control c); - void bind_joyaxis(int axis, Control c); - void bind_joyhat(int dir, 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); private: - friend class KeyboardMenu; - friend class JoystickMenu; + Controller *controller; - typedef std::map KeyMap; - typedef std::map ButtonMap; - typedef std::map AxisMap; - typedef std::map HatMap; - -private: KeyMap keymap; ButtonMap joy_button_map; diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 4715cd8fb..b889ce777 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -237,52 +237,53 @@ Menu::update() effect_progress = 0.0f; } + Controller *controller = g_jk_controller->get_main_controller(); /** check main input controller... */ - if(g_main_controller->pressed(Controller::UP)) { + if(controller->pressed(Controller::UP)) { menuaction = MENU_ACTION_UP; menu_repeat_time = real_time + MENU_REPEAT_INITIAL; } - if(g_main_controller->hold(Controller::UP) && + if(controller->hold(Controller::UP) && menu_repeat_time != 0 && real_time > menu_repeat_time) { menuaction = MENU_ACTION_UP; menu_repeat_time = real_time + MENU_REPEAT_RATE; } - if(g_main_controller->pressed(Controller::DOWN)) { + if(controller->pressed(Controller::DOWN)) { menuaction = MENU_ACTION_DOWN; menu_repeat_time = real_time + MENU_REPEAT_INITIAL; } - if(g_main_controller->hold(Controller::DOWN) && + if(controller->hold(Controller::DOWN) && menu_repeat_time != 0 && real_time > menu_repeat_time) { menuaction = MENU_ACTION_DOWN; menu_repeat_time = real_time + MENU_REPEAT_RATE; } - if(g_main_controller->pressed(Controller::LEFT)) { + if(controller->pressed(Controller::LEFT)) { menuaction = MENU_ACTION_LEFT; menu_repeat_time = real_time + MENU_REPEAT_INITIAL; } - if(g_main_controller->hold(Controller::LEFT) && + if(controller->hold(Controller::LEFT) && menu_repeat_time != 0 && real_time > menu_repeat_time) { menuaction = MENU_ACTION_LEFT; menu_repeat_time = real_time + MENU_REPEAT_RATE; } - if(g_main_controller->pressed(Controller::RIGHT)) { + if(controller->pressed(Controller::RIGHT)) { menuaction = MENU_ACTION_RIGHT; menu_repeat_time = real_time + MENU_REPEAT_INITIAL; } - if(g_main_controller->hold(Controller::RIGHT) && + if(controller->hold(Controller::RIGHT) && menu_repeat_time != 0 && real_time > menu_repeat_time) { menuaction = MENU_ACTION_RIGHT; menu_repeat_time = real_time + MENU_REPEAT_RATE; } - if(g_main_controller->pressed(Controller::ACTION) - || g_main_controller->pressed(Controller::MENU_SELECT)) { + if(controller->pressed(Controller::ACTION) + || controller->pressed(Controller::MENU_SELECT)) { menuaction = MENU_ACTION_HIT; } - if(g_main_controller->pressed(Controller::PAUSE_MENU)) { + if(controller->pressed(Controller::PAUSE_MENU)) { menuaction = MENU_ACTION_BACK; } diff --git a/src/gui/menu_manager.cpp b/src/gui/menu_manager.cpp index c30471e31..7e911365a 100644 --- a/src/gui/menu_manager.cpp +++ b/src/gui/menu_manager.cpp @@ -75,7 +75,7 @@ MenuManager::set_current(Menu* menu) } // just to be sure... - g_main_controller->reset(); + g_jk_controller->reset(); } void diff --git a/src/object/player.cpp b/src/object/player.cpp index 86318a521..c456dec9a 100644 --- a/src/object/player.cpp +++ b/src/object/player.cpp @@ -152,7 +152,7 @@ Player::Player(PlayerStatus* _player_status, const std::string& name) : climbing(0) { this->name = name; - controller = g_main_controller; + controller = g_jk_controller->get_main_controller(); scripting_controller.reset(new CodeController()); // if/when we have complete penny gfx, we can // load those instead of Tux's sprite in the diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 786d23ff9..65db0454c 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -94,7 +94,7 @@ GameSession::restart_level() game_pause = false; end_sequence = 0; - g_main_controller->reset(); + g_jk_controller->reset(); currentsector = 0; @@ -344,12 +344,13 @@ GameSession::process_events() // save input for demo? if(capture_demo_stream != 0) { - capture_demo_stream ->put(g_main_controller->hold(Controller::LEFT)); - capture_demo_stream ->put(g_main_controller->hold(Controller::RIGHT)); - capture_demo_stream ->put(g_main_controller->hold(Controller::UP)); - capture_demo_stream ->put(g_main_controller->hold(Controller::DOWN)); - capture_demo_stream ->put(g_main_controller->hold(Controller::JUMP)); - capture_demo_stream ->put(g_main_controller->hold(Controller::ACTION)); + Controller *controller = g_jk_controller->get_main_controller(); + capture_demo_stream ->put(controller->hold(Controller::LEFT)); + capture_demo_stream ->put(controller->hold(Controller::RIGHT)); + capture_demo_stream ->put(controller->hold(Controller::UP)); + capture_demo_stream ->put(controller->hold(Controller::DOWN)); + capture_demo_stream ->put(controller->hold(Controller::JUMP)); + capture_demo_stream ->put(controller->hold(Controller::ACTION)); } } @@ -430,7 +431,7 @@ void GameSession::update(float elapsed_time) { // handle controller - if(g_main_controller->pressed(Controller::PAUSE_MENU)) + if(g_jk_controller->get_main_controller()->pressed(Controller::PAUSE_MENU)) on_escape_press(); process_events(); diff --git a/src/supertux/gameconfig.cpp b/src/supertux/gameconfig.cpp index 59a2c0a7f..add3908f8 100644 --- a/src/supertux/gameconfig.cpp +++ b/src/supertux/gameconfig.cpp @@ -90,8 +90,8 @@ Config::load() } const lisp::Lisp* config_control_lisp = config_lisp->get_lisp("control"); - if(config_control_lisp && g_main_controller) { - g_main_controller->read(*config_control_lisp); + if(config_control_lisp && g_jk_controller) { + g_jk_controller->read(*config_control_lisp); } const lisp::Lisp* config_addons_lisp = config_lisp->get_lisp("addons"); @@ -132,9 +132,9 @@ Config::save() writer.write("music_enabled", music_enabled); writer.end_list("audio"); - if(g_main_controller) { + if(g_jk_controller) { writer.start_list("control"); - g_main_controller->write(writer); + g_jk_controller->write(writer); writer.end_list("control"); } diff --git a/src/supertux/globals.cpp b/src/supertux/globals.cpp index 4672875f4..e67c243f8 100644 --- a/src/supertux/globals.cpp +++ b/src/supertux/globals.cpp @@ -18,7 +18,7 @@ #include SDL_Surface* g_screen; -JoystickKeyboardController* g_main_controller = 0; +JoystickKeyboardController* g_jk_controller = 0; tinygettext::DictionaryManager* dictionary_manager = 0; int SCREEN_WIDTH; diff --git a/src/supertux/globals.hpp b/src/supertux/globals.hpp index 08a48304e..8f21b1388 100644 --- a/src/supertux/globals.hpp +++ b/src/supertux/globals.hpp @@ -40,7 +40,7 @@ extern int SCREEN_WIDTH; extern int SCREEN_HEIGHT; // global variables -extern JoystickKeyboardController* g_main_controller; +extern JoystickKeyboardController* g_jk_controller; extern SDL_Surface* g_screen; diff --git a/src/supertux/levelintro.cpp b/src/supertux/levelintro.cpp index e8feeac96..98c56234e 100644 --- a/src/supertux/levelintro.cpp +++ b/src/supertux/levelintro.cpp @@ -52,12 +52,13 @@ LevelIntro::setup() void LevelIntro::update(float elapsed_time) { + Controller *controller = g_jk_controller->get_main_controller(); // Check if it's time to exit the screen - if(g_main_controller->pressed(Controller::JUMP) - || g_main_controller->pressed(Controller::ACTION) - || g_main_controller->pressed(Controller::MENU_SELECT) - || g_main_controller->pressed(Controller::PAUSE_MENU)) { + if(controller->pressed(Controller::JUMP) + || controller->pressed(Controller::ACTION) + || controller->pressed(Controller::MENU_SELECT) + || controller->pressed(Controller::PAUSE_MENU)) { g_screen_manager->exit_screen(new FadeOut(0.1)); } diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index ab5b4713f..390eb207c 100644 --- a/src/supertux/main.cpp +++ b/src/supertux/main.cpp @@ -558,7 +558,7 @@ Main::run(int argc, char** argv) init_sdl(); timelog("controller"); - g_main_controller = new JoystickKeyboardController(); + g_jk_controller = new JoystickKeyboardController(); timelog("config"); init_config(); @@ -645,8 +645,8 @@ Main::run(int argc, char** argv) g_config->save(); delete g_config; g_config = NULL; - delete g_main_controller; - g_main_controller = NULL; + delete g_jk_controller; + g_jk_controller = NULL; delete Console::instance; Console::instance = NULL; scripting::exit_squirrel(); diff --git a/src/supertux/menu/menu_storage.cpp b/src/supertux/menu/menu_storage.cpp index ea1ae467c..3a3ea91f7 100644 --- a/src/supertux/menu/menu_storage.cpp +++ b/src/supertux/menu/menu_storage.cpp @@ -46,7 +46,7 @@ MenuStorage::get_key_options_menu() { if (!key_options_menu) { // FIXME: this in never freed - key_options_menu = new KeyboardMenu(g_main_controller); + key_options_menu = new KeyboardMenu(g_jk_controller); } return key_options_menu; @@ -57,7 +57,7 @@ MenuStorage::get_joystick_options_menu() { if (!joystick_options_menu) { // FIXME: this in never freed - joystick_options_menu = new JoystickMenu(g_main_controller); + joystick_options_menu = new JoystickMenu(g_jk_controller); } return joystick_options_menu; diff --git a/src/supertux/screen_manager.cpp b/src/supertux/screen_manager.cpp index 493aaecb3..ffd95992c 100644 --- a/src/supertux/screen_manager.cpp +++ b/src/supertux/screen_manager.cpp @@ -189,12 +189,12 @@ ScreenManager::update_gamelogic(float elapsed_time) void ScreenManager::process_events() { - g_main_controller->update(); + g_jk_controller->update(); Uint8* keystate = SDL_GetKeyState(NULL); SDL_Event event; while(SDL_PollEvent(&event)) { - g_main_controller->process_event(event); + g_jk_controller->process_event(event); if(MenuManager::current() != NULL) MenuManager::current()->event(event); diff --git a/src/supertux/textscroller.cpp b/src/supertux/textscroller.cpp index f828bf0b0..9fffbbaaf 100644 --- a/src/supertux/textscroller.cpp +++ b/src/supertux/textscroller.cpp @@ -93,18 +93,19 @@ TextScroller::setup() void TextScroller::update(float elapsed_time) { - if(g_main_controller->hold(Controller::UP)) { + Controller *controller = g_jk_controller->get_main_controller(); + if(controller->hold(Controller::UP)) { speed = -defaultspeed*5; - } else if(g_main_controller->hold(Controller::DOWN)) { + } else if(controller->hold(Controller::DOWN)) { speed = defaultspeed*5; } else { speed = defaultspeed; } - if(g_main_controller->pressed(Controller::JUMP) - || g_main_controller->pressed(Controller::ACTION) - || g_main_controller->pressed(Controller::MENU_SELECT)) + if(controller->pressed(Controller::JUMP) + || controller->pressed(Controller::ACTION) + || controller->pressed(Controller::MENU_SELECT)) scroll += SCROLL; - if(g_main_controller->pressed(Controller::PAUSE_MENU)) { + if(controller->pressed(Controller::PAUSE_MENU)) { g_screen_manager->exit_screen(new FadeOut(0.5)); } diff --git a/src/worldmap/tux.cpp b/src/worldmap/tux.cpp index bce207ae1..7503d627e 100644 --- a/src/worldmap/tux.cpp +++ b/src/worldmap/tux.cpp @@ -287,13 +287,14 @@ Tux::tryContinueWalking(float elapsed_time) void Tux::updateInputDirection() { - if(g_main_controller->hold(Controller::UP)) + Controller *controller = g_jk_controller->get_main_controller(); + if(controller->hold(Controller::UP)) input_direction = D_NORTH; - else if(g_main_controller->hold(Controller::DOWN)) + else if(controller->hold(Controller::DOWN)) input_direction = D_SOUTH; - else if(g_main_controller->hold(Controller::LEFT)) + else if(controller->hold(Controller::LEFT)) input_direction = D_WEST; - else if(g_main_controller->hold(Controller::RIGHT)) + else if(controller->hold(Controller::RIGHT)) input_direction = D_EAST; } diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index e508596a0..dc57331ba 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -615,15 +615,16 @@ WorldMap::update(float delta) } // handle input + Controller *controller = g_jk_controller->get_main_controller(); bool enter_level = false; - if(g_main_controller->pressed(Controller::ACTION) - || g_main_controller->pressed(Controller::JUMP) - || g_main_controller->pressed(Controller::MENU_SELECT)) { + if(controller->pressed(Controller::ACTION) + || controller->pressed(Controller::JUMP) + || controller->pressed(Controller::MENU_SELECT)) { /* some people define UP and JUMP on the same key... */ - if(!g_main_controller->pressed(Controller::UP)) + if(!controller->pressed(Controller::UP)) enter_level = true; } - if(g_main_controller->pressed(Controller::PAUSE_MENU)) + if(controller->pressed(Controller::PAUSE_MENU)) on_escape_press(); // check for teleporters -- 2.11.0