Commit 7a461c18 authored by chili's avatar chili
Browse files

mousetrap

parent 12914fe9
...@@ -119,18 +119,20 @@ void Window::SetTitle( const std::string& title ) ...@@ -119,18 +119,20 @@ void Window::SetTitle( const std::string& title )
} }
} }
void Window::EnableCursor() void Window::EnableCursor() noexcept
{ {
cursorEnabled = true; cursorEnabled = true;
ShowCursor(); ShowCursor();
EnableImGuiMouse(); EnableImGuiMouse();
FreeCursor();
} }
void Window::DisableCursor() void Window::DisableCursor() noexcept
{ {
cursorEnabled = false; cursorEnabled = false;
HideCursor(); HideCursor();
DisableImGuiMouse(); DisableImGuiMouse();
ConfineCursor();
} }
std::optional<int> Window::ProcessMessages() noexcept std::optional<int> Window::ProcessMessages() noexcept
...@@ -164,22 +166,35 @@ Graphics& Window::Gfx() ...@@ -164,22 +166,35 @@ Graphics& Window::Gfx()
return *pGfx; return *pGfx;
} }
void Window::HideCursor() void Window::ConfineCursor() noexcept
{
RECT rect;
GetClientRect( hWnd,&rect );
MapWindowPoints( hWnd,nullptr,reinterpret_cast<POINT*>(&rect),2 );
ClipCursor( &rect );
}
void Window::FreeCursor() noexcept
{
ClipCursor( nullptr );
}
void Window::HideCursor() noexcept
{ {
while( ::ShowCursor( FALSE ) >= 0 ); while( ::ShowCursor( FALSE ) >= 0 );
} }
void Window::ShowCursor() void Window::ShowCursor() noexcept
{ {
while( ::ShowCursor( TRUE ) < 0 ); while( ::ShowCursor( TRUE ) < 0 );
} }
void Window::EnableImGuiMouse() void Window::EnableImGuiMouse() noexcept
{ {
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouse; ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouse;
} }
void Window::DisableImGuiMouse() void Window::DisableImGuiMouse() noexcept
{ {
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouse; ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouse;
} }
...@@ -267,6 +282,7 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex ...@@ -267,6 +282,7 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
/************* MOUSE MESSAGES ****************/ /************* MOUSE MESSAGES ****************/
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
{ {
const POINTS pt = MAKEPOINTS( lParam );
// cursorless exclusive gets first dibs // cursorless exclusive gets first dibs
if( !cursorEnabled ) if( !cursorEnabled )
{ {
...@@ -283,7 +299,6 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex ...@@ -283,7 +299,6 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
{ {
break; break;
} }
const POINTS pt = MAKEPOINTS( lParam );
// in client region -> log move, and log enter + capture mouse (if not previously in window) // in client region -> log move, and log enter + capture mouse (if not previously in window)
if( pt.x >= 0 && pt.x < width && pt.y >= 0 && pt.y < height ) if( pt.x >= 0 && pt.x < width && pt.y >= 0 && pt.y < height )
{ {
......
...@@ -75,15 +75,17 @@ public: ...@@ -75,15 +75,17 @@ public:
Window( const Window& ) = delete; Window( const Window& ) = delete;
Window& operator=( const Window& ) = delete; Window& operator=( const Window& ) = delete;
void SetTitle( const std::string& title ); void SetTitle( const std::string& title );
void EnableCursor(); void EnableCursor() noexcept;
void DisableCursor(); void DisableCursor() noexcept;
static std::optional<int> ProcessMessages() noexcept; static std::optional<int> ProcessMessages() noexcept;
Graphics& Gfx(); Graphics& Gfx();
private: private:
void HideCursor(); void ConfineCursor() noexcept;
void ShowCursor(); void FreeCursor() noexcept;
void EnableImGuiMouse(); void ShowCursor() noexcept;
void DisableImGuiMouse(); void HideCursor() noexcept;
void EnableImGuiMouse() noexcept;
void DisableImGuiMouse() noexcept;
static LRESULT CALLBACK HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept; static LRESULT CALLBACK HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
static LRESULT CALLBACK HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept; static LRESULT CALLBACK HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept; LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment