Base layout for the input manager
This commit is contained in:
		
							
								
								
									
										104
									
								
								BarinkEngine/Input/GLFWInput.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								BarinkEngine/Input/GLFWInput.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,104 @@
 | 
			
		||||
#include "BarinkEngine.h"
 | 
			
		||||
#include "../Include/Input/GLFWInput.h"
 | 
			
		||||
#include "../Include/EventSystem/InputSystemEvents.h"
 | 
			
		||||
#include "../Include/Input/InputManager.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
namespace BarinkEngine {
 | 
			
		||||
	namespace Input {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		void BE_GLFW_KEYS(GLFWwindow* window, int key, int scancode, int action, int mods)
 | 
			
		||||
		{
 | 
			
		||||
 | 
			
		||||
			switch (action)
 | 
			
		||||
			{
 | 
			
		||||
			case GLFW_KEY_DOWN: {
 | 
			
		||||
				KEY_DOWN_EVENT keydown{};
 | 
			
		||||
				keydown.name = "KEY::DOWN";
 | 
			
		||||
				keydown.mods = mods;
 | 
			
		||||
				keydown.scancode = scancode;
 | 
			
		||||
				keydown.keycode = key;
 | 
			
		||||
 | 
			
		||||
				InputSystem.EmitEvent(keydown);
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
				
 | 
			
		||||
			case GLFW_KEY_UP: {
 | 
			
		||||
				KEY_UP_EVENT keyup{};
 | 
			
		||||
				keyup.name = "KEY::DOWN";
 | 
			
		||||
				keyup.mods = mods;
 | 
			
		||||
				keyup.scancode = scancode;
 | 
			
		||||
				keyup.keycode = key;
 | 
			
		||||
 | 
			
		||||
				InputSystem.EmitEvent(keyup);
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
				
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			default:
 | 
			
		||||
				Event KeyEvent{};
 | 
			
		||||
				KeyEvent.name = "KEY";
 | 
			
		||||
 | 
			
		||||
				InputSystem.EmitEvent(KeyEvent);
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		void BE_GLFW_CURSOR_POSITION(GLFWwindow* window, double x, double y) 
 | 
			
		||||
		{
 | 
			
		||||
			Event CursorPosUpdate{};
 | 
			
		||||
			CursorPosUpdate.name = "UPDATE::CURSOR:POSITION";
 | 
			
		||||
 | 
			
		||||
			InputSystem.EmitEvent(CursorPosUpdate);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		void BE_GLFW_CURSOR_ENTER(GLFWwindow* window, int entered) 
 | 
			
		||||
		{
 | 
			
		||||
			if (entered) {
 | 
			
		||||
				Event mouseEntered{};
 | 
			
		||||
				mouseEntered.name = "Mouse Entered Window's confines!";
 | 
			
		||||
 | 
			
		||||
				InputSystem.EmitEvent(mouseEntered);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
				Event mouseLeft{};
 | 
			
		||||
				mouseLeft.name = "Mouse Left Window's confines!";
 | 
			
		||||
 | 
			
		||||
				InputSystem.EmitEvent(mouseLeft);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		void BE_GLFW_MOUSE_BUTTON(GLFWwindow* window, int button, int action, int mods) 
 | 
			
		||||
		{
 | 
			
		||||
			Event MouseButtonEvent{};
 | 
			
		||||
			MouseButtonEvent.name = "MOUSEBUTTON";
 | 
			
		||||
 | 
			
		||||
			InputSystem.EmitEvent(MouseButtonEvent);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		void BE_GLFW_SCROLL(GLFWwindow* window, double xoffset, double yoffset) 
 | 
			
		||||
		{
 | 
			
		||||
			Event ScrollEvent{};
 | 
			
		||||
			ScrollEvent.name = "SCROLL";
 | 
			
		||||
 | 
			
		||||
			InputSystem.EmitEvent(ScrollEvent);
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -1,118 +1,44 @@
 | 
			
		||||
#include "BarinkEngine.h"
 | 
			
		||||
#include "Input/InputManager.h"
 | 
			
		||||
#include "GLFW/glfw3.h"
 | 
			
		||||
#include "spdlog/spdlog.h"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
void BarinkEngine::InputManager::PollEvents()
 | 
			
		||||
{
 | 
			
		||||
	for (auto it = windows.begin(); it != windows.end(); ++it) {
 | 
			
		||||
		(*it)->Poll();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BarinkEngine::InputManager::KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) 
 | 
			
		||||
{
 | 
			
		||||
namespace BarinkEngine {
 | 
			
		||||
 | 
			
		||||
	Event KeyEvent{};
 | 
			
		||||
	KeyEvent.name = "KEY";
 | 
			
		||||
	 
 | 
			
		||||
	InputSystem.EmitEvent(KeyEvent);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (key == GLFW_KEY_A && action == GLFW_PRESS)
 | 
			
		||||
	void InputManager::PollEvents()
 | 
			
		||||
	{
 | 
			
		||||
		
 | 
			
		||||
		std::cout << "'a' key was pressed" << std::endl;
 | 
			
		||||
		for (auto it = windows.begin(); it != windows.end(); ++it) {
 | 
			
		||||
			(*it)->Poll();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BarinkEngine::InputManager::CursorPositionCallback(GLFWwindow* window, double x, double y) 
 | 
			
		||||
{
 | 
			
		||||
	//std::cout << "Cursor Position  x: " << x << ", y: " << y << std::endl;
 | 
			
		||||
	Event CursorPosUpdate{};
 | 
			
		||||
	CursorPosUpdate.name = "UPDATE::CURSOR:POSITION";
 | 
			
		||||
 | 
			
		||||
	InputSystem.EmitEvent(CursorPosUpdate);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BarinkEngine::InputManager::CursorEnterCallback(GLFWwindow* window, int entered)
 | 
			
		||||
{
 | 
			
		||||
	if (entered) {
 | 
			
		||||
		Event mouseEntered {};
 | 
			
		||||
		mouseEntered.name = "Mouse Entered Window's confines!";
 | 
			
		||||
		mouseEntered.argc = 0;
 | 
			
		||||
		
 | 
			
		||||
		InputSystem.EmitEvent(mouseEntered);
 | 
			
		||||
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		Event mouseLeft{};
 | 
			
		||||
		mouseLeft.name = "Mouse Left Window's confines!";
 | 
			
		||||
		mouseLeft.argc = 0;
 | 
			
		||||
 | 
			
		||||
		InputSystem.EmitEvent(mouseLeft);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void BarinkEngine::InputManager::MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	Event MouseButtonEvent{};
 | 
			
		||||
	MouseButtonEvent.name = "MOUSEBUTTON";
 | 
			
		||||
 | 
			
		||||
	InputSystem.EmitEvent(MouseButtonEvent);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
 | 
			
		||||
		std::cout << "Right mouse button was pressed!" << std::endl;
 | 
			
		||||
	void InputManager::setupGLFWInput(GLFWwindow* window) {
 | 
			
		||||
		// Attach callbacks
 | 
			
		||||
		glfwSetKeyCallback(window, BarinkEngine::Input::BE_GLFW_KEYS);
 | 
			
		||||
		glfwSetCursorPosCallback(window, BarinkEngine::Input::BE_GLFW_CURSOR_POSITION);
 | 
			
		||||
		glfwSetCursorEnterCallback(window, BarinkEngine::Input::BE_GLFW_CURSOR_ENTER);
 | 
			
		||||
		glfwSetMouseButtonCallback(window, BarinkEngine::Input::BE_GLFW_MOUSE_BUTTON);
 | 
			
		||||
		glfwSetScrollCallback(window, BarinkEngine::Input::BE_GLFW_SCROLL);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BarinkEngine::InputManager::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
 | 
			
		||||
{
 | 
			
		||||
	std::cout << "Scroll: x: " << xoffset << ", y: " << yoffset << std::endl;
 | 
			
		||||
	
 | 
			
		||||
	Event ScrollEvent{};
 | 
			
		||||
	ScrollEvent.name = "SCROLL";
 | 
			
		||||
	void InputManager::attach(BarinkWindow* window)
 | 
			
		||||
	{
 | 
			
		||||
		windows.push_back(window);
 | 
			
		||||
		this->Subscribe((EventListener&)(*window));
 | 
			
		||||
 | 
			
		||||
	InputSystem.EmitEvent(ScrollEvent);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	void InputManager::detach(BarinkWindow* window)
 | 
			
		||||
	{
 | 
			
		||||
		windows.remove(window);
 | 
			
		||||
		this->Unsubscribe((EventListener&)*window);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	InputManager::InputManager() : EventEmitter()
 | 
			
		||||
	{
 | 
			
		||||
		windows = std::list<BarinkWindow*>();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void BarinkEngine::InputManager::attach(BarinkWindow* window)
 | 
			
		||||
{
 | 
			
		||||
	
 | 
			
		||||
	windows.push_back(window);
 | 
			
		||||
 | 
			
		||||
	// Attach callbacks
 | 
			
		||||
	glfwSetKeyCallback(window->windowptr(), KeyCallback);
 | 
			
		||||
	glfwSetCursorPosCallback(window->windowptr(), CursorPositionCallback);
 | 
			
		||||
	glfwSetCursorEnterCallback(window->windowptr(), CursorEnterCallback);
 | 
			
		||||
	glfwSetMouseButtonCallback(window->windowptr(), MouseButtonCallback);
 | 
			
		||||
	glfwSetScrollCallback(window->windowptr(), ScrollCallback);
 | 
			
		||||
 | 
			
		||||
	this->Subscribe( (EventListener&)(*window));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BarinkEngine::InputManager::InputManager() : EventEmitter ()
 | 
			
		||||
{
 | 
			
		||||
	windows = std::vector<BarinkWindow*>();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user