engine/src/input_manager.cpp

146 lines
3.5 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
2023-04-29 14:22:25 +00:00
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<int>(e.device)]) {
if (e.is_button_axis) {
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
}
}
2023-04-29 14:22:25 +00:00
return 0.0f; // instead of throwing an exception, just return nothing
}
2022-10-02 15:34:51 +00:00
2023-04-29 14:22:25 +00:00
bool InputManager::GetButton(const std::string& buttonName) const
{
bool isDown = false;
2022-10-02 15:34:51 +00:00
2023-04-29 14:22:25 +00:00
for (const ButtonEntry& e : button_entries_) {
if (e.name == buttonName) {
if (enabled_devices_[static_cast<int>(e.device)]) {
if (GetDeviceButton(e.device, e.button) == true) {
isDown = true;
break;
2022-09-02 11:06:59 +00:00
}
}
}
}
2023-04-29 14:22:25 +00:00
return isDown;
}
2022-09-02 11:06:59 +00:00
2023-04-29 14:22:25 +00:00
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<int>(e.device)]) {
if (getDeviceButtonDown(e.device, e.button) == true) {
isPressed = true;
break;
2022-09-02 11:06:59 +00:00
}
}
}
}
2023-04-29 14:22:25 +00:00
return isPressed;
}
2022-09-02 11:06:59 +00:00
2023-04-29 14:22:25 +00:00
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<int>(e.device)]) {
if (GetDeviceButtonUp(e.device, e.button) == true) {
isReleased = true;
break;
2022-10-02 15:34:51 +00:00
}
}
}
}
2023-04-29 14:22:25 +00:00
return isReleased;
}
2022-09-02 11:06:59 +00:00
2023-04-29 14:22:25 +00:00
float InputManager::GetDeviceAxis(enum InputDevice device, int axis) const
{
switch (device) {
case InputDevice::kMouse:
switch (static_cast<inputs::MouseAxis>(axis)) {
case inputs::MouseAxis::X:
2023-05-01 12:14:46 +00:00
return static_cast<float>(win_->GetMouseDX());
2023-04-29 14:22:25 +00:00
case inputs::MouseAxis::Y:
2023-05-01 12:14:46 +00:00
return static_cast<float>(win_->GetMouseDY());
2023-04-29 14:22:25 +00:00
case inputs::MouseAxis::X_SCR:
2023-05-01 12:14:46 +00:00
return win_->GetMouseScrollX();
2023-04-29 14:22:25 +00:00
case inputs::MouseAxis::Y_SCR:
2023-05-01 12:14:46 +00:00
return win_->GetMouseScrollY();
2023-04-29 14:22:25 +00:00
default: break;
2022-09-02 11:06:59 +00:00
}
2023-04-29 14:22:25 +00:00
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:
2023-04-29 14:57:32 +00:00
return win_->GetButton(static_cast<inputs::MouseButton>(button));
2023-04-29 14:22:25 +00:00
case InputDevice::kKeyboard:
2023-04-29 14:57:32 +00:00
return win_->GetKey(static_cast<inputs::Key>(button));
2023-04-29 14:22:25 +00:00
case InputDevice::kController:
break;
default: break;
}
throw std::runtime_error("Error getting device button");
}
2022-10-02 15:34:51 +00:00
2023-04-29 14:22:25 +00:00
bool InputManager::getDeviceButtonDown(enum InputDevice device, int button) const
{
switch (device) {
case InputDevice::kMouse:
2023-04-29 14:57:32 +00:00
return win_->GetButtonPress(static_cast<enum inputs::MouseButton>(button));
2023-04-29 14:22:25 +00:00
case InputDevice::kKeyboard:
2023-04-29 14:57:32 +00:00
return win_->GetKeyPress(static_cast<enum inputs::Key>(button));
2023-04-29 14:22:25 +00:00
case InputDevice::kController:
break;
default: break;
}
throw std::runtime_error("Error getting device button");
2022-11-09 15:37:16 +00:00
}
2023-04-29 14:22:25 +00:00
bool InputManager::GetDeviceButtonUp(enum InputDevice device, int button) const
{
switch (device) {
case InputDevice::kMouse:
2023-04-29 14:57:32 +00:00
return win_->GetButtonRelease(static_cast<enum inputs::MouseButton>(button));
2023-04-29 14:22:25 +00:00
case InputDevice::kKeyboard:
2023-04-29 14:57:32 +00:00
return win_->GetKeyRelease(static_cast<enum inputs::Key>(button));
2023-04-29 14:22:25 +00:00
case InputDevice::kController:
break;
default: break;
}
throw std::runtime_error("Error getting device button");
}
}