Constrain the mouse cursor to keep it inside the screen rect.

This commit is contained in:
Andreas Kling 2019-01-12 01:00:24 +01:00
parent b95aa18315
commit 9bc7b128b2
Notes: sideshowbarker 2024-07-19 16:04:55 +09:00
3 changed files with 23 additions and 6 deletions

View file

@ -40,6 +40,7 @@ void AbstractScreen::did_receive_mouse_data(int dx, int dy, bool left_button, bo
{
auto prev_location = m_cursor_location;
m_cursor_location.moveBy(dx, dy);
m_cursor_location.constrain(rect());
if (m_cursor_location.x() >= width())
m_cursor_location.setX(width() - 1);
if (m_cursor_location.y() >= height())

View file

@ -1,5 +1,7 @@
#pragma once
class Rect;
class Point {
public:
Point() { }
@ -22,6 +24,8 @@ public:
moveBy(delta.x(), delta.y());
}
void constrain(const Rect&);
bool operator==(const Point& other) const
{
return m_x == other.m_x

View file

@ -64,9 +64,9 @@ public:
}
int left() const { return x(); }
int right() const { return x() + width(); }
int right() const { return x() + width() - 1; }
int top() const { return y(); }
int bottom() const { return y() + height(); }
int bottom() const { return y() + height() - 1; }
void setLeft(int left)
{
@ -82,10 +82,10 @@ public:
bool intersects(const Rect& other) const
{
return left() < other.right()
&& other.left() < right()
&& top() < other.bottom()
&& other.top() < bottom();
return left() <= other.right()
&& other.left() <= right()
&& top() <= other.bottom()
&& other.top() <= bottom();
}
int x() const { return location().x(); }
@ -120,3 +120,15 @@ private:
Point m_location;
Size m_size;
};
inline void Point::constrain(const Rect& rect)
{
if (x() < rect.left())
setX(rect.left());
else if (x() > rect.right())
setX(rect.right());
if (y() < rect.top())
setY(rect.top());
else if (y() > rect.bottom())
setY(rect.bottom());
}