diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index fd17136b..5205d75d 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -55,7 +55,7 @@ set(SGL_LOCAL_SLANG OFF CACHE BOOL "Use a local build of slang instead of downlo set(SGL_LOCAL_SLANG_DIR "${CMAKE_SOURCE_DIR}/../slang" CACHE PATH "Path to a local slang build") set(SGL_LOCAL_SLANG_BUILD_DIR "build/Debug" CACHE STRING "Build directory of the local slang build") -set(SLANG_VERSION "2024.15.1") +set(SLANG_VERSION "2024.15.2") set(SLANG_URL_BASE "https://github.com/shader-slang/slang/releases/download/v${SLANG_VERSION}") if(SGL_WINDOWS) diff --git a/src/sgl/core/input.h b/src/sgl/core/input.h index 6dfb43be..28b3dc41 100644 --- a/src/sgl/core/input.h +++ b/src/sgl/core/input.h @@ -10,6 +10,26 @@ namespace sgl { +/// Mouse cursor modes. +enum class CursorMode : uint32_t { + /// The cursor is visible and behaves normally. + normal, + /// The cursor is hidden when over the window. + hidden, + /// The cursor is hidden and locked to the window. + disabled, +}; + +SGL_ENUM_INFO( + CursorMode, + { + {CursorMode::normal, "normal"}, + {CursorMode::hidden, "hidden"}, + {CursorMode::disabled, "disabled"}, + } +); +SGL_ENUM_REGISTER(CursorMode); + /// Mouse buttons. enum class MouseButton : uint32_t { left, diff --git a/src/sgl/core/python/input.cpp b/src/sgl/core/python/input.cpp index cd28398c..73d5b7fc 100644 --- a/src/sgl/core/python/input.cpp +++ b/src/sgl/core/python/input.cpp @@ -8,6 +8,7 @@ SGL_PY_EXPORT(core_input) { using namespace sgl; + nb::sgl_enum(m, "CursorMode", D_NA(CursorMode)); nb::sgl_enum(m, "MouseButton", D(MouseButton)); nb::sgl_enum(m, "KeyModifierFlags", D(KeyModifierFlags)); nb::sgl_enum(m, "KeyModifier", D(KeyModifier)); diff --git a/src/sgl/core/python/window.cpp b/src/sgl/core/python/window.cpp index ea73807f..321263d7 100644 --- a/src/sgl/core/python/window.cpp +++ b/src/sgl/core/python/window.cpp @@ -60,6 +60,7 @@ SGL_PY_EXPORT(core_window) window.def("process_events", &Window::process_events, D(Window, process_events)); window.def("set_clipboard", &Window::set_clipboard, "text"_a, D(Window, set_clipboard)); window.def("get_clipboard", &Window::get_clipboard, D(Window, get_clipboard)); + window.def_prop_rw("cursor_mode", &Window::cursor_mode, &Window::set_cursor_mode, D_NA(Window, cursor_mode)); window.def_prop_rw("on_resize", &Window::on_resize, &Window::set_on_resize, nb::arg().none(), D(Window, on_resize)); window.def_prop_rw( diff --git a/src/sgl/core/window.cpp b/src/sgl/core/window.cpp index f40c2e26..1eca5460 100644 --- a/src/sgl/core/window.cpp +++ b/src/sgl/core/window.cpp @@ -453,6 +453,26 @@ std::optional Window::get_clipboard() const return text ? std::optional(text) : std::nullopt; } +void Window::set_cursor_mode(CursorMode mode) +{ + if (mode != m_cursor_mode) { + m_cursor_mode = mode; + switch (mode) { + case CursorMode::normal: + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + break; + case CursorMode::hidden: + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + break; + case CursorMode::disabled: + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + break; + default: + SGL_UNREACHABLE(); + } + } +} + std::string Window::to_string() const { return fmt::format( diff --git a/src/sgl/core/window.h b/src/sgl/core/window.h index 992b2713..5b0e03f7 100644 --- a/src/sgl/core/window.h +++ b/src/sgl/core/window.h @@ -92,6 +92,10 @@ class SGL_API Window : public Object { /// Get the clipboard content. std::optional get_clipboard() const; + /// The mouse cursor mode. + CursorMode cursor_mode() const { return m_cursor_mode; } + void set_cursor_mode(CursorMode mode); + // events using ResizeCallback = std::function; @@ -152,6 +156,7 @@ class SGL_API Window : public Object { bool m_should_close{false}; + CursorMode m_cursor_mode{CursorMode::normal}; float2 m_mouse_pos{0.f, 0.f}; KeyModifierFlags m_mods{KeyModifierFlags::none};