From b66fb2e56bcb9cc66771f01240855d79cccac7ee Mon Sep 17 00:00:00 2001 From: hiker Date: Tue, 28 Oct 2014 09:00:22 +1100 Subject: [PATCH] Removed unnecessary parameter for gamepads in processAndMapInput, started a common virtual function for processAndMapInput. Removed unnecessary #includes, cosmetic changes. --- src/input/device_config.hpp | 2 +- src/input/device_manager.cpp | 72 +++++++++++-------- src/input/device_manager.hpp | 52 ++++++-------- src/input/gamepad_device.cpp | 21 +++--- src/input/gamepad_device.hpp | 6 +- src/input/input_device.cpp | 13 +--- src/input/input_device.hpp | 55 ++++++++++---- src/input/input_manager.cpp | 2 +- src/network/protocols/start_game_protocol.cpp | 1 + src/states_screens/kart_selection.cpp | 2 +- src/states_screens/options_screen_input2.cpp | 2 +- src/tracks/track_object_presentation.cpp | 1 + 12 files changed, 125 insertions(+), 104 deletions(-) diff --git a/src/input/device_config.hpp b/src/input/device_config.hpp index 51ed257f6..155b69ee8 100644 --- a/src/input/device_config.hpp +++ b/src/input/device_config.hpp @@ -118,7 +118,7 @@ public: // ------------------------------------------------------------------------ /** Returns the binding of a given index. */ - Binding& getBinding (int i) {return m_bindings[i];} + Binding& getBinding(int i) {return m_bindings[i];} // ------------------------------------------------------------------------ /** At this time only relevant for gamepads, keyboards are always enabled */ diff --git a/src/input/device_manager.cpp b/src/input/device_manager.cpp index 74b54cdc8..248c79b7a 100644 --- a/src/input/device_manager.cpp +++ b/src/input/device_manager.cpp @@ -239,7 +239,8 @@ bool DeviceManager::getConfigForGamepad(const int irr_id, } return configCreated; -} +} // getConfigForGamepad + // ----------------------------------------------------------------------------- void DeviceManager::addKeyboard(KeyboardDevice* d) { @@ -296,25 +297,30 @@ bool DeviceManager::deleteConfig(DeviceConfig* config) } // deleteConfig // ----------------------------------------------------------------------------- - -InputDevice* DeviceManager::mapKeyboardInput( int btnID, InputManager::InputDriverMode mode, - StateManager::ActivePlayer **player /* out */, - PlayerAction *action /* out */ ) +/** Helper method, only used internally. Takes care of analyzing keyboard input. + * \param[in] button_id Id of the key pressed. + * \param[in] mode Used to determine whether to determine menu actions + * or game actions + * \param[out] player Which player this input belongs to (only set in + * ASSIGN mode). + * \param[out] action Which action is related to this input trigger. + * \return The device to which this input belongs + */ +InputDevice* DeviceManager::mapKeyboardInput(int button_id, + InputManager::InputDriverMode mode, + StateManager::ActivePlayer **player, + PlayerAction *action /* out */) { const int keyboard_amount = m_keyboards.size(); - //Log::info("DeviceManager::mapKeyboardInput", "Map %d to %d", btnID, keyboard_amount); - for (int n=0; nprocessAndMapInput(btnID, mode, action)) + if (keyboard->processAndMapInput(button_id, mode, action)) { - //Log::info("DeviceManager::mapKeyboardInput", "Binding found in keyboard #%d; action is %s", n + 1, KartActionStrings[*action]); if (m_single_player != NULL) { - //Log::info("DeviceManager", "Single player"); *player = m_single_player; } else if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode @@ -323,7 +329,7 @@ InputDevice* DeviceManager::mapKeyboardInput( int btnID, InputManager::InputDriv } else { - *player = keyboard->m_player; + *player = keyboard->getPlayer(); } return keyboard; } @@ -333,21 +339,31 @@ InputDevice* DeviceManager::mapKeyboardInput( int btnID, InputManager::InputDriv } // mapKeyboardInput //----------------------------------------------------------------------------- +/** Helper method, only used internally. Takes care of analyzing gamepad + * input. + * \param[in] type Type of gamepad event (IT_STICKMOTION etc). + * \param[out] player Which player this input belongs to (only set in + * ASSIGN mode) + * \param[out] action Which action is related to this input trigger + * \param mode Used to determine whether to determine menu actions + * or game actions + * \return The device to which this input belongs + */ -InputDevice *DeviceManager::mapGamepadInput( Input::InputType type, - int deviceID, - int btnID, - int axisDir, +InputDevice *DeviceManager::mapGamepadInput(Input::InputType type, + int device_id, + int button_id, + int axis_dir, int *value /* inout */, InputManager::InputDriverMode mode, StateManager::ActivePlayer **player /* out */, PlayerAction *action /* out */) { - GamePadDevice *gPad = getGamePadFromIrrID(deviceID); + GamePadDevice *gPad = getGamePadFromIrrID(device_id); if (gPad != NULL) { - if (gPad->processAndMapInput(type, btnID, value, mode, gPad->getPlayer(), action)) + if (gPad->processAndMapInput(type, button_id, value, mode, action)) { if (m_single_player != NULL) { @@ -374,9 +390,9 @@ InputDevice *DeviceManager::mapGamepadInput( Input::InputType type, //----------------------------------------------------------------------------- bool DeviceManager::translateInput( Input::InputType type, - int deviceID, - int btnID, - int axisDir, + int device_id, + int button_id, + int axis_dir, int* value /* inout */, InputManager::InputDriverMode mode, StateManager::ActivePlayer** player /* out */, @@ -384,7 +400,7 @@ bool DeviceManager::translateInput( Input::InputType type, { if (GUIEngine::getCurrentScreen() != NULL) { - GUIEngine::getCurrentScreen()->filterInput(type, deviceID, btnID, axisDir, *value); + GUIEngine::getCurrentScreen()->filterInput(type, device_id, button_id, axis_dir, *value); } InputDevice *device = NULL; @@ -393,12 +409,12 @@ bool DeviceManager::translateInput( Input::InputType type, switch (type) { case Input::IT_KEYBOARD: - device = mapKeyboardInput(btnID, mode, player, action); + device = mapKeyboardInput(button_id, mode, player, action); // If the action is not recognised, check if it's a fire key // that should be mapped to select if(!device && m_map_fire_to_select) { - device = mapKeyboardInput(btnID, InputManager::INGAME, player, + device = mapKeyboardInput(button_id, InputManager::INGAME, player, action); if(device && *action == PA_FIRE) *action = PA_MENU_SELECT; @@ -406,11 +422,11 @@ bool DeviceManager::translateInput( Input::InputType type, break; case Input::IT_STICKBUTTON: case Input::IT_STICKMOTION: - device = mapGamepadInput(type, deviceID, btnID, axisDir, value, + device = mapGamepadInput(type, device_id, button_id, axis_dir, value, mode, player, action); if(!device && m_map_fire_to_select) { - device = mapGamepadInput(type, deviceID, btnID, axisDir, value, + device = mapGamepadInput(type, device_id, button_id, axis_dir, value, InputManager::INGAME, player, action); if(device && *action == PA_FIRE) *action = PA_MENU_SELECT; @@ -569,17 +585,17 @@ void DeviceManager::save() // ----------------------------------------------------------------------------- -KeyboardDevice* DeviceManager::getKeyboardFromBtnID(const int btnID) +KeyboardDevice* DeviceManager::getKeyboardFromBtnID(const int button_id) { const int keyboard_amount = m_keyboards.size(); for (int n=0; nhasBindingFor(btnID)) + if (m_keyboards[n].getConfiguration()->hasBindingFor(button_id)) return m_keyboards.get(n); } return NULL; -} // getKeyboardFromBtnID +} // getKeyboardFromButtonID // ----------------------------------------------------------------------------- diff --git a/src/input/device_manager.hpp b/src/input/device_manager.hpp index 8b58a980b..2723fb938 100644 --- a/src/input/device_manager.hpp +++ b/src/input/device_manager.hpp @@ -21,16 +21,21 @@ #include "input/device_config.hpp" #include "input/gamepad_config.hpp" -#include "input/input_device.hpp" +#include "input/input_manager.hpp" #include "input/keyboard_config.hpp" +#include "states_screens/state_manager.hpp" #include "utils/no_copy.hpp" #include "utils/ptr_vector.hpp" #include +#include +using namespace irr; +class InputDevice; class GamePadDevice; class KeyboardDevice; + enum PlayerAssignMode { NO_ASSIGN, //!< react to all devices @@ -63,47 +68,32 @@ private: PtrVector m_gamepad_configs; /** The list of all joysticks that were found and activated. */ - core::array m_irrlicht_gamepads; - InputDevice* m_latest_used_device; - PlayerAssignMode m_assign_mode; + core::array m_irrlicht_gamepads; + InputDevice* m_latest_used_device; + PlayerAssignMode m_assign_mode; - /** - * Helper method, only used internally. Takes care of analyzing gamepad input. - * - * \param[out] player Which player this input belongs to (only set in ASSIGN mode) - * \param[out] action Which action is related to this input trigger - * \param mode Used to determine whether to determine menu actions or game actions - * \return The device to which this input belongs - */ - InputDevice *mapGamepadInput ( Input::InputType type, - int deviceID, - int btnID, - int axisDir, - int* value /* inout */, - InputManager::InputDriverMode mode, - StateManager::ActivePlayer **player /* out */, - PlayerAction *action /* out */); /** Will be non-null in single-player mode */ StateManager::ActivePlayer* m_single_player; - /** - * Helper method, only used internally. Takes care of analyzing keyboard input. - * - * \param[out] player Which player this input belongs to (only set in ASSIGN mode) - * \param[out] action Which action is related to this input trigger - * \param mode Used to determine whether to determine menu actions or game actions - * \return The device to which this input belongs - */ - InputDevice *mapKeyboardInput ( int btnID, InputManager::InputDriverMode mode, - StateManager::ActivePlayer **player /* out */, - PlayerAction *action /* out */); /** If this is flag is set the next fire event (if the fire key is not * mapped to anything else) will be mapped to 'select'. This is used * in the kart select GUI to support the old way of adding players by * pressing fire. */ bool m_map_fire_to_select; + + InputDevice *mapGamepadInput( Input::InputType type, int deviceID, + int btnID, int axisDir, + int* value /* inout */, + InputManager::InputDriverMode mode, + StateManager::ActivePlayer **player /* out */, + PlayerAction *action /* out */); + InputDevice *mapKeyboardInput(int button_id, + InputManager::InputDriverMode mode, + StateManager::ActivePlayer **player /* out */, + PlayerAction *action /* out */); + bool load(); void shutdown(); diff --git a/src/input/gamepad_device.cpp b/src/input/gamepad_device.cpp index 9b21afea7..a5bcc5a11 100644 --- a/src/input/gamepad_device.cpp +++ b/src/input/gamepad_device.cpp @@ -28,7 +28,6 @@ GamePadDevice::GamePadDevice(const int irrIndex, const std::string name, GamepadConfig *configuration) { m_type = DT_GAMEPAD; - m_deadzone = DEADZONE_JOYSTICK; m_prev_axis_directions = NULL; m_configuration = configuration; m_axis_count = axis_count; @@ -79,14 +78,13 @@ void GamePadDevice::setButtonPressed(const int i, bool isButtonPressed) // ---------------------------------------------------------------------------- void GamePadDevice::resetAxisDirection(const int axis, - Input::AxisDirection direction, - StateManager::ActivePlayer* player) + Input::AxisDirection direction) { // ignore this while in menus if (StateManager::get()->getGameState() != GUIEngine::GAME) return; - AbstractKart* pk = player->getKart(); - if (pk == NULL) + AbstractKart* pk = getPlayer()->getKart(); + if (!pk) { Log::error("Binding", "Trying to reset axis for an unknown player."); return; @@ -113,7 +111,6 @@ void GamePadDevice::resetAxisDirection(const int axis, bool GamePadDevice::processAndMapInput(Input::InputType type, const int id, int* value, /* inout */ InputManager::InputDriverMode mode, - StateManager::ActivePlayer* player, PlayerAction* action /* out */) { if (!m_configuration->isEnabled()) return false; @@ -126,20 +123,20 @@ bool GamePadDevice::processAndMapInput(Input::InputType type, const int id, // this gamepad doesn't even have that many axes if (id >= m_axis_count) return false; - if (player != NULL) + if (getPlayer()) { // going to negative from positive if (*value < 0 && m_prev_axis_directions[id] == Input::AD_POSITIVE) { // set positive id to 0 - resetAxisDirection(id, Input::AD_POSITIVE, player); + resetAxisDirection(id, Input::AD_POSITIVE); } // going to positive from negative else if (*value > 0 && m_prev_axis_directions[id] == Input::AD_NEGATIVE) { // set negative id to 0 - resetAxisDirection(id, Input::AD_NEGATIVE, player); + resetAxisDirection(id, Input::AD_NEGATIVE); } } @@ -161,7 +158,7 @@ bool GamePadDevice::processAndMapInput(Input::InputType type, const int id, } // check if within deadzone - if(*value > -m_deadzone && *value < m_deadzone && player != NULL) + if(*value > -m_deadzone && *value < m_deadzone && getPlayer()) { // Axis stands still: This is reported once for digital axes and // can be called multipled times for analog ones. Uses the @@ -174,12 +171,12 @@ bool GamePadDevice::processAndMapInput(Input::InputType type, const int id, if(m_prev_axis_directions[id] == Input::AD_NEGATIVE) { // set negative id to 0 - resetAxisDirection(id, Input::AD_NEGATIVE, player); + resetAxisDirection(id, Input::AD_NEGATIVE); } else if(m_prev_axis_directions[id] == Input::AD_POSITIVE) { // set positive id to 0 - resetAxisDirection(id, Input::AD_POSITIVE, player); + resetAxisDirection(id, Input::AD_POSITIVE); } m_prev_axis_directions[id] = Input::AD_NEUTRAL; diff --git a/src/input/gamepad_device.hpp b/src/input/gamepad_device.hpp index ef20b919c..4147e222c 100644 --- a/src/input/gamepad_device.hpp +++ b/src/input/gamepad_device.hpp @@ -28,8 +28,7 @@ class GamepadConfig; */ class GamePadDevice : public InputDevice { - void resetAxisDirection(const int axis, Input::AxisDirection direction, - StateManager::ActivePlayer* player); + void resetAxisDirection(const int axis, Input::AxisDirection direction); bool m_buttonPressed[SEvent::SJoystickEvent::NUMBER_OF_BUTTONS]; public: @@ -69,8 +68,6 @@ public: * was pressed is associated with a binding. If yes, sets action and * returns true; otherwise returns false. * - * \param player Only passed to know where to send 'axis reset's - * when necessary * \param id ID of the key that was pressed or of the axis * that was triggered (depending on * the value of the 'type' parameter) @@ -84,7 +81,6 @@ public: bool processAndMapInput(Input::InputType type, const int id, int* value, InputManager::InputDriverMode mode, - StateManager::ActivePlayer* player, PlayerAction* action); }; diff --git a/src/input/input_device.cpp b/src/input/input_device.cpp index b672f6adf..a90ff3152 100644 --- a/src/input/input_device.cpp +++ b/src/input/input_device.cpp @@ -30,19 +30,12 @@ InputDevice::InputDevice() { - m_player = NULL; + m_player = NULL; m_configuration = NULL; -} +} // InputDevice + // ---------------------------------------------------------------------------- InputDevice::~InputDevice() { } // ~InputDevice -// ---------------------------------------------------------------------------- -/** - * Sets which players uses this device; or pass NULL to say no player uses it. - */ -void InputDevice::setPlayer(StateManager::ActivePlayer* owner) -{ - m_player = owner; -} diff --git a/src/input/input_device.hpp b/src/input/input_device.hpp index c2880f3c6..987310978 100644 --- a/src/input/input_device.hpp +++ b/src/input/input_device.hpp @@ -42,27 +42,54 @@ enum DeviceType */ class InputDevice: public NoCopy { - friend class DeviceManager; protected: + /** Device type (keyboard, gamepad). */ DeviceType m_type; + + /** Which player is using this device. */ StateManager::ActivePlayer* m_player; + + /** The configuration for this device. */ DeviceConfig* m_configuration; -public: - /** If device has a name; unused for keyboards since AFAIK we - * can't tell keyboards apart. */ + /** If device has a name; unused for keyboards since AFAIK we + * can't tell keyboards apart. */ std::string m_name; - InputDevice(); - ~InputDevice(); +public: - void setConfiguration(DeviceConfig *config) {m_configuration = config;} - DeviceConfig *getConfiguration() {return m_configuration;} - - DeviceType getType() const { return m_type; }; - - void setPlayer(StateManager::ActivePlayer* owner); - StateManager::ActivePlayer *getPlayer() { return m_player; } -}; + InputDevice(); + virtual ~InputDevice(); +#ifdef NOTYET + virtual bool processAndMapInput(Input::InputType type, const int id, + int* value, + InputManager::InputDriverMode mode, + PlayerAction* action); + +#endif + // ------------------------------------------------------------------------ + /** Sets which players uses this device; or pass NULL to say no player + * uses it. */ + void setPlayer(StateManager::ActivePlayer* owner) { m_player = owner; } + + // ------------------------------------------------------------------------ + /** Sets the configuration to be used by this input device. */ + void setConfiguration(DeviceConfig *config) {m_configuration = config;} + + // ------------------------------------------------------------------------ + /** Returns the configuration for this device. */ + DeviceConfig *getConfiguration() {return m_configuration;} + + // ------------------------------------------------------------------------ + /** Returns the type of this device. */ + DeviceType getType() const { return m_type; }; + + // ------------------------------------------------------------------------ + /** Returns the player using this device. */ + StateManager::ActivePlayer *getPlayer() { return m_player; } + // ------------------------------------------------------------------------ + /** Returns the name of this device. */ + const std::string& getName() const { return m_name; } +}; // class InputDevice #endif diff --git a/src/input/input_manager.cpp b/src/input/input_manager.cpp index 907102d77..a23414c2a 100644 --- a/src/input/input_manager.cpp +++ b/src/input/input_manager.cpp @@ -646,7 +646,7 @@ void InputManager::dispatchInput(Input::InputType type, int deviceID, // is not associated to any player GUIEngine::showMessage( _("Ignoring '%s', you needed to join earlier to play!", - irr::core::stringw(gp->m_name.c_str()).c_str()) ); + irr::core::stringw(gp->getName().c_str()).c_str()) ); } } return; diff --git a/src/network/protocols/start_game_protocol.cpp b/src/network/protocols/start_game_protocol.cpp index dd3aeec60..76d10804e 100644 --- a/src/network/protocols/start_game_protocol.cpp +++ b/src/network/protocols/start_game_protocol.cpp @@ -2,6 +2,7 @@ #include "config/player_manager.hpp" #include "input/device_manager.hpp" +#include "input/input_device.hpp" #include "input/input_manager.hpp" #include "challenges/unlock_manager.hpp" #include "modes/world.hpp" diff --git a/src/states_screens/kart_selection.cpp b/src/states_screens/kart_selection.cpp index 1cddeff89..1d06f4301 100644 --- a/src/states_screens/kart_selection.cpp +++ b/src/states_screens/kart_selection.cpp @@ -1787,7 +1787,7 @@ void KartSelectionScreen::allPlayersDone() Log::info("[KartSelectionScreen]", " Player %u is %s on %s",n, core::stringc( players[n].getConstProfile()->getName().c_str()).c_str(), - players[n].getDevice()->m_name.c_str()); + players[n].getDevice()->getName().c_str()); } } diff --git a/src/states_screens/options_screen_input2.cpp b/src/states_screens/options_screen_input2.cpp index 66499b930..0835b2f91 100644 --- a/src/states_screens/options_screen_input2.cpp +++ b/src/states_screens/options_screen_input2.cpp @@ -434,7 +434,7 @@ void OptionsScreenInput2::gotSensedInput(const Input& sensed_input) std::string gamepad_name = input_manager->getDeviceManager() ->getGamePadFromIrrID(sensed_input.m_device_id) - ->m_name; + ->getName(); if (m_config->getName() == gamepad_name) { GamepadConfig* config = (GamepadConfig*)m_config; diff --git a/src/tracks/track_object_presentation.cpp b/src/tracks/track_object_presentation.cpp index fd9233616..fda773cea 100644 --- a/src/tracks/track_object_presentation.cpp +++ b/src/tracks/track_object_presentation.cpp @@ -31,6 +31,7 @@ #include "io/file_manager.hpp" #include "io/xml_node.hpp" #include "input/device_manager.hpp" +#include "input/input_device.hpp" #include "input/input_manager.hpp" #include "items/item_manager.hpp" #include "modes/world.hpp"