tibia-client/src/framework/ui/uianchorlayout.cpp

124 lines
4.3 KiB
C++
Raw Normal View History

2011-08-14 04:09:11 +02:00
#include "uianchorlayout.h"
#include "uiwidget.h"
2011-08-14 04:09:11 +02:00
bool UIAnchorLayout::addAnchor(const UIWidgetPtr& anchoredWidget, AnchorPoint anchoredEdge, const AnchorLine& anchorLine)
{
2011-08-14 04:09:11 +02:00
UIAnchor anchor(anchoredWidget, anchoredEdge, anchorLine);
UIWidgetPtr anchorLineWidget = anchor.getAnchorLineWidget();
2011-08-14 04:09:11 +02:00
/*
if(!anchorLineWidget) {
2011-08-20 22:30:41 +02:00
logError("could not find the widget to anchor on, wrong id?");
2011-05-09 23:08:17 +02:00
return false;
}
2011-08-14 04:09:11 +02:00
*/
2011-05-09 23:08:17 +02:00
// we can never anchor with itself
2011-08-14 04:09:11 +02:00
if(anchoredWidget == anchorLineWidget) {
2011-08-20 22:30:41 +02:00
logError("anchoring with itself is not possible");
return false;
}
2011-05-09 23:08:17 +02:00
// we must never anchor to an anchor child
2011-08-14 04:09:11 +02:00
if(anchoredWidget && hasWidgetInAnchorTree(anchorLineWidget, anchoredWidget)) {
2011-08-20 22:30:41 +02:00
logError("anchors is miss configured, you must never make an anchor chains in loops");
2011-05-09 23:08:17 +02:00
return false;
}
2011-08-20 22:30:41 +02:00
// avoid duplicated anchors
for(auto it = m_anchors.begin(); it != m_anchors.end(); ++it) {
const UIAnchor& otherAnchor = *it;
if(otherAnchor.getAnchoredWidget() == anchoredWidget && otherAnchor.getAnchoredEdge() == anchoredEdge) {
m_anchors.erase(it);
break;
}
}
// setup the anchor
m_anchors.push_back(anchor);
2011-08-14 04:09:11 +02:00
// recalculate anchored widget layout
updateWidget(anchoredWidget);
return true;
}
2011-08-14 04:09:11 +02:00
void UIAnchorLayout::updateWidget(const UIWidgetPtr& widget)
{
2011-08-14 04:09:11 +02:00
Rect rect = widget->getGeometry();
bool verticalMoved = false;
bool horizontalMoved = false;
// FIXME: release expired anchors
2011-08-14 04:09:11 +02:00
for(const UIAnchor& anchor : m_anchors) {
if(anchor.getAnchoredWidget() == widget && anchor.getAnchorLineWidget()) {
int point = anchor.getAnchorLinePoint();
switch(anchor.getAnchoredEdge()) {
2011-07-17 08:56:57 +02:00
case AnchorHorizontalCenter:
2011-08-14 04:09:11 +02:00
rect.moveHorizontalCenter(point + widget->getMarginLeft() - widget->getMarginRight());
horizontalMoved = true;
break;
2011-07-17 08:56:57 +02:00
case AnchorLeft:
if(!horizontalMoved) {
2011-08-14 04:09:11 +02:00
rect.moveLeft(point + widget->getMarginLeft());
horizontalMoved = true;
} else
2011-08-14 04:09:11 +02:00
rect.setLeft(point + widget->getMarginLeft());
break;
2011-07-17 08:56:57 +02:00
case AnchorRight:
if(!horizontalMoved) {
2011-08-14 04:09:11 +02:00
rect.moveRight(point - widget->getMarginRight());
horizontalMoved = true;
} else
2011-08-14 04:09:11 +02:00
rect.setRight(point - widget->getMarginRight());
break;
2011-07-17 08:56:57 +02:00
case AnchorVerticalCenter:
2011-08-14 04:09:11 +02:00
rect.moveVerticalCenter(point + widget->getMarginTop() - widget->getMarginBottom());
verticalMoved = true;
break;
2011-07-17 08:56:57 +02:00
case AnchorTop:
if(!verticalMoved) {
2011-08-14 04:09:11 +02:00
rect.moveTop(point + widget->getMarginTop());
verticalMoved = true;
} else
2011-08-14 04:09:11 +02:00
rect.setTop(point + widget->getMarginTop());
break;
2011-07-17 08:56:57 +02:00
case AnchorBottom:
if(!verticalMoved) {
2011-08-14 04:09:11 +02:00
rect.moveBottom(point - widget->getMarginBottom());
verticalMoved = true;
} else
2011-08-14 04:09:11 +02:00
rect.setBottom(point - widget->getMarginBottom());
break;
default:
break;
}
}
}
2011-08-14 04:09:11 +02:00
if(rect != widget->getGeometry())
widget->setGeometry(rect);
}
2011-08-14 04:09:11 +02:00
void UIAnchorLayout::updateWidgetChildren(const UIWidgetPtr& parent)
{
2011-08-14 04:09:11 +02:00
for(const UIAnchor& anchor : m_anchors) {
if(anchor.getAnchorLineWidget() == parent) {
UIWidgetPtr child = anchor.getAnchoredWidget();
if(child)
2011-08-14 04:09:11 +02:00
updateWidget(child);
}
}
}
2011-08-14 04:09:11 +02:00
bool UIAnchorLayout::hasWidgetInAnchorTree(const UIWidgetPtr& widget, const UIWidgetPtr& treeAnchor)
2011-05-09 23:08:17 +02:00
{
2011-08-14 04:09:11 +02:00
for(const UIAnchor& anchor : m_anchors) {
if(anchor.getAnchorLineWidget() == treeAnchor) {
if(anchor.getAnchoredWidget() == widget || hasWidgetInAnchorTree(widget, anchor.getAnchoredWidget()))
2011-05-09 23:08:17 +02:00
return true;
}
}
return false;
}