#include "input_manager.hpp" #include "window.hpp" #include #include namespace engine { float InputManager::GetAxis(const std::string& axis_name) const { for (const AxisEntry& e : axis_entries_) { if (e.name == axis_name) { if (enabled_devices_[static_cast(e.device)]) { if (e.is_button_axis) { return GetButtonAxis(e.device, e.high, e.low); } else { return GetDeviceAxis(e.device, e.axis); } } } } return 0.0f; // instead of throwing an exception, just return nothing } bool InputManager::GetButton(const std::string& buttonName) const { bool isDown = false; for (const ButtonEntry& e : button_entries_) { if (e.name == buttonName) { if (enabled_devices_[static_cast(e.device)]) { if (GetDeviceButton(e.device, e.button) == true) { isDown = true; break; } } } } return isDown; } bool InputManager::GetButtonPress(const std::string& buttonName) const { bool isPressed = false; for (const ButtonEntry& e : button_entries_) { if (e.name == buttonName) { if (enabled_devices_[static_cast(e.device)]) { if (getDeviceButtonDown(e.device, e.button) == true) { isPressed = true; break; } } } } return isPressed; } bool InputManager::GetButtonRelease(const std::string& buttonName) const { bool isReleased = false; for (const ButtonEntry& e : button_entries_) { if (e.name == buttonName) { if (enabled_devices_[static_cast(e.device)]) { if (GetDeviceButtonUp(e.device, e.button) == true) { isReleased = true; break; } } } } return isReleased; } float InputManager::GetDeviceAxis(enum InputDevice device, int axis) const { switch (device) { case InputDevice::kMouse: switch (static_cast(axis)) { case inputs::MouseAxis::X: return static_cast(win_->getMouseDX()); case inputs::MouseAxis::Y: return static_cast(win_->getMouseDY()); case inputs::MouseAxis::X_SCR: return win_->getMouseScrollX(); case inputs::MouseAxis::Y_SCR: return win_->getMouseScrollY(); default: break; } break; case InputDevice::kKeyboard: break; case InputDevice::kController: break; default: break; } throw std::runtime_error("Error getting device axis"); } bool InputManager::GetDeviceButton(enum InputDevice device, int button) const { switch (device) { case InputDevice::kMouse: return win_->getButton(static_cast(button)); case InputDevice::kKeyboard: return win_->getKey(static_cast(button)); case InputDevice::kController: break; default: break; } throw std::runtime_error("Error getting device button"); } bool InputManager::getDeviceButtonDown(enum InputDevice device, int button) const { switch (device) { case InputDevice::kMouse: return win_->getButtonPress(static_cast(button)); case InputDevice::kKeyboard: return win_->getKeyPress(static_cast(button)); case InputDevice::kController: break; default: break; } throw std::runtime_error("Error getting device button"); } bool InputManager::GetDeviceButtonUp(enum InputDevice device, int button) const { switch (device) { case InputDevice::kMouse: return win_->getButtonRelease(static_cast(button)); case InputDevice::kKeyboard: return win_->getKeyRelease(static_cast(button)); case InputDevice::kController: break; default: break; } throw std::runtime_error("Error getting device button"); } }