Replaced irrXML usage with out XMLNode.

This commit is contained in:
hiker 2014-10-24 21:47:50 +11:00
parent 2d5af3bee8
commit 6893e94e71
6 changed files with 149 additions and 182 deletions

View File

@ -16,11 +16,16 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <assert.h>
#include "config/device_config.hpp" #include "config/device_config.hpp"
#include "io/xml_node.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include <SKeyMap.h> #include <SKeyMap.h>
#include <assert.h>
using namespace irr; using namespace irr;
//==== D E V I C E C O N F I G ================================================= //==== D E V I C E C O N F I G =================================================
@ -223,39 +228,51 @@ void DeviceConfig::serialize (std::ofstream& stream)
} // serialize } // serialize
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/** Reads a device configuration from input.xml.
bool DeviceConfig::deserializeAction(irr::io::IrrXMLReader* xml) * \param config The XML Node of the configuration.
* \return False if an error occurred.
*/
bool DeviceConfig::load(const XMLNode *config)
{ {
int binding_id = -1; bool error = false;
for(unsigned int i=0; i<config->getNumNodes(); i++)
// Never hurts to check ;)
if (xml == NULL)
{ {
Log::error("DeviceConfig", "Null pointer (DeviceConfig::deserializeAction)"); const XMLNode *action = config->getNode(i);
return false; if(action->getName()!="action")
}
// Read tags from XML
const char *name_string = xml->getAttributeValue("name");
// Try to determine action # for verbose action name
for (int i = 0; i < PA_COUNT; i++)
{
if (strcmp(name_string, KartActionStrings[i].c_str()) == 0)
{ {
binding_id = i; Log::warn("DeviceConfig", "Invalid configuration '%s' - ignored.");
break; continue;
}
std::string name;
action->get("name", &name);
int binding_id = -1;
for (int i = 0; i < PA_COUNT; i++)
{
if (name==KartActionStrings[i])
{
binding_id = i;
break;
}
}
if (binding_id == -1)
{
Log::warn("DeviceConfig",
"DeviceConfig::deserializeAction : action '%s' is unknown.",
name.c_str());
error=true;
} }
}
if(binding_id==-1)
{
Log::warn("DeviceConfig", "DeviceConfig::deserializeAction : action '%s' is unknown.",
name_string);
return false;
}
return m_bindings[binding_id].deserialize(xml); if(!m_bindings[binding_id].load(action))
} // deserializeAction {
Log::error("Device manager",
"Ignoring an ill-formed keyboard action in input config.");
error=true;
}
} // for i in nodes
return !error;
} // load
// ---------------------------------------------------------------------------
// KeyboardConfig & GamepadConfig classes really should be in a separate cpp // 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. // file but they are so small that we'll just leave them here for now.
@ -350,19 +367,13 @@ GamepadConfig::GamepadConfig ( const std::string &name,
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
GamepadConfig::GamepadConfig(irr::io::IrrXMLReader* xml) : DeviceConfig( DEVICE_CONFIG_TYPE_GAMEPAD ) GamepadConfig::GamepadConfig(const XMLNode *config)
: DeviceConfig( DEVICE_CONFIG_TYPE_GAMEPAD )
{ {
const char* name_string = xml->getAttributeValue("name"); if(!config->get("name", &m_name))
if(name_string == NULL)
Log::error("DeviceConfig", "Unnamed joystick in config file."); Log::error("DeviceConfig", "Unnamed joystick in config file.");
else
m_name = name_string;
const char* enabled_string = xml->getAttributeValue("enabled"); config->get("enabled", &m_enabled);
if (enabled_string != NULL)
m_enabled = (strcmp(enabled_string, "true") == 0);
else
m_enabled = true;
m_plugged = 0; m_plugged = 0;
setDefaultBinds(); setDefaultBinds();

View File

@ -86,7 +86,7 @@ public:
irr::core::stringw getMappingIdString (const PlayerAction action) const; irr::core::stringw getMappingIdString (const PlayerAction action) const;
void serialize (std::ofstream& stream); void serialize (std::ofstream& stream);
bool deserializeAction (irr::io::IrrXMLReader* xml); bool load(const XMLNode *config);
void setBinding (const PlayerAction action, void setBinding (const PlayerAction action,
const Input::InputType type, const Input::InputType type,
@ -172,7 +172,7 @@ public:
void serialize (std::ofstream& stream); void serialize (std::ofstream& stream);
void setDefaultBinds (); void setDefaultBinds ();
GamepadConfig (irr::io::IrrXMLReader* xml); GamepadConfig (const XMLNode *config);
GamepadConfig (const std::string &name, GamepadConfig (const std::string &name,
const int axis_count=0, const int axis_count=0,
const int button_ount=0); const int button_ount=0);

View File

@ -16,12 +16,14 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <SKeyMap.h>
#include "input/binding.hpp" #include "input/binding.hpp"
#include "io/xml_node.hpp"
#include "utils/string_utils.hpp" #include "utils/string_utils.hpp"
#include "utils/translation.hpp" #include "utils/translation.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
#include <SKeyMap.h>
/** Convert thjis binding to XML attributes. The full XML node is actually /** Convert thjis binding to XML attributes. The full XML node is actually
* written by device_config, so we only have to add the attributes here. * written by device_config, so we only have to add the attributes here.
*/ */
@ -40,51 +42,37 @@ void Binding::serialize(std::ofstream& stream) const
} // serialize } // serialize
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool Binding::deserialize(irr::io::IrrXMLReader* xml) bool Binding::load(const XMLNode *action)
{ {
const char *id_string = xml->getAttributeValue("id"); int n;
const char *event_string = xml->getAttributeValue("event"); if(!action->get("id", &m_id) || !action->get("event", &n) )
const char *dir_string = xml->getAttributeValue("direction");
const char *range_string = xml->getAttributeValue("range");
const char *character = xml->getAttributeValue("character");
// Proceed only if neccesary tags were found
if ((id_string == NULL) || (event_string == NULL))
{ {
Log::warn("Binding", "No id-string or event-string given - ignored."); Log::warn("Binding", "No id-string or event-string given - ignored.");
return false; return false;
} }
m_type = (Input::InputType)n;
// Convert strings to string tags to integer types core::stringw s;
m_type = (Input::InputType)atoi(event_string); m_character = 0;
m_id = atoi(id_string); action->get("character", &s);
m_character = character ? atoi(character) : 0; if(s.size()>0)
m_character = s[0];
// If the action is not a stick motion (button or key) // If the action is not a stick motion (button or key)
if (m_type == Input::IT_STICKMOTION) if (m_type == Input::IT_STICKMOTION)
{ {
// If the action is a stick motion & a direction is defined if(!action->get("direction", &n))
if (dir_string == NULL)
{ {
Log::warn("Binding", "IT_STICKMOTION without direction, ignoring."); Log::warn("Binding", "IT_STICKMOTION without direction, ignoring.");
return false; return false;
} }
m_dir = (Input::AxisDirection)n;
// If the action is a stick motion & a range is defined
if (range_string == NULL) if(!action->get("range", &n)) m_range = Input::AR_HALF;
{ else m_range = (Input::AxisRange)n;
m_range = Input::AR_HALF;
}
else
{
m_range = (Input::AxisRange)atoi(range_string);
}
m_dir = (Input::AxisDirection)atoi(dir_string);
} // if m_type!=stickmotion } // if m_type!=stickmotion
return true; return true;
} // deserialize } // load
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/** Returns a string representing this binding, which can be displayed on the /** Returns a string representing this binding, which can be displayed on the

View File

@ -20,17 +20,20 @@
#ifndef BINDING_HPP #ifndef BINDING_HPP
#define BINDING_HPP #define BINDING_HPP
#include <fstream>
#include <irrString.h>
#include "input/input.hpp" #include "input/input.hpp"
#include "utils/no_copy.hpp" #include "utils/no_copy.hpp"
#include <irrXML.h>
#include "irrString.h"
#include <fstream>
class XMLNode;
/** /**
* \ingroup config * \ingroup config
*/ */
class Binding class Binding : public NoCopy
{ {
private: private:
Input::InputType m_type; Input::InputType m_type;
@ -62,7 +65,7 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void serialize (std::ofstream& stream) const; void serialize (std::ofstream& stream) const;
bool deserialize(irr::io::IrrXMLReader* xml); bool load(const XMLNode *action);
irr::core::stringw getAsString() const; irr::core::stringw getAsString() const;
}; };
#endif #endif

View File

@ -61,7 +61,7 @@ bool DeviceManager::initialize()
Log::info("-","---------------------------"); Log::info("-","---------------------------");
} }
deserialize(); load();
// Assign a configuration to the keyboard, or create one if we haven't yet // Assign a configuration to the keyboard, or create one if we haven't yet
if(UserConfigParams::logMisc()) Log::info("Device manager","Initializing keyboard support."); if(UserConfigParams::logMisc()) Log::info("Device manager","Initializing keyboard support.");
@ -437,123 +437,88 @@ void DeviceManager::clearLatestUsedDevice()
} // clearLatestUsedDevice } // clearLatestUsedDevice
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool DeviceManager::deserialize() /** Loads the configuration from the user's input.xml file.
*/
bool DeviceManager::load()
{ {
static std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME); std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME);
if(UserConfigParams::logMisc()) if(UserConfigParams::logMisc())
Log::info("Device manager","Deserializing input.xml..."); Log::info("Device manager","Loading input.xml...");
if(!file_manager->fileExists(filepath)) const XMLNode *input = file_manager->createXMLTree(filepath);
if(!input)
{ {
if(UserConfigParams::logMisc()) if(UserConfigParams::logMisc())
Log::warn("Device manager","No configuration file exists."); Log::warn("Device manager","No configuration file exists.");
return false;
} }
else
if(input->getName()!="input")
{ {
irr::io::IrrXMLReader* xml = irr::io::createIrrXMLReader( filepath.c_str() ); Log::warn("DeviceManager", "Invalid input.xml file - no input node.");
return false;
const int GAMEPAD = 1;
const int KEYBOARD = 2;
const int NOTHING = 3;
int reading_now = NOTHING;
KeyboardConfig* keyboard_config = NULL;
GamepadConfig* gamepad_config = NULL;
// parse XML file
while(xml && xml->read())
{
switch(xml->getNodeType())
{
case irr::io::EXN_TEXT:
break;
case irr::io::EXN_ELEMENT:
{
if (strcmp("input", xml->getNodeName()) == 0)
{
const char *version_string = xml->getAttributeValue("version");
if (version_string == NULL || atoi(version_string) != INPUT_FILE_VERSION)
{
//I18N: shown when config file is too old
GUIEngine::showMessage( _("Please re-configure your key bindings.") );
GUIEngine::showMessage( _("Your input config file is not compatible with this version of STK.") );
return false;
}
}
if (strcmp("keyboard", xml->getNodeName()) == 0)
{
keyboard_config = new KeyboardConfig();
reading_now = KEYBOARD;
}
else if (strcmp("gamepad", xml->getNodeName()) == 0)
{
gamepad_config = new GamepadConfig(xml);
reading_now = GAMEPAD;
}
else if (strcmp("action", xml->getNodeName()) == 0)
{
if(reading_now == KEYBOARD)
{
if(keyboard_config != NULL)
if(!keyboard_config->deserializeAction(xml))
Log::error("Device manager","Ignoring an ill-formed keyboard action in input config.");
}
else if(reading_now == GAMEPAD)
{
if(gamepad_config != NULL)
if(!gamepad_config->deserializeAction(xml))
Log::error("Device manager","Ignoring an ill-formed gamepad action in input config.");
}
else Log::warn("Device manager","An action is placed in an unexpected area in the input config file.");
}
}
break;
// ---- section ending
case irr::io::EXN_ELEMENT_END:
{
if (strcmp("keyboard", xml->getNodeName()) == 0)
{
m_keyboard_configs.push_back(keyboard_config);
reading_now = NOTHING;
}
else if (strcmp("gamepad", xml->getNodeName()) == 0)
{
m_gamepad_configs.push_back(gamepad_config);
reading_now = NOTHING;
}
}
break;
default: break;
} // end switch
} // end while
if(UserConfigParams::logMisc())
{
Log::info("Device manager","Found %d keyboard and %d gamepad configurations.",
m_keyboard_configs.size(), m_gamepad_configs.size());
}
// For Debugging....
/*
for (int n = 0; n < m_keyboard_configs.size(); n++)
printf("Config #%d\n%s", n + 1, m_keyboard_configs[n].toString().c_str());
for (int n = 0; n < m_gamepad_configs.size(); n++)
printf("%s", m_gamepad_configs[n].toString().c_str());
*/
delete xml;
} }
int version=0;
if(!input->get("version", &version) || version != INPUT_FILE_VERSION )
{
//I18N: shown when config file is too old
GUIEngine::showMessage(_("Please re-configure your key bindings."));
GUIEngine::showMessage(_("Your input config file is not compatible "
"with this version of STK."));
return false;
}
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
{
Log::warn("DeviceManager",
"Invalid node '%s' in input.xml - ignored.",
config->getName().c_str());
continue;
}
} // for i < getNumNodes
if (UserConfigParams::logMisc())
{
Log::info("Device manager",
"Found %d keyboard and %d gamepad configurations.",
m_keyboard_configs.size(), m_gamepad_configs.size());
}
// For Debugging....
/*
for (int n = 0; n < m_keyboard_configs.size(); n++)
printf("Config #%d\n%s", n + 1, m_keyboard_configs[n].toString().c_str());
for (int n = 0; n < m_gamepad_configs.size(); n++)
printf("%s", m_gamepad_configs[n].toString().c_str());
*/
delete input;
return true; return true;
} // deserialize } // load
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void DeviceManager::serialize() void DeviceManager::serialize()

View File

@ -99,7 +99,7 @@ private:
* pressing fire. */ * pressing fire. */
bool m_map_fire_to_select; bool m_map_fire_to_select;
bool deserialize(); bool load();
void shutdown(); void shutdown();
public: public: