From ee31d72f912ec93f43feff8bfbd1c594bc6f6524 Mon Sep 17 00:00:00 2001 From: NOTPIES Date: Mon, 2 Mar 2026 00:56:17 -0300 Subject: [PATCH] fix: better cursor handling --- .../UI/UIScene_AbstractContainerMenu.cpp | 17 ++------- Minecraft.Client/KeyboardMouseInput.cpp | 36 +++++++++++++++++-- Minecraft.Client/KeyboardMouseInput.h | 5 +++ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Minecraft.Client/Common/UI/UIScene_AbstractContainerMenu.cpp b/Minecraft.Client/Common/UI/UIScene_AbstractContainerMenu.cpp index 66b3569..427567b 100644 --- a/Minecraft.Client/Common/UI/UIScene_AbstractContainerMenu.cpp +++ b/Minecraft.Client/Common/UI/UIScene_AbstractContainerMenu.cpp @@ -45,7 +45,7 @@ void UIScene_AbstractContainerMenu::handleDestroy() g_savedInventoryCursorPos.y = m_pointerPos.y; g_savedInventoryCursorPos.hasSavedPos = true; - while (ShowCursor(TRUE) < 0); + g_KBMInput.SetCursorHiddenForUI(false); #endif Minecraft *pMinecraft = Minecraft::GetInstance(); @@ -82,7 +82,7 @@ void UIScene_AbstractContainerMenu::InitDataAssociations(int iPad, AbstractConta void UIScene_AbstractContainerMenu::PlatformInitialize(int iPad, int startIndex) { #ifdef _WINDOWS64 - while (ShowCursor(FALSE) >= 0); + g_KBMInput.SetCursorHiddenForUI(true); #endif m_labelInventory.init( app.GetString(IDS_INVENTORY) ); @@ -179,15 +179,6 @@ void UIScene_AbstractContainerMenu::PlatformInitialize(int iPad, int startIndex) if (m_pointerPos.y < m_fPointerMinY) m_pointerPos.y = m_fPointerMinY; if (m_pointerPos.y > m_fPointerMaxY) m_pointerPos.y = m_fPointerMaxY; } - - extern HWND g_hWnd; - RECT rc; - GetClientRect(g_hWnd, &rc); - POINT center; - center.x = (rc.right - rc.left) / 2; - center.y = (rc.bottom - rc.top) / 2; - ClientToScreen(g_hWnd, ¢er); - SetCursorPos(center.x, center.y); #endif IggyEvent mouseEvent; @@ -212,10 +203,6 @@ void UIScene_AbstractContainerMenu::tick() { UIScene::tick(); -#ifdef _WINDOWS64 - SetCursor(NULL); -#endif - onMouseTick(); IggyEvent mouseEvent; diff --git a/Minecraft.Client/KeyboardMouseInput.cpp b/Minecraft.Client/KeyboardMouseInput.cpp index 31c5233..4d5855c 100644 --- a/Minecraft.Client/KeyboardMouseInput.cpp +++ b/Minecraft.Client/KeyboardMouseInput.cpp @@ -33,6 +33,7 @@ void KeyboardMouseInput::Init() m_mouseWheel = 0; m_mouseWheelAccum = 0; m_mouseGrabbed = false; + m_cursorHiddenForUI = false; m_windowFocused = true; m_hasInput = false; @@ -106,7 +107,7 @@ void KeyboardMouseInput::Tick() } } - if (m_mouseGrabbed && g_hWnd) + if ((m_mouseGrabbed || m_cursorHiddenForUI) && g_hWnd) { RECT rc; GetClientRect(g_hWnd, &rc); @@ -239,7 +240,36 @@ void KeyboardMouseInput::SetMouseGrabbed(bool grabbed) m_mouseDeltaAccumX = 0; m_mouseDeltaAccumY = 0; } - else if (!grabbed && g_hWnd) + else if (!grabbed && !m_cursorHiddenForUI && g_hWnd) + { + while (ShowCursor(TRUE) < 0) {} + ClipCursor(NULL); + } +} + +void KeyboardMouseInput::SetCursorHiddenForUI(bool hidden) +{ + if (m_cursorHiddenForUI == hidden) + return; + + m_cursorHiddenForUI = hidden; + if (hidden && g_hWnd) + { + while (ShowCursor(FALSE) >= 0) {} + ClipCursorToWindow(g_hWnd); + + RECT rc; + GetClientRect(g_hWnd, &rc); + POINT center; + center.x = (rc.right - rc.left) / 2; + center.y = (rc.bottom - rc.top) / 2; + ClientToScreen(g_hWnd, ¢er); + SetCursorPos(center.x, center.y); + + m_mouseDeltaAccumX = 0; + m_mouseDeltaAccumY = 0; + } + else if (!hidden && !m_mouseGrabbed && g_hWnd) { while (ShowCursor(TRUE) < 0) {} ClipCursor(NULL); @@ -264,7 +294,7 @@ void KeyboardMouseInput::SetWindowFocused(bool focused) m_windowFocused = focused; if (focused) { - if (m_mouseGrabbed) + if (m_mouseGrabbed || m_cursorHiddenForUI) { while (ShowCursor(FALSE) >= 0) {} ClipCursorToWindow(g_hWnd); diff --git a/Minecraft.Client/KeyboardMouseInput.h b/Minecraft.Client/KeyboardMouseInput.h index c82509a..03e4e29 100644 --- a/Minecraft.Client/KeyboardMouseInput.h +++ b/Minecraft.Client/KeyboardMouseInput.h @@ -58,6 +58,9 @@ public: void SetMouseGrabbed(bool grabbed); bool IsMouseGrabbed() const { return m_mouseGrabbed; } + void SetCursorHiddenForUI(bool hidden); + bool IsCursorHiddenForUI() const { return m_cursorHiddenForUI; } + void SetWindowFocused(bool focused); bool IsWindowFocused() const { return m_windowFocused; } @@ -99,6 +102,8 @@ private: bool m_mouseGrabbed; + bool m_cursorHiddenForUI; + bool m_windowFocused; bool m_hasInput;