mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 12:35:14 +00:00
Constrain the mouse cursor to keep it inside the screen rect.
This commit is contained in:
parent
b95aa18315
commit
9bc7b128b2
Notes:
sideshowbarker
2024-07-19 16:04:55 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9bc7b128b2a
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;
|
||||
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())
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue