Dawn/src/dawnglfw/input/InputManager.cpp
2024-11-25 22:12:03 -06:00

68 lines
1.8 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "InputManager.hpp"
#include "game/Game.hpp"
using namespace Dawn;
void InputManager::init(const std::shared_ptr<Game> game) {
auto window = game->renderHost->window;
glfwSetCursorPosCallback(window, [](
GLFWwindow* window,
double_t x,
double_t y
) {
auto game = (Game*)glfwGetWindowUserPointer(window);
game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = (float_t) x;
game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = (float_t) y;
});
glfwSetMouseButtonCallback(window, [](
GLFWwindow* window,
int32_t button,
int32_t action,
int32_t mods
) {
auto game = (Game*)glfwGetWindowUserPointer(window);
game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_0 + button] = (
action == GLFW_PRESS ? 1.0f : 0.0f
);
});
glfwSetKeyCallback(window, [](
GLFWwindow* window,
int32_t key,
int32_t scancode,
int32_t action,
int32_t mods
) {
auto game = (Game*)glfwGetWindowUserPointer(window);
game->inputManager.rawInputValues[key] = (
action == GLFW_PRESS ? 1.0f : 0.0f
);
});
// Default bindings
this->bind(InputBind::UP, GLFW_KEY_W);
this->bind(InputBind::UP, GLFW_KEY_UP);
this->bind(InputBind::DOWN, GLFW_KEY_S);
this->bind(InputBind::DOWN, GLFW_KEY_DOWN);
this->bind(InputBind::LEFT, GLFW_KEY_A);
this->bind(InputBind::LEFT, GLFW_KEY_LEFT);
this->bind(InputBind::RIGHT, GLFW_KEY_D);
this->bind(InputBind::RIGHT, GLFW_KEY_RIGHT);
}
float_t InputManager::getInputValue(int32_t axis) {
auto exist = rawInputValues.find(axis);
if(exist == rawInputValues.end()) return 0.0f;
return exist->second;
}
InputManager::~InputManager() {
// Nothing to do here.
}