keyboards will now read their config from file (only default keyboard, support for multiple configs to come later)

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3315 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2009-03-30 00:19:24 +00:00
parent 51231429e1
commit 06cfbc2aeb
6 changed files with 152 additions and 15 deletions

View File

@ -28,7 +28,7 @@ bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int id0, i
{
// TODO - auto-detect player ID from device
*player = 0;
if(type == Input::IT_KEYBOARD)
{
for(unsigned int n=0; n<m_keyboard_amount; n++)
@ -63,6 +63,92 @@ bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int id0, i
return false;
}
bool DeviceManager::deserialize()
{
static std::string filepath = file_manager->getHomeDir() + "/input.config";
if(!file_manager->fileExists(filepath)) return false;
irr::io::IrrXMLReader* xml = irr::io::createIrrXMLReader( filepath.c_str() );
const int GAMEPAD = 1;
const int KEYBOARD = 2;
const int NOTHING = 3;
int reading_now = NOTHING;
KeyboardDevice* keyboard_device;
GamePadDevice* gamepad_device;
// parse XML file
while(xml && xml->read())
{
switch(xml->getNodeType())
{
case irr::io::EXN_TEXT:
break;
case irr::io::EXN_ELEMENT:
{
if (strcmp("keyboard", xml->getNodeName()) == 0)
{
keyboard_device = new KeyboardDevice();
// TODO - read name and owner attributes
reading_now = KEYBOARD;
}
else if (strcmp("gamepad", xml->getNodeName()) == 0)
{
// TODO
//gamepad_device = new GamePadDevice();
// TODO - read name and owner attributes
reading_now = GAMEPAD;
}
else if (strcmp("action", xml->getNodeName()) == 0)
{
if(reading_now == KEYBOARD)
{
if(!keyboard_device->deserializeAction(xml))
std::cerr << "Ignoring an ill-formed action in input config\n";
}
else if(reading_now == GAMEPAD)
{
// TODO
//if(!gamepad_device->deserializeAction(xml))
// std::cerr << "Ignoring an ill-formed action in input config\n";
}
else std::cerr << "Warning: An action is placed in an unexpected area in the input config file\n";
}
}
break;
// ---- section ending
case irr::io::EXN_ELEMENT_END:
{
if (strcmp("keyboard", xml->getNodeName()) == 0)
{
add(keyboard_device);
reading_now = NOTHING;
}
else if (strcmp("gamepad", xml->getNodeName()) == 0)
{
// TODO
// add(gamepad_device);
reading_now = GAMEPAD;
}
}
break;
default: break;
} // end switch
} // end while
return true;
}
void DeviceManager::serialize()
{
static std::string filepath = file_manager->getHomeDir() + "/input.config";
@ -76,7 +162,7 @@ void DeviceManager::serialize()
return;
}
for(unsigned int n=0; n<m_keyboard_amount; n++)
{
m_keyboards[n].serialize(configfile);

View File

@ -24,7 +24,7 @@ public:
int* player /* out */, PlayerAction* action /* out */ );
void serialize();
bool deserialize();
};

View File

@ -19,10 +19,7 @@ void InputDevice::serialize(std::ofstream& stream)
{
if (m_type == DT_KEYBOARD) stream << "<keyboard ";
else if (m_type == DT_GAMEPAD) stream << "<gamepad name=\"" << m_name << "\" ";
else
{
std::cerr << "Warning, unknown input device type, skipping it\n";
}
else std::cerr << "Warning, unknown input device type, skipping it\n";
stream << "owner=\"" << m_player << "\">\n\n";
@ -40,6 +37,54 @@ void InputDevice::serialize(std::ofstream& stream)
else if (m_type == DT_GAMEPAD) stream << "\n</gamepad>\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; n<PA_COUNT; n++)
{
if(strcmp(name_string,KartActionStrings[n].c_str()) == 0)
{
binding_id = n;
break;
}
}
if(binding_id == -1)
{
std::cerr << "Unknown action type : " << name_string << std::endl;
return false;
}
// ---- read id
const char* id_string = xml->getAttributeValue("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_bindings[binding_id].id = id;
m_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_bindings[binding_id].dir = (Input::AxisDirection)dir;
}
return true;
}
// -----------------------------------------------------------------------------
KeyboardDevice::KeyboardDevice()
{
m_type = DT_KEYBOARD;

View File

@ -6,6 +6,8 @@
#include "input/input.hpp"
#include <iostream>
#include <fstream>
#include "io/xml_reader.hpp"
#include "io/xml_node.hpp"
enum DeviceType
{
@ -38,6 +40,7 @@ public:
DeviceType getType() const { return m_type; };
void serialize(std::ofstream& stream);
bool deserializeAction(irr::io::IrrXMLReader* xml);
};
class KeyboardDevice : public InputDevice

View File

@ -57,18 +57,19 @@ m_mode(BOOTSTRAP), m_mouse_val_x(0), m_mouse_val_y(0)
m_device_manager = new DeviceManager();
// init keyboard. TODO - load key bindings from file
KeyboardDevice* default_device = new KeyboardDevice();
default_device->loadDefaults();
m_device_manager->add( default_device );
if(!m_device_manager->deserialize())
{
// could not read config file so use defaults
KeyboardDevice* default_device = new KeyboardDevice();
default_device->loadDefaults();
m_device_manager->add( default_device );
m_device_manager->serialize();
}
initGamePadDevices();
// FIXME - for testing purposes only
m_device_manager->serialize();
}
// -----------------------------------------------------------------------------
/** Initialises joystick/gamepad info.
*/

View File

@ -81,6 +81,8 @@ public:
bool is_full_path=false,
bool make_full_path=false) const;
bool fileExists (const std::string& path) { return m_file_system->existFile(path.c_str()); }
void pushTextureSearchPath(const std::string& path);
void pushModelSearchPath (const std::string& path);
void pushMusicSearchPath (const std::string& path)