parent
6d871b305f
commit
51fe97644d
@ -0,0 +1,162 @@
|
|||||||
|
/* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <prerequisites.h>
|
||||||
|
#include <ui/uianchorlayout.h>
|
||||||
|
|
||||||
|
UIElementPtr Anchor::getAnchorLineElement() const
|
||||||
|
{
|
||||||
|
if(!m_anchoredElement.expired())
|
||||||
|
return m_anchoredElement.lock()->backwardsGetElementById(m_anchorLine.getElementId());
|
||||||
|
return UIElementPtr();
|
||||||
|
}
|
||||||
|
|
||||||
|
int Anchor::getAnchorLinePoint() const
|
||||||
|
{
|
||||||
|
UIElementPtr anchorLineElement = getAnchorLineElement();
|
||||||
|
if(anchorLineElement) {
|
||||||
|
switch(m_anchorLine.getEdge()) {
|
||||||
|
case UI::AnchorLeft:
|
||||||
|
return anchorLineElement->getRect().left();
|
||||||
|
case UI::AnchorRight:
|
||||||
|
return anchorLineElement->getRect().right();
|
||||||
|
case UI::AnchorTop:
|
||||||
|
return anchorLineElement->getRect().top();
|
||||||
|
case UI::AnchorBottom:
|
||||||
|
return anchorLineElement->getRect().bottom();
|
||||||
|
case UI::AnchorHorizontalCenter:
|
||||||
|
return anchorLineElement->getRect().horizontalCenter();
|
||||||
|
case UI::AnchorVerticalCenter:
|
||||||
|
return anchorLineElement->getRect().verticalCenter();
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -9999;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UIAnchorLayout::addAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const AnchorLine& anchorLine)
|
||||||
|
{
|
||||||
|
Anchor anchor(anchoredElement, anchoredEdge, anchorLine);
|
||||||
|
UIElementPtr anchorLineElement = anchor.getAnchorLineElement();
|
||||||
|
|
||||||
|
// we can never anchor with itself
|
||||||
|
if(anchoredElement == anchorLineElement) {
|
||||||
|
logError("ERROR: anchoring with itself is not possible");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: check if itself is already anchored on the anchor element
|
||||||
|
|
||||||
|
// setup the anchor
|
||||||
|
m_anchors.push_back(anchor);
|
||||||
|
|
||||||
|
// recalculate anchored element layout
|
||||||
|
recalculateElementLayout(anchoredElement);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UIAnchorLayout::recalculateElementLayout(const UIElementPtr& element)
|
||||||
|
{
|
||||||
|
Rect rect = element->getRect();
|
||||||
|
bool verticalMoved = false;
|
||||||
|
bool horizontalMoved = false;
|
||||||
|
|
||||||
|
foreach(const Anchor& anchor, m_anchors) {
|
||||||
|
if(anchor.getAnchoredElement() == element && anchor.getAnchorLineElement()) {
|
||||||
|
int point = anchor.getAnchorLinePoint();
|
||||||
|
switch(anchor.getAnchoredEdge()) {
|
||||||
|
case UI::AnchorHorizontalCenter:
|
||||||
|
rect.moveHorizontalCenter(point + element->getMarginLeft() - element->getMarginRight());
|
||||||
|
horizontalMoved = true;
|
||||||
|
break;
|
||||||
|
case UI::AnchorLeft:
|
||||||
|
if(!horizontalMoved) {
|
||||||
|
rect.moveLeft(point + element->getMarginLeft());
|
||||||
|
horizontalMoved = true;
|
||||||
|
} else
|
||||||
|
rect.setLeft(point + element->getMarginLeft());
|
||||||
|
break;
|
||||||
|
case UI::AnchorRight:
|
||||||
|
if(!horizontalMoved) {
|
||||||
|
rect.moveRight(point - element->getMarginRight());
|
||||||
|
horizontalMoved = true;
|
||||||
|
} else
|
||||||
|
rect.setRight(point - element->getMarginRight());
|
||||||
|
break;
|
||||||
|
case UI::AnchorVerticalCenter:
|
||||||
|
rect.moveVerticalCenter(point + element->getMarginTop() - element->getMarginBottom());
|
||||||
|
verticalMoved = true;
|
||||||
|
break;
|
||||||
|
case UI::AnchorTop:
|
||||||
|
if(!verticalMoved) {
|
||||||
|
rect.moveTop(point + element->getMarginTop());
|
||||||
|
verticalMoved = true;
|
||||||
|
} else
|
||||||
|
rect.setTop(point + element->getMarginTop());
|
||||||
|
break;
|
||||||
|
case UI::AnchorBottom:
|
||||||
|
if(!verticalMoved) {
|
||||||
|
rect.moveBottom(point - element->getMarginBottom());
|
||||||
|
verticalMoved = true;
|
||||||
|
} else
|
||||||
|
rect.setBottom(point - element->getMarginBottom());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(rect != element->getRect())
|
||||||
|
element->setRect(rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void UIAnchorLayout::recalculateChildrenLayout(const UIElementPtr& parent)
|
||||||
|
{
|
||||||
|
foreach(const Anchor& anchor, m_anchors) {
|
||||||
|
if(anchor.getAnchorLineElement() == parent) {
|
||||||
|
UIElementPtr child = anchor.getAnchoredElement();
|
||||||
|
if(child)
|
||||||
|
recalculateElementLayout(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
UI::AnchorPoint UIAnchorLayout::parseAnchorPoint(const std::string& anchorPointStr)
|
||||||
|
{
|
||||||
|
if(anchorPointStr == "left")
|
||||||
|
return UI::AnchorLeft;
|
||||||
|
else if(anchorPointStr == "right")
|
||||||
|
return UI::AnchorRight;
|
||||||
|
else if(anchorPointStr == "top")
|
||||||
|
return UI::AnchorTop;
|
||||||
|
else if(anchorPointStr == "bottom")
|
||||||
|
return UI::AnchorBottom;
|
||||||
|
else if(anchorPointStr == "horizontalCenter")
|
||||||
|
return UI::AnchorHorizontalCenter;
|
||||||
|
else if(anchorPointStr == "verticalCenter")
|
||||||
|
return UI::AnchorVerticalCenter;
|
||||||
|
return UI::AnchorNone;
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
/* The MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef UIANCHORLAYOUT_H
|
||||||
|
#define UIANCHORLAYOUT_H
|
||||||
|
|
||||||
|
#include <prerequisites.h>
|
||||||
|
#include <ui/uiconstants.h>
|
||||||
|
#include <ui/uilayout.h>
|
||||||
|
#include <ui/uielement.h>
|
||||||
|
|
||||||
|
class AnchorLine
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AnchorLine(const std::string& elementId, UI::AnchorPoint edge) : m_elementId(elementId), m_edge(edge) { }
|
||||||
|
AnchorLine(const AnchorLine& other) : m_elementId(other.m_elementId), m_edge(other.m_edge) { }
|
||||||
|
UI::AnchorPoint getEdge() const { return m_edge; }
|
||||||
|
const std::string& getElementId() const { return m_elementId; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_elementId;
|
||||||
|
UI::AnchorPoint m_edge;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Anchor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Anchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const AnchorLine& anchorLine)
|
||||||
|
: m_anchoredElement(anchoredElement), m_anchoredEdge(anchoredEdge), m_anchorLine(anchorLine) { }
|
||||||
|
UIElementPtr getAnchorLineElement() const ;
|
||||||
|
UIElementPtr getAnchoredElement() const { return m_anchoredElement.lock(); }
|
||||||
|
UI::AnchorPoint getAnchoredEdge() const { return m_anchoredEdge; }
|
||||||
|
int getAnchorLinePoint() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
UIElementWeakPtr m_anchoredElement;
|
||||||
|
UI::AnchorPoint m_anchoredEdge;
|
||||||
|
AnchorLine m_anchorLine;
|
||||||
|
};
|
||||||
|
|
||||||
|
class UIAnchorLayout : public UILayout
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool addAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const AnchorLine& anchorLine);
|
||||||
|
void recalculateElementLayout(const UIElementPtr& element);
|
||||||
|
void recalculateChildrenLayout(const UIElementPtr& parent);
|
||||||
|
|
||||||
|
static UI::AnchorPoint parseAnchorPoint(const std::string& anchorPointStr);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Anchor> m_anchors;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef boost::shared_ptr<UIAnchorLayout> UIAnchorLayoutPtr;
|
||||||
|
|
||||||
|
#endif // UIANCHORLAYOUT_H
|
@ -1,160 +0,0 @@
|
|||||||
/* The MIT License
|
|
||||||
*
|
|
||||||
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include <prerequisites.h>
|
|
||||||
#include <ui/uielement.h>
|
|
||||||
#include <ui/uilayout.h>
|
|
||||||
|
|
||||||
int AnchorLine::getPos() const
|
|
||||||
{
|
|
||||||
UILayoutPtr element = m_relativeElement.lock();
|
|
||||||
if(element) {
|
|
||||||
switch(m_anchorType) {
|
|
||||||
case ANCHOR_LEFT:
|
|
||||||
return element->getRect().left();
|
|
||||||
case ANCHOR_RIGHT:
|
|
||||||
return element->getRect().right();
|
|
||||||
case ANCHOR_TOP:
|
|
||||||
return element->getRect().top();
|
|
||||||
case ANCHOR_BOTTOM:
|
|
||||||
return element->getRect().bottom();
|
|
||||||
case ANCHOR_HORIZONTAL_CENTER:
|
|
||||||
return element->getRect().horizontalCenter();
|
|
||||||
case ANCHOR_VERTICAL_CENTER:
|
|
||||||
return element->getRect().verticalCenter();
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
logError("ERROR: anchor line of an element has expired, your UI is missconfigured");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UILayout::setSize(const Size& size)
|
|
||||||
{
|
|
||||||
if(m_rect.isValid())
|
|
||||||
m_rect.setSize(size);
|
|
||||||
else
|
|
||||||
m_rect = Rect(0, 0, size);
|
|
||||||
|
|
||||||
// rect updated, recalculate itself and anchored elements positions
|
|
||||||
recalculateLayout();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UILayout::setRect(const Rect& rect)
|
|
||||||
{
|
|
||||||
m_rect = rect;
|
|
||||||
|
|
||||||
// rect updated, recalculate itself and anchored elements positions
|
|
||||||
recalculateAnchoredLayout();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool UILayout::addAnchor(EAnchorType type, const AnchorLine& anchorLine)
|
|
||||||
{
|
|
||||||
// we can never anchor with itself
|
|
||||||
if(anchorLine.getRelativeElement() == asUILayout()) {
|
|
||||||
logError("ERROR: anchoring with itself is not possible");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if this layout is already anchored with the relative element
|
|
||||||
// this only happens in missconfigurations
|
|
||||||
for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) {
|
|
||||||
if((*it).lock() == anchorLine.getRelativeElement()) {
|
|
||||||
logError("ERROR: anchoring elements with each other is not possible");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// setup the anchor
|
|
||||||
m_anchors[type] = anchorLine;
|
|
||||||
anchorLine.getRelativeElement()->addAnchoredElement(asUILayout());
|
|
||||||
|
|
||||||
// recalculate itself and anchored elements
|
|
||||||
recalculateLayout();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UILayout::addAnchoredElement(UILayoutPtr anchoredElement)
|
|
||||||
{
|
|
||||||
// check if is already anchored
|
|
||||||
bool found = false;
|
|
||||||
for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) {
|
|
||||||
if((*it).lock() == anchoredElement) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if not, anchor it
|
|
||||||
if(!found)
|
|
||||||
m_anchoredElements.push_back(anchoredElement);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UILayout::recalculateLayout()
|
|
||||||
{
|
|
||||||
// recalculate horizontal position
|
|
||||||
if(m_anchors[ANCHOR_HORIZONTAL_CENTER].isValid()) {
|
|
||||||
m_rect.moveHorizontalCenter(m_anchors[ANCHOR_HORIZONTAL_CENTER].getPos() + m_marginLeft - m_marginRight);
|
|
||||||
} else {
|
|
||||||
if(m_anchors[ANCHOR_LEFT].isValid() && m_anchors[ANCHOR_RIGHT].isValid()) {
|
|
||||||
m_rect.setLeft(m_anchors[ANCHOR_LEFT].getPos() + m_marginLeft);
|
|
||||||
m_rect.setRight(m_anchors[ANCHOR_RIGHT].getPos() - m_marginRight);
|
|
||||||
} else if(m_anchors[ANCHOR_LEFT].isValid()) {
|
|
||||||
m_rect.moveLeft(m_anchors[ANCHOR_LEFT].getPos() + m_marginLeft);
|
|
||||||
} else if(m_anchors[ANCHOR_RIGHT].isValid()) {
|
|
||||||
m_rect.moveRight(m_anchors[ANCHOR_RIGHT].getPos() - m_marginRight);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// recalculate vertical position
|
|
||||||
if(m_anchors[ANCHOR_VERTICAL_CENTER].isValid()) {
|
|
||||||
m_rect.moveVerticalCenter(m_anchors[ANCHOR_VERTICAL_CENTER].getPos() + m_marginTop - m_marginBottom);
|
|
||||||
} else {
|
|
||||||
if(m_anchors[ANCHOR_TOP].isValid() && m_anchors[ANCHOR_BOTTOM].isValid()) {
|
|
||||||
m_rect.setTop(m_anchors[ANCHOR_TOP].getPos() + m_marginTop);
|
|
||||||
m_rect.setBottom(m_anchors[ANCHOR_BOTTOM].getPos() - m_marginBottom);
|
|
||||||
} else if(m_anchors[ANCHOR_TOP].isValid()) {
|
|
||||||
m_rect.moveTop(m_anchors[ANCHOR_TOP].getPos() + m_marginTop);
|
|
||||||
} else if(m_anchors[ANCHOR_BOTTOM].isValid()) {
|
|
||||||
m_rect.moveBottom(m_anchors[ANCHOR_BOTTOM].getPos() - m_marginBottom);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
recalculateAnchoredLayout();
|
|
||||||
|
|
||||||
// fire layout update event
|
|
||||||
onLayoutRectChange(m_rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UILayout::recalculateAnchoredLayout()
|
|
||||||
{
|
|
||||||
// recalculate anchored elements positions
|
|
||||||
for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) {
|
|
||||||
UILayoutPtr element = (*it).lock();
|
|
||||||
if(element)
|
|
||||||
element->recalculateLayout();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in new issue