mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:07:45 +00:00
Constrain the mouse cursor to keep it inside the screen rect.
This commit is contained in:
parent
b95aa18315
commit
9bc7b128b2
3 changed files with 23 additions and 6 deletions
|
@ -40,6 +40,7 @@ void AbstractScreen::did_receive_mouse_data(int dx, int dy, bool left_button, bo
|
||||||
{
|
{
|
||||||
auto prev_location = m_cursor_location;
|
auto prev_location = m_cursor_location;
|
||||||
m_cursor_location.moveBy(dx, dy);
|
m_cursor_location.moveBy(dx, dy);
|
||||||
|
m_cursor_location.constrain(rect());
|
||||||
if (m_cursor_location.x() >= width())
|
if (m_cursor_location.x() >= width())
|
||||||
m_cursor_location.setX(width() - 1);
|
m_cursor_location.setX(width() - 1);
|
||||||
if (m_cursor_location.y() >= height())
|
if (m_cursor_location.y() >= height())
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
class Rect;
|
||||||
|
|
||||||
class Point {
|
class Point {
|
||||||
public:
|
public:
|
||||||
Point() { }
|
Point() { }
|
||||||
|
@ -22,6 +24,8 @@ public:
|
||||||
moveBy(delta.x(), delta.y());
|
moveBy(delta.x(), delta.y());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void constrain(const Rect&);
|
||||||
|
|
||||||
bool operator==(const Point& other) const
|
bool operator==(const Point& other) const
|
||||||
{
|
{
|
||||||
return m_x == other.m_x
|
return m_x == other.m_x
|
||||||
|
|
|
@ -64,9 +64,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
int left() const { return x(); }
|
int left() const { return x(); }
|
||||||
int right() const { return x() + width(); }
|
int right() const { return x() + width() - 1; }
|
||||||
int top() const { return y(); }
|
int top() const { return y(); }
|
||||||
int bottom() const { return y() + height(); }
|
int bottom() const { return y() + height() - 1; }
|
||||||
|
|
||||||
void setLeft(int left)
|
void setLeft(int left)
|
||||||
{
|
{
|
||||||
|
@ -82,10 +82,10 @@ public:
|
||||||
|
|
||||||
bool intersects(const Rect& other) const
|
bool intersects(const Rect& other) const
|
||||||
{
|
{
|
||||||
return left() < other.right()
|
return left() <= other.right()
|
||||||
&& other.left() < right()
|
&& other.left() <= right()
|
||||||
&& top() < other.bottom()
|
&& top() <= other.bottom()
|
||||||
&& other.top() < bottom();
|
&& other.top() <= bottom();
|
||||||
}
|
}
|
||||||
|
|
||||||
int x() const { return location().x(); }
|
int x() const { return location().x(); }
|
||||||
|
@ -120,3 +120,15 @@ private:
|
||||||
Point m_location;
|
Point m_location;
|
||||||
Size m_size;
|
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());
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue