Browse Source

Add onPositionChanged callback for widgets

pull/77/head
Patrick Desaulniers Filipe Coelho <falktx@falktx.com> 3 years ago
parent
commit
bf7594cbad
2 changed files with 33 additions and 10 deletions
  1. +21
    -0
      dgl/Widget.hpp
  2. +12
    -10
      dgl/src/Widget.cpp

+ 21
- 0
dgl/Widget.hpp View File

@@ -175,6 +175,22 @@ public:
oldSize(0, 0) {}
};

/**
Widget position changed event.
@a pos The new absolute position of the widget.
@a oldPos The previous absolute position of the widget.
@see onPositionChanged
*/
struct PositionChangedEvent {
Point<int> pos;
Point<int> oldPos;

/** Constuctor */
PositionChangedEvent() noexcept
: pos(0, 0),
oldPos(0, 0) {}
};

/**
Constructor.
*/
@@ -362,6 +378,11 @@ protected:
*/
virtual void onResize(const ResizeEvent&);

/**
A function called when the widget's absolute position is changed.
*/
virtual void onPositionChanged(const PositionChangedEvent&);

private:
struct PrivateData;
PrivateData* const pData;


+ 12
- 10
dgl/src/Widget.cpp View File

@@ -151,20 +151,12 @@ const Point<int>& Widget::getAbsolutePos() const noexcept

void Widget::setAbsoluteX(int x) noexcept
{
if (pData->absolutePos.getX() == x)
return;

pData->absolutePos.setX(x);
pData->parent.repaint();
setAbsolutePos(Point<int>(x, getAbsoluteY()));
}

void Widget::setAbsoluteY(int y) noexcept
{
if (pData->absolutePos.getY() == y)
return;

pData->absolutePos.setY(y);
pData->parent.repaint();
setAbsolutePos(Point<int>(getAbsoluteX(), y));
}

void Widget::setAbsolutePos(int x, int y) noexcept
@@ -177,7 +169,13 @@ void Widget::setAbsolutePos(const Point<int>& pos) noexcept
if (pData->absolutePos == pos)
return;

PositionChangedEvent ev;
ev.oldPos = pData->absolutePos;
ev.pos = pos;

pData->absolutePos = pos;
onPositionChanged(ev);

pData->parent.repaint();
}

@@ -245,6 +243,10 @@ void Widget::onResize(const ResizeEvent&)
{
}

void Widget::onPositionChanged(const PositionChangedEvent&)
{
}

// -----------------------------------------------------------------------

END_NAMESPACE_DGL

Loading…
Cancel
Save