mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
a bunch of stuff
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include <prerequisites.h>
|
||||
#include <core/dispatcher.h>
|
||||
#include <ui/uibutton.h>
|
||||
#include "uicontainer.h"
|
||||
|
||||
void UIButton::onInputEvent(const InputEvent& event)
|
||||
{
|
||||
@@ -35,5 +36,10 @@ void UIButton::onInputEvent(const InputEvent& event)
|
||||
if(getRect().contains(event.mousePos)) {
|
||||
g_dispatcher.addTask(boost::bind(&Scriptable::callLuaTableField, shared_from_this(), "onClick"));
|
||||
}
|
||||
} else if(event.type == EV_MOUSE_MOVE && m_state != ButtonDown) {
|
||||
if(getRect().contains(event.mousePos) && UIContainer::getRoot()->recursiveGetChildByPos(event.mousePos) == asUIElement())
|
||||
m_state = ButtonMouseOver;
|
||||
else
|
||||
m_state = ButtonUp;
|
||||
}
|
||||
}
|
||||
|
@@ -31,17 +31,21 @@ void UIButtonSkin::load(const YAML::Node& node)
|
||||
{
|
||||
UIElementSkin::load(node);
|
||||
|
||||
m_buttonDownImage = loadImage(node["down state"]);
|
||||
m_buttonDownTextColor = getFontColor();
|
||||
m_buttonHoverTextColor = getFontColor();
|
||||
|
||||
if(node["down state"].FindValue("text translate"))
|
||||
node["down state"]["text translate"] >> m_buttonDownTranslate;
|
||||
if(yamlHasValue(node, "down state")) {
|
||||
const YAML::Node& cnode = node["down state"];
|
||||
m_buttonDownImage = loadImage(cnode);
|
||||
m_buttonDownTranslate = yamlRead(cnode, "text translate", Point());
|
||||
m_buttonDownTextColor = yamlRead(cnode, "font color", getFontColor());
|
||||
}
|
||||
|
||||
if(node.FindValue("mouse over state"))
|
||||
m_buttonHoverImage = loadImage(node["mouse over state"]);
|
||||
|
||||
m_font = g_fonts.get(node["font"].Read<std::string>());
|
||||
|
||||
node["text color"] >> m_textColor;
|
||||
if(yamlHasValue(node, "hover state")) {
|
||||
const YAML::Node& cnode = node["hover state"];
|
||||
m_buttonHoverImage = loadImage(cnode);
|
||||
m_buttonHoverTextColor = yamlRead(cnode, "font color", getFontColor());
|
||||
}
|
||||
}
|
||||
|
||||
void UIButtonSkin::draw(UIElement *element)
|
||||
@@ -59,5 +63,10 @@ void UIButtonSkin::draw(UIElement *element)
|
||||
UIElementSkin::draw(element);
|
||||
}
|
||||
|
||||
m_font->renderText(button->getText(), textRect, ALIGN_CENTER, m_textColor);
|
||||
Color textColor = getFontColor();
|
||||
if(button->getState() == UIButton::ButtonDown)
|
||||
textColor = m_buttonDownTextColor;
|
||||
else if(button->getState() == UIButton::ButtonMouseOver)
|
||||
textColor = m_buttonHoverTextColor;
|
||||
getFont()->renderText(button->getText(), textRect, AlignCenter, textColor);
|
||||
}
|
||||
|
@@ -44,8 +44,8 @@ private:
|
||||
ImagePtr m_buttonDownImage;
|
||||
ImagePtr m_buttonHoverImage;
|
||||
Point m_buttonDownTranslate;
|
||||
Font *m_font;
|
||||
Color m_textColor;
|
||||
Color m_buttonDownTextColor;
|
||||
Color m_buttonHoverTextColor;
|
||||
};
|
||||
|
||||
#endif // UIBUTTONSKIN_H
|
||||
|
@@ -106,17 +106,6 @@ UIElementPtr UIContainer::getChildById(const std::string& id)
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
UIElementPtr UIContainer::getChildByPos(const Point& pos)
|
||||
{
|
||||
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
|
||||
const UIElementPtr& element = (*it);
|
||||
if(element->getRect().contains(pos))
|
||||
return element;
|
||||
}
|
||||
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
UIElementPtr UIContainer::recursiveGetChildById(const std::string& id)
|
||||
{
|
||||
if(getId() == id || id == "self")
|
||||
@@ -144,6 +133,35 @@ UIElementPtr UIContainer::recursiveGetChildById(const std::string& id)
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
UIElementPtr UIContainer::getChildByPos(const Point& pos)
|
||||
{
|
||||
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
|
||||
const UIElementPtr& element = (*it);
|
||||
if(element->getRect().contains(pos))
|
||||
return element;
|
||||
}
|
||||
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
UIElementPtr UIContainer::recursiveGetChildByPos(const Point& pos)
|
||||
{
|
||||
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
|
||||
const UIElementPtr& element = (*it);
|
||||
if(element->getRect().contains(pos)) {
|
||||
if(UIContainerPtr container = element->asUIContainer()) {
|
||||
if(UIElementPtr containerChild = container->recursiveGetChildByPos(pos))
|
||||
return containerChild;
|
||||
else
|
||||
return container;
|
||||
}
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
void UIContainer::pushChildToTop(const UIElementPtr& child)
|
||||
{
|
||||
auto it = std::find(m_children.begin(), m_children.end(), child);
|
||||
@@ -227,12 +245,13 @@ void UIContainer::focusNextElement()
|
||||
void UIContainer::setFocusedElement(const UIElementPtr& focusedElement)
|
||||
{
|
||||
if(focusedElement != m_focusedElement) {
|
||||
if(m_focusedElement)
|
||||
m_focusedElement->onFocusChange();
|
||||
|
||||
UIElementPtr oldFocused = m_focusedElement;
|
||||
m_focusedElement = focusedElement;
|
||||
if(m_focusedElement)
|
||||
m_focusedElement->onFocusChange();
|
||||
|
||||
if(oldFocused)
|
||||
oldFocused->onFocusChange();
|
||||
if(focusedElement)
|
||||
focusedElement->onFocusChange();
|
||||
}
|
||||
|
||||
// when containers are focused they go to the top
|
||||
|
@@ -48,10 +48,12 @@ public:
|
||||
bool hasChild(const UIElementPtr& child);
|
||||
/// Find an element in this container by id
|
||||
UIElementPtr getChildById(const std::string& id);
|
||||
/// Find an element by position
|
||||
UIElementPtr getChildByPos(const Point& pos);
|
||||
/// Find an element in this container and in its children by id
|
||||
UIElementPtr recursiveGetChildById(const std::string& id);
|
||||
/// Find an element by position
|
||||
UIElementPtr getChildByPos(const Point& pos);
|
||||
/// Find an element in this container and in its children by position
|
||||
UIElementPtr recursiveGetChildByPos(const Point& pos);
|
||||
/// Get children
|
||||
const std::list<UIElementPtr>& getChildren() const { return m_children; }
|
||||
/// Pushs a child to the top
|
||||
|
@@ -28,12 +28,14 @@
|
||||
#include <ui/uielementskin.h>
|
||||
#include <graphics/borderedimage.h>
|
||||
#include <graphics/textures.h>
|
||||
#include <graphics/fonts.h>
|
||||
|
||||
void UIElementSkin::load(const YAML::Node& node)
|
||||
{
|
||||
if(node.FindValue("default size"))
|
||||
node["default size"] >> m_defaultSize;
|
||||
m_defaultSize = yamlRead(node, "default size", Size());
|
||||
m_defaultImage = loadImage(node);
|
||||
m_font = loadFont(node);
|
||||
m_fontColor = yamlRead(node, "font color", g_uiSkins.getDefaultFontColor());
|
||||
}
|
||||
|
||||
void UIElementSkin::apply(UIElement* element)
|
||||
@@ -47,40 +49,29 @@ void UIElementSkin::draw(UIElement *element)
|
||||
if(m_defaultImage)
|
||||
m_defaultImage->draw(element->getRect());
|
||||
}
|
||||
|
||||
ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
|
||||
{
|
||||
ImagePtr image;
|
||||
TexturePtr texture;
|
||||
|
||||
if(node.FindValue("bordered image")) {
|
||||
const YAML::Node& child = node["bordered image"];
|
||||
Rect left, right, top, bottom, topLeft, topRight, bottomLeft, bottomRight, center;
|
||||
if(child.FindValue("left border"))
|
||||
child["left border"] >> left;
|
||||
if(child.FindValue("right border"))
|
||||
child["right border"] >> right;
|
||||
if(child.FindValue("top border"))
|
||||
child["top border"] >> top;
|
||||
if(child.FindValue("bottom border"))
|
||||
child["bottom border"] >> bottom;
|
||||
if(child.FindValue("top left corner"))
|
||||
child["top left corner"] >> topLeft;
|
||||
if(child.FindValue("top right corner"))
|
||||
child["top right corner"] >> topRight;
|
||||
if(child.FindValue("bottom left corner"))
|
||||
child["bottom left corner"] >> bottomLeft;
|
||||
if(child.FindValue("bottom right corner"))
|
||||
child["bottom right corner"] >> bottomRight;
|
||||
if(child.FindValue("center"))
|
||||
child["center"] >> center;
|
||||
if(yamlHasValue(node, "bordered image")) {
|
||||
const YAML::Node& cnode = node["bordered image"];
|
||||
Rect left = yamlRead(cnode, "left border", Rect());
|
||||
Rect right = yamlRead(cnode, "right border", Rect());
|
||||
Rect top = yamlRead(cnode, "top border", Rect());
|
||||
Rect bottom = yamlRead(cnode, "bottom border", Rect());
|
||||
Rect topLeft = yamlRead(cnode, "top left corner", Rect());
|
||||
Rect topRight = yamlRead(cnode, "top right corner", Rect());
|
||||
Rect bottomLeft = yamlRead(cnode, "bottom left corner", Rect());
|
||||
Rect bottomRight = yamlRead(cnode, "bottom right corner", Rect());
|
||||
Rect center = yamlRead(cnode, "center", Rect());
|
||||
std::string textureName = yamlRead(cnode, "source", std::string());
|
||||
|
||||
if(child.FindValue("image")) {
|
||||
std::string textureName;
|
||||
child["image"] >> textureName;
|
||||
texture = g_textures.get(textureName);
|
||||
} else {
|
||||
if(!textureName.empty())
|
||||
texture = g_textures.get("skins/" + textureName);
|
||||
else
|
||||
texture = g_uiSkins.getDefaultTexture();
|
||||
}
|
||||
|
||||
if(texture) {
|
||||
image = ImagePtr(new BorderedImage(texture,
|
||||
@@ -94,18 +85,20 @@ ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
|
||||
bottomRight,
|
||||
center));
|
||||
}
|
||||
} else if(node.FindValue("image")) {
|
||||
std::string textureName;
|
||||
node["image"] >> textureName;
|
||||
texture = g_textures.get(textureName);
|
||||
if(texture) {
|
||||
|
||||
if(!image)
|
||||
logError(yamlErrorDesc(cnode, "failed to load bordered image"));
|
||||
} else if(yamlHasValue(node, "image")) {
|
||||
texture = g_textures.get("skins/" + yamlRead<std::string>(node, "image"));
|
||||
if(texture)
|
||||
image = ImagePtr(new Image(texture));
|
||||
}
|
||||
|
||||
if(!image)
|
||||
logError(yamlErrorDesc(node["image"], "failed to load image"));
|
||||
}
|
||||
|
||||
if(texture && node.FindValue("antialised")){
|
||||
bool antialised;
|
||||
node["antialised"] >> antialised;
|
||||
if(texture) {
|
||||
bool antialised = yamlRead(node, "antialised", false);
|
||||
if(antialised)
|
||||
texture->enableBilinearFilter();
|
||||
}
|
||||
@@ -113,3 +106,14 @@ ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
|
||||
return image;
|
||||
}
|
||||
|
||||
FontPtr UIElementSkin::loadFont(const YAML::Node& node)
|
||||
{
|
||||
FontPtr font;
|
||||
if(yamlHasValue(node, "font"))
|
||||
font = g_fonts.get(yamlRead<std::string>(node, "font"));
|
||||
if(!font)
|
||||
font = g_uiSkins.getDefaultFont();
|
||||
return font;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -28,6 +28,7 @@
|
||||
#include <prerequisites.h>
|
||||
#include <graphics/image.h>
|
||||
#include <ui/uiconstants.h>
|
||||
#include <graphics/font.h>
|
||||
|
||||
class UIElement;
|
||||
|
||||
@@ -51,15 +52,20 @@ public:
|
||||
const Size& getDefaultSize() const { return m_defaultSize; }
|
||||
UI::ElementType getElementType() const { return m_elementType; }
|
||||
ImagePtr getDefaultImage() const { return m_defaultImage; }
|
||||
FontPtr getFont() const { return m_font; }
|
||||
Color getFontColor() const { return m_fontColor; }
|
||||
|
||||
protected:
|
||||
ImagePtr loadImage(const YAML::Node& node);
|
||||
FontPtr loadFont(const YAML::Node &node);
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
UI::ElementType m_elementType;
|
||||
Size m_defaultSize;
|
||||
ImagePtr m_defaultImage;
|
||||
FontPtr m_font;
|
||||
Color m_fontColor;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UIElementSkin> UIElementSkinPtr;
|
||||
|
@@ -34,20 +34,20 @@ class UILabel : public UIElement
|
||||
public:
|
||||
UILabel() :
|
||||
UIElement(UI::Label),
|
||||
m_align(ALIGN_TOP_LEFT),
|
||||
m_align(AlignTopLeft),
|
||||
m_color(Color::white) { }
|
||||
|
||||
void setText(const std::string& text);
|
||||
std::string getText() const { return m_text; }
|
||||
|
||||
void setAlign(int align) { m_align = align; }
|
||||
int getAlign() const { return m_align; }
|
||||
void setAlign(AlignmentFlag align) { m_align = align; }
|
||||
AlignmentFlag getAlign() const { return m_align; }
|
||||
|
||||
virtual const char *getScriptableName() const { return "UILabel"; }
|
||||
|
||||
private:
|
||||
std::string m_text;
|
||||
int m_align;
|
||||
AlignmentFlag m_align;
|
||||
Color m_color;
|
||||
};
|
||||
|
||||
|
@@ -30,28 +30,18 @@
|
||||
void UILabelSkin::load(const YAML::Node& node)
|
||||
{
|
||||
UIElementSkin::load(node);
|
||||
|
||||
if(node.FindValue("font"))
|
||||
m_font = g_fonts.get(node["font"].Read<std::string>());
|
||||
else
|
||||
m_font = g_fonts.getDefaultFont();
|
||||
|
||||
if(node.FindValue("text color"))
|
||||
node["text color"] >> m_textColor;
|
||||
else
|
||||
m_textColor = Color::white;
|
||||
}
|
||||
|
||||
void UILabelSkin::apply(UIElement* element)
|
||||
{
|
||||
UIElementSkin::apply(element);
|
||||
UILabel *label = static_cast<UILabel*>(element);
|
||||
label->setSize(m_font->calculateTextRectSize(label->getText()));
|
||||
label->setSize(getFont()->calculateTextRectSize(label->getText()));
|
||||
}
|
||||
|
||||
void UILabelSkin::draw(UIElement *element)
|
||||
{
|
||||
UIElementSkin::draw(element);
|
||||
UILabel *label = static_cast<UILabel*>(element);
|
||||
m_font->renderText(label->getText(), label->getRect(), label->getAlign(), m_textColor);
|
||||
getFont()->renderText(label->getText(), label->getRect(), label->getAlign(), getFontColor());
|
||||
}
|
||||
|
@@ -33,19 +33,11 @@ class UILabelSkin : public UIElementSkin
|
||||
{
|
||||
public:
|
||||
UILabelSkin(const std::string& name) :
|
||||
UIElementSkin(name, UI::Label),
|
||||
m_align(ALIGN_TOP_LEFT) { }
|
||||
UIElementSkin(name, UI::Label) { }
|
||||
|
||||
void load(const YAML::Node& node);
|
||||
void apply(UIElement *element);
|
||||
void draw(UIElement *element);
|
||||
|
||||
Font *getFont() const { return m_font; }
|
||||
|
||||
private:
|
||||
Font *m_font;
|
||||
int m_align;
|
||||
Color m_textColor;
|
||||
};
|
||||
|
||||
#endif // UILABELSKIN_H
|
||||
|
@@ -96,7 +96,7 @@ UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& p
|
||||
// create element interpreting it's id
|
||||
UIElementPtr element = createElementFromId(elementId);
|
||||
if(!element)
|
||||
throw YAML::Exception(doc.begin().first().GetMark(), "invalid element type");
|
||||
yamlThrowError(doc.begin().first(), "invalid element type");
|
||||
parent->addChild(element);
|
||||
|
||||
// populete it
|
||||
@@ -136,7 +136,7 @@ void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node&
|
||||
|
||||
UIElementPtr element = createElementFromId(id);
|
||||
if(!element) {
|
||||
logError(YAML::Exception(cnode.GetMark(), "invalid element type").what());
|
||||
logError(yamlErrorDesc(cnode, "invalid element type"));
|
||||
continue;
|
||||
}
|
||||
parent->addChild(element);
|
||||
@@ -167,78 +167,59 @@ void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
|
||||
void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
|
||||
{
|
||||
// set element skin
|
||||
if(node.FindValue("skin")) {
|
||||
if(node["skin"].GetType() == YAML::CT_SCALAR) {
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), node["skin"]));
|
||||
if(yamlHasValue(node, "skin")) {
|
||||
const YAML::Node& cnode = node["skin"];
|
||||
if(cnode.GetType() == YAML::CT_SCALAR) {
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), cnode));
|
||||
} else {
|
||||
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin());
|
||||
skin->load(node["skin"]);
|
||||
skin->load(cnode);
|
||||
element->setSkin(skin);
|
||||
}
|
||||
} else // apply default skin
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), "default"));
|
||||
|
||||
// load elements common proprieties
|
||||
if(node.FindValue("size")) {
|
||||
Size size;
|
||||
node["size"] >> size;
|
||||
element->setSize(size);
|
||||
}
|
||||
if(yamlHasValue(node, "size"))
|
||||
element->setSize(yamlRead<Size>(node, "size"));
|
||||
element->setMarginLeft(yamlRead(node, "margin.left", 0));
|
||||
element->setMarginRight(yamlRead(node, "margin.right", 0));
|
||||
element->setMarginTop(yamlRead(node, "margin.top", 0));
|
||||
element->setMarginBottom(yamlRead(node, "margin.bottom", 0));
|
||||
|
||||
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"))
|
||||
if(yamlHasValue(node, "anchors.left"))
|
||||
loadElementAnchor(element, UI::AnchorLeft, node["anchors.left"]);
|
||||
|
||||
if(node.FindValue("anchors.right"))
|
||||
if(yamlHasValue(node, "anchors.right"))
|
||||
loadElementAnchor(element, UI::AnchorRight, node["anchors.right"]);
|
||||
|
||||
if(node.FindValue("anchors.top"))
|
||||
if(yamlHasValue(node, "anchors.top"))
|
||||
loadElementAnchor(element, UI::AnchorTop, node["anchors.top"]);
|
||||
|
||||
if(node.FindValue("anchors.bottom"))
|
||||
if(yamlHasValue(node, "anchors.bottom"))
|
||||
loadElementAnchor(element, UI::AnchorBottom, node["anchors.bottom"]);
|
||||
|
||||
if(node.FindValue("anchors.horizontalCenter"))
|
||||
if(yamlHasValue(node, "anchors.horizontalCenter"))
|
||||
loadElementAnchor(element, UI::AnchorHorizontalCenter, node["anchors.horizontalCenter"]);
|
||||
|
||||
if(node.FindValue("anchors.verticalCenter"))
|
||||
if(yamlHasValue(node, "anchors.verticalCenter"))
|
||||
loadElementAnchor(element, UI::AnchorVerticalCenter, node["anchors.verticalCenter"]);
|
||||
|
||||
// load events
|
||||
if(node.FindValue("onLoad")) {
|
||||
if(yamlHasValue(node, "onLoad")) {
|
||||
const YAML::Node& cnode = node["onLoad"];
|
||||
if(g_lua.loadBufferAsFunction(cnode.Read<std::string>(), element->getId() + ":onLoad"))
|
||||
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), element->getId() + ":onLoad"))
|
||||
g_lua.setScriptableField(element, "onLoad");
|
||||
else
|
||||
logError(YAML::Exception(cnode.GetMark(), "failed to parse inline lua script").what());
|
||||
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
|
||||
}
|
||||
|
||||
if(node.FindValue("onDestroy")) {
|
||||
if(yamlHasValue(node, "onDestroy")) {
|
||||
const YAML::Node& cnode = node["onDestroy"];
|
||||
if(g_lua.loadBufferAsFunction(cnode.Read<std::string>(), element->getId() + ":onDestroy"))
|
||||
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), element->getId() + ":onDestroy"))
|
||||
g_lua.setScriptableField(element, "onDestroy");
|
||||
else
|
||||
logError(YAML::Exception(cnode.GetMark(), "failed to parse inline lua script").what());
|
||||
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
|
||||
}
|
||||
|
||||
// load specific element type
|
||||
@@ -246,19 +227,12 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
|
||||
loadButton(boost::static_pointer_cast<UIButton>(element), node);
|
||||
else if(element->getElementType() == UI::Window) {
|
||||
UIWindowPtr window = boost::static_pointer_cast<UIWindow>(element);
|
||||
if(node.FindValue("title"))
|
||||
window->setTitle(node["title"].Read<std::string>());
|
||||
window->setTitle(yamlRead(node, "title", std::string()));
|
||||
}
|
||||
else if(element->getElementType() == UI::Label) {
|
||||
UILabelPtr label = boost::static_pointer_cast<UILabel>(element);
|
||||
if(node.FindValue("text"))
|
||||
label->setText(node["text"].Read<std::string>());
|
||||
if(node.FindValue("align")) {
|
||||
std::string alignDesc;
|
||||
node["align"] >> alignDesc;
|
||||
if(alignDesc == "center")
|
||||
label->setAlign(ALIGN_CENTER);
|
||||
}
|
||||
label->setText(yamlRead(node, "text", std::string()));
|
||||
label->setAlign(parseAlignment(yamlRead(node, "align", std::string())));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +240,7 @@ void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::Anchor
|
||||
{
|
||||
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
|
||||
if(!layout) {
|
||||
logError(YAML::Exception(node.GetMark(), "could not add anchor, because this element does not participate of an anchor layout").what());
|
||||
logError(yamlErrorDesc(node, "could not add anchor, because this element does not participate of an anchor layout"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -276,7 +250,7 @@ void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::Anchor
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, anchorDescription, boost::is_any_of(std::string(".")));
|
||||
if(split.size() != 2) {
|
||||
logError(YAML::Exception(node.GetMark(), "invalid anchor").what());
|
||||
logError(yamlErrorDesc(node, "invalid anchor"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -284,12 +258,12 @@ void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::Anchor
|
||||
UI::AnchorPoint anchorLineEdge = UIAnchorLayout::parseAnchorPoint(split[1]);
|
||||
|
||||
if(anchorLineEdge == UI::AnchorNone) {
|
||||
logError(YAML::Exception(node.GetMark(), "invalid anchor type").what());
|
||||
logError(yamlErrorDesc(node, "invalid anchor type"));
|
||||
return;
|
||||
}
|
||||
|
||||
if(!layout->addAnchor(anchoredElement, anchoredEdge, AnchorLine(anchorLineElementId, anchorLineEdge)))
|
||||
logError(YAML::Exception(node.GetMark(), "anchoring failed").what());
|
||||
logError(yamlErrorDesc(node, "anchoring failed"));
|
||||
}
|
||||
|
||||
void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
|
||||
@@ -297,10 +271,11 @@ void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
|
||||
button->setText(node["text"].Read<std::string>());
|
||||
|
||||
// set on click event
|
||||
if(node.FindValue("onClick")) {
|
||||
if(g_lua.loadBufferAsFunction(node["onClick"].Read<std::string>(), button->getId() + ":onClick"))
|
||||
if(yamlHasValue(node, "onClick")) {
|
||||
const YAML::Node& cnode = node["onClick"];
|
||||
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), button->getId() + ":onClick"))
|
||||
g_lua.setScriptableField(button, "onClick");
|
||||
else
|
||||
logError(YAML::Exception(node["onClick"].GetMark(), "failed to parse inline lua script").what());
|
||||
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
|
||||
}
|
||||
}
|
||||
|
@@ -31,6 +31,7 @@
|
||||
#include <ui/uiwindowskin.h>
|
||||
#include <ui/uitexteditskin.h>
|
||||
#include <ui/uilabelskin.h>
|
||||
#include <graphics/fonts.h>
|
||||
|
||||
UISkins g_uiSkins;
|
||||
|
||||
@@ -48,9 +49,15 @@ void UISkins::load(const std::string& skinsFile)
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
|
||||
std::string defaultTexture;
|
||||
doc["default skin image"] >> defaultTexture;
|
||||
m_defaultTexture = g_textures.get("skins/" + defaultTexture);
|
||||
m_defaultFont = g_fonts.get(yamlRead<std::string>(doc, "default font"));
|
||||
if(!m_defaultFont)
|
||||
logFatal("FATAL ERROR: Could not load skin default font");
|
||||
|
||||
m_defaultFontColor = yamlRead(doc, "default font color", Color::white);
|
||||
|
||||
std::string defaultTextureName = yamlRead(doc, "default texture", std::string());
|
||||
if(!defaultTextureName.empty())
|
||||
m_defaultTexture = g_textures.get("skins/" + defaultTextureName);
|
||||
|
||||
{
|
||||
const YAML::Node& node = doc["buttons"];
|
||||
|
@@ -28,6 +28,7 @@
|
||||
#include <prerequisites.h>
|
||||
#include <graphics/texture.h>
|
||||
#include <ui/uielementskin.h>
|
||||
#include <graphics/font.h>
|
||||
|
||||
class UISkins
|
||||
{
|
||||
@@ -39,9 +40,13 @@ public:
|
||||
|
||||
UIElementSkinPtr getElementSkin(UI::ElementType elementType, const std::string& name = "default");
|
||||
TexturePtr getDefaultTexture() { return m_defaultTexture; }
|
||||
FontPtr getDefaultFont() { return m_defaultFont; }
|
||||
Color getDefaultFontColor() { return m_defaultFontColor; }
|
||||
|
||||
private:
|
||||
TexturePtr m_defaultTexture;
|
||||
FontPtr m_defaultFont;
|
||||
Color m_defaultFontColor;
|
||||
|
||||
std::vector<UIElementSkinPtr> m_elementSkins;
|
||||
};
|
||||
|
@@ -23,7 +23,6 @@
|
||||
|
||||
|
||||
#include <prerequisites.h>
|
||||
#include <graphics/fonts.h>
|
||||
#include <ui/uitextedit.h>
|
||||
#include <ui/uitexteditskin.h>
|
||||
#include <ui/uicontainer.h>
|
||||
|
@@ -30,28 +30,15 @@
|
||||
void UITextEditSkin::load(const YAML::Node& node)
|
||||
{
|
||||
UIElementSkin::load(node);
|
||||
|
||||
if(node.FindValue("font")) {
|
||||
m_font = g_fonts.get(node["font"].Read<std::string>());
|
||||
} else
|
||||
m_font = g_fonts.getDefaultFont();
|
||||
|
||||
if(node.FindValue("text color"))
|
||||
node["text color"] >> m_textColor;
|
||||
else
|
||||
m_textColor = Color::white;
|
||||
|
||||
if(node.FindValue("text margin"))
|
||||
node["text margin"] >> m_textMargin;
|
||||
else
|
||||
m_textMargin = 2;
|
||||
m_textMargin = yamlRead(node, "text margin", 2);
|
||||
}
|
||||
|
||||
void UITextEditSkin::apply(UIElement* element)
|
||||
{
|
||||
UIElementSkin::apply(element);
|
||||
UITextEdit *textEdit = static_cast<UITextEdit*>(element);
|
||||
textEdit->getTextArea().setFont(m_font);
|
||||
textEdit->getTextArea().setFont(getFont());
|
||||
textEdit->getTextArea().setColor(getFontColor());
|
||||
}
|
||||
|
||||
void UITextEditSkin::draw(UIElement* element)
|
||||
|
@@ -40,13 +40,10 @@ public:
|
||||
void apply(UIElement *element);
|
||||
void draw(UIElement *element);
|
||||
|
||||
Font *getFont() const { return m_font; }
|
||||
int getTextMargin() const { return m_textMargin; }
|
||||
|
||||
private:
|
||||
Font *m_font;
|
||||
int m_textMargin;
|
||||
Color m_textColor;
|
||||
};
|
||||
|
||||
#endif // UITEXTEDITSKIN_H
|
||||
|
@@ -30,34 +30,36 @@ void UIWindowSkin::load(const YAML::Node& node)
|
||||
{
|
||||
UIElementSkin::load(node);
|
||||
|
||||
node["head"]["height"] >> m_headHeight;
|
||||
m_headImage = loadImage(node["head"]);
|
||||
m_bodyImage = loadImage(node["body"]);
|
||||
const YAML::Node& headNode = node["head"];
|
||||
const YAML::Node& bodyNode = node["body"];
|
||||
|
||||
std::string fontName;
|
||||
node["head"]["font"] >> fontName;
|
||||
m_titleFont = g_fonts.get(fontName);
|
||||
|
||||
node["head"]["text color"] >> m_headTextColor;
|
||||
m_headImage = boost::dynamic_pointer_cast<BorderedImage>(loadImage(headNode));
|
||||
m_headHeight = yamlRead(headNode, "height", m_headImage->getDefaultSize().height());
|
||||
m_headMargin = yamlRead(headNode, "margin", 0);
|
||||
m_titleAlign = parseAlignment(yamlRead(headNode, "text align", std::string("center")));
|
||||
m_bodyImage = loadImage(bodyNode);
|
||||
}
|
||||
|
||||
void UIWindowSkin::draw(UIElement* element)
|
||||
{
|
||||
UIElementSkin::draw(element);
|
||||
|
||||
UIWindow *window = static_cast<UIWindow*>(element);
|
||||
|
||||
// draw window head
|
||||
Rect headRect = window->getRect();
|
||||
Rect bodyRect = window->getRect();
|
||||
|
||||
headRect.setHeight(m_headHeight);
|
||||
bodyRect.setTop(headRect.bottom() + 1);
|
||||
|
||||
m_headImage->draw(headRect);
|
||||
m_titleFont->renderText(window->getTitle(),
|
||||
headRect,
|
||||
ALIGN_CENTER,
|
||||
m_headTextColor);
|
||||
|
||||
// draw window head text
|
||||
Rect headTextRect = headRect;
|
||||
if(m_titleAlign & AlignLeft)
|
||||
headTextRect.addLeft(-m_headMargin);
|
||||
else if(m_titleAlign & AlignRight)
|
||||
headTextRect.addRight(-m_headMargin);
|
||||
getFont()->renderText(window->getTitle(), headTextRect, m_titleAlign, getFontColor());
|
||||
|
||||
// draw window body
|
||||
Rect bodyRect = window->getRect();
|
||||
bodyRect.setTop(headRect.bottom() + 1);
|
||||
m_bodyImage->draw(bodyRect);
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@
|
||||
#include <prerequisites.h>
|
||||
#include <graphics/font.h>
|
||||
#include <ui/uielementskin.h>
|
||||
#include <graphics/borderedimage.h>
|
||||
|
||||
class UIWindowSkin : public UIElementSkin
|
||||
{
|
||||
@@ -40,11 +41,12 @@ public:
|
||||
int getHeadHeight() const { return m_headHeight; }
|
||||
|
||||
private:
|
||||
ImagePtr m_headImage;
|
||||
BorderedImagePtr m_headImage;
|
||||
ImagePtr m_bodyImage;
|
||||
Font *m_titleFont;
|
||||
FontPtr m_titleFont;
|
||||
int m_headHeight;
|
||||
Color m_headTextColor;
|
||||
int m_headMargin;
|
||||
AlignmentFlag m_titleAlign;
|
||||
};
|
||||
|
||||
#endif // UIWINDOWSKIN_H
|
||||
|
Reference in New Issue
Block a user