From a0f81b0bd54621125d1eb1e0a5f9a94e6d17ec1f Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 14 Jun 2021 22:21:14 +0100 Subject: [PATCH] Allow event coordinate margins in SubWidget Signed-off-by: falkTX --- dgl/SubWidget.hpp | 16 ++++++++++++++++ dgl/src/SubWidget.cpp | 25 ++++++++++++++++++++----- dgl/src/SubWidgetPrivateData.cpp | 1 + dgl/src/SubWidgetPrivateData.hpp | 1 + dgl/src/WidgetPrivateData.cpp | 12 ++++++------ 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/dgl/SubWidget.hpp b/dgl/SubWidget.hpp index 9bd1614e..eab84b74 100644 --- a/dgl/SubWidget.hpp +++ b/dgl/SubWidget.hpp @@ -111,6 +111,22 @@ public: */ void setAbsolutePos(const Point& pos) noexcept; + /** + Get the margin currently in use for widget coordinates. + By default this value is (0,0). + */ + Point getMargin() const noexcept; + + /** + Set a margin to be used for widget coordinates using @a x and @a y values. + */ + void setMargin(int x, int y) noexcept; + + /** + Set a margin to be used for widget coordinates. + */ + void setMargin(const Point& offset) noexcept; + /** Get parent Widget, as passed in the constructor. */ diff --git a/dgl/src/SubWidget.cpp b/dgl/src/SubWidget.cpp index f8e1d01a..c4cfd0b8 100644 --- a/dgl/src/SubWidget.cpp +++ b/dgl/src/SubWidget.cpp @@ -32,9 +32,9 @@ SubWidget::~SubWidget() } template -bool SubWidget::contains(T x, T y) const noexcept +bool SubWidget::contains(const T x, const T y) const noexcept { - return Rectangle(0, 0, getWidth(), getHeight()).contains(x, y); + return Rectangle(0, 0, getWidth()-pData->margin.getX(), getHeight()-pData->margin.getY()).contains(x, y); } template @@ -70,17 +70,17 @@ Rectangle SubWidget::getConstrainedAbsoluteArea() const noexcept getSize()); } -void SubWidget::setAbsoluteX(int x) noexcept +void SubWidget::setAbsoluteX(const int x) noexcept { setAbsolutePos(Point(x, getAbsoluteY())); } -void SubWidget::setAbsoluteY(int y) noexcept +void SubWidget::setAbsoluteY(const int y) noexcept { setAbsolutePos(Point(getAbsoluteX(), y)); } -void SubWidget::setAbsolutePos(int x, int y) noexcept +void SubWidget::setAbsolutePos(const int x, const int y) noexcept { setAbsolutePos(Point(x, y)); } @@ -100,6 +100,21 @@ void SubWidget::setAbsolutePos(const Point& pos) noexcept repaint(); } +Point SubWidget::getMargin() const noexcept +{ + return pData->margin; +} + +void SubWidget::setMargin(const int x, const int y) noexcept +{ + pData->margin = Point(x, y); +} + +void SubWidget::setMargin(const Point& offset) noexcept +{ + pData->margin = offset; +} + Widget* SubWidget::getParentWidget() const noexcept { return pData->parentWidget; diff --git a/dgl/src/SubWidgetPrivateData.cpp b/dgl/src/SubWidgetPrivateData.cpp index b470f7f5..a0ce404c 100644 --- a/dgl/src/SubWidgetPrivateData.cpp +++ b/dgl/src/SubWidgetPrivateData.cpp @@ -26,6 +26,7 @@ SubWidget::PrivateData::PrivateData(SubWidget* const s, Widget* const pw) selfw((Widget*)s), parentWidget(pw), absolutePos(), + margin(), needsFullViewportForDrawing(false), needsViewportScaling(false), skipDrawing(false), diff --git a/dgl/src/SubWidgetPrivateData.hpp b/dgl/src/SubWidgetPrivateData.hpp index a46655b2..a06f59d1 100644 --- a/dgl/src/SubWidgetPrivateData.hpp +++ b/dgl/src/SubWidgetPrivateData.hpp @@ -28,6 +28,7 @@ struct SubWidget::PrivateData { Widget* const selfw; Widget* const parentWidget; Point absolutePos; + Point margin; bool needsFullViewportForDrawing; // needed for widgets drawing out of bounds bool needsViewportScaling; // needed for NanoVG bool skipDrawing; // for context reuse in NanoVG based guis diff --git a/dgl/src/WidgetPrivateData.cpp b/dgl/src/WidgetPrivateData.cpp index 60be6c88..304ad6ef 100644 --- a/dgl/src/WidgetPrivateData.cpp +++ b/dgl/src/WidgetPrivateData.cpp @@ -152,8 +152,8 @@ bool Widget::PrivateData::giveMouseEventForSubWidgets(MouseEvent& ev) if (! widget->isVisible()) continue; - ev.pos = Point(x - widget->getAbsoluteX(), - y - widget->getAbsoluteY()); + ev.pos = Point(x - widget->getAbsoluteX() + widget->getMargin().getX(), + y - widget->getAbsoluteY() + widget->getMargin().getY()); if (widget->onMouse(ev)) return true; @@ -191,8 +191,8 @@ bool Widget::PrivateData::giveMotionEventForSubWidgets(MotionEvent& ev) if (! widget->isVisible()) continue; - ev.pos = Point(x - widget->getAbsoluteX(), - y - widget->getAbsoluteY()); + ev.pos = Point(x - widget->getAbsoluteX() + widget->getMargin().getX(), + y - widget->getAbsoluteY() + widget->getMargin().getY()); if (widget->onMotion(ev)) return true; @@ -230,8 +230,8 @@ bool Widget::PrivateData::giveScrollEventForSubWidgets(ScrollEvent& ev) if (! widget->isVisible()) continue; - ev.pos = Point(x - widget->getAbsoluteX(), - y - widget->getAbsoluteY()); + ev.pos = Point(x - widget->getAbsoluteX() + widget->getMargin().getX(), + y - widget->getAbsoluteY() + widget->getMargin().getY()); if (widget->onScroll(ev)) return true;