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:
@@ -10,6 +10,7 @@ struct BindMouseButton : Event
|
||||
{
|
||||
int Button;
|
||||
std::string Command;
|
||||
float Value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+16
-27
@@ -13,10 +13,20 @@ void GameWorld::Initialize()
|
||||
BindKey(GLFW_KEY_A, "horizontal", -1.f);
|
||||
BindKey(GLFW_KEY_D, "horizontal", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::LeftX, "horizontal", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::RightTrigger, "vertical", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::LeftTrigger, "vertical", -1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::LeftY, "vertical", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::RightX, "horizontal", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::RightY, "vertical", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::RightTrigger, "normal", 1.f);
|
||||
BindGamepadAxis(Gamepad::Axis::LeftTrigger, "normal", -1.f);
|
||||
BindKey(GLFW_KEY_SPACE, "normal", 1.f);
|
||||
BindKey(GLFW_KEY_LEFT_CONTROL, "normal", -1.f);
|
||||
|
||||
BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f);
|
||||
BindKey(GLFW_KEY_LEFT_SHIFT, "speed", 1.f);
|
||||
BindKey(GLFW_KEY_LEFT_ALT, "speed", -1.f);
|
||||
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_1, "attack", 1.f);
|
||||
|
||||
/*BindKey(GLFW_KEY_UP, "barrel_rotation", 1.f);
|
||||
BindKey(GLFW_KEY_DOWN, "barrel_rotation", -1.f);
|
||||
BindKey(GLFW_KEY_LEFT, "tower_rotation", -1.f);
|
||||
BindKey(GLFW_KEY_RIGHT, "tower_rotation", 1.f);
|
||||
@@ -26,7 +36,7 @@ void GameWorld::Initialize()
|
||||
BindKey(GLFW_KEY_SPACE, "handbrake", 1.f);
|
||||
BindGamepadButton(Gamepad::Button::A, "handbrake", 1.f);
|
||||
|
||||
BindKey(GLFW_KEY_Z, "shoot", 1.f);
|
||||
BindKey(GLFW_KEY_Z, "shoot", 1.f);*/
|
||||
|
||||
//BindGamepadButton(Gamepad::Button::Up, "Gamepad::Button::Up", 1.f);
|
||||
//BindGamepadButton(Gamepad::Button::Down, "Gamepad::Button::Down", 1.f);
|
||||
@@ -42,28 +52,6 @@ void GameWorld::Initialize()
|
||||
//BindGamepadButton(Gamepad::Button::B, "Gamepad::Button::B", 1.f);
|
||||
//BindGamepadButton(Gamepad::Button::X, "Gamepad::Button::X", 1.f);
|
||||
//BindGamepadButton(Gamepad::Button::Y, "Gamepad::Button::Y", 1.f);
|
||||
//
|
||||
// BindKey(GLFW_KEY_UP, "vertical", -1.f);
|
||||
// BindKey(GLFW_KEY_DOWN, "vertical", 1.f);
|
||||
// BindKey(GLFW_KEY_LEFT, "horizontal", -1.f);
|
||||
// BindKey(GLFW_KEY_RIGHT, "horizontal", 1.f);
|
||||
/*
|
||||
BindKey(GLFW_KEY_Q, "+tower_right");
|
||||
BindKey(GLFW_KEY_E, "+tower_left");
|
||||
|
||||
BindKey(GLFW_KEY_Q, "+up");
|
||||
BindKey(GLFW_KEY_LEFT_CONTROL, "+down");
|
||||
BindKey(GLFW_KEY_LEFT_ALT, "+slow");
|
||||
BindKey(GLFW_KEY_LEFT_SHIFT, "+fast");
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack");
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2");
|
||||
BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3");
|
||||
|
||||
|
||||
BindKey(GLFW_KEY_UP, "+cam_forward");
|
||||
BindKey(GLFW_KEY_DOWN, "+cam_backward");
|
||||
BindKey(GLFW_KEY_LEFT, "+cam_left");
|
||||
BindKey(GLFW_KEY_RIGHT, "+cam_right");*/
|
||||
|
||||
RegisterComponents();
|
||||
|
||||
@@ -793,11 +781,12 @@ void GameWorld::BindKey(int keyCode, std::string command, float value)
|
||||
m_EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
void GameWorld::BindMouseButton(int button, std::string command)
|
||||
void GameWorld::BindMouseButton(int button, std::string command, float value)
|
||||
{
|
||||
Events::BindMouseButton e;
|
||||
e.Button = button;
|
||||
e.Command = command;
|
||||
e.Value = value;
|
||||
m_EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ private:
|
||||
std::shared_ptr<Renderer> m_Renderer;
|
||||
|
||||
void BindKey(int keyCode, std::string command, float value);
|
||||
void BindMouseButton(int button, std::string command);
|
||||
void BindMouseButton(int button, std::string command, float value);
|
||||
void BindGamepadAxis(Gamepad::Axis axis, std::string command, float value);
|
||||
void BindGamepadButton(Gamepad::Button button, std::string command, float value);
|
||||
};
|
||||
|
||||
+64
-14
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -34,10 +34,13 @@ public:
|
||||
void Update(double dt) override;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, float> m_CommandValues; // command string -> command current value
|
||||
std::unordered_map<std::string, std::unordered_map<int, float>> m_CommandKeyboardValues; // command string -> keyboard key value for command
|
||||
std::unordered_map<std::string, std::unordered_map<int, float>> m_CommandMouseButtonValues; // command string -> mouse button value for command
|
||||
std::unordered_map<std::string, std::unordered_map<Gamepad::Axis, float>> m_CommandGamepadAxisValues; // command string -> gamepad axis value for command
|
||||
std::unordered_map<std::string, std::unordered_map<Gamepad::Button, float>> m_CommandGamepadButtonValues; // command string -> gamepad button value for command
|
||||
// Input binding tables
|
||||
std::unordered_map<int, std::tuple<std::string, float>> m_KeyBindings; // GLFW_KEY... -> command string & value
|
||||
std::unordered_map<int, std::string> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
|
||||
std::unordered_map<int, std::tuple<std::string, float>> m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string
|
||||
std::unordered_map<Gamepad::Axis, std::tuple<std::string, float>> m_GamepadAxisBindings; // Gamepad::Axis -> command string & value
|
||||
std::unordered_map<Gamepad::Button, std::tuple<std::string, float>> m_GamepadButtonBindings; // Gamepad::Button -> command string
|
||||
|
||||
@@ -66,6 +69,7 @@ private:
|
||||
EventRelay<Events::BindGamepadButton> m_EBindGamepadButton;
|
||||
bool OnBindGamepadButton(const Events::BindGamepadButton &event);
|
||||
|
||||
float GetCommandTotalValue(std::string command);
|
||||
void PublishCommand(int playerID, std::string command, float value);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user