2009-03-23 14:38:06 -04:00
|
|
|
|
|
|
|
#include "input/device_manager.hpp"
|
2009-03-29 19:37:21 -04:00
|
|
|
#include "io/file_manager.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
2009-03-23 14:38:06 -04:00
|
|
|
|
|
|
|
DeviceManager::DeviceManager()
|
|
|
|
{
|
|
|
|
m_keyboard_amount = 0;
|
|
|
|
m_gamepad_amount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::add(KeyboardDevice* d)
|
|
|
|
{
|
|
|
|
m_keyboards.push_back(d);
|
|
|
|
m_keyboard_amount = m_keyboards.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::add(GamePadDevice* d)
|
|
|
|
{
|
|
|
|
m_gamepads.push_back(d);
|
|
|
|
m_gamepad_amount = m_gamepads.size();
|
|
|
|
}
|
|
|
|
|
2009-03-23 15:09:16 -04:00
|
|
|
bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int id0, int id1, int id2, int value,
|
2009-03-23 14:38:06 -04:00
|
|
|
int* player /* out */, PlayerAction* action /* out */ )
|
|
|
|
{
|
|
|
|
// TODO - auto-detect player ID from device
|
|
|
|
*player = 0;
|
|
|
|
|
|
|
|
if(type == Input::IT_KEYBOARD)
|
|
|
|
{
|
|
|
|
for(unsigned int n=0; n<m_keyboard_amount; n++)
|
|
|
|
{
|
2009-03-29 11:51:55 -04:00
|
|
|
if( m_keyboards[n].hasBinding(id0, action) ) return true;
|
2009-03-23 14:38:06 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-23 14:46:35 -04:00
|
|
|
else if(type == Input::IT_MOUSEBUTTON)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(type == Input::IT_STICKBUTTON)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(type == Input::IT_STICKMOTION)
|
|
|
|
{
|
2009-03-29 12:15:17 -04:00
|
|
|
std::cout << "stick motion, ID=" <<id0 << " axis=" << id1 << " value=" << value << std::endl;
|
2009-03-23 15:09:16 -04:00
|
|
|
for(unsigned int n=0; n<m_gamepad_amount; n++)
|
|
|
|
{
|
2009-03-29 12:15:17 -04:00
|
|
|
if( /*m_gamepads[n].m_index == id0 &&*/ m_gamepads[n].hasBinding(id1 /* axis */, value, *player, action /* out */) )
|
|
|
|
return true;
|
2009-03-23 15:09:16 -04:00
|
|
|
}
|
2009-03-23 14:46:35 -04:00
|
|
|
return false;
|
|
|
|
}
|
2009-03-23 14:38:06 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-23 14:46:35 -04:00
|
|
|
|
|
|
|
return false;
|
2009-03-29 19:37:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::serialize()
|
|
|
|
{
|
|
|
|
static std::string filepath = file_manager->getHomeDir() + "/input.config";
|
|
|
|
|
|
|
|
std::ofstream configfile;
|
|
|
|
configfile.open (filepath.c_str());
|
|
|
|
|
|
|
|
if(!configfile.is_open())
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to open " << filepath.c_str() << " for writing, controls won't be saved\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for(unsigned int n=0; n<m_keyboard_amount; n++)
|
|
|
|
{
|
|
|
|
m_keyboards[n].serialize(configfile);
|
|
|
|
}
|
|
|
|
for(unsigned int n=0; n<m_gamepad_amount; n++)
|
|
|
|
{
|
|
|
|
m_gamepads[n].serialize(configfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
configfile.close();
|
|
|
|
}
|