* Improve mouse grabbing.

This fixes a bug where the mouse would be restricted to the window after the level is loaded even if the window isn't focused.
This commit is contained in:
iProgramInCpp
2023-08-01 11:08:56 +03:00
parent 67d1fc4f44
commit af11c5281c
6 changed files with 30 additions and 2 deletions

View File

@@ -244,8 +244,13 @@ void AppPlatform_windows::setMouseGrabbed(bool b)
m_bGrabbedMouse = b;
if (!b)
if (m_bActuallyGrabbedMouse == (b && m_bIsFocused))
return;
if (!b || !m_bIsFocused)
{
m_bActuallyGrabbedMouse = false;
//show the cursor
ShowCursor(TRUE);
@@ -256,6 +261,8 @@ void AppPlatform_windows::setMouseGrabbed(bool b)
}
else
{
m_bActuallyGrabbedMouse = true;
//hide the cursor
ShowCursor(FALSE);
@@ -290,6 +297,12 @@ void AppPlatform_windows::clearDiff()
m_MouseDiffX = m_MouseDiffY = 0;
}
void AppPlatform_windows::updateFocused(bool focused)
{
m_bIsFocused = focused;
setMouseGrabbed(m_bGrabbedMouse);
}
bool AppPlatform_windows::shiftPressed()
{
return m_bShiftPressed;

View File

@@ -42,6 +42,7 @@ public:
void setMouseGrabbed(bool b) override;
void getMouseDiff(int& x, int& y) override;
void clearDiff() override;
void updateFocused(bool focused) override;
// Also add these to allow proper text input within the game.
bool shiftPressed() override;
@@ -56,7 +57,9 @@ private:
std::vector<std::string> m_UserInput;
int m_UserInputStatus = -1;
bool m_bIsFocused = false;
bool m_bGrabbedMouse = false;
bool m_bActuallyGrabbedMouse = false;
bool m_bWasUnfocused = false;
bool m_bShiftPressed = false;

View File

@@ -77,6 +77,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
default:
return DefWindowProc(hWnd, iMsg, wParam, lParam);
case WM_SETFOCUS:
case WM_KILLFOCUS:
{
g_AppPlatform.updateFocused(iMsg == WM_SETFOCUS);
break;
}
case WM_LBUTTONUP:
{
if (g_LButtonDown)

View File

@@ -114,6 +114,10 @@ void AppPlatform::clearDiff()
}
void AppPlatform::updateFocused(bool focused)
{
}
bool AppPlatform::shiftPressed()
{
return false;

View File

@@ -47,6 +47,7 @@ public:
virtual void setMouseGrabbed(bool b);
virtual void getMouseDiff(int& x, int& y);
virtual void clearDiff();
virtual void updateFocused(bool focused);
// Also add this to allow proper text input within the game.
virtual bool shiftPressed();
#endif

View File

@@ -188,7 +188,7 @@ label_5:
pInv->setSelectionSlotItemId(0, item);
pInv->selectSlot(0);
// @TODO: SoundEngine
m_pMinecraft->m_pSoundEngine->play("random.click");
m_pMinecraft->setScreen(nullptr);
}