X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fcontrol%2Fjoystickkeyboardcontroller.cpp;h=cffa6f5cb053f12d4454ed2de12b1d21bef542e4;hb=67690e081c28b818e94796be284206326bc8a6b9;hp=8e715f01a7baaa737d8cc91b96d90729e132917f;hpb=c954adb5469fca0729e8c76c3bc50b70db15f2c1;p=supertux.git diff --git a/src/control/joystickkeyboardcontroller.cpp b/src/control/joystickkeyboardcontroller.cpp index 8e715f01a..cffa6f5cb 100644 --- a/src/control/joystickkeyboardcontroller.cpp +++ b/src/control/joystickkeyboardcontroller.cpp @@ -70,6 +70,7 @@ JoystickKeyboardController::JoystickKeyboardController() keymap.insert(std::make_pair(SDLK_PAUSE, PAUSE_MENU)); keymap.insert(std::make_pair(SDLK_RETURN, MENU_SELECT)); keymap.insert(std::make_pair(SDLK_KP_ENTER, MENU_SELECT)); + keymap.insert(std::make_pair(SDLK_CARET, CONSOLE)); int joystick_count = SDL_NumJoysticks(); min_joybuttons = -1; @@ -78,12 +79,12 @@ JoystickKeyboardController::JoystickKeyboardController() SDL_Joystick* joystick = SDL_JoystickOpen(i); bool good = true; if(SDL_JoystickNumButtons(joystick) < 2) { - msg_warning("Joystick " << i << " has less than 2 buttons"); + msg_warning << "Joystick " << i << " has less than 2 buttons" << std::endl; good = false; } if(SDL_JoystickNumAxes(joystick) < 2 && SDL_JoystickNumHats(joystick) == 0) { - msg_warning("Joystick " << i << " has less than 2 axes and no hat"); + msg_warning << "Joystick " << i << " has less than 2 axes and no hat" << std::endl; good = false; } if(!good) { @@ -152,7 +153,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp) map->get("key", key); map->get("control", control); if(key < SDLK_FIRST || key >= SDLK_LAST) { - msg_warning("Invalid key '" << key << "' in keymap"); + msg_warning << "Invalid key '" << key << "' in keymap" << std::endl; continue; } @@ -162,12 +163,12 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp) break; } if(controlNames[i] == 0) { - msg_warning("Invalid control '" << control << "' in keymap"); + msg_warning << "Invalid control '" << control << "' in keymap" << std::endl; continue; } keymap.insert(std::make_pair((SDLKey) key, (Control) i)); } else { - msg_warning("Invalid lisp element '" << iter.item() << "' in keymap"); + msg_warning << "Invalid lisp element '" << iter.item() << "' in keymap" << std::endl; } } } @@ -188,7 +189,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp) map->get("button", button); map->get("control", control); if(button < 0 || button >= max_joybuttons) { - msg_warning("Invalid button '" << button << "' in buttonmap"); + msg_warning << "Invalid button '" << button << "' in buttonmap" << std::endl; continue; } @@ -198,7 +199,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp) break; } if(controlNames[i] == 0) { - msg_warning("Invalid control '" << control << "' in buttonmap"); + msg_warning << "Invalid control '" << control << "' in buttonmap" << std::endl; continue; } reset_joybutton(button, (Control) i); @@ -246,57 +247,7 @@ JoystickKeyboardController::process_event(const SDL_Event& event) switch(event.type) { case SDL_KEYUP: case SDL_KEYDOWN: - if (event.key.keysym.scancode == 49) { - // console key was pressed - toggle console - if (event.type == SDL_KEYDOWN) { - if (Console::hasFocus()) { - Console::hide(); - } else { - Console::show(); - } - } - } else if (Console::hasFocus()) { - // console is open - send key there - if (event.type == SDL_KEYDOWN) { - int c = event.key.keysym.unicode; - if ((c >= 32) && (c <= 126)) { - Console::input << (char)c; - } - switch (event.key.keysym.sym) { - case SDLK_RETURN: - Console::input << std::endl; - break; - case SDLK_BACKSPACE: - Console::backspace(); - break; - case SDLK_TAB: - Console::autocomplete(); - break; - case SDLK_PAGEUP: - Console::scroll(-1); - break; - case SDLK_PAGEDOWN: - Console::scroll(+1); - break; - default: - break; - } - } - } - else if (Menu::current()) { - // menu mode - send key there - process_menu_key_event(event); - return; - } else { - // normal mode - find key in keymap - KeyMap::iterator i = keymap.find(event.key.keysym.sym); - if(i == keymap.end()) { - msg_debug("Pressed key without mapping"); - return; - } - Control control = i->second; - controls[control] = event.type == SDL_KEYDOWN ? true : false; - } + process_key_event(event); break; case SDL_JOYAXISMOTION: @@ -370,8 +321,7 @@ JoystickKeyboardController::process_event(const SDL_Event& event) ButtonMap::iterator i = joy_button_map.find(event.jbutton.button); if(i == joy_button_map.end()) { - msg_debug("Unmapped joybutton " << (int) event.jbutton.button - << " pressed"); + msg_debug << "Unmapped joybutton " << (int)event.jbutton.button << " pressed" << std::endl; return; } @@ -386,6 +336,72 @@ JoystickKeyboardController::process_event(const SDL_Event& event) } void +JoystickKeyboardController::process_key_event(const SDL_Event& event) +{ + KeyMap::iterator key_mapping = keymap.find(event.key.keysym.sym); + + // if console key was pressed: toggle console + if ((key_mapping != keymap.end()) && (key_mapping->second == CONSOLE)) { + if (event.type != SDL_KEYDOWN) return; + Console::toggle(); + return; + } + + // if console is open: send key there + if (Console::hasFocus()) { + process_console_key_event(event); + return; + } + + // if menu mode: send key there + if (Menu::current()) { + process_menu_key_event(event); + return; + } + + // default action: update controls + if(key_mapping == keymap.end()) { + msg_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl; + return; + } + Control control = key_mapping->second; + controls[control] = event.type == SDL_KEYDOWN ? true : false; +} + +void +JoystickKeyboardController::process_console_key_event(const SDL_Event& event) +{ + if (event.type != SDL_KEYDOWN) return; + + switch (event.key.keysym.sym) { + case SDLK_RETURN: + Console::input << std::endl; + break; + case SDLK_BACKSPACE: + Console::backspace(); + break; + case SDLK_TAB: + Console::autocomplete(); + break; + case SDLK_PAGEUP: + Console::scroll(-1); + break; + case SDLK_PAGEDOWN: + Console::scroll(+1); + break; + case SDLK_END: + Console::scroll(+65535); + break; + default: + int c = event.key.keysym.unicode; + if ((c >= 32) && (c <= 126)) { + Console::input << (char)c; + } + break; + } +} + +void JoystickKeyboardController::process_menu_key_event(const SDL_Event& event) { // wait for key mode?