2022-06-10 20:44:40 +00:00
|
|
|
#include "BarinkEngine.h"
|
2022-06-04 16:26:58 +00:00
|
|
|
#include "Input/InputManager.h"
|
2022-06-10 19:06:45 +00:00
|
|
|
#include "GLFW/glfw3.h"
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
#include <iostream>
|
2022-06-04 16:26:58 +00:00
|
|
|
void BarinkEngine::InputManager::PollEvents()
|
|
|
|
{
|
2022-06-10 20:44:40 +00:00
|
|
|
for (auto it = windows.begin(); it != windows.end(); ++it) {
|
2022-06-04 16:26:58 +00:00
|
|
|
(*it)->Poll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
void BarinkEngine::InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
|
|
{
|
2022-06-10 20:44:40 +00:00
|
|
|
|
|
|
|
Event KeyEvent{};
|
|
|
|
KeyEvent.name = "KEY";
|
|
|
|
|
|
|
|
InputSystem.EmitEvent(KeyEvent);
|
|
|
|
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
if (key == GLFW_KEY_A && action == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
|
|
|
|
std::cout << "'a' key was pressed" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void BarinkEngine::InputManager::CursorPositionCallback(GLFWwindow* window, double x, double y)
|
|
|
|
{
|
2022-06-10 20:44:40 +00:00
|
|
|
//std::cout << "Cursor Position x: " << x << ", y: " << y << std::endl;
|
|
|
|
Event CursorPosUpdate{};
|
|
|
|
CursorPosUpdate.name = "UPDATE::CURSOR:POSITION";
|
|
|
|
|
|
|
|
InputSystem.EmitEvent(CursorPosUpdate);
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BarinkEngine::InputManager::CursorEnterCallback(GLFWwindow* window, int entered)
|
|
|
|
{
|
|
|
|
if (entered) {
|
2022-06-10 20:44:40 +00:00
|
|
|
Event mouseEntered {};
|
|
|
|
mouseEntered.name = "Mouse Entered Window's confines!";
|
|
|
|
mouseEntered.argc = 0;
|
|
|
|
|
|
|
|
InputSystem.EmitEvent(mouseEntered);
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-06-10 20:44:40 +00:00
|
|
|
Event mouseLeft{};
|
|
|
|
mouseLeft.name = "Mouse Left Window's confines!";
|
|
|
|
mouseLeft.argc = 0;
|
|
|
|
|
|
|
|
InputSystem.EmitEvent(mouseLeft);
|
|
|
|
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BarinkEngine::InputManager::MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
|
|
|
|
{
|
2022-06-10 20:44:40 +00:00
|
|
|
|
|
|
|
Event MouseButtonEvent{};
|
|
|
|
MouseButtonEvent.name = "MOUSEBUTTON";
|
|
|
|
|
|
|
|
InputSystem.EmitEvent(MouseButtonEvent);
|
|
|
|
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
|
|
|
|
std::cout << "Right mouse button was pressed!" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void BarinkEngine::InputManager::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
|
|
|
|
{
|
|
|
|
std::cout << "Scroll: x: " << xoffset << ", y: " << yoffset << std::endl;
|
2022-06-10 20:44:40 +00:00
|
|
|
|
|
|
|
Event ScrollEvent{};
|
|
|
|
ScrollEvent.name = "SCROLL";
|
|
|
|
|
|
|
|
InputSystem.EmitEvent(ScrollEvent);
|
|
|
|
|
2022-06-10 19:06:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-04 16:26:58 +00:00
|
|
|
void BarinkEngine::InputManager::attach(BarinkWindow* window)
|
|
|
|
{
|
2022-06-10 19:06:45 +00:00
|
|
|
|
2022-06-04 16:26:58 +00:00
|
|
|
windows.push_back(window);
|
2022-06-10 19:06:45 +00:00
|
|
|
|
|
|
|
// Attach callbacks
|
|
|
|
glfwSetKeyCallback(window->windowptr(), KeyCallback);
|
|
|
|
glfwSetCursorPosCallback(window->windowptr(), CursorPositionCallback);
|
|
|
|
glfwSetCursorEnterCallback(window->windowptr(), CursorEnterCallback);
|
|
|
|
glfwSetMouseButtonCallback(window->windowptr(), MouseButtonCallback);
|
|
|
|
glfwSetScrollCallback(window->windowptr(), ScrollCallback);
|
|
|
|
|
2022-06-10 20:44:40 +00:00
|
|
|
this->Subscribe( (EventListener&)(*window));
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-04 16:26:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 20:44:40 +00:00
|
|
|
BarinkEngine::InputManager::InputManager() : EventEmitter ()
|
2022-06-04 16:26:58 +00:00
|
|
|
{
|
|
|
|
windows = std::vector<BarinkWindow*>();
|
|
|
|
}
|
2022-06-10 20:44:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|