engine/src/input_manager.cpp

237 lines
6.1 KiB
C++
Raw Normal View History

2022-11-29 14:22:03 +00:00
#include "input_manager.hpp"
2022-09-02 11:06:59 +00:00
#include "window.hpp"
#include <string>
#include <stdexcept>
2022-10-02 15:34:51 +00:00
namespace engine {
2022-09-02 11:06:59 +00:00
2022-11-29 14:22:03 +00:00
InputManager::InputManager(const Window* win) : m_win(win)
2022-10-02 15:34:51 +00:00
{
m_enabledDevices.fill(true);
}
2022-11-29 14:22:03 +00:00
InputManager::~InputManager()
2022-10-02 15:34:51 +00:00
{
}
2022-09-02 11:06:59 +00:00
2022-10-02 15:34:51 +00:00
// private methods
2022-09-02 11:06:59 +00:00
2022-11-29 14:22:03 +00:00
float InputManager::getDeviceAxis(enum InputDevice device, int axis) const
2022-10-02 15:34:51 +00:00
{
switch (device) {
2022-09-02 11:06:59 +00:00
case InputDevice::MOUSE:
switch (static_cast<inputs::MouseAxis>(axis)) {
2022-10-02 15:34:51 +00:00
case inputs::MouseAxis::X:
2022-11-29 14:22:03 +00:00
return static_cast<float>(m_win->getMouseDX());
2022-10-02 15:34:51 +00:00
case inputs::MouseAxis::Y:
2022-11-29 14:22:03 +00:00
return static_cast<float>(m_win->getMouseDY());
2022-10-02 15:34:51 +00:00
case inputs::MouseAxis::X_SCR:
2022-11-29 14:22:03 +00:00
return m_win->getMouseScrollX();
2022-10-02 15:34:51 +00:00
case inputs::MouseAxis::Y_SCR:
2022-11-29 14:22:03 +00:00
return m_win->getMouseScrollY();
2022-10-02 15:34:51 +00:00
default: break;
2022-09-02 11:06:59 +00:00
}
break;
case InputDevice::KEYBOARD:
break;
case InputDevice::CONTROLLER:
break;
default: break;
2022-10-02 15:34:51 +00:00
}
throw std::runtime_error("Error getting device axis");
2022-09-02 11:06:59 +00:00
}
2022-11-29 14:22:03 +00:00
bool InputManager::getDeviceButton(enum InputDevice device, int button) const
2022-10-02 15:34:51 +00:00
{
switch (device) {
2022-09-02 11:06:59 +00:00
case InputDevice::MOUSE:
2022-11-29 14:22:03 +00:00
return m_win->getButton(static_cast<inputs::MouseButton>(button));
2022-09-02 11:06:59 +00:00
case InputDevice::KEYBOARD:
2022-11-29 14:22:03 +00:00
return m_win->getKey(static_cast<inputs::Key>(button));
2022-09-02 11:06:59 +00:00
case InputDevice::CONTROLLER:
break;
default: break;
2022-10-02 15:34:51 +00:00
}
throw std::runtime_error("Error getting device button");
2022-09-02 11:06:59 +00:00
}
2022-11-29 14:22:03 +00:00
bool InputManager::getDeviceButtonDown(enum InputDevice device, int button) const
2022-10-02 15:34:51 +00:00
{
switch (device) {
2022-09-02 11:06:59 +00:00
case InputDevice::MOUSE:
2022-11-29 14:22:03 +00:00
return m_win->getButtonPress(static_cast<enum inputs::MouseButton>(button));
2022-09-02 11:06:59 +00:00
case InputDevice::KEYBOARD:
2022-11-29 14:22:03 +00:00
return m_win->getKeyPress(static_cast<enum inputs::Key>(button));
2022-09-02 11:06:59 +00:00
case InputDevice::CONTROLLER:
break;
default: break;
2022-10-02 15:34:51 +00:00
}
throw std::runtime_error("Error getting device button");
2022-09-02 11:06:59 +00:00
}
2022-11-29 14:22:03 +00:00
bool InputManager::getDeviceButtonUp(enum InputDevice device, int button) const
2022-10-02 15:34:51 +00:00
{
switch (device) {
2022-09-02 11:06:59 +00:00
case InputDevice::MOUSE:
2022-11-29 14:22:03 +00:00
return m_win->getButtonRelease(static_cast<enum inputs::MouseButton>(button));
2022-09-02 11:06:59 +00:00
case InputDevice::KEYBOARD:
2022-11-29 14:22:03 +00:00
return m_win->getKeyRelease(static_cast<enum inputs::Key>(button));
2022-09-02 11:06:59 +00:00
case InputDevice::CONTROLLER:
break;
default: break;
2022-10-02 15:34:51 +00:00
}
throw std::runtime_error("Error getting device button");
2022-09-02 11:06:59 +00:00
}
2022-10-02 15:34:51 +00:00
2022-11-29 14:22:03 +00:00
float InputManager::getButtonAxis(enum InputDevice device, int high, int low) const
2022-10-02 15:34:51 +00:00
{
float value = 0.0f;
if (getDeviceButton(device, high)) value += 1.0f;
if (low != 0) {
if (getDeviceButton(device, low)) value += -1.0f;
2022-09-02 11:06:59 +00:00
}
2022-10-02 15:34:51 +00:00
return value;
2022-09-02 11:06:59 +00:00
}
2022-10-02 15:34:51 +00:00
// public methods
2022-11-29 14:22:03 +00:00
void InputManager::addInputButton(const std::string& name, InputDevice device, int button)
2022-10-02 15:34:51 +00:00
{
m_buttonEntries.push_back({ name, device, button });
}
2022-11-29 14:22:03 +00:00
void InputManager::addInputAxis(const std::string& name, InputDevice device, int axis)
2022-10-02 15:34:51 +00:00
{
m_axisEntries.push_back({ name, device, axis, false, 0, 0 });
}
2022-11-29 14:22:03 +00:00
void InputManager::addInputButtonAsAxis(const std::string& name, InputDevice device, int high, int low)
2022-10-02 15:34:51 +00:00
{
m_axisEntries.push_back({ name, device, 0, true, high, low });
}
// OVERLOADS:
// Add a mouse input
2022-11-29 14:22:03 +00:00
void InputManager::addInputButton(const std::string& name, inputs::MouseButton button)
2022-10-02 15:34:51 +00:00
{
addInputButton(name, InputDevice::MOUSE, static_cast<int>(button));
}
2022-11-29 14:22:03 +00:00
void InputManager::addInputAxis(const std::string& name, inputs::MouseAxis axis)
2022-10-02 15:34:51 +00:00
{
addInputAxis(name, InputDevice::MOUSE, static_cast<int>(axis));
}
2022-09-02 11:06:59 +00:00
2022-11-29 14:22:03 +00:00
void InputManager::addInputButtonAsAxis(const std::string& name, inputs::MouseButton high, inputs::MouseButton low)
2022-10-02 15:34:51 +00:00
{
addInputButtonAsAxis(name, InputDevice::MOUSE, static_cast<int>(high), static_cast<int>(low));
}
2022-11-29 14:22:03 +00:00
void InputManager::addInputButton(const std::string& name, inputs::Key button)
2022-10-02 15:34:51 +00:00
{
addInputButton(name, InputDevice::KEYBOARD, static_cast<int>(button));
}
2022-11-29 14:22:03 +00:00
void InputManager::addInputButtonAsAxis(const std::string& name, inputs::Key high, inputs::Key low)
2022-10-02 15:34:51 +00:00
{
addInputButtonAsAxis(name, InputDevice::KEYBOARD, static_cast<int>(high), static_cast<int>(low));
}
2022-11-29 14:22:03 +00:00
void InputManager::delInputButton(int index)
2022-10-02 15:34:51 +00:00
{
std::vector<struct ButtonEntry>::iterator it = m_buttonEntries.begin();
std::advance(it, index);
m_buttonEntries.erase(it);
}
2022-11-29 14:22:03 +00:00
void InputManager::delInputAxis(int index)
2022-10-02 15:34:51 +00:00
{
std::vector<struct AxisEntry>::iterator it = m_axisEntries.begin();
std::advance(it, index);
m_axisEntries.erase(it);
}
2022-11-29 14:22:03 +00:00
void InputManager::setDeviceActive(enum InputDevice device, bool active)
2022-10-02 15:34:51 +00:00
{
m_enabledDevices[static_cast<int>(device)] = active;
}
2022-11-29 14:22:03 +00:00
bool InputManager::getDeviceActive(enum InputDevice device) const
2022-10-02 15:34:51 +00:00
{
return m_enabledDevices[static_cast<int>(device)];
}
2022-11-29 14:22:03 +00:00
float InputManager::getAxis(const std::string& axisName) const
2022-10-02 15:34:51 +00:00
{
for (const AxisEntry& e : m_axisEntries) {
if (e.name == axisName) {
if (m_enabledDevices[static_cast<int>(e.device)]) {
if (e.isButtonAxis) {
return getButtonAxis(e.device, e.high, e.low);
}
else {
return getDeviceAxis(e.device, e.axis);
}
2022-09-02 11:06:59 +00:00
}
}
}
2022-10-02 15:34:51 +00:00
return 0.0f; // instead of throwing an exception, just return nothing
// throw std::runtime_error("Unable to find mapping in input table");
2022-09-02 11:06:59 +00:00
}
2022-11-29 14:22:03 +00:00
bool InputManager::getButton(const std::string& buttonName) const
2022-10-02 15:34:51 +00:00
{
bool isDown = false;
for (const ButtonEntry& e : m_buttonEntries) {
if (e.name == buttonName) {
if (m_enabledDevices[static_cast<int>(e.device)]) {
if (getDeviceButton(e.device, e.button) == true) {
isDown = true;
break;
}
2022-09-02 11:06:59 +00:00
}
}
}
2022-10-02 15:34:51 +00:00
return isDown;
2022-09-02 11:06:59 +00:00
}
2022-11-29 14:22:03 +00:00
bool InputManager::getButtonPress(const std::string& buttonName) const
2022-10-02 15:34:51 +00:00
{
bool isPressed = false;
for (const ButtonEntry& e : m_buttonEntries) {
if (e.name == buttonName) {
if (m_enabledDevices[static_cast<int>(e.device)]) {
if (getDeviceButtonDown(e.device, e.button) == true) {
isPressed = true;
break;
}
}
}
}
return isPressed;
}
2022-09-02 11:06:59 +00:00
2022-11-29 14:22:03 +00:00
bool InputManager::getButtonRelease(const std::string& buttonName) const
2022-10-02 15:34:51 +00:00
{
bool isReleased = false;
for (const ButtonEntry& e : m_buttonEntries) {
if (e.name == buttonName) {
if (m_enabledDevices[static_cast<int>(e.device)]) {
if (getDeviceButtonUp(e.device, e.button) == true) {
isReleased = true;
break;
}
2022-09-02 11:06:59 +00:00
}
}
}
2022-10-02 15:34:51 +00:00
return isReleased;
2022-09-02 11:06:59 +00:00
}
2022-10-02 15:34:51 +00:00
2022-11-09 15:37:16 +00:00
}