From dcbe4855bdf2fc11f05f1f6dc5948ab9477c0c51 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sun, 10 Apr 2011 18:12:42 -0300 Subject: [PATCH] ui more error proning --- src/framework/ui/anchorlayout.h | 120 -------------------------------- src/framework/ui/uicontainer.h | 1 - src/framework/ui/uielement.cpp | 19 +++++ src/framework/ui/uielement.h | 2 + src/framework/ui/uilayout.cpp | 37 ++++++++-- src/framework/ui/uilayout.h | 10 ++- src/framework/ui/uiloader.cpp | 5 +- 7 files changed, 62 insertions(+), 132 deletions(-) delete mode 100644 src/framework/ui/anchorlayout.h diff --git a/src/framework/ui/anchorlayout.h b/src/framework/ui/anchorlayout.h deleted file mode 100644 index f217067c..00000000 --- a/src/framework/ui/anchorlayout.h +++ /dev/null @@ -1,120 +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. - */ - - -#ifndef ANCHORLAYOUT_H -#define ANCHORLAYOUT_H - -#include "prerequisites.h" -#include "uiconstants.h" - -enum EAnchorType { - ANCHOR_LEFT = 0, - ANCHOR_RIGHT, - ANCHOR_TOP, - ANCHOR_BOTTOM, - ANCHOR_HORIZONTAL_CENTER, - ANCHOR_VERTICAL_CENTER, - ANCHOR_NONE -}; - -class AnchorLayout; -typedef std::shared_ptr AnchorLayoutPtr; -typedef std::weak_ptr AnchorLayoutWeakPtr; - -class AnchorLine -{ -public: - AnchorLine() : m_anchorType(ANCHOR_NONE) { } - AnchorLine(const AnchorLine& other) : - m_relativeElement(other.m_relativeElement), m_anchorType(other.m_anchorType) { } - AnchorLine(AnchorLayoutPtr relativeElement, EAnchorType anchorType) : - m_relativeElement(relativeElement), m_anchorType(anchorType) { } - bool isValid() const { return (m_anchorType != ANCHOR_NONE && !m_relativeElement.expired()); } - - int getPos() const; - EAnchorType getAnchorType() const { return m_anchorType; } - AnchorLayoutPtr getRelativeElement() const { return m_relativeElement.lock(); } - -private: - AnchorLayoutWeakPtr m_relativeElement; - EAnchorType m_anchorType; -}; - -class AnchorLayout : public std::enable_shared_from_this -{ -public: - AnchorLayout() : - m_marginLeft(0), - m_marginRight(0), - m_marginTop(0), - m_marginBottom(0) { } - virtual ~AnchorLayout() { } - - void setSize(const Size& size); - Size getSize() { return m_rect.size(); } - - void setRect(const Rect& rect); - const Rect& getRect() const{ return m_rect; } - - void addAnchor(EAnchorType type, const AnchorLine& anchorLine); - void anchorLeft(const AnchorLine& anchorLine) { addAnchor(ANCHOR_LEFT, anchorLine); } - void anchorRight(const AnchorLine& anchorLine) { addAnchor(ANCHOR_RIGHT, anchorLine); } - void anchorTop(const AnchorLine& anchorLine) { addAnchor(ANCHOR_TOP, anchorLine); } - void anchorBottom(const AnchorLine& anchorLine) { addAnchor(ANCHOR_BOTTOM, anchorLine); } - void anchorHorizontalCenter(const AnchorLine& anchorLine) { addAnchor(ANCHOR_HORIZONTAL_CENTER, anchorLine); } - void anchorVerticalCenter(const AnchorLine& anchorLine) { addAnchor(ANCHOR_VERTICAL_CENTER, anchorLine); } - - AnchorLine left() { return AnchorLine(asAnchorLayout(), ANCHOR_LEFT); } - AnchorLine right() { return AnchorLine(asAnchorLayout(), ANCHOR_RIGHT); } - AnchorLine top() { return AnchorLine(asAnchorLayout(), ANCHOR_TOP); } - AnchorLine bottom() { return AnchorLine(asAnchorLayout(), ANCHOR_BOTTOM); } - AnchorLine horizontalCenter() { return AnchorLine(asAnchorLayout(), ANCHOR_HORIZONTAL_CENTER); } - AnchorLine verticalCenter() { return AnchorLine(asAnchorLayout(), ANCHOR_VERTICAL_CENTER); } - - void setMargin(int top, int left, int bottom, int right) { m_marginLeft = left; m_marginRight = right; m_marginTop = top; m_marginBottom = bottom; recalculateAnchors(); } - void setMargin(int horizontal, int vertical) { m_marginLeft = m_marginRight = horizontal; m_marginTop = m_marginBottom = vertical; recalculateAnchors(); } - void setMargin(int margin) { m_marginLeft = m_marginRight = m_marginTop = m_marginBottom = margin; recalculateAnchors(); } - - void setMarginLeft(int margin) { m_marginLeft = margin; recalculateAnchors(); } - void setMarginRight(int margin) { m_marginRight = margin; recalculateAnchors(); } - void setMarginTop(int margin) { m_marginTop = margin; recalculateAnchors(); } - void setMarginBottom(int margin) { m_marginBottom = margin; recalculateAnchors(); } - - AnchorLayoutPtr asAnchorLayout() { return shared_from_this(); } - -private: - void recalculateAnchors(); - void addAnchoredElement(AnchorLayoutPtr anchoredElement); - - AnchorLine m_anchors[6]; - - Rect m_rect; - int m_marginLeft; - int m_marginRight; - int m_marginTop; - int m_marginBottom; - std::list m_anchoredElements; -}; - -#endif // ANCHORLAYOUT_H diff --git a/src/framework/ui/uicontainer.h b/src/framework/ui/uicontainer.h index 66c3b0a5..0eeeaa55 100644 --- a/src/framework/ui/uicontainer.h +++ b/src/framework/ui/uicontainer.h @@ -38,7 +38,6 @@ public: void removeChild(UIElementPtr child); UIElementPtr getChildById(const std::string& id); - //TODO: make this backwards UIElementPtr recursiveGetChildById(const std::string& id); virtual void render(); diff --git a/src/framework/ui/uielement.cpp b/src/framework/ui/uielement.cpp index dfbe0c6e..540b6b2d 100644 --- a/src/framework/ui/uielement.cpp +++ b/src/framework/ui/uielement.cpp @@ -57,3 +57,22 @@ void UIElement::render() if(m_skin) m_skin->draw(this); } + + +UIElementPtr UIElement::backwardsGetElementById(const std::string& id) +{ + if(getId() == id) + return asUIElement(); + + UIElementPtr element; + + if(asUIContainer()) { + element = asUIContainer()->recursiveGetChildById(id); + if(element) + return element; + } + + if(getParent()) + element = getParent()->backwardsGetElementById(id); + return element; +} diff --git a/src/framework/ui/uielement.h b/src/framework/ui/uielement.h index 8b4efb34..89f61e55 100644 --- a/src/framework/ui/uielement.h +++ b/src/framework/ui/uielement.h @@ -49,6 +49,8 @@ public: virtual void render(); virtual bool onInputEvent(const InputEvent& event) { return false; } + UIElementPtr backwardsGetElementById(const std::string& id); + bool setSkin(const std::string& skinName); void setSkin(UIElementSkin *skin); UIElementSkin *getSkin() { return m_skin; } diff --git a/src/framework/ui/uilayout.cpp b/src/framework/ui/uilayout.cpp index a5689b4a..1945ede8 100644 --- a/src/framework/ui/uilayout.cpp +++ b/src/framework/ui/uilayout.cpp @@ -46,35 +46,55 @@ int AnchorLine::getPos() const return 0; } } - logError("anchor line of an element have expired"); + logError("anchor line of an element has expired, your UI is missconfigured"); return 0; } void UILayout::setSize(const Size& size) { m_rect.setSize(size); + + // rect updated, recalculate itself and anchored elements positions recalculateAnchors(); } void UILayout::setRect(const Rect& rect) { m_rect = rect; + + // rect updated, recalculate itself and anchored elements positions recalculateAnchors(); } -void UILayout::addAnchor(EAnchorType type, const AnchorLine& anchorLine) +bool UILayout::addAnchor(EAnchorType type, const AnchorLine& anchorLine) { - if(!anchorLine.isValid()) { - logError("anchoring for an element has failed, got an invalid anchor line"); - return; + // we can never anchor with itself + if(anchorLine.getRelativeElement() == asUILayout()) { + logError("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("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 recalculateAnchors(); + 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) { @@ -82,13 +102,15 @@ void UILayout::addAnchoredElement(UILayoutPtr anchoredElement) break; } } + + // if not anchor it if(!found) m_anchoredElements.push_back(anchoredElement); } void UILayout::recalculateAnchors() { - // horizontal + // recalculate horizontal position if(m_anchors[ANCHOR_HORIZONTAL_CENTER].isValid()) { m_rect.moveHorizontalCenter(m_anchors[ANCHOR_HORIZONTAL_CENTER].getPos() + m_marginLeft - m_marginRight); } else { @@ -102,7 +124,7 @@ void UILayout::recalculateAnchors() } } - // vertical + // recalculate vertical position if(m_anchors[ANCHOR_VERTICAL_CENTER].isValid()) { m_rect.moveVerticalCenter(m_anchors[ANCHOR_VERTICAL_CENTER].getPos() + m_marginTop - m_marginBottom); } else { @@ -116,6 +138,7 @@ void UILayout::recalculateAnchors() } } + // recalculate anchored elements positions for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) { UILayoutPtr element = (*it).lock(); if(element) diff --git a/src/framework/ui/uilayout.h b/src/framework/ui/uilayout.h index 757fe1f6..a5e028c1 100644 --- a/src/framework/ui/uilayout.h +++ b/src/framework/ui/uilayout.h @@ -52,6 +52,7 @@ public: m_relativeElement(relativeElement), m_anchorType(anchorType) { } bool isValid() const { return (m_anchorType != ANCHOR_NONE && !m_relativeElement.expired()); } + /// Get the position relative to this anchor line int getPos() const; EAnchorType getAnchorType() const { return m_anchorType; } UILayoutPtr getRelativeElement() const { return m_relativeElement.lock(); } @@ -74,10 +75,13 @@ public: void setSize(const Size& size); Size getSize() { return m_rect.size(); } + /// Set the layout rect, always absolute position void setRect(const Rect& rect); + /// Get layout size, it always return the absolute position const Rect& getRect() const{ return m_rect; } - void addAnchor(EAnchorType type, const AnchorLine& anchorLine); + // anchors add methods + bool addAnchor(EAnchorType type, const AnchorLine& anchorLine); void anchorLeft(const AnchorLine& anchorLine) { addAnchor(ANCHOR_LEFT, anchorLine); } void anchorRight(const AnchorLine& anchorLine) { addAnchor(ANCHOR_RIGHT, anchorLine); } void anchorTop(const AnchorLine& anchorLine) { addAnchor(ANCHOR_TOP, anchorLine); } @@ -85,6 +89,7 @@ public: void anchorHorizontalCenter(const AnchorLine& anchorLine) { addAnchor(ANCHOR_HORIZONTAL_CENTER, anchorLine); } void anchorVerticalCenter(const AnchorLine& anchorLine) { addAnchor(ANCHOR_VERTICAL_CENTER, anchorLine); } + // anchor lines AnchorLine left() { return AnchorLine(asUILayout(), ANCHOR_LEFT); } AnchorLine right() { return AnchorLine(asUILayout(), ANCHOR_RIGHT); } AnchorLine top() { return AnchorLine(asUILayout(), ANCHOR_TOP); } @@ -92,10 +97,10 @@ public: AnchorLine horizontalCenter() { return AnchorLine(asUILayout(), ANCHOR_HORIZONTAL_CENTER); } AnchorLine verticalCenter() { return AnchorLine(asUILayout(), ANCHOR_VERTICAL_CENTER); } + // margins void setMargin(int top, int left, int bottom, int right) { m_marginLeft = left; m_marginRight = right; m_marginTop = top; m_marginBottom = bottom; recalculateAnchors(); } void setMargin(int horizontal, int vertical) { m_marginLeft = m_marginRight = horizontal; m_marginTop = m_marginBottom = vertical; recalculateAnchors(); } void setMargin(int margin) { m_marginLeft = m_marginRight = m_marginTop = m_marginBottom = margin; recalculateAnchors(); } - void setMarginLeft(int margin) { m_marginLeft = margin; recalculateAnchors(); } void setMarginRight(int margin) { m_marginRight = margin; recalculateAnchors(); } void setMarginTop(int margin) { m_marginTop = margin; recalculateAnchors(); } @@ -104,6 +109,7 @@ public: UILayoutPtr asUILayout() { return shared_from_this(); } private: + /// Recalculate itself and anchored elements positions void recalculateAnchors(); void addAnchoredElement(UILayoutPtr anchoredElement); diff --git a/src/framework/ui/uiloader.cpp b/src/framework/ui/uiloader.cpp index 446d116e..921bcc78 100644 --- a/src/framework/ui/uiloader.cpp +++ b/src/framework/ui/uiloader.cpp @@ -241,13 +241,14 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type, if(relativeElementId == "parent" && element->getParent()) { relativeElement = element->getParent()->asUILayout(); } else { - UIElementPtr tmp = UIContainer::getRootContainer()->recursiveGetChildById(relativeElementId); + UIElementPtr tmp = element->backwardsGetElementById(relativeElementId); if(tmp) relativeElement = tmp->asUILayout(); } if(relativeElement) { - element->addAnchor(type, AnchorLine(relativeElement, relativeAnchorType)); + if(!element->addAnchor(type, AnchorLine(relativeElement, relativeAnchorType))) + throw YAML::Exception(node.GetMark(), "error while processing anchors"); } else { throw YAML::Exception(node.GetMark(), "anchoring failed, does the relative element really exists?"); }