From a9a110f44b0a1d10f208e02da99cc4294c7e8ecf Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sat, 9 Apr 2011 16:08:43 -0300 Subject: [PATCH] missing files --- src/framework/ui/anchorlayout.cpp | 123 ++++++++++++++++++++++++++++++ src/framework/ui/anchorlayout.h | 116 ++++++++++++++++++++++++++++ src/framework/ui/uibuttonskin.cpp | 51 +++++++++++++ src/framework/ui/uibuttonskin.h | 45 +++++++++++ 4 files changed, 335 insertions(+) create mode 100644 src/framework/ui/anchorlayout.cpp create mode 100644 src/framework/ui/anchorlayout.h create mode 100644 src/framework/ui/uibuttonskin.cpp create mode 100644 src/framework/ui/uibuttonskin.h diff --git a/src/framework/ui/anchorlayout.cpp b/src/framework/ui/anchorlayout.cpp new file mode 100644 index 00000000..78739e64 --- /dev/null +++ b/src/framework/ui/anchorlayout.cpp @@ -0,0 +1,123 @@ +/* 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 "anchorlayout.h" + +int AnchorLine::getPos() const +{ + AnchorLayoutPtr 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("anchor line of an element have expired"); + return 0; +} + +void AnchorLayout::setSize(const Size& size) +{ + m_rect.setSize(size); + recalculateAnchors(); +} + +void AnchorLayout::setRect(const Rect& rect) +{ + m_rect = rect; + recalculateAnchors(); +} + +void AnchorLayout::addAnchor(EAnchorType type, const AnchorLine& anchorLine) +{ + if(!anchorLine.isValid()) { + logError("anchoring for an element has failed, got an invalid anchor line"); + return; + } + m_anchors[type] = anchorLine; + anchorLine.getRelativeElement()->addAnchoredElement(asAnchorLayout()); + recalculateAnchors(); +} + +void AnchorLayout::addAnchoredElement(AnchorLayoutPtr anchoredElement) +{ + bool found = false; + for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) { + if((*it).lock() == anchoredElement) { + found = true; + break; + } + } + if(!found) + m_anchoredElements.push_back(anchoredElement); +} + +void AnchorLayout::recalculateAnchors() +{ + // horizontal + 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); + } + } + + // vertical + 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.setLeft(m_anchors[ANCHOR_TOP].getPos() + m_marginTop); + m_rect.setRight(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); + } + } + + for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) { + AnchorLayoutPtr element = (*it).lock(); + if(element) + element->recalculateAnchors(); + } +} diff --git a/src/framework/ui/anchorlayout.h b/src/framework/ui/anchorlayout.h new file mode 100644 index 00000000..96c944d6 --- /dev/null +++ b/src/framework/ui/anchorlayout.h @@ -0,0 +1,116 @@ +/* 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 "../rect.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(); } + + 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/uibuttonskin.cpp b/src/framework/ui/uibuttonskin.cpp new file mode 100644 index 00000000..8b3d8f88 --- /dev/null +++ b/src/framework/ui/uibuttonskin.cpp @@ -0,0 +1,51 @@ +/* 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 "uibuttonskin.h" +#include "uibutton.h" + +void UIButtonSkin::draw(UIElement *element) +{ + + UIButton *button = static_cast(element); + + if(button->getState() == UI::ButtonDown && m_buttonDownImage) { + m_buttonDownImage->draw(element->getRect()); + } else if(button->getState() == UI::ButtonMouseOver && m_buttonHoverImage) { + m_buttonHoverImage->draw(element->getRect()); + } else { + UIElementSkin::draw(element); + } +} + +void UIButtonSkin::load(const YAML::Node& node) +{ + UIElementSkin::load(node); + + if(node.FindValue("down state")) + m_buttonDownImage = loadImage(node["down state"]); + + if(node.FindValue("mouse over state")) + m_buttonHoverImage = loadImage(node["mouse over state"]); +} diff --git a/src/framework/ui/uibuttonskin.h b/src/framework/ui/uibuttonskin.h new file mode 100644 index 00000000..268348fa --- /dev/null +++ b/src/framework/ui/uibuttonskin.h @@ -0,0 +1,45 @@ +/* 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 UIBUTTONSKIN_H +#define UIBUTTONSKIN_H + +#include "../prerequisites.h" +#include "uiconstants.h" +#include "uielementskin.h" + +class UIButtonSkin : public UIElementSkin +{ +public: + UIButtonSkin(const std::string& name, UI::EElementType elementType) :UIElementSkin(name, elementType) { } + + void load(const YAML::Node& node); + void draw(UIElement *element); + +private: + ImagePtr m_buttonDownImage; + ImagePtr m_buttonHoverImage; +}; + +#endif // UIBUTTONSKIN_H