mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 14:03:26 +02:00
ui loader and some refactoring
This commit is contained in:
@@ -36,5 +36,6 @@
|
||||
#include "uiskins.h"
|
||||
#include "uiwindow.h"
|
||||
#include "uitextedit.h"
|
||||
#include "uiloader.h"
|
||||
|
||||
#endif // UI_H
|
||||
|
@@ -26,12 +26,6 @@
|
||||
#include "graphics/fonts.h"
|
||||
#include "graphics/font.h"
|
||||
|
||||
void UIButton::load(const YAML::Node& node)
|
||||
{
|
||||
UIElement::load(node);
|
||||
node["text"] >> m_text;
|
||||
}
|
||||
|
||||
void UIButton::render()
|
||||
{
|
||||
UIElement::render();
|
||||
|
@@ -40,11 +40,12 @@ public:
|
||||
UIElement();
|
||||
}
|
||||
|
||||
void load(const YAML::Node& node);
|
||||
|
||||
virtual void render();
|
||||
bool onInputEvent(const InputEvent& event);
|
||||
|
||||
void setText(const std::string& text) { m_text = text; }
|
||||
const std::string& getText() const { return m_text; }
|
||||
|
||||
UI::EButtonState getState() { return m_state; }
|
||||
|
||||
void onClick(const Callback& callback) { m_buttonClickCallback = callback; }
|
||||
|
@@ -30,90 +30,11 @@
|
||||
#include "uitextedit.h"
|
||||
#include "uiwindow.h"
|
||||
|
||||
UIContainerPtr g_ui(new UIContainer);
|
||||
UIContainerPtr rootContainer(new UIContainer);
|
||||
|
||||
UIElementPtr createElementFromDescription(std::string elementId)
|
||||
UIContainerPtr& UIContainer::getRootContainer()
|
||||
{
|
||||
UIElementPtr element;
|
||||
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, elementId, boost::is_any_of("-"));
|
||||
if(split.size() != 2) {
|
||||
logError("incorrect element id format: %s", elementId.c_str());
|
||||
return element;
|
||||
}
|
||||
|
||||
std::string elementType = split[1];
|
||||
if(elementType == "panel") {
|
||||
element = UIElementPtr(new UIPanel);
|
||||
} else if(elementType == "button") {
|
||||
element = UIElementPtr(new UIButton);
|
||||
} else if(elementType == "label") {
|
||||
element = UIElementPtr(new UILabel);
|
||||
} else if(elementType == "window") {
|
||||
element = UIElementPtr(new UIWindow);
|
||||
} else if(elementType == "textEdit") {
|
||||
element = UIElementPtr(new UITextEdit);
|
||||
}
|
||||
|
||||
if(element)
|
||||
element->setId(elementId);
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
void UIContainer::load(const YAML::Node& node)
|
||||
{
|
||||
UIElement::load(node);
|
||||
|
||||
for(auto it = node.begin(); it != node.end(); ++it) {
|
||||
std::string elementDesc;
|
||||
it.first() >> elementDesc;
|
||||
|
||||
if(elementDesc.find("-") != std::string::npos) {
|
||||
UIElementPtr element = createElementFromDescription(elementDesc);
|
||||
if(element) {
|
||||
addChild(element);
|
||||
element->load(it.second());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UIContainerPtr UIContainer::load(const std::string& file)
|
||||
{
|
||||
//TODO: handle errors
|
||||
//TODO: display errors in which file and line
|
||||
UIContainerPtr container;
|
||||
|
||||
std::string fileContents = g_resources.loadTextFile(file);
|
||||
if(!fileContents.size()) {
|
||||
logFatal("could not load ui file \"%s", file.c_str());
|
||||
return UIContainerPtr();
|
||||
}
|
||||
|
||||
std::istringstream fin(fileContents);
|
||||
|
||||
try {
|
||||
YAML::Parser parser(fin);
|
||||
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string elementDesc;
|
||||
doc.begin().first() >> elementDesc;
|
||||
UIElementPtr element = createElementFromDescription(elementDesc);
|
||||
if(element) {
|
||||
g_ui->addChild(element);
|
||||
element->load(doc.begin().second());
|
||||
return element->asUIContainer();
|
||||
}
|
||||
} catch (YAML::ParserException& e) {
|
||||
logError("Malformed ui file \"%s\": %s", file.c_str(), e.what());
|
||||
return UIContainerPtr();
|
||||
}
|
||||
|
||||
return container;
|
||||
return rootContainer;
|
||||
}
|
||||
|
||||
void UIContainer::addChild(UIElementPtr child)
|
||||
|
@@ -34,11 +34,6 @@ public:
|
||||
UIContainer(UI::EElementType type = UI::Container) : UIElement(type) { }
|
||||
virtual ~UIContainer() { }
|
||||
|
||||
virtual void load(const YAML::Node& node);
|
||||
|
||||
//TODO: move this shit
|
||||
static UIContainerPtr load(const std::string& file);
|
||||
|
||||
void addChild(UIElementPtr child);
|
||||
void removeChild(UIElementPtr child);
|
||||
UIElementPtr getChildById(const std::string& id);
|
||||
@@ -56,11 +51,11 @@ public:
|
||||
|
||||
UIContainerPtr asUIContainer() { return std::static_pointer_cast<UIContainer>(shared_from_this()); }
|
||||
|
||||
static UIContainerPtr& getRootContainer();
|
||||
|
||||
protected:
|
||||
std::list<UIElementPtr> m_children;
|
||||
UIElementPtr m_activeElement;
|
||||
};
|
||||
|
||||
extern UIContainerPtr g_ui;
|
||||
|
||||
#endif // UICONTAINER_H
|
||||
|
@@ -27,7 +27,7 @@
|
||||
#include "uielementskin.h"
|
||||
|
||||
UIElement::UIElement(UI::EElementType type) :
|
||||
AnchorLayout(),
|
||||
UILayout(),
|
||||
m_type(type),
|
||||
m_skin(NULL),
|
||||
m_visible(true),
|
||||
@@ -38,107 +38,6 @@ UIElement::UIElement(UI::EElementType type) :
|
||||
setSkin(g_uiSkins.getElementSkin(type));
|
||||
}
|
||||
|
||||
void UIElement::load(const YAML::Node& node)
|
||||
{
|
||||
if(node.FindValue("skin"))
|
||||
setSkin(g_uiSkins.getElementSkin(m_type, node["skin"]));
|
||||
|
||||
if(node.FindValue("size")) {
|
||||
Size size;
|
||||
node["size"] >> size;
|
||||
setSize(size);
|
||||
}
|
||||
|
||||
int margin;
|
||||
if(node.FindValue("margin.left")) {
|
||||
node["margin.left"] >> margin;
|
||||
setMarginLeft(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("margin.right")) {
|
||||
node["margin.right"] >> margin;
|
||||
setMarginRight(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("margin.top")) {
|
||||
node["margin.top"] >> margin;
|
||||
setMarginTop(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("margin.bottom")) {
|
||||
node["margin.bottom"] >> margin;
|
||||
setMarginBottom(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("anchors.left"))
|
||||
loadAnchor(ANCHOR_LEFT, node["anchors.left"]);
|
||||
|
||||
if(node.FindValue("anchors.right"))
|
||||
loadAnchor(ANCHOR_RIGHT, node["anchors.right"]);
|
||||
|
||||
if(node.FindValue("anchors.top"))
|
||||
loadAnchor(ANCHOR_TOP, node["anchors.top"]);
|
||||
|
||||
if(node.FindValue("anchors.bottom"))
|
||||
loadAnchor(ANCHOR_BOTTOM, node["anchors.bottom"]);
|
||||
|
||||
if(node.FindValue("anchors.horizontalCenter"))
|
||||
loadAnchor(ANCHOR_HORIZONTAL_CENTER, node["anchors.horizontalCenter"]);
|
||||
|
||||
if(node.FindValue("anchors.verticalCenter"))
|
||||
loadAnchor(ANCHOR_VERTICAL_CENTER, node["anchors.verticalCenter"]);
|
||||
}
|
||||
|
||||
void UIElement::loadAnchor(EAnchorType type, const YAML::Node& node)
|
||||
{
|
||||
std::string anchorDescription;
|
||||
node >> anchorDescription;
|
||||
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, anchorDescription, boost::is_any_of("."));
|
||||
if(split.size() != 2) {
|
||||
logError("wrong anchors description: %s", anchorDescription.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
std::string relativeElementId = split[0];
|
||||
std::string relativeAnchorTypeId = split[1];
|
||||
EAnchorType relativeAnchorType;
|
||||
|
||||
if(relativeAnchorTypeId == "left")
|
||||
relativeAnchorType = ANCHOR_LEFT;
|
||||
else if(relativeAnchorTypeId == "right")
|
||||
relativeAnchorType = ANCHOR_RIGHT;
|
||||
else if(relativeAnchorTypeId == "top")
|
||||
relativeAnchorType = ANCHOR_TOP;
|
||||
else if(relativeAnchorTypeId == "bottom")
|
||||
relativeAnchorType = ANCHOR_BOTTOM;
|
||||
else if(relativeAnchorTypeId == "horizontalCenter")
|
||||
relativeAnchorType = ANCHOR_HORIZONTAL_CENTER;
|
||||
else if(relativeAnchorTypeId == "verticalCenter")
|
||||
relativeAnchorType = ANCHOR_VERTICAL_CENTER;
|
||||
else {
|
||||
logError("wrong anchors description: %s", anchorDescription.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
AnchorLayoutPtr relativeElement;
|
||||
if(relativeElementId == "parent" && getParent()) {
|
||||
relativeElement = getParent()->asAnchorLayout();
|
||||
} else {
|
||||
UIElementPtr element = g_ui->recursiveGetChildById(relativeElementId);
|
||||
if(element)
|
||||
relativeElement = element->asAnchorLayout();
|
||||
}
|
||||
|
||||
if(relativeElement) {
|
||||
addAnchor(type, AnchorLine(relativeElement, relativeAnchorType));
|
||||
} else {
|
||||
logError("anchoring has failed: %s", anchorDescription.c_str());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool UIElement::setSkin(const std::string& skinName)
|
||||
{
|
||||
setSkin(g_uiSkins.getElementSkin(m_type, skinName));
|
||||
|
@@ -28,7 +28,7 @@
|
||||
#include "prerequisites.h"
|
||||
#include "core/input.h"
|
||||
#include "uiconstants.h"
|
||||
#include "anchorlayout.h"
|
||||
#include "uilayout.h"
|
||||
|
||||
class UIElementSkin;
|
||||
|
||||
@@ -40,7 +40,7 @@ class UIElement;
|
||||
typedef std::shared_ptr<UIElement> UIElementPtr;
|
||||
typedef std::weak_ptr<UIElement> UIElementWeakPtr;
|
||||
|
||||
class UIElement : public AnchorLayout
|
||||
class UIElement : public UILayout
|
||||
{
|
||||
public:
|
||||
UIElement(UI::EElementType type = UI::Element);
|
||||
@@ -49,9 +49,6 @@ public:
|
||||
virtual void render();
|
||||
virtual bool onInputEvent(const InputEvent& event) { return false; }
|
||||
|
||||
virtual void load(const YAML::Node& node);
|
||||
void loadAnchor(EAnchorType type, const YAML::Node& node);
|
||||
|
||||
bool setSkin(const std::string& skinName);
|
||||
void setSkin(UIElementSkin *skin);
|
||||
UIElementSkin *getSkin() { return m_skin; }
|
||||
|
@@ -35,15 +35,6 @@ UILabel::UILabel(const std::string& text, Font* font) :
|
||||
setSize(m_font->calculateTextRectSize(text));
|
||||
}
|
||||
|
||||
void UILabel::load(const YAML::Node& node)
|
||||
{
|
||||
UIElement::load(node);
|
||||
|
||||
std::string text;
|
||||
node["text"] >> text;
|
||||
setText(text);
|
||||
}
|
||||
|
||||
void UILabel::render()
|
||||
{
|
||||
m_font->renderText(m_text, getRect(), ALIGN_LEFT, Color(0xFFBFBFBF));
|
||||
|
@@ -35,8 +35,6 @@ class UILabel : public UIElement
|
||||
public:
|
||||
UILabel(const std::string& text = std::string(), Font *font = NULL);
|
||||
|
||||
void load(const YAML::Node& node);
|
||||
|
||||
void render();
|
||||
|
||||
void setText(const std::string& text);
|
||||
|
@@ -22,12 +22,12 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "anchorlayout.h"
|
||||
#include "uilayout.h"
|
||||
#include "uielement.h"
|
||||
|
||||
int AnchorLine::getPos() const
|
||||
{
|
||||
AnchorLayoutPtr element = m_relativeElement.lock();
|
||||
UILayoutPtr element = m_relativeElement.lock();
|
||||
if(element) {
|
||||
switch(m_anchorType) {
|
||||
case ANCHOR_LEFT:
|
||||
@@ -50,30 +50,30 @@ int AnchorLine::getPos() const
|
||||
return 0;
|
||||
}
|
||||
|
||||
void AnchorLayout::setSize(const Size& size)
|
||||
void UILayout::setSize(const Size& size)
|
||||
{
|
||||
m_rect.setSize(size);
|
||||
recalculateAnchors();
|
||||
}
|
||||
|
||||
void AnchorLayout::setRect(const Rect& rect)
|
||||
void UILayout::setRect(const Rect& rect)
|
||||
{
|
||||
m_rect = rect;
|
||||
recalculateAnchors();
|
||||
}
|
||||
|
||||
void AnchorLayout::addAnchor(EAnchorType type, const AnchorLine& anchorLine)
|
||||
void UILayout::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());
|
||||
anchorLine.getRelativeElement()->addAnchoredElement(asUILayout());
|
||||
recalculateAnchors();
|
||||
}
|
||||
|
||||
void AnchorLayout::addAnchoredElement(AnchorLayoutPtr anchoredElement)
|
||||
void UILayout::addAnchoredElement(UILayoutPtr anchoredElement)
|
||||
{
|
||||
bool found = false;
|
||||
for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) {
|
||||
@@ -86,7 +86,7 @@ void AnchorLayout::addAnchoredElement(AnchorLayoutPtr anchoredElement)
|
||||
m_anchoredElements.push_back(anchoredElement);
|
||||
}
|
||||
|
||||
void AnchorLayout::recalculateAnchors()
|
||||
void UILayout::recalculateAnchors()
|
||||
{
|
||||
// horizontal
|
||||
if(m_anchors[ANCHOR_HORIZONTAL_CENTER].isValid()) {
|
||||
@@ -117,7 +117,7 @@ void AnchorLayout::recalculateAnchors()
|
||||
}
|
||||
|
||||
for(auto it = m_anchoredElements.begin(); it != m_anchoredElements.end(); ++it) {
|
||||
AnchorLayoutPtr element = (*it).lock();
|
||||
UILayoutPtr element = (*it).lock();
|
||||
if(element)
|
||||
element->recalculateAnchors();
|
||||
}
|
120
src/framework/ui/uilayout.h
Normal file
120
src/framework/ui/uilayout.h
Normal file
@@ -0,0 +1,120 @@
|
||||
/* 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 UILAYOUT_H
|
||||
#define UILAYOUT_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 UILayout;
|
||||
typedef std::shared_ptr<UILayout> UILayoutPtr;
|
||||
typedef std::weak_ptr<UILayout> UILayoutWeakPtr;
|
||||
|
||||
class AnchorLine
|
||||
{
|
||||
public:
|
||||
AnchorLine() : m_anchorType(ANCHOR_NONE) { }
|
||||
AnchorLine(const AnchorLine& other) :
|
||||
m_relativeElement(other.m_relativeElement), m_anchorType(other.m_anchorType) { }
|
||||
AnchorLine(UILayoutPtr 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; }
|
||||
UILayoutPtr getRelativeElement() const { return m_relativeElement.lock(); }
|
||||
|
||||
private:
|
||||
UILayoutWeakPtr m_relativeElement;
|
||||
EAnchorType m_anchorType;
|
||||
};
|
||||
|
||||
class UILayout : public std::enable_shared_from_this<UILayout>
|
||||
{
|
||||
public:
|
||||
UILayout() :
|
||||
m_marginLeft(0),
|
||||
m_marginRight(0),
|
||||
m_marginTop(0),
|
||||
m_marginBottom(0) { }
|
||||
virtual ~UILayout() { }
|
||||
|
||||
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(asUILayout(), ANCHOR_LEFT); }
|
||||
AnchorLine right() { return AnchorLine(asUILayout(), ANCHOR_RIGHT); }
|
||||
AnchorLine top() { return AnchorLine(asUILayout(), ANCHOR_TOP); }
|
||||
AnchorLine bottom() { return AnchorLine(asUILayout(), ANCHOR_BOTTOM); }
|
||||
AnchorLine horizontalCenter() { return AnchorLine(asUILayout(), ANCHOR_HORIZONTAL_CENTER); }
|
||||
AnchorLine verticalCenter() { return AnchorLine(asUILayout(), 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(); }
|
||||
|
||||
UILayoutPtr asUILayout() { return shared_from_this(); }
|
||||
|
||||
private:
|
||||
void recalculateAnchors();
|
||||
void addAnchoredElement(UILayoutPtr anchoredElement);
|
||||
|
||||
AnchorLine m_anchors[6];
|
||||
|
||||
Rect m_rect;
|
||||
int m_marginLeft;
|
||||
int m_marginRight;
|
||||
int m_marginTop;
|
||||
int m_marginBottom;
|
||||
std::list<UILayoutWeakPtr> m_anchoredElements;
|
||||
};
|
||||
|
||||
#endif // UILAYOUT_H
|
254
src/framework/ui/uiloader.cpp
Normal file
254
src/framework/ui/uiloader.cpp
Normal file
@@ -0,0 +1,254 @@
|
||||
/* 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 "uiloader.h"
|
||||
#include "core/resources.h"
|
||||
#include "ui.h"
|
||||
|
||||
UIElementPtr UILoader::createElementFromId(const std::string& id)
|
||||
{
|
||||
UIElementPtr element;
|
||||
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, id, boost::is_any_of("#"));
|
||||
if(split.size() != 2)
|
||||
return element;
|
||||
|
||||
std::string elementType = split[0];
|
||||
std::string elementId = split[1];
|
||||
|
||||
if(elementType == "panel") {
|
||||
element = UIElementPtr(new UIPanel);
|
||||
} else if(elementType == "button") {
|
||||
element = UIElementPtr(new UIButton);
|
||||
} else if(elementType == "label") {
|
||||
element = UIElementPtr(new UILabel);
|
||||
} else if(elementType == "window") {
|
||||
element = UIElementPtr(new UIWindow);
|
||||
} else if(elementType == "textEdit") {
|
||||
element = UIElementPtr(new UITextEdit);
|
||||
}
|
||||
|
||||
if(element)
|
||||
element->setId(elementId);
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& parent)
|
||||
{
|
||||
std::string fileContents = g_resources.loadTextFile(file);
|
||||
if(!fileContents.size()) {
|
||||
logFatal("Could not load ui file \"%s", file.c_str());
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
std::istringstream fin(fileContents);
|
||||
|
||||
try {
|
||||
YAML::Parser parser(fin);
|
||||
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
// get element id
|
||||
std::string elementId;
|
||||
doc.begin().first() >> elementId;
|
||||
|
||||
// first we should populate all elements
|
||||
// only after that we can load anchors
|
||||
|
||||
// create element interpreting it's id
|
||||
UIElementPtr element = createElementFromId(elementId);
|
||||
if(!element)
|
||||
throw YAML::Exception(doc.begin().first().GetMark(), "invalid element type");
|
||||
parent->addChild(element);
|
||||
|
||||
// populete it
|
||||
if(element->asUIContainer())
|
||||
populateContainer(element->asUIContainer(), doc.begin().second());
|
||||
|
||||
// now do the real load
|
||||
loadElements(element, doc.begin().second());
|
||||
} catch (YAML::Exception& e) {
|
||||
logFatal("Failed to load ui file \"%s\":\n %s", file.c_str(), e.what());
|
||||
}
|
||||
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node& node)
|
||||
{
|
||||
for(auto it = node.begin(); it != node.end(); ++it) {
|
||||
std::string id;
|
||||
it.first() >> id;
|
||||
|
||||
// check if it's and element id
|
||||
if(id.find("#") != std::string::npos) {
|
||||
UIElementPtr element = createElementFromId(id);
|
||||
if(!element)
|
||||
throw YAML::Exception(it.first().GetMark(), "invalid element type");
|
||||
parent->addChild(element);
|
||||
|
||||
// also populate this element if it's a parent
|
||||
if(element->asUIContainer())
|
||||
populateContainer(element->asUIContainer(), it.second());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
|
||||
{
|
||||
loadElement(parent, node);
|
||||
|
||||
if(parent->asUIContainer()) {
|
||||
UIContainerPtr container = parent->asUIContainer();
|
||||
for(auto it = node.begin(); it != node.end(); ++it) {
|
||||
std::string id;
|
||||
it.first() >> id;
|
||||
|
||||
// check if it's and element id
|
||||
if(id.find("#") != std::string::npos) {
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, id, boost::is_any_of("#"));
|
||||
loadElements(container->getChildById(split[1]), it.second());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
|
||||
{
|
||||
std::string tmp;
|
||||
|
||||
if(node.FindValue("skin"))
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), node["skin"]));
|
||||
|
||||
if(node.FindValue("size")) {
|
||||
Size size;
|
||||
node["size"] >> size;
|
||||
element->setSize(size);
|
||||
}
|
||||
|
||||
int margin;
|
||||
if(node.FindValue("margin.left")) {
|
||||
node["margin.left"] >> margin;
|
||||
element->setMarginLeft(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("margin.right")) {
|
||||
node["margin.right"] >> margin;
|
||||
element->setMarginRight(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("margin.top")) {
|
||||
node["margin.top"] >> margin;
|
||||
element->setMarginTop(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("margin.bottom")) {
|
||||
node["margin.bottom"] >> margin;
|
||||
element->setMarginBottom(margin);
|
||||
}
|
||||
|
||||
if(node.FindValue("anchors.left"))
|
||||
loadElementAnchor(element, ANCHOR_LEFT, node["anchors.left"]);
|
||||
|
||||
if(node.FindValue("anchors.right"))
|
||||
loadElementAnchor(element, ANCHOR_RIGHT, node["anchors.right"]);
|
||||
|
||||
if(node.FindValue("anchors.top"))
|
||||
loadElementAnchor(element, ANCHOR_TOP, node["anchors.top"]);
|
||||
|
||||
if(node.FindValue("anchors.bottom"))
|
||||
loadElementAnchor(element, ANCHOR_BOTTOM, node["anchors.bottom"]);
|
||||
|
||||
if(node.FindValue("anchors.horizontalCenter"))
|
||||
loadElementAnchor(element, ANCHOR_HORIZONTAL_CENTER, node["anchors.horizontalCenter"]);
|
||||
|
||||
if(node.FindValue("anchors.verticalCenter"))
|
||||
loadElementAnchor(element, ANCHOR_VERTICAL_CENTER, node["anchors.verticalCenter"]);
|
||||
|
||||
// load specific element type
|
||||
if(element->getElementType() == UI::Button) {
|
||||
UIButtonPtr button = std::static_pointer_cast<UIButton>(element);
|
||||
node["text"] >> tmp;
|
||||
button->setText(tmp);
|
||||
}
|
||||
else if(element->getElementType() == UI::Window) {
|
||||
UIWindowPtr window = std::static_pointer_cast<UIWindow>(element);
|
||||
node["title"] >> tmp;
|
||||
window->setTitle(tmp);
|
||||
}
|
||||
else if(element->getElementType() == UI::Label) {
|
||||
UILabelPtr label = std::static_pointer_cast<UILabel>(element);
|
||||
node["text"] >> tmp;
|
||||
label->setText(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type, const YAML::Node& node)
|
||||
{
|
||||
std::string anchorDescription;
|
||||
node >> anchorDescription;
|
||||
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, anchorDescription, boost::is_any_of("."));
|
||||
if(split.size() != 2)
|
||||
throw YAML::Exception(node.GetMark(), "invalid anchors description");
|
||||
|
||||
std::string relativeElementId = split[0];
|
||||
std::string relativeAnchorTypeId = split[1];
|
||||
EAnchorType relativeAnchorType;
|
||||
|
||||
if(relativeAnchorTypeId == "left")
|
||||
relativeAnchorType = ANCHOR_LEFT;
|
||||
else if(relativeAnchorTypeId == "right")
|
||||
relativeAnchorType = ANCHOR_RIGHT;
|
||||
else if(relativeAnchorTypeId == "top")
|
||||
relativeAnchorType = ANCHOR_TOP;
|
||||
else if(relativeAnchorTypeId == "bottom")
|
||||
relativeAnchorType = ANCHOR_BOTTOM;
|
||||
else if(relativeAnchorTypeId == "horizontalCenter")
|
||||
relativeAnchorType = ANCHOR_HORIZONTAL_CENTER;
|
||||
else if(relativeAnchorTypeId == "verticalCenter")
|
||||
relativeAnchorType = ANCHOR_VERTICAL_CENTER;
|
||||
else
|
||||
throw YAML::Exception(node.GetMark(), "invalid anchors description");
|
||||
|
||||
UILayoutPtr relativeElement;
|
||||
if(relativeElementId == "parent" && element->getParent()) {
|
||||
relativeElement = element->getParent()->asUILayout();
|
||||
} else {
|
||||
UIElementPtr tmp = UIContainer::getRootContainer()->recursiveGetChildById(relativeElementId);
|
||||
if(tmp)
|
||||
relativeElement = tmp->asUILayout();
|
||||
}
|
||||
|
||||
if(relativeElement) {
|
||||
element->addAnchor(type, AnchorLine(relativeElement, relativeAnchorType));
|
||||
} else {
|
||||
throw YAML::Exception(node.GetMark(), "anchoring failed, does the relative element really exists?");
|
||||
}
|
||||
}
|
53
src/framework/ui/uiloader.h
Normal file
53
src/framework/ui/uiloader.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/* 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 UILOADER_H
|
||||
#define UILOADER_H
|
||||
|
||||
#include "prerequisites.h"
|
||||
#include "uiconstants.h"
|
||||
#include "uicontainer.h"
|
||||
|
||||
namespace UILoader
|
||||
{
|
||||
/// Detect element type and create it
|
||||
UIElementPtr createElementFromId(const std::string& id);
|
||||
|
||||
/// Loads an UIElement and it's children from a YAML file
|
||||
UIElementPtr loadFile(const std::string& file, const UIContainerPtr& parent);
|
||||
|
||||
/// Populate container children from a YAML node
|
||||
void populateContainer(const UIContainerPtr& parent, const YAML::Node& node);
|
||||
|
||||
/// Load element and its children from a YAML node
|
||||
void loadElements(const UIElementPtr& parent, const YAML::Node& node);
|
||||
|
||||
/// Load element proprieties from a YAML node
|
||||
void loadElement(const UIElementPtr& element, const YAML::Node& node);
|
||||
|
||||
/// Load anchor from a YAML node
|
||||
void loadElementAnchor(const UIElementPtr& element, EAnchorType type, const YAML::Node& node);
|
||||
};
|
||||
|
||||
#endif // UILOADER_H
|
@@ -23,9 +23,3 @@
|
||||
|
||||
|
||||
#include "uiwindow.h"
|
||||
|
||||
void UIWindow::load(const YAML::Node& node)
|
||||
{
|
||||
UIContainer::load(node);
|
||||
node["title"] >> m_title;
|
||||
}
|
||||
|
@@ -35,8 +35,7 @@ public:
|
||||
UIContainer(UI::Window),
|
||||
m_title(title) { }
|
||||
|
||||
void load(const YAML::Node& node);
|
||||
|
||||
void setTitle(const std::string& title) { m_title = title; }
|
||||
const std::string& getTitle() const { return m_title; }
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user