From f3e3206b26a1684443da72e2fca835a434a6e2f3 Mon Sep 17 00:00:00 2001 From: rforder Date: Thu, 6 Aug 2009 19:05:26 +0000 Subject: [PATCH] More cleanup for the input system. Removed a lot of functions from InputDevice that won't be used there anymore. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3815 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/config/device_config.cpp | 1 + src/config/device_config.hpp | 1 + src/input/device_manager.cpp | 44 +++-- src/input/device_manager.hpp | 4 +- src/input/input_device.cpp | 246 ++------------------------ src/input/input_device.hpp | 36 +--- src/input/input_manager.cpp | 2 - src/states_screens/options_screen.cpp | 25 +-- 8 files changed, 64 insertions(+), 295 deletions(-) diff --git a/src/config/device_config.cpp b/src/config/device_config.cpp index 2ef589f37..6e2e83a15 100644 --- a/src/config/device_config.cpp +++ b/src/config/device_config.cpp @@ -47,6 +47,7 @@ void DeviceConfig::setBinding ( const PlayerAction action, //------------------------------------------------------------------------------ +// Don't call this directly unless you are KeyboardDevice or GamepadDevice bool DeviceConfig::getBinding ( Input::InputType type, const int id, const int value, diff --git a/src/config/device_config.hpp b/src/config/device_config.hpp index 0bac79f2e..7b9cce794 100644 --- a/src/config/device_config.hpp +++ b/src/config/device_config.hpp @@ -41,6 +41,7 @@ class DeviceConfig const int id, Input::AxisDirection direction = Input::AD_NEUTRAL); + // Don't call this directly unless you are KeyboardDevice or GamepadDevice bool getBinding (Input::InputType type, const int id, const int value, diff --git a/src/input/device_manager.cpp b/src/input/device_manager.cpp index 045b5b3ee..d9750c0c0 100644 --- a/src/input/device_manager.cpp +++ b/src/input/device_manager.cpp @@ -12,8 +12,7 @@ DeviceManager::DeviceManager() { - m_keyboards.push_back(new KeyboardDevice()); - m_keyboard_amount = m_keyboards.size(); + m_keyboard_amount = 0; m_gamepad_amount = 0; m_latest_used_device = NULL; m_assign_mode = NO_ASSIGN; @@ -58,10 +57,7 @@ void DeviceManager::setAssignMode(const PlayerAssignMode assignMode) { m_gamepads[i].setPlayer(NULL); } - for(unsigned int n=0; nsetPlayer(NULL); } } // ----------------------------------------------------------------------------- @@ -113,8 +109,11 @@ GamepadConfig *DeviceManager::getGamepadConfig(const int irr_id) // ----------------------------------------------------------------------------- void DeviceManager::add(KeyboardDevice* d) { + m_keyboard = d; +/* m_keyboards.push_back(d); m_keyboard_amount = m_keyboards.size(); +*/ } // ----------------------------------------------------------------------------- void DeviceManager::add(GamePadDevice* d) @@ -137,23 +136,23 @@ bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int device { for(unsigned int n=0; nhasBinding(btnID, action) ) { // We found which device was triggered. if(m_assign_mode == NO_ASSIGN) { // In no-assign mode, simply keep track of which device is used - if(!programaticallyGenerated) m_latest_used_device = m_keyboards.get(n); + if(!programaticallyGenerated) m_latest_used_device = m_keyboard; //if(programaticallyGenerated) std::cout << "devieManager ignores programatical event\n"; } else { // In assign mode, find to which active player this binding belongs - if (m_keyboards[n].m_player != NULL) + if (m_keyboard->m_player != NULL) { - *player = m_keyboards[n].m_player; + *player = m_keyboard->m_player; if (m_assign_mode == DETECT_NEW && *action == PA_RESCUE) { @@ -168,21 +167,18 @@ bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int device // check now if (m_assign_mode == DETECT_NEW) { - for(unsigned int n=0; nhasBinding(btnID, &localaction)) { - PlayerAction localaction = PA_FIRST; // none - if (m_keyboards[n].hasBinding(btnID, &localaction)) + if(localaction == PA_FIRE) { - if(localaction == PA_FIRE) - { - if (value > Input::MAX_VALUE/2) - KartSelectionScreen::firePressedOnNewDevice( m_keyboards.get(n) ); - } - - *action = PA_FIRST; // FIXME : returning PA_FIRST is quite a hackish way to tell input was handled internally - return true; + if (value > Input::MAX_VALUE/2) + KartSelectionScreen::firePressedOnNewDevice( m_keyboard ); } - } // end for + + *action = PA_FIRST; // FIXME : returning PA_FIRST is quite a hackish way to tell input was handled internally + return true; + } } // end if assign_mode == DETECT_NEW } @@ -281,7 +277,7 @@ bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int device InputDevice* DeviceManager::getLatestUsedDevice() { // If none, probably the user clicked or used enter; give keyboard by default - if (m_latest_used_device == NULL ) return m_keyboards.get(0); + if (m_latest_used_device == NULL ) return m_keyboard; return m_latest_used_device; } @@ -365,6 +361,8 @@ bool DeviceManager::deserialize() if (m_keyboard_configs.size() == 0) m_keyboard_configs.push_back(new KeyboardConfig()); + m_keyboard->setConfiguration(m_keyboard_configs.get(0)); + for (int n = 0; n < m_keyboard_configs.size(); n++) printf("%s\n", m_keyboard_configs[n].toString().c_str()); diff --git a/src/input/device_manager.hpp b/src/input/device_manager.hpp index 1dd3e4dda..d5827f56f 100644 --- a/src/input/device_manager.hpp +++ b/src/input/device_manager.hpp @@ -14,7 +14,7 @@ enum PlayerAssignMode class DeviceManager { - ptr_vector m_keyboards; + KeyboardDevice *m_keyboard; ptr_vector m_gamepads; ptr_vector m_keyboard_configs; ptr_vector m_gamepad_configs; @@ -49,7 +49,7 @@ public: void setAssignMode(const PlayerAssignMode assignMode); int getKeyboardAmount() const { return m_keyboard_amount; } - KeyboardDevice* getKeyboard(const int i) { return m_keyboards.get(i); } + KeyboardDevice* getKeyboard(const int i) { return m_keyboard; } /** Given some input, finds to which device it belongs and, using the corresponding device object, maps this input to the corresponding player and game action. returns false if player/action could not be set. diff --git a/src/input/input_device.cpp b/src/input/input_device.cpp index de9ce6310..49212f421 100644 --- a/src/input/input_device.cpp +++ b/src/input/input_device.cpp @@ -9,12 +9,6 @@ InputDevice::InputDevice() { - for(int n=0; n\n\n"; - else if (m_type == DT_GAMEPAD) stream << "\n\n"; - else std::cerr << "Warning, unknown input device type, skipping it\n"; - - // stream << "owner=\"" << m_player << "\">\n\n"; - - - for(int n=0; n\n"; - } - - if (m_type == DT_KEYBOARD) stream << "\n\n\n\n"; - else if (m_type == DT_GAMEPAD) stream << "\n\n\n\n"; -} -// ----------------------------------------------------------------------------- -bool InputDevice::deserializeAction(irr::io::IrrXMLReader* xml) -{ - // ---- read name - const char* name_string = xml->getAttributeValue("name"); - if(name_string == NULL) return false; - - int binding_id = -1; - - for(int n=0; ngetAttributeValue("id"); - if(id_string == NULL) return false; - const int id = atoi(id_string); - - // ---- read event type - const char* event_string = xml->getAttributeValue("event"); - if(event_string == NULL) return false; - const int event_id = atoi(event_string); - - m_default_bindings[binding_id].id = id; - m_default_bindings[binding_id].type = (Input::InputType)event_id; - - - // ---- read axis direction - const char* dir_string = xml->getAttributeValue("direction"); - if(dir_string != NULL) - { - const int dir = atoi(dir_string); - m_default_bindings[binding_id].dir = (Input::AxisDirection)dir; - } - - return true; - -} -// ----------------------------------------------------------------------------- -std::string InputDevice::getBindingAsString(const PlayerAction action) const -{ - return Input::getInputAsString(m_default_bindings[action].type, m_default_bindings[action].id, m_default_bindings[action].dir); -} - -#if 0 -#pragma mark - -#pragma mark Keyboard -#endif // ----------------------------------------------------------------------------- KeyboardDevice::KeyboardDevice(KeyboardConfig *configuration) @@ -119,55 +32,11 @@ KeyboardDevice::KeyboardDevice() m_configuration = new KeyboardConfig(); m_type = DT_KEYBOARD; } -// ----------------------------------------------------------------------------- -KeyboardDevice::KeyboardDevice(irr::io::IrrXMLReader* xml) -{ - m_type = DT_KEYBOARD; -} // ----------------------------------------------------------------------------- -void KeyboardDevice::loadDefaults() +bool KeyboardDevice::hasBinding(const int id, PlayerAction* action) { - m_default_bindings[PA_NITRO].id = KEY_KEY_N; - m_default_bindings[PA_ACCEL].id = KEY_UP; - m_default_bindings[PA_BRAKE].id = KEY_DOWN; - m_default_bindings[PA_LEFT].id = KEY_LEFT; - m_default_bindings[PA_RIGHT].id = KEY_RIGHT; - m_default_bindings[PA_DRIFT].id = KEY_KEY_V; - m_default_bindings[PA_RESCUE].id = KEY_BACK; - m_default_bindings[PA_FIRE].id = KEY_SPACE; - m_default_bindings[PA_LOOK_BACK].id = KEY_KEY_B ; - - m_default_bindings[PA_NITRO].type = Input::IT_KEYBOARD; - m_default_bindings[PA_ACCEL].type = Input::IT_KEYBOARD; - m_default_bindings[PA_BRAKE].type = Input::IT_KEYBOARD; - m_default_bindings[PA_LEFT].type = Input::IT_KEYBOARD; - m_default_bindings[PA_RIGHT].type = Input::IT_KEYBOARD; - m_default_bindings[PA_DRIFT].type = Input::IT_KEYBOARD; - m_default_bindings[PA_RESCUE].type = Input::IT_KEYBOARD; - m_default_bindings[PA_FIRE].type = Input::IT_KEYBOARD; - m_default_bindings[PA_LOOK_BACK].type = Input::IT_KEYBOARD; -} -// ----------------------------------------------------------------------------- -void KeyboardDevice::editBinding(PlayerAction action, int key_id) -{ - m_default_bindings[action].id = key_id; -} -// ----------------------------------------------------------------------------- -/** checks if this key belongs to this belongs. if yes, sets action and returns true; otherwise returns false */ -bool KeyboardDevice::hasBinding(const int key_id, PlayerAction* action /* out */) const -{ -/* - for(int n=0; ngetBinding(Input::IT_KEYBOARD, key_id, 0, action); + return m_configuration->getBinding(Input::IT_KEYBOARD, id, 0, action); } // ----------------------------------------------------------------------------- @@ -177,28 +46,7 @@ bool KeyboardDevice::hasBinding(const int key_id, PlayerAction* action /* out */ #pragma mark gamepad #endif -/** - * Creates a GamePade device from a config file. Note that this device will not yet be ready to be used, - * it must first be detected to be connected by irrLicht, to be properly initialized - */ -GamePadDevice::GamePadDevice(irr::io::IrrXMLReader* xml) -{ - m_type = DT_GAMEPAD; - m_prevAxisDirections = NULL; - m_deadzone = DEADZONE_JOYSTICK; - const char* name_string = xml->getAttributeValue("name"); - if(name_string == NULL) - { - std::cerr << "Warning, joystick without name in config file, making it undetectable\n"; - } - else m_name = name_string; - m_index = -1; // Set to -1 so we can establish when a device ID has been associated - - for(int n=0; ntoString().c_str()); - + std::cout << "(i) This gamepad has " << axis_count << " axes and " << m_button_count << " buttons\n"; - open(irrIndex, name, axis_count, btnAmount); + m_index = irrIndex; m_name = name; - - loadDefaults(); + for (int i = 0; i < axis_count; i++) + m_prevAxisDirections[i] = Input::AD_NEUTRAL; + for(int n=0; nisGameState()) return; // ignore this while in menus @@ -313,6 +96,7 @@ void GamePadDevice::resetAxisDirection(const int axis, Input::AxisDirection dire return; } +/* for(int n=0; nloadDefaults(); - something_new_to_write = true; } diff --git a/src/states_screens/options_screen.cpp b/src/states_screens/options_screen.cpp index bd0d5d3a2..951f882e9 100644 --- a/src/states_screens/options_screen.cpp +++ b/src/states_screens/options_screen.cpp @@ -19,6 +19,7 @@ #include "audio/sfx_manager.hpp" #include "audio/sfx_base.hpp" #include "config/player.hpp" +#include "config/device_config.hpp" #include "graphics/irr_driver.hpp" #include "guiengine/engine.hpp" #include "guiengine/screen.hpp" @@ -222,44 +223,44 @@ namespace OptionsScreen } // ----------------------------------------------------------------------------- - void updateInputButtons(const InputDevice* device) + void updateInputButtons(InputDevice* device) { { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_up"); - btn->setLabel( device->getBindingAsString(PA_ACCEL).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_ACCEL).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_down"); - btn->setLabel( device->getBindingAsString(PA_BRAKE).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_BRAKE).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_left"); - btn->setLabel( device->getBindingAsString(PA_LEFT).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_LEFT).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_right"); - btn->setLabel( device->getBindingAsString(PA_RIGHT).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_RIGHT).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_fire"); - btn->setLabel( device->getBindingAsString(PA_FIRE).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_FIRE).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_nitro"); - btn->setLabel( device->getBindingAsString(PA_NITRO).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_NITRO).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_drift"); - btn->setLabel( device->getBindingAsString(PA_DRIFT).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_DRIFT).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_rescue"); - btn->setLabel( device->getBindingAsString(PA_RESCUE).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_RESCUE).c_str() ); } { ButtonWidget* btn = getCurrentScreen()->getWidget("binding_look_back"); - btn->setLabel( device->getBindingAsString(PA_LOOK_BACK).c_str() ); + btn->setLabel( device->getConfiguration()->getBindingAsString(PA_LOOK_BACK).c_str() ); } } @@ -462,7 +463,7 @@ namespace OptionsScreen std::cout << "% Binding " << KartActionStrings[binding_to_set] << " : setting to keyboard key " << sensedInput->btnID << " \n\n"; KeyboardDevice* keyboard = input_manager->getDeviceList()->getKeyboard(0); - keyboard->editBinding(binding_to_set, sensedInput->btnID); + keyboard->getConfiguration()->setBinding(binding_to_set, Input::IT_KEYBOARD, sensedInput->btnID, Input::AD_NEUTRAL); // refresh display initInput(NULL, "init"); @@ -503,7 +504,7 @@ namespace OptionsScreen } GamePadDevice* gamepad = input_manager->getDeviceList()->getGamePad(gamepadID); - gamepad->editBinding(binding_to_set, sensedInput->type, sensedInput->btnID, + gamepad->getConfiguration()->setBinding(binding_to_set, sensedInput->type, sensedInput->btnID, (Input::AxisDirection)sensedInput->axisDirection); // refresh display