Input System improved to allow for multiple inputs for same bind

Did not work properly between keyboard and controller before.
This commit is contained in:
2014-05-17 00:52:11 +02:00
parent 2a28f65c37
commit f180abcd63
5 changed files with 89 additions and 45 deletions
+64 -14
View File
@@ -52,8 +52,8 @@ bool Systems::InputSystem::OnKeyDown(const Events::KeyDown &event)
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_CommandValues[command] += value;
PublishCommand(0, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f)));
m_CommandKeyboardValues[command][event.KeyCode] = value;
PublishCommand(0, command, GetCommandTotalValue(command));
}
return true;
@@ -67,8 +67,8 @@ bool Systems::InputSystem::OnKeyUp(const Events::KeyUp &event)
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_CommandValues[command] -= value;
PublishCommand(0, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f)));
m_CommandKeyboardValues[command][event.KeyCode] = 0;
PublishCommand(0, command, GetCommandTotalValue(command));;
}
return true;
@@ -79,7 +79,11 @@ bool Systems::InputSystem::OnMousePress(const Events::MousePress &event)
auto bindingIt = m_MouseButtonBindings.find(event.Button);
if (bindingIt != m_MouseButtonBindings.end())
{
PublishCommand(0, bindingIt->second, 1.f);
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_CommandMouseButtonValues[command][event.Button] = value;
PublishCommand(0, command, GetCommandTotalValue(command));
}
return true;
@@ -90,7 +94,11 @@ bool Systems::InputSystem::OnMouseRelease(const Events::MouseRelease &event)
auto bindingIt = m_MouseButtonBindings.find(event.Button);
if (bindingIt != m_MouseButtonBindings.end())
{
PublishCommand(0, bindingIt->second, 1.f);
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_CommandMouseButtonValues[command][event.Button] = 0;
PublishCommand(0, command, GetCommandTotalValue(command));
}
return true;
@@ -104,7 +112,8 @@ bool Systems::InputSystem::OnGamepadAxis(const Events::GamepadAxis &event)
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
PublishCommand(event.GamepadID + 1, command, event.Value * value);
m_CommandGamepadAxisValues[command][event.Axis] = event.Value * value;
PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command));
}
return true;
@@ -118,8 +127,8 @@ bool Systems::InputSystem::OnGamepadButtonDown(const Events::GamepadButtonDown &
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_CommandValues[command] += value;
PublishCommand(event.GamepadID + 1, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f)));
m_CommandGamepadButtonValues[command][event.Button] = value;
PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command));
}
return true;
@@ -133,8 +142,8 @@ bool Systems::InputSystem::OnGamepadButtonUp(const Events::GamepadButtonUp &even
std::string command;
float value;
std::tie(command, value) = bindingIt->second;
m_CommandValues[command] -= value;
PublishCommand(event.GamepadID + 1, command, std::max(-1.f, std::min(m_CommandValues[command], 1.f)));
m_CommandGamepadButtonValues[command][event.Button] = 0;
PublishCommand(event.GamepadID + 1, command, GetCommandTotalValue(command));
}
return true;
@@ -164,7 +173,7 @@ bool Systems::InputSystem::OnBindMouseButton(const Events::BindMouseButton &even
}
else
{
m_MouseButtonBindings[event.Button] = event.Command;
m_MouseButtonBindings[event.Button] = std::make_tuple(event.Command, event.Value);
LOG_DEBUG("Input: Bound mouse button %i to %s", event.Button, event.Command.c_str());
}
@@ -201,6 +210,49 @@ bool Systems::InputSystem::OnBindGamepadButton(const Events::BindGamepadButton &
return true;
}
float Systems::InputSystem::GetCommandTotalValue(std::string command)
{
float value = 0.f;
auto keyboardIt = m_CommandKeyboardValues.find(command);
if (keyboardIt != m_CommandKeyboardValues.end())
{
for (auto &key : keyboardIt->second)
{
value += key.second;
}
}
auto mouseButtonIt = m_CommandMouseButtonValues.find(command);
if (mouseButtonIt != m_CommandMouseButtonValues.end())
{
for (auto &button : mouseButtonIt->second)
{
value += button.second;
}
}
auto gamepadAxisIt = m_CommandGamepadAxisValues.find(command);
if (gamepadAxisIt != m_CommandGamepadAxisValues.end())
{
for (auto &axis : gamepadAxisIt->second)
{
value += axis.second;
}
}
auto gamepadButtonIt = m_CommandGamepadButtonValues.find(command);
if (gamepadButtonIt != m_CommandGamepadButtonValues.end())
{
for (auto &button : gamepadButtonIt->second)
{
value += button.second;
}
}
return std::max(-1.f, std::min(value, 1.f));
}
void Systems::InputSystem::PublishCommand(int playerID, std::string command, float value)
{
Events::InputCommand e;
@@ -211,5 +263,3 @@ void Systems::InputSystem::PublishCommand(int playerID, std::string command, flo
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, playerID);
}