More refactoring. Created factory for DeviceConfigs.

This commit is contained in:
hiker
2014-10-29 08:22:25 +11:00
parent 92196c7534
commit 29f5b14e45
11 changed files with 102 additions and 56 deletions

View File

@@ -19,6 +19,8 @@
#include "input/device_config.hpp"
#include "input/gamepad_config.hpp"
#include "input/keyboard_config.hpp"
#include "io/xml_node.hpp"
#include "utils/log.hpp"
@@ -28,6 +30,39 @@
using namespace irr;
// ------------------------------------------------------------------------
/** A simple factory that creates either a gamepad or a keyboard
* configuration.
* \param type "gamepad" or "keyboard".
* \param config The XML node with additional configuration parameters.
*/
DeviceConfig* DeviceConfig::create(const XMLNode *config)
{
DeviceConfig *device_config = NULL;
if(config->getName()=="keyboard")
{
device_config = new KeyboardConfig();
}
else if(config->getName()=="gamepad")
{
device_config = new GamepadConfig();
}
else
{
Log::error("DeviceConfig", "Incorrect type: '%s'.",
config->getName().c_str());
return NULL;
}
// A default keyboard etc is created without
if(config && !device_config->load(config))
{
delete device_config;
return NULL;
}
return device_config;
} // create
// ------------------------------------------------------------------------
DeviceConfig::DeviceConfig(DeviceConfigType type)
{
m_type = type;
@@ -265,6 +300,7 @@ void DeviceConfig::save (std::ofstream& stream)
*/
bool DeviceConfig::load(const XMLNode *config)
{
config->get("enabled", &m_enabled);
bool error = false;
for(unsigned int i=0; i<config->getNumNodes(); i++)
{

View File

@@ -78,8 +78,10 @@ protected:
const int id,
int* value, /* inout */
PlayerAction* action /* out */);
public:
static DeviceConfig* create(const XMLNode *config);
irr::core::stringw toString();
bool hasBindingFor(const int buttonID) const;
bool hasBindingFor(const int buttonID, PlayerAction from,

View File

@@ -115,7 +115,7 @@ bool DeviceManager::initialize()
}
// Returns true if new configuration was created
if (getConfigForGamepad(id, name, &gamepadConfig) == true)
if (getConfigForGamepad(id, name.c_str(), &gamepadConfig) == true)
{
if(UserConfigParams::logMisc())
Log::info("Device manager","creating new configuration.");
@@ -200,7 +200,7 @@ GamePadDevice* DeviceManager::getGamePadFromIrrID(const int id)
* otherwise false.
*/
bool DeviceManager::getConfigForGamepad(const int irr_id,
const core::stringc& name,
const std::string& name,
GamepadConfig **config)
{
bool found = false;
@@ -209,7 +209,7 @@ bool DeviceManager::getConfigForGamepad(const int irr_id,
// Find appropriate configuration
for(unsigned int n=0; n < m_gamepad_configs.size(); n++)
{
if(m_gamepad_configs[n].getName() == name.c_str())
if(m_gamepad_configs[n].getName() == name)
{
*config = m_gamepad_configs.get(n);
found = true;
@@ -221,7 +221,7 @@ bool DeviceManager::getConfigForGamepad(const int irr_id,
{
if(irr_id < (int)(m_irrlicht_gamepads.size()))
{
*config = new GamepadConfig( name.c_str(),
*config = new GamepadConfig( name,
m_irrlicht_gamepads[irr_id].Axes,
m_irrlicht_gamepads[irr_id].Buttons );
}
@@ -504,29 +504,24 @@ bool DeviceManager::load()
for(unsigned int i=0; i<input->getNumNodes(); i++)
{
const XMLNode *config = input->getNode(i);
if(config->getName()=="keyboard")
{
KeyboardConfig* keyboard_config = new KeyboardConfig();
if(!keyboard_config->load(config))
{
Log::error("Device manager",
"Ignoring an ill-formed keyboard action in input config.");
}
m_keyboard_configs.push_back(keyboard_config);
}
else if (config->getName()=="gamepad")
{
GamepadConfig* gamepad_config = new GamepadConfig(config);
gamepad_config->load(config);
m_gamepad_configs.push_back(gamepad_config);
}
else
DeviceConfig *device_config = DeviceConfig::create(config);
if(!device_config)
{
Log::warn("DeviceManager",
"Invalid node '%s' in input.xml - ignored.",
config->getName().c_str());
continue;
}
if(config->getName()=="keyboard")
{
KeyboardConfig *kc = static_cast<KeyboardConfig*>(device_config);
m_keyboard_configs.push_back(kc);
}
else if (config->getName()=="gamepad")
{
GamepadConfig *gc = static_cast<GamepadConfig*>(device_config);
m_gamepad_configs.push_back(gc);
}
} // for i < getNumNodes
if (UserConfigParams::logMisc())

View File

@@ -19,7 +19,6 @@
#ifndef DEVICE_MANAGER_HPP
#define DEVICE_MANAGER_HPP
#include "input/device_config.hpp"
#include "input/gamepad_config.hpp"
#include "input/input_manager.hpp"
#include "input/keyboard_config.hpp"
@@ -31,6 +30,7 @@
#include <IEventReceiver.h>
using namespace irr;
class DeviceConfig;
class InputDevice;
class GamePadDevice;
class KeyboardDevice;
@@ -115,7 +115,9 @@ public:
GamePadDevice* getGamePadFromIrrID(const int i);
void clearGamepads();
/** Returns the keyboard that has a binding for this button, or NULL if none */
bool getConfigForGamepad(const int sdl_id, const core::stringc& pname, GamepadConfig **config);
bool getConfigForGamepad(const int sdl_id,
const std::string& name,
GamepadConfig **config);
// ---- Keyboard(s) ----
void addEmptyKeyboard();

View File

@@ -44,22 +44,26 @@ GamepadConfig::GamepadConfig ( const std::string &name,
//------------------------------------------------------------------------------
GamepadConfig::GamepadConfig(const XMLNode *config)
: DeviceConfig( DEVICE_CONFIG_TYPE_GAMEPAD )
GamepadConfig::GamepadConfig() : DeviceConfig( DEVICE_CONFIG_TYPE_GAMEPAD )
{
if(!config->get("name", &m_name))
Log::error("DeviceConfig", "Unnamed joystick in config file.");
config->get("enabled", &m_enabled);
m_plugged = 0;
m_plugged = 0;
m_deadzone = 2000;
m_name = "";
setDefaultBinds();
} // GamepadConfig(XMLNode)
} // GamepadConfig
//------------------------------------------------------------------------------
/** Loads this configuration from the given XML node.
* \param config The XML tree.
* \return False in case of an error.
*/
bool GamepadConfig::load(const XMLNode *config)
{
if(!config->get("name", &m_name))
{
Log::error("DeviceConfig", "Unnamed joystick in config file.");
return false;
}
m_deadzone = 2000;
config->get("deadzone", &m_deadzone);
return DeviceConfig::load(config);

View File

@@ -22,6 +22,7 @@
#include "input/binding.hpp"
#include "input/device_config.hpp"
#include "input/input.hpp"
#include "utils/cpp2011.hpp"
#include "utils/no_copy.hpp"
#include <iosfwd>
@@ -39,11 +40,12 @@ class GamepadConfig : public DeviceConfig
private:
/** Number of axis this device has. */
int m_axis_count;
int m_axis_count;
/** Number of buttons this device has. */
int m_button_count;
int m_button_count;
/** Deadzone of this gamepad. */
int m_deadzone;
public:
@@ -52,11 +54,11 @@ public:
virtual void save(std::ofstream& stream);
void setDefaultBinds ();
GamepadConfig (const XMLNode *config);
GamepadConfig ();
GamepadConfig (const std::string &name,
const int axis_count=0,
const int button_ount=0);
virtual bool load(const XMLNode *config);
virtual bool load(const XMLNode *config) OVERRIDE;
// ------------------------------------------------------------------------
/** Sets the number of buttons this device has. */
void setNumberOfButtons(int count) { m_button_count = count; }
@@ -66,6 +68,9 @@ public:
void setNumberOfAxis(int count) { m_axis_count = count; }
// ~GamepadConfig();
// ------------------------------------------------------------------------
/** Return deadzone of this configuration. */
int getDeadzone() const { return m_deadzone; }
// ------------------------------------------------------------------------
/** Returns the type of this configuration. */
virtual DeviceConfig::DeviceConfigType getType() const

View File

@@ -67,7 +67,14 @@ GamePadDevice::~GamePadDevice()
} // ~GamePadDevice
// ----------------------------------------------------------------------------
/** Returns if the specified value is larger than the deadzone. */
bool GamePadDevice::moved(int value) const
{
int dz = static_cast<GamepadConfig*>(m_configuration)->getDeadzone();
return abs(value) > dz;
} // moved
// ----------------------------------------------------------------------------
bool GamePadDevice::isButtonPressed(const int i)
{
return m_buttonPressed[i];
@@ -177,8 +184,9 @@ bool GamePadDevice::processAndMapInput(Input::InputType type, const int id,
}
}
int dz = static_cast<GamepadConfig*>(m_configuration)->getDeadzone();
// check if within deadzone
if(*value > -m_deadzone && *value < m_deadzone && getPlayer())
if(*value > -dz && *value < dz && getPlayer())
{
// Axis stands still: This is reported once for digital axes and
// can be called multipled times for analog ones. Uses the

View File

@@ -48,9 +48,6 @@ class GamePadDevice : public InputDevice
/** \see m_prev_axis_value */
bool *m_axis_ok;
/** Deadzone for this gamepad. */
int m_deadzone;
/** Irrlicht index of this gamepad. */
int m_irr_index;
@@ -73,16 +70,17 @@ public:
InputManager::InputDriverMode mode,
PlayerAction *action, int* value = NULL
) OVERRIDE;
bool moved(int value) const;
// ------------------------------------------------------------------------
/** Returns the irrlicht index of this gamepad. */
int getIrrIndex() const { return m_irr_index; }
// ------------------------------------------------------------------------
/** Returns the deadzone of this gamepad. */
int getDeadzone() const { return m_deadzone; }
// ------------------------------------------------------------------------
/** Returns the number of buttons of this gamepad. */
int getNumberOfButtons() const { return m_button_count; }
// ------------------------------------------------------------------------
}; // class GamepadDevice
#endif

View File

@@ -639,8 +639,8 @@ void InputManager::dispatchInput(Input::InputType type, int deviceID,
GamePadDevice* gp =
getDeviceManager()->getGamePadFromIrrID(deviceID);
if (gp != NULL &&
abs(value)>gp->getDeadzone())
// Check for deadzone
if (gp != NULL && gp->moved(value))
{
//I18N: message shown when an input device is used but
// is not associated to any player

View File

@@ -28,11 +28,15 @@
using namespace irr;
// KeyboardConfig & GamepadConfig classes really should be in a separate cpp
// file but they are so small that we'll just leave them here for now.
//==== K E Y B O A R D C O N F I G =============================================
KeyboardConfig::KeyboardConfig() : DeviceConfig(DEVICE_CONFIG_TYPE_KEYBOARD)
{
m_name = "Keyboard";
m_plugged = 1;
setDefaultBinds();
} // KeyboardConfig
// ----------------------------------------------------------------------------
void KeyboardConfig::save(std::ofstream& stream)
{
stream << "<keyboard>\n";
@@ -65,10 +69,3 @@ void KeyboardConfig::setDefaultBinds()
//------------------------------------------------------------------------------
KeyboardConfig::KeyboardConfig() : DeviceConfig(DEVICE_CONFIG_TYPE_KEYBOARD)
{
m_name = "Keyboard";
m_plugged = 1;
setDefaultBinds();
}

View File

@@ -28,7 +28,6 @@
#include <irrString.h>
#include <string>
//==== K E Y B O A R D C O N F I G =============================================
/**
* \brief specialisation of DeviceConfig for keyboard type devices