Commit 41b57602 authored by chili's avatar chili
Browse files

capture mouse on drag and track mouse entry/exit

parent 569af691
......@@ -36,6 +36,11 @@ int Mouse::GetPosY() const noexcept
return y;
}
bool Mouse::IsInWindow() const noexcept
{
return isInWindow;
}
bool Mouse::LeftIsPressed() const noexcept
{
return leftIsPressed;
......@@ -74,6 +79,20 @@ void Mouse::OnMouseMove( int newx,int newy ) noexcept
TrimBuffer();
}
void Mouse::OnMouseLeave() noexcept
{
isInWindow = false;
buffer.push( Mouse::Event( Mouse::Event::Type::Leave,*this ) );
TrimBuffer();
}
void Mouse::OnMouseEnter() noexcept
{
isInWindow = true;
buffer.push( Mouse::Event( Mouse::Event::Type::Enter,*this ) );
TrimBuffer();
}
void Mouse::OnLeftPressed( int x,int y ) noexcept
{
leftIsPressed = true;
......
......@@ -37,6 +37,8 @@ public:
WheelUp,
WheelDown,
Move,
Enter,
Leave,
Invalid
};
private:
......@@ -98,6 +100,7 @@ public:
std::pair<int,int> GetPos() const noexcept;
int GetPosX() const noexcept;
int GetPosY() const noexcept;
bool IsInWindow() const noexcept;
bool LeftIsPressed() const noexcept;
bool RightIsPressed() const noexcept;
Mouse::Event Read() noexcept;
......@@ -108,6 +111,8 @@ public:
void Flush() noexcept;
private:
void OnMouseMove( int x,int y ) noexcept;
void OnMouseLeave() noexcept;
void OnMouseEnter() noexcept;
void OnLeftPressed( int x,int y ) noexcept;
void OnLeftReleased( int x,int y ) noexcept;
void OnRightPressed( int x,int y ) noexcept;
......@@ -121,5 +126,6 @@ private:
int y;
bool leftIsPressed = false;
bool rightIsPressed = false;
bool isInWindow = false;
std::queue<Event> buffer;
};
\ No newline at end of file
......@@ -175,7 +175,30 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
case WM_MOUSEMOVE:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnMouseMove( pt.x,pt.y );
// 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 )
{
mouse.OnMouseMove( pt.x,pt.y );
if( !mouse.IsInWindow() )
{
SetCapture( hWnd );
mouse.OnMouseEnter();
}
}
// not in client -> log move / maintain capture if button down
else
{
if( wParam & (MK_LBUTTON | MK_RBUTTON) )
{
mouse.OnMouseMove( pt.x,pt.y );
}
// button up -> release capture / log event for leaving
else
{
ReleaseCapture();
mouse.OnMouseLeave();
}
}
break;
}
case WM_LBUTTONDOWN:
......@@ -194,12 +217,24 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnLeftReleased( pt.x,pt.y );
// release mouse if outside of window
if( pt.x < 0 || pt.x >= width || pt.y < 0 || pt.y >= height )
{
ReleaseCapture();
mouse.OnMouseLeave();
}
break;
}
case WM_RBUTTONUP:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnRightReleased( pt.x,pt.y );
// release mouse if outside of window
if( pt.x < 0 || pt.x >= width || pt.y < 0 || pt.y >= height )
{
ReleaseCapture();
mouse.OnMouseLeave();
}
break;
}
case WM_MOUSEWHEEL:
......
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