added input system
This commit is contained in:
111
TSE_GlfwImpl/src/InputGlfw.cpp
Normal file
111
TSE_GlfwImpl/src/InputGlfw.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user