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,50 @@
#pragma once
#include <unordered_map>
#include "interfaces/IShader.hpp"
#include "Types.hpp"
namespace TSE
{
class ShaderRegistry
{
private:
inline static std::unordered_map<string, IShader*> registeredShaders = {};
public:
inline static void SetShader(string& name, IShader* shader)
{
registeredShaders[name] = shader;
};
inline static IShader* GetShader(string& name)
{
return registeredShaders.at(name);
};
inline static void RemoveShader(string& name)
{
registeredShaders.erase(name);
};
inline static int GetShaderCount()
{
return registeredShaders.size();
}
inline static IShader* GetShaderAt(int i)
{
auto it = registeredShaders.begin();
for (int j = 0; j < i; j++)
{
it++;
}
return it->second;
}
inline static string GetNameAt(int i)
{
auto it = registeredShaders.begin();
for (int j = 0; j < i; j++)
{
it++;
}
return it->first;
}
};
} // namespace TSE

View File

@@ -0,0 +1,154 @@
#pragma once
namespace TSE
{
enum MouseBtn
{
Button1 = 0,
Button2 = 1,
Button3 = 2,
Button4 = 3,
Button5 = 4,
Button6 = 5,
Button7 = 6,
Button8 = 7,
LeftMouse = Button1,
RightMouse = Button2,
MiddleMouse = Button3
};
enum Modifier
{
ShiftMod = 0x0001,
ControlMod = 0x0002,
AltMod = 0x0004,
SuperMod = 0x0008,
CapsLockMod = 0x0010,
NumLockMod = 0x0020
};
enum Key
{
Unknown = -1,
Space = 32,
Apostrophe = 39,
Comma = 44,
Minus = 45,
Period = 46,
Slash = 47,
Alpha0 = 48,
Alpha1 = 49,
Alpha2 = 50,
Alpha3 = 51,
Alpha4 = 52,
Alpha5 = 53,
Alpha6 = 54,
Alpha7 = 55,
Alpha8 = 56,
Alpha9 = 57,
SemiColon = 59,
Equal = 61,
A = 65,
B = 66,
C = 67,
D = 68,
E = 69,
F = 70,
G = 71,
H = 72,
I = 73,
J = 74,
K = 75,
L = 76,
M = 77,
N = 78,
O = 79,
P = 80,
Q = 81,
R = 82,
S = 83,
T = 84,
U = 85,
V = 86,
W = 87,
X = 88,
Y = 89,
Z = 90,
LeftBracket = 91,
Backslash = 92,
RightBracket = 93,
GraveAccent = 96,
World1 = 161,
World2 = 162,
Escape = 256,
Enter = 257,
Tab = 258,
Backspace = 259,
Insert = 260,
Delete = 261,
Right = 262,
Left = 263,
Down = 264,
Up = 265,
PageUp = 266,
PageDown = 267,
Home = 268,
End = 269,
CapsLock = 280,
ScrollLock = 281,
NumLock = 282,
PrintScreen = 283,
Pause = 284,
F1 = 290,
F2 = 291,
F3 = 292,
F4 = 293,
F5 = 294,
F6 = 295,
F7 = 296,
F8 = 297,
F9 = 298,
F10 = 299,
F11 = 300,
F12 = 301,
F13 = 302,
F14 = 303,
F15 = 304,
F16 = 305,
F17 = 306,
F18 = 307,
F19 = 308,
F20 = 309,
F21 = 310,
F22 = 311,
F23 = 312,
F24 = 313,
F25 = 314,
Numpad0 = 320,
Numpad1 = 321,
Numpad2 = 322,
Numpad3 = 323,
Numpad4 = 324,
Numpad5 = 325,
Numpad6 = 326,
Numpad7 = 327,
Numpad8 = 328,
Numpad9 = 329,
NumpadDecimal = 330,
NumpadDivide = 331,
NumpadMultiply = 332,
NumpadSubtract = 333,
NumpadAdd = 334,
NumpadEnter = 335,
NumpadEqual = 336,
LeftShift = 340,
LeftControl = 341,
LeftAlt = 342,
LeftSuper = 343,
RightShift = 344,
RightControl = 345,
RightAlt = 346,
RightSuper = 347,
Menu = 348
};
} // namespace TSE

View File

@@ -0,0 +1,54 @@
#include "ICursorHandler.hpp"
#include "IInputManager.hpp"
TSE::ICursorHandler::~ICursorHandler()
{
if(enabled()) Disable();
}
bool TSE::ICursorHandler::enabled() const
{
return Enabled;
}
void TSE::ICursorHandler::Enable()
{
if(!enabled())
{
IInputManager::instance()->RegisterCursorHandler(this);
}
}
void TSE::ICursorHandler::Disable()
{
if(enabled())
{
IInputManager::instance()->UnregisterCursorHandler(this);
}
}
void TSE::ICursorHandler::onMouseButtonDown(ICursorHandler *handler, const MouseBtn &btn, const Modifier &mods)
{
handler->OnMouseButtonDown(btn, mods);
}
void TSE::ICursorHandler::onMouseButtonUp(ICursorHandler *handler, const MouseBtn &btn, const Modifier &mods)
{
handler->OnMouseButtonUp(btn, mods);
}
void TSE::ICursorHandler::onMouseButtonHold(ICursorHandler *handler, const MouseBtn &btn, const Modifier &mods)
{
handler->OnMouseButtonHold(btn, mods);
}
void TSE::ICursorHandler::onMousePosition(ICursorHandler *handler, const Vector2 &pos)
{
handler->OnMousePosition(pos);
}
void TSE::ICursorHandler::onMouseScroll(ICursorHandler *handler, const Vector2 &delta)
{
handler->OnMouseScroll(delta);
}

View File

@@ -0,0 +1,36 @@
#pragma once
#include "enums/InputEnums.hpp"
#include "Vector2.hpp"
namespace TSE
{
class ICursorHandler
{
private:
bool Enabled = false;
public:
ICursorHandler() = default;
~ICursorHandler();
bool enabled() const;
void Enable();
void Disable();
inline virtual void OnMouseButtonDown(const MouseBtn& btn, const Modifier& mods) { };
inline virtual void OnMouseButtonUp(const MouseBtn& btn, const Modifier& mods) { };
inline virtual void OnMouseButtonHold(const MouseBtn& btn, const Modifier& mods) { };
inline virtual void OnMousePosition(const Vector2& pos) { };
inline virtual void OnMouseScroll(const Vector2& delta) { };
private:
static void onMouseButtonDown( ICursorHandler* handler, const MouseBtn& btn, const Modifier& mods);
static void onMouseButtonUp( ICursorHandler* handler, const MouseBtn& btn, const Modifier& mods);
static void onMouseButtonHold( ICursorHandler* handler, const MouseBtn& btn, const Modifier& mods);
static void onMousePosition( ICursorHandler* handler, const Vector2& pos);
static void onMouseScroll( ICursorHandler* handler, const Vector2& delta);
};
} // namespace TSE

View File

@@ -0,0 +1,42 @@
#include "IInputManager.hpp"
TSE::IInputManager* TSE::IInputManager::Instance = nullptr;
TSE::IInputManager *TSE::IInputManager::instance()
{
return Instance;
}
void TSE::IInputManager::RegisterCursorHandler(ICursorHandler *handler)
{
cursorHandler.push_back(handler);
}
void TSE::IInputManager::UnregisterCursorHandler(ICursorHandler *handler)
{
for(auto it = cursorHandler.begin(); it != cursorHandler.end(); it++)
{
if(*it == handler)
{
cursorHandler.erase(it);
return;
}
}
}
void TSE::IInputManager::RegisterKeyHandler(IKeyInputHandler *handler)
{
keyHandler.push_back(handler);
}
void TSE::IInputManager::UnregisterKeyHandler(IKeyInputHandler *handler)
{
for(auto it = keyHandler.begin(); it != keyHandler.end(); it++)
{
if(*it == handler)
{
keyHandler.erase(it);
return;
}
}
}

View File

@@ -0,0 +1,28 @@
#pragma once
#include "enums/InputEnums.hpp"
#include "ICursorHandler.hpp"
#include "IKeyInputHandler.hpp"
#include <vector>
namespace TSE
{
class IInputManager
{
protected:
static IInputManager* Instance;
std::vector<ICursorHandler*> cursorHandler;
std::vector<IKeyInputHandler*> keyHandler;
public:
static IInputManager* instance();
virtual void Delete() = 0;
void RegisterCursorHandler(ICursorHandler* handler);
void UnregisterCursorHandler(ICursorHandler* handler);
void RegisterKeyHandler(IKeyInputHandler* handler);
void UnregisterKeyHandler(IKeyInputHandler* handler);
};
} // namespace TSE

View File

@@ -0,0 +1,49 @@
#include "IKeyInputHandler.hpp"
#include "IInputManager.hpp"
TSE::IKeyInputHandler::~IKeyInputHandler()
{
if(enabled()) Disable();
}
bool TSE::IKeyInputHandler::enabled() const
{
return Enabled;
}
void TSE::IKeyInputHandler::Enable()
{
if(!enabled())
{
IInputManager::instance()->RegisterKeyHandler(this);
}
}
void TSE::IKeyInputHandler::Disable()
{
if(enabled())
{
IInputManager::instance()->UnregisterKeyHandler(this);
}
}
void TSE::IKeyInputHandler::onKeyDown(IKeyInputHandler *handler, const Key &key, const Modifier &mods)
{
handler->OnKeyDown(key, mods);
}
void TSE::IKeyInputHandler::onKeyUp(IKeyInputHandler *handler, const Key &key, const Modifier &mods)
{
handler->OnKeyUp(key, mods);
}
void TSE::IKeyInputHandler::onKeyHold(IKeyInputHandler *handler, const Key &key, const Modifier &mods)
{
handler->OnKeyHold(key, mods);
}
void TSE::IKeyInputHandler::onChar(IKeyInputHandler *handler, const std::string &c)
{
handler->OnChar(c);
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include "enums/InputEnums.hpp"
#include "Types.hpp"
namespace TSE
{
class IKeyInputHandler
{
private:
bool Enabled = false;
public:
IKeyInputHandler() = default;
~IKeyInputHandler();
bool enabled() const;
void Enable();
void Disable();
inline virtual void OnKeyDown(const Key& key, const Modifier& mods) { };
inline virtual void OnKeyUp(const Key& key, const Modifier& mods) { };
inline virtual void OnKeyHold(const Key& key, const Modifier& mods) { };
inline virtual void OnChar(const std::string& c) { };
private:
static void onKeyDown(IKeyInputHandler* handler, const Key& key, const Modifier& mods);
static void onKeyUp(IKeyInputHandler* handler, const Key& key, const Modifier& mods);
static void onKeyHold(IKeyInputHandler* handler, const Key& key, const Modifier& mods);
static void onChar(IKeyInputHandler* handler, const std::string& c);
};
} // namespace TSE

View File

@@ -3,6 +3,7 @@
#include "LuaStateHandler.hpp"
#include "Time.hpp"
#include "version.h"
#include "IInputManager.hpp"
TSE::IWindow* TSE::IWindow::lastWindow = nullptr;
@@ -21,5 +22,7 @@ void TSE::IWindow::BaseUpdate() const
TSE::IWindow::~IWindow()
{
IInputManager::instance()->Delete();
Time::Destroy();
Debug::Close();
}

View File

@@ -0,0 +1,79 @@
#include "JsonExports.hpp"
nlohmann::json TSE::ExportColor(Color &color)
{
using json = nlohmann::json;
json exp;
exp["r"] = color.r;
exp["g"] = color.g;
exp["b"] = color.b;
exp["a"] = color.a;
return exp;
}
nlohmann::json TSE::ExportVector2(Vector2 &vec2)
{
using json = nlohmann::json;
json exp;
exp["x"] = vec2.x;
exp["y"] = vec2.y;
return exp;
}
nlohmann::json TSE::ExportVector3(Vector3 &vec3)
{
using json = nlohmann::json;
json exp;
exp["x"] = vec3.x;
exp["y"] = vec3.y;
exp["z"] = vec3.z;
return exp;
}
nlohmann::json TSE::ExportVector4(Vector4 &vec4)
{
using json = nlohmann::json;
json exp;
exp["x"] = vec4.x;
exp["y"] = vec4.y;
exp["z"] = vec4.z;
exp["w"] = vec4.w;
return exp;
}
TSE::Color TSE::ImportColor(nlohmann::json json)
{
Color imp;
imp.r = json["r"];
imp.g = json["g"];
imp.b = json["b"];
imp.a = json["a"];
return imp;
}
TSE::Vector2 TSE::ImportVector2(nlohmann::json json)
{
Vector2 imp;
imp.x = json["x"];
imp.y = json["y"];
return imp;
}
TSE::Vector3 TSE::ImportVector3(nlohmann::json json)
{
Vector3 imp;
imp.x = json["x"];
imp.y = json["y"];
imp.z = json["z"];
return imp;
}
TSE::Vector4 TSE::ImportVector4(nlohmann::json json)
{
Vector4 imp;
imp.x = json["x"];
imp.y = json["y"];
imp.z = json["z"];
imp.w = json["w"];
return imp;
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "json.hpp"
#include "Color.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "Vector4.hpp"
namespace TSE
{
nlohmann::json ExportColor(Color& color);
nlohmann::json ExportVector2(Vector2& vec2);
nlohmann::json ExportVector3(Vector3& vec3);
nlohmann::json ExportVector4(Vector4& vec4);
Color ImportColor(nlohmann::json json);
Vector2 ImportVector2(nlohmann::json json);
Vector3 ImportVector3(nlohmann::json json);
Vector4 ImportVector4(nlohmann::json json);
} // namespace TSE