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,24 +228,27 @@ 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")
{
Log::warn("DeviceConfig", "Invalid configuration '%s' - ignored.");
continue;
} }
std::string name;
// Read tags from XML action->get("name", &name);
const char *name_string = xml->getAttributeValue("name"); int binding_id = -1;
// Try to determine action # for verbose action name
for (int i = 0; i < PA_COUNT; i++) for (int i = 0; i < PA_COUNT; i++)
{ {
if (strcmp(name_string, KartActionStrings[i].c_str()) == 0) if (name==KartActionStrings[i])
{ {
binding_id = i; binding_id = i;
break; break;
@ -248,14 +256,23 @@ bool DeviceConfig::deserializeAction(irr::io::IrrXMLReader* xml)
} }
if (binding_id == -1) if (binding_id == -1)
{ {
Log::warn("DeviceConfig", "DeviceConfig::deserializeAction : action '%s' is unknown.", Log::warn("DeviceConfig",
name_string); "DeviceConfig::deserializeAction : action '%s' is unknown.",
return false; name.c_str());
error=true;
} }
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(!action->get("range", &n)) m_range = Input::AR_HALF;
if (range_string == NULL) 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,106 +437,72 @@ 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; int version=0;
const int KEYBOARD = 2; if(!input->get("version", &version) || version != INPUT_FILE_VERSION )
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 //I18N: shown when config file is too old
GUIEngine::showMessage(_("Please re-configure your key bindings.")); GUIEngine::showMessage(_("Please re-configure your key bindings."));
GUIEngine::showMessage(_("Your input config file is not compatible "
GUIEngine::showMessage( _("Your input config file is not compatible with this version of STK.") ); "with this version of STK."));
return false; return false;
} }
}
if (strcmp("keyboard", xml->getNodeName()) == 0) for(unsigned int i=0; i<input->getNumNodes(); i++)
{ {
keyboard_config = new KeyboardConfig(); const XMLNode *config = input->getNode(i);
reading_now = KEYBOARD; if(config->getName()=="keyboard")
}
else if (strcmp("gamepad", xml->getNodeName()) == 0)
{ {
gamepad_config = new GamepadConfig(xml); KeyboardConfig* keyboard_config = new KeyboardConfig();
reading_now = GAMEPAD; if(!keyboard_config->load(config))
}
else if (strcmp("action", xml->getNodeName()) == 0)
{ {
if(reading_now == KEYBOARD) Log::error("Device manager",
{ "Ignoring an ill-formed keyboard action in input config.");
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); m_keyboard_configs.push_back(keyboard_config);
reading_now = NOTHING;
} }
else if (strcmp("gamepad", xml->getNodeName()) == 0) else if (config->getName()=="gamepad")
{ {
GamepadConfig* gamepad_config = new GamepadConfig(config);
gamepad_config->load(config);
m_gamepad_configs.push_back(gamepad_config); m_gamepad_configs.push_back(gamepad_config);
reading_now = NOTHING;
} }
else
{
Log::warn("DeviceManager",
"Invalid node '%s' in input.xml - ignored.",
config->getName().c_str());
continue;
} }
break; } // for i < getNumNodes
default: break;
} // end switch
} // end while
if (UserConfigParams::logMisc()) if (UserConfigParams::logMisc())
{ {
Log::info("Device manager","Found %d keyboard and %d gamepad configurations.", Log::info("Device manager",
"Found %d keyboard and %d gamepad configurations.",
m_keyboard_configs.size(), m_gamepad_configs.size()); m_keyboard_configs.size(), m_gamepad_configs.size());
} }
@ -549,11 +515,10 @@ bool DeviceManager::deserialize()
printf("%s", m_gamepad_configs[n].toString().c_str()); printf("%s", m_gamepad_configs[n].toString().c_str());
*/ */
delete xml; 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: