summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-01-12 01:00:24 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-01-12 01:00:24 +0100
commit9bc7b128b2a4412ec4dd0467720d2e795af3b6cc (patch)
tree9a38e3f88cb85b810c7a1679bacb305abbb33551
parentb95aa1831529d7cbd9cdfa05797836bf5fce21be (diff)
downloadserenity-9bc7b128b2a4412ec4dd0467720d2e795af3b6cc.zip
Constrain the mouse cursor to keep it inside the screen rect.
-rw-r--r--Widgets/AbstractScreen.cpp1
-rw-r--r--Widgets/Point.h4
-rw-r--r--Widgets/Rect.h24
3 files changed, 23 insertions, 6 deletions
diff --git a/Widgets/AbstractScreen.cpp b/Widgets/AbstractScreen.cpp
index f3da849f50..fea72183ce 100644
--- a/Widgets/AbstractScreen.cpp
+++ b/Widgets/AbstractScreen.cpp
@@ -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())
diff --git a/Widgets/Point.h b/Widgets/Point.h
index 4cb2e28057..99fd6db1bd 100644
--- a/Widgets/Point.h
+++ b/Widgets/Point.h
@@ -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
diff --git a/Widgets/Rect.h b/Widgets/Rect.h
index 924f661624..f5e8f773fa 100644
--- a/Widgets/Rect.h
+++ b/Widgets/Rect.h
@@ -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());
+}