Code: Select all
glfwSetKeyCallback(window.get(), [](GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) // TODO: maybe temporary
glfwSetWindowShouldClose(window, GL_TRUE);
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS)
{
mw->kbd.OnKeyPressed(key);
}
else if (action == GLFW_RELEASE)
{
mw->kbd.OnKeyReleased(key);
}
else if (action == GLFW_REPEAT)
{
if (mw->kbd.AutorepeatIsEnabled())
mw->kbd.OnKeyPressed(key);
}
// else don't handle GLFW_KEY_UNKNOWN
});
glfwSetWindowFocusCallback(window.get(), [](GLFWwindow* window, int focused)
{
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
if (!focused)
{
// The window lost input focus
mw->kbd.ClearState();
}
});
glfwSetCharCallback(window.get(), [](GLFWwindow* window, std::uint32_t codepoint)
{
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
mw->kbd.OnChar(codepoint);
});
glfwSetCursorPosCallback(window.get(), [](GLFWwindow* window, double xpos, double ypos)
{
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
mw->mouse.OnMouseMove((int)xpos, (int)ypos);
// std::cout << xpos << ":" << ypos << '\n'; // TODO: remove
});
glfwSetCursorEnterCallback(window.get(), [](GLFWwindow* window, int entered)
{
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
if (entered)
{
mw->mouse.OnMouseEnter();
}
else
{
// The cursor left the client area of the window
auto left_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
auto right_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT);
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
short xposi = (short)xpos, yposi = (short)ypos;
if (left_state == GLFW_PRESS || right_state == GLFW_PRESS)
{
xposi = std::clamp((unsigned int)xposi, 0u, Graphics::ScreenWidth - 1);
yposi = std::clamp((unsigned int)yposi, 0u, Graphics::ScreenHeight - 1);
mw->mouse.OnMouseMove(xposi, yposi);
}
else
{
mw->mouse.OnMouseLeave();
mw->mouse.OnLeftReleased(xposi, yposi);
mw->mouse.OnRightReleased(xposi, yposi);
}
}
});
glfwSetMouseButtonCallback(window.get(), [](GLFWwindow* window, int button, int action, int mods)
{
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
if (action == GLFW_PRESS)
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
mw->mouse.OnLeftPressed((int)xpos, (int)ypos);
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
mw->mouse.OnRightPressed((int)xpos, (int)ypos);
}
}
else if (action == GLFW_RELEASE)
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
mw->mouse.OnLeftReleased((int)xpos, (int)ypos);
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
mw->mouse.OnRightReleased((int)xpos, (int)ypos);
}
}
});
glfwSetScrollCallback(window.get(), [](GLFWwindow* window, double xoffset, double yoffset)
{
MainWindow* mw = reinterpret_cast<MainWindow*>(glfwGetWindowUserPointer(window));
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
if (yoffset > 0.f) // TODO: check directions
{
mw->mouse.OnWheelDown((int)xpos, (int)ypos);
}
else if (yoffset < 0.f)
{
mw->mouse.OnWheelUp((int)xpos, (int)ypos);
}
});