added input system

This commit is contained in:
2026-01-17 18:01:48 +01:00
parent 6d90c91209
commit cf5602417b
21 changed files with 120171 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
#include "InputGlfw.hpp"
void TSE::InputGlfw::Init()
{
Instance = new InputGlfw();
}
void TSE::InputGlfw::Delete()
{
delete(Instance);
}
void TSE::InputGlfw::KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
switch (action)
{
case 1: //down
for(auto handler : ((InputGlfw*)instance())->keyHandler)
{
handler->OnKeyDown((Key)key, (Modifier)mods);
}
break;
case 0: //up
for(auto handler : ((InputGlfw*)instance())->keyHandler)
{
handler->OnKeyUp((Key)key, (Modifier)mods);
}
break;
case 2: //hold
for(auto handler : ((InputGlfw*)instance())->keyHandler)
{
handler->OnKeyHold((Key)key, (Modifier)mods);
}
break;
}
}
void TSE::InputGlfw::MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{
switch (action)
{
case 1: //down
for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{
handler->OnMouseButtonDown((MouseBtn)button, (Modifier)mods);
}
break;
case 0: //up
for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{
handler->OnMouseButtonUp((MouseBtn)button, (Modifier)mods);
}
break;
case 2: //hold
for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{
handler->OnMouseButtonHold((MouseBtn)button, (Modifier)mods);
}
break;
}
}
void TSE::InputGlfw::CursorPosCallback(GLFWwindow *window, double xpos, double ypos)
{
for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{
handler->OnMousePosition(Vector2(xpos, ypos));
}
}
void TSE::InputGlfw::ScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{
for(auto handler : ((InputGlfw*)instance())->cursorHandler)
{
handler->OnMouseScroll(Vector2(xoffset, yoffset));
}
}
std::string UTF32ToUTF8(char32_t ch)
{
std::string result;
if (ch <= 0x7F)
result += static_cast<char>(ch);
else if (ch <= 0x7FF) {
result += static_cast<char>(0xC0 | ((ch >> 6) & 0x1F));
result += static_cast<char>(0x80 | (ch & 0x3F));
}
else if (ch <= 0xFFFF) {
result += static_cast<char>(0xE0 | ((ch >> 12) & 0x0F));
result += static_cast<char>(0x80 | ((ch >> 6) & 0x3F));
result += static_cast<char>(0x80 | (ch & 0x3F));
}
else if (ch <= 0x10FFFF) {
result += static_cast<char>(0xF0 | ((ch >> 18) & 0x07));
result += static_cast<char>(0x80 | ((ch >> 12) & 0x3F));
result += static_cast<char>(0x80 | ((ch >> 6) & 0x3F));
result += static_cast<char>(0x80 | (ch & 0x3F));
}
return result;
}
void TSE::InputGlfw::CharCallback(GLFWwindow *window, unsigned int codepoint)
{
char32_t c = static_cast<char32_t>(codepoint);
std::string msg = UTF32ToUTF8(c);
for(auto handler : ((InputGlfw*)instance())->keyHandler)
{
handler->OnChar(msg);
}
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include "interfaces/IInputManager.hpp"
#include "GLFW/glfw3.h"
namespace TSE
{
class InputGlfw : public IInputManager
{
public:
static void Init();
void Delete() override;
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods);
static void CursorPosCallback(GLFWwindow *window, double xpos, double ypos);
static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset);
static void CharCallback(GLFWwindow *window, unsigned int codepoint);
};
} // namespace TSE

View File

@@ -2,6 +2,7 @@
#include "utils/Time.hpp"
#include "TimeInterfaceGlfw.hpp"
#include "Debug.hpp"
#include "InputGlfw.hpp"
TSE::GLFW::WindowGlfw::WindowGlfw(string _title, int _width, int _height, IRenderingBackend* backend)
: WindowGlfw(_title, _width, _height, backend, WindowType::Windowed, 32){ }
@@ -38,6 +39,7 @@ bool TSE::GLFW::WindowGlfw::Init()
return false;
}
InputGlfw::Init();
Time::Init(new TimeInterfaceGlfw());
renderingBackend->InitPreWindow();
@@ -95,6 +97,11 @@ bool TSE::GLFW::WindowGlfw::Init()
//callbacks
glfwSetWindowSizeCallback(window, ResizeWindow);
glfwSetKeyCallback(window, InputGlfw::KeyCallback);
glfwSetMouseButtonCallback(window, InputGlfw::MouseButtonCallback);
glfwSetCursorPosCallback(window, InputGlfw::CursorPosCallback);
glfwSetScrollCallback(window, InputGlfw::ScrollCallback);
glfwSetCharCallback(window, InputGlfw::CharCallback);
Debug::Log("GLFW:" + std::string(glfwGetVersionString()));