more input code clean-up

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3823 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
rforder 2009-08-09 04:12:04 +00:00
parent 21fb622a6d
commit cf8e767f5c
5 changed files with 177 additions and 155 deletions

View File

@ -30,7 +30,6 @@ std::string DeviceConfig::toString ()
m_bindings[n].dir)); m_bindings[n].dir));
returnString = returnString.append("\n"); returnString = returnString.append("\n");
} }
returnString = returnString.append("\n");
return returnString; return returnString;
} }

View File

@ -13,20 +13,38 @@
DeviceManager::DeviceManager() DeviceManager::DeviceManager()
{ {
m_latest_used_device = NULL; m_latest_used_device = NULL;
m_keyboard = NULL;
m_assign_mode = NO_ASSIGN; m_assign_mode = NO_ASSIGN;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
bool DeviceManager::initGamePadSupport() bool DeviceManager::initialize()
{ {
GamepadConfig *gamepadConfig = NULL; GamepadConfig *gamepadConfig = NULL;
GamePadDevice *gamepadDevice = NULL; GamePadDevice *gamepadDevice = NULL;
bool created = false; bool created = false;
int numGamepads; int numGamepads;
printf("================================================================================\n");
printf("Initializing Gamepad Support\n"); // Shutdown in case the device manager is being re-initialized
printf("================================================================================\n\n"); shutdown();
printf("Initializing Device Manager\n");
printf("---------------------------\n");
deserialize();
// Assign a configuration to the keyboard, or create one if we haven't yet
printf("Initializing keyboard support.\n");
if (m_keyboard_configs.size() == 0)
{
printf("No keyboard configuration exists, creating one.\n");
m_keyboard_configs.push_back(new KeyboardConfig());
created = true;
}
m_keyboard = new KeyboardDevice(m_keyboard_configs.get(0));
printf("Initializing gamepad support.\n");
irr_driver->getDevice()->activateJoysticks(m_irrlicht_gamepads); irr_driver->getDevice()->activateJoysticks(m_irrlicht_gamepads);
numGamepads = m_irrlicht_gamepads.size(); numGamepads = m_irrlicht_gamepads.size();
@ -36,7 +54,7 @@ bool DeviceManager::initGamePadSupport()
{ {
printf("#%d: %s detected...", id, m_irrlicht_gamepads[id].Name.c_str()); printf("#%d: %s detected...", id, m_irrlicht_gamepads[id].Name.c_str());
// Returns true if new configuration was created // Returns true if new configuration was created
if (getGamepadConfig(id, &gamepadConfig) == true) if (getConfigForGamepad(id, &gamepadConfig) == true)
{ {
printf("creating new configuration.\n"); printf("creating new configuration.\n");
created = true; created = true;
@ -51,12 +69,10 @@ bool DeviceManager::initGamePadSupport()
m_irrlicht_gamepads[id].Axes, m_irrlicht_gamepads[id].Axes,
m_irrlicht_gamepads[id].Buttons, m_irrlicht_gamepads[id].Buttons,
gamepadConfig ); gamepadConfig );
addGamepad(gamepadDevice); addGamepad(gamepadDevice);
} // end for } // end for
printf("Gamepad support initialization complete.\n\n");
if (created) serialize();
return created; return created;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -95,7 +111,7 @@ GamePadDevice* DeviceManager::getGamePadFromIrrID(const int id)
* Check if we already have a config object for gamepad 'irr_id' as reported by irrLicht * Check if we already have a config object for gamepad 'irr_id' as reported by irrLicht
* If no, create one. Returns true if new configuration was created, otherwise false. * If no, create one. Returns true if new configuration was created, otherwise false.
*/ */
bool DeviceManager::getGamepadConfig(const int irr_id, GamepadConfig **config) bool DeviceManager::getConfigForGamepad(const int irr_id, GamepadConfig **config)
{ {
bool found = false; bool found = false;
bool configCreated = false; bool configCreated = false;
@ -136,6 +152,7 @@ void DeviceManager::addGamepad(GamePadDevice* d)
m_gamepads.push_back(d); m_gamepads.push_back(d);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
InputDevice *DeviceManager::mapKeyboardInput( int deviceID, InputDevice *DeviceManager::mapKeyboardInput( int deviceID,
int btnID, int btnID,
const bool progGen, const bool progGen,
@ -158,6 +175,7 @@ InputDevice *DeviceManager::mapKeyboardInput( int deviceID,
return device; return device;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
InputDevice *DeviceManager::mapGamepadInput( Input::InputType type, InputDevice *DeviceManager::mapGamepadInput( Input::InputType type,
int deviceID, int deviceID,
int btnID, int btnID,
@ -169,17 +187,23 @@ InputDevice *DeviceManager::mapGamepadInput( Input::InputType type,
{ {
GamePadDevice *gPad = getGamePadFromIrrID(deviceID); GamePadDevice *gPad = getGamePadFromIrrID(deviceID);
if (gPad->hasBinding(type, btnID, value, NULL, action)) if (gPad != NULL)
{ {
if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode if (gPad->hasBinding(type, btnID, value, NULL, action))
{ {
*player = NULL; if (m_assign_mode == NO_ASSIGN) // Don't set the player in NO_ASSIGN mode
// IT_STICKMOTION happens all the time, don't consider it discrete input {
if ((!progGen) && (type == Input::IT_STICKBUTTON)) m_latest_used_device = gPad; *player = NULL;
// IT_STICKMOTION happens all the time, don't consider it discrete input
if ((!progGen) && (type == Input::IT_STICKBUTTON)) m_latest_used_device = gPad;
}
else
{
*player = gPad->m_player;
}
} }
else *player = gPad->m_player; else gPad = NULL; // If no bind was found, return NULL
} }
else gPad = NULL; // If no bind was found, return NULL
return gPad; return gPad;
} }
@ -218,11 +242,15 @@ bool DeviceManager::translateInput( Input::InputType type,
if (device != NULL) if (device != NULL)
{ {
// Handle internal events // Handle internal events
/* FIXME: only call when in kart selection screen
if ((*player != NULL) && (*action == PA_RESCUE)) if ((*player != NULL) && (*action == PA_RESCUE))
{ {
KartSelectionScreen::playerPressedRescue( *player ); KartSelectionScreen::playerPressedRescue( *player );
*action = PA_FIRST; // FIXME: action set to PA_FIRST if handled internally (too hackish) *action = PA_FIRST; // FIXME: action set to PA_FIRST if handled internally (too hackish)
} }
*/
if ((*player == NULL) && (*action == PA_FIRE) && (m_assign_mode == DETECT_NEW)) if ((*player == NULL) && (*action == PA_FIRE) && (m_assign_mode == DETECT_NEW))
{ {
@ -231,7 +259,7 @@ bool DeviceManager::translateInput( Input::InputType type,
} }
} }
// Return true if a matching device was found // Return true if input was successfully translated to an action and player
return (device != NULL); return (device != NULL);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -251,98 +279,95 @@ bool DeviceManager::deserialize()
{ {
static std::string filepath = file_manager->getHomeDir() + "/input.config"; static std::string filepath = file_manager->getHomeDir() + "/input.config";
if(!file_manager->fileExists(filepath)) return false; printf("Deserializing input.config...\n");
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;
KeyboardConfig* keyboard_config = NULL;
GamepadConfig* gamepad_config = NULL;
printf("================================================================================\n");
printf("Deserializing input.config\n");
printf("================================================================================\n\n");
// parse XML file if(!file_manager->fileExists(filepath))
while(xml && xml->read())
{ {
switch(xml->getNodeType()) printf("Warning: no configuration file exists.\n");
{
case irr::io::EXN_TEXT:
break;
case irr::io::EXN_ELEMENT:
{
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))
std::cerr << "Ignoring an ill-formed action in input config.\n";
}
else if(reading_now == GAMEPAD)
{
if(gamepad_config != NULL)
if(!gamepad_config->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)
{
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
// For Debugging....
printf("Keyboard Configs:\n");
for (int n = 0; n < m_keyboard_configs.size(); n++)
printf("%s\n", m_keyboard_configs[n].toString().c_str());
printf("Gamepad Configs:\n");
for (int n = 0; n < m_gamepad_configs.size(); n++)
printf("%s\n", m_gamepad_configs[n].toString().c_str());
if (m_keyboard_configs.size() == 0)
{
printf("No keyboard configuration exists, creating one.\n");
m_keyboard_configs.push_back(new KeyboardConfig());
} }
else
{
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;
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("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))
std::cerr << "Ignoring an ill-formed action in input config.\n";
}
else if(reading_now == GAMEPAD)
{
if(gamepad_config != NULL)
if(!gamepad_config->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)
{
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
m_keyboard = new KeyboardDevice(m_keyboard_configs.get(0)); printf("Found %d keyboard and %d gamepad configurations.\n", m_keyboard_configs.size(), m_gamepad_configs.size());
printf("Deserialization completed.\n\n"); // 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());
*/
}
return true; return true;
} }
@ -351,7 +376,7 @@ void DeviceManager::serialize()
{ {
static std::string filepath = file_manager->getHomeDir() + "/input.config"; static std::string filepath = file_manager->getHomeDir() + "/input.config";
user_config->CheckAndCreateDir(); user_config->CheckAndCreateDir();
printf("Saving Gamepad & Keyboard Configuration\n"); printf("Serializing input.config...\n");
std::ofstream configfile; std::ofstream configfile;
@ -376,6 +401,23 @@ void DeviceManager::serialize()
} }
configfile << "</input>\n"; configfile << "</input>\n";
configfile.close();
configfile.close(); printf("Serialization complete.\n\n");
} }
// -----------------------------------------------------------------------------
void DeviceManager::shutdown()
{
m_gamepads.clearAndDeleteAll();
m_gamepad_configs.clearAndDeleteAll();
m_keyboard_configs.clearAndDeleteAll();
m_latest_used_device = NULL;
if (m_keyboard != NULL)
{
delete m_keyboard;
m_keyboard = NULL;
}
}

View File

@ -15,6 +15,7 @@ enum PlayerAssignMode
class DeviceManager class DeviceManager
{ {
private: private:
KeyboardDevice *m_keyboard; KeyboardDevice *m_keyboard;
ptr_vector<GamePadDevice, HOLD> m_gamepads; ptr_vector<GamePadDevice, HOLD> m_gamepads;
ptr_vector<KeyboardConfig, HOLD> m_keyboard_configs; ptr_vector<KeyboardConfig, HOLD> m_keyboard_configs;
@ -39,8 +40,19 @@ private:
ActivePlayer **player, ActivePlayer **player,
PlayerAction *action ); PlayerAction *action );
bool deserialize();
void shutdown();
public: public:
/**
* The device manager starts in "no-assign" mode, which means no input configuration is associated
* to any player. So all devices will react. This is used in menus before player set-up is done.
* Switching back to no-assign mode will also clear anything in devices that was associated with
* players in assign mode.
*/
DeviceManager(); DeviceManager();
void clearGamepads() { m_gamepads.clearAndDeleteAll(); } void clearGamepads() { m_gamepads.clearAndDeleteAll(); }
@ -49,21 +61,14 @@ public:
void addGamepad(GamePadDevice* d); void addGamepad(GamePadDevice* d);
int getGamePadAmount() const { return m_gamepads.size(); } int getGamePadAmount() const { return m_gamepads.size(); }
GamePadDevice* getGamePad(const int i) { return m_gamepads.get(i); } GamePadDevice* getGamePad(const int i) { return m_gamepads.get(i); }
GamePadDevice* getGamePadFromIrrID(const int i); PlayerAssignMode playerAssignMode() const { return m_assign_mode; }
InputDevice* getLatestUsedDevice(); KeyboardDevice* getKeyboard(const int i) { return m_keyboard; }
GamePadDevice* getGamePadFromIrrID(const int i);
/** InputDevice* getLatestUsedDevice();
* The device manager starts in "no-assign" mode, which means no input configuration is associated void setAssignMode(const PlayerAssignMode assignMode);
* to any player. So all devices will react. This is used in menus before player set-up is done. bool getConfigForGamepad(const int sdl_id, GamepadConfig **config);
* Switching back to no-assign mode will also clear anything in devices that was associated with
* players in assign mode.
*/
PlayerAssignMode playerAssignMode() const { return m_assign_mode; }
void setAssignMode(const PlayerAssignMode assignMode);
KeyboardDevice* getKeyboard(const int i) { return m_keyboard; }
/** Given some input, finds to which device it belongs and, using the corresponding device object, /** Given some input, finds to which device it belongs and, using the corresponding device object,
* maps this input to the corresponding player and game action. returns false if player/action could not be set. * maps this input to the corresponding player and game action. returns false if player/action could not be set.
* Special case : can return true but set action to PA_FIRST if the input was used but is not associated to an * Special case : can return true but set action to PA_FIRST if the input was used but is not associated to an
@ -78,14 +83,10 @@ public:
const bool programaticallyGenerated, const bool programaticallyGenerated,
ActivePlayer** player /* out */, ActivePlayer** player /* out */,
PlayerAction* action /* out */ ); PlayerAction* action /* out */ );
void serialize();
bool deserialize();
/* returns whether a new gamepad was detected */
bool initGamePadSupport();
bool getGamepadConfig(const int sdl_id, GamepadConfig **config); /* returns whether a new gamepad was detected */
bool initialize();
void serialize();
}; };

View File

@ -26,12 +26,14 @@ KeyboardDevice::KeyboardDevice(KeyboardConfig *configuration)
{ {
m_configuration = configuration; m_configuration = configuration;
m_type = DT_KEYBOARD; m_type = DT_KEYBOARD;
m_player = NULL;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
KeyboardDevice::KeyboardDevice() KeyboardDevice::KeyboardDevice()
{ {
m_configuration = new KeyboardConfig(); m_configuration = new KeyboardConfig();
m_type = DT_KEYBOARD; m_type = DT_KEYBOARD;
m_player = NULL;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -52,32 +52,11 @@ InputManager::InputManager() : m_sensed_input(0), m_mode(BOOTSTRAP),
m_mouse_val_x(0), m_mouse_val_y(0) m_mouse_val_x(0), m_mouse_val_y(0)
{ {
m_device_manager = new DeviceManager(); m_device_manager = new DeviceManager();
m_device_manager->deserialize(); m_device_manager->initialize();
m_timer_in_use = false; m_timer_in_use = false;
m_timer = 0; m_timer = 0;
bool something_new_to_write = false;
/*
if(!m_device_manager->deserialize())
{
std::cerr << "Failed to read input config file, using defaults\n";
// could not read config file so use defaults
KeyboardDevice* default_device = new KeyboardDevice();
something_new_to_write = true;
}
*/
if(m_device_manager->initGamePadSupport() /* returns whether a new gamepad was detected */)
{
something_new_to_write = true;
}
// write config file if necessary
if(something_new_to_write) m_device_manager->serialize();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void InputManager::update(float dt) void InputManager::update(float dt)
@ -259,7 +238,6 @@ void InputManager::input(Input::InputType type, int deviceID, int btnID, int axi
{ {
ActivePlayer* player = NULL; ActivePlayer* player = NULL;
PlayerAction action; PlayerAction action;
bool btnAlreadyPressed = false;
bool action_found = m_device_manager->translateInput( type, deviceID, btnID, axisDirection, bool action_found = m_device_manager->translateInput( type, deviceID, btnID, axisDirection,
value, programaticallyGenerated, &player, &action ); value, programaticallyGenerated, &player, &action );