mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
new script engine, and things maybe be bugged for a while
This commit is contained in:
@@ -5,8 +5,8 @@
|
||||
#include <ui/uilayout.h>
|
||||
|
||||
class UIElement;
|
||||
typedef boost::shared_ptr<UIElement> UIElementPtr;
|
||||
typedef boost::weak_ptr<UIElement> UIElementWeakPtr;
|
||||
typedef std::shared_ptr<UIElement> UIElementPtr;
|
||||
typedef std::weak_ptr<UIElement> UIElementWeakPtr;
|
||||
|
||||
enum AnchorPoint {
|
||||
AnchorNone = 0,
|
||||
@@ -61,6 +61,6 @@ private:
|
||||
std::vector<Anchor> m_anchors;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UIAnchorLayout> UIAnchorLayoutPtr;
|
||||
typedef std::shared_ptr<UIAnchorLayout> UIAnchorLayoutPtr;
|
||||
|
||||
#endif // UIANCHORLAYOUT_H
|
||||
|
@@ -10,7 +10,10 @@ void UIButton::onInputEvent(const InputEvent& event)
|
||||
} else if(event.type == EV_MOUSE_LUP && m_state == ButtonDown) {
|
||||
m_state = ButtonUp;
|
||||
if(getRect().contains(event.mousePos)) {
|
||||
g_dispatcher.addTask(boost::bind(&ScriptObject::callScriptTableField, shared_from_this(), "onClick", 0));
|
||||
LuaObjectPtr me = asLuaObject();
|
||||
g_dispatcher.addTask([me] {
|
||||
me->callField("onClick");
|
||||
});
|
||||
}
|
||||
} else if(event.type == EV_MOUSE_MOVE && m_state != ButtonDown) {
|
||||
if(isMouseOver())
|
||||
|
@@ -6,7 +6,7 @@
|
||||
#include <graphics/borderedimage.h>
|
||||
|
||||
class UIButton;
|
||||
typedef boost::shared_ptr<UIButton> UIButtonPtr;
|
||||
typedef std::shared_ptr<UIButton> UIButtonPtr;
|
||||
|
||||
class UIButton : public UIElement
|
||||
{
|
||||
@@ -22,6 +22,8 @@ public:
|
||||
UIElement(UI::Button),
|
||||
m_state(ButtonUp) { }
|
||||
|
||||
static UIButtonPtr create() { return UIButtonPtr(new UIButton); }
|
||||
|
||||
void onInputEvent(const InputEvent& event);
|
||||
|
||||
void setText(const std::string& text) { m_text = text; }
|
||||
@@ -29,7 +31,7 @@ public:
|
||||
|
||||
ButtonState getState() { return m_state; }
|
||||
|
||||
virtual const char *getScriptObjectType() const { return "UIButton"; }
|
||||
virtual const char *getLuaTypeName() const { return "UIButton"; }
|
||||
|
||||
private:
|
||||
std::string m_text;
|
||||
|
@@ -11,7 +11,7 @@ struct UIButtonStateSkin {
|
||||
Point textTranslate;
|
||||
Color textColor;
|
||||
};
|
||||
typedef boost::shared_ptr<UIButtonStateSkin> UIButtonStateSkinPtr;
|
||||
typedef std::shared_ptr<UIButtonStateSkin> UIButtonStateSkinPtr;
|
||||
|
||||
class UIButtonSkin : public UIElementSkin
|
||||
{
|
||||
|
@@ -9,7 +9,7 @@ class UICheckBox : public UIElement
|
||||
public:
|
||||
UICheckBox(UI::ElementType type = UI::Element);
|
||||
|
||||
virtual const char *getScriptObjectType() const { return "UICheckBox"; }
|
||||
virtual const char *getLuaTypeName() const { return "UICheckBox"; }
|
||||
};
|
||||
|
||||
#endif // UICHECKBOX_H
|
||||
|
@@ -67,18 +67,48 @@ UIElementPtr UIContainer::getChildById(const std::string& id)
|
||||
{
|
||||
if(getId() == id || id == "self")
|
||||
return asUIElement();
|
||||
|
||||
if(id == "parent")
|
||||
else if(id == "parent")
|
||||
return getParent();
|
||||
|
||||
if(id == "root")
|
||||
else if(id == "root")
|
||||
return getRoot();
|
||||
|
||||
foreach(const UIElementPtr& child, m_children) {
|
||||
if(child->getId() == id)
|
||||
return child;
|
||||
else if(id == "prev") {
|
||||
if(UIContainerPtr parent = getParent())
|
||||
return parent->getChildBefore(asUIElement());
|
||||
} else if(id == "next") {
|
||||
if(UIContainerPtr parent = getParent())
|
||||
return parent->getChildAfter(asUIElement());
|
||||
} else {
|
||||
foreach(const UIElementPtr& child, m_children) {
|
||||
if(child->getId() == id)
|
||||
return child;
|
||||
}
|
||||
}
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
UIElementPtr UIContainer::getChildBefore(const UIElementPtr& reference)
|
||||
{
|
||||
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
|
||||
const UIElementPtr& element = (*it);
|
||||
if(element == reference) {
|
||||
if(++it != m_children.rend())
|
||||
return (*it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
UIElementPtr UIContainer::getChildAfter(const UIElementPtr& reference)
|
||||
{
|
||||
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
const UIElementPtr& element = (*it);
|
||||
if(element == reference) {
|
||||
if(++it != m_children.end())
|
||||
return (*it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
@@ -86,26 +116,30 @@ UIElementPtr UIContainer::recursiveGetChildById(const std::string& id)
|
||||
{
|
||||
if(getId() == id || id == "self")
|
||||
return asUIElement();
|
||||
|
||||
if(id == "parent")
|
||||
else if(id == "parent")
|
||||
return getParent();
|
||||
|
||||
if(id == "root")
|
||||
else if(id == "root")
|
||||
return getRoot();
|
||||
|
||||
foreach(const UIElementPtr& element, m_children) {
|
||||
if(element->getId() == id)
|
||||
return element;
|
||||
else {
|
||||
UIContainerPtr container = element->asUIContainer();
|
||||
if(container) {
|
||||
UIElementPtr element2 = container->recursiveGetChildById(id);
|
||||
if(element2)
|
||||
return element2;
|
||||
else if(id == "prev") {
|
||||
if(UIContainerPtr parent = getParent())
|
||||
return parent->getChildBefore(asUIElement());
|
||||
} else if(id == "next") {
|
||||
if(UIContainerPtr parent = getParent())
|
||||
return parent->getChildAfter(asUIElement());
|
||||
} else {
|
||||
foreach(const UIElementPtr& element, m_children) {
|
||||
if(element->getId() == id)
|
||||
return element;
|
||||
else {
|
||||
UIContainerPtr container = element->asUIContainer();
|
||||
if(container) {
|
||||
UIElementPtr element2 = container->recursiveGetChildById(id);
|
||||
if(element2)
|
||||
return element2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
@@ -239,7 +273,7 @@ void UIContainer::setFocusedElement(const UIElementPtr& focusedElement)
|
||||
|
||||
// when containers are focused they go to the top
|
||||
if(focusedElement && focusedElement->asUIContainer()) {
|
||||
g_dispatcher.addTask(boost::bind(&UIContainer::pushChildToTop, asUIContainer(), m_focusedElement));
|
||||
g_dispatcher.addTask(std::bind(&UIContainer::pushChildToTop, asUIContainer(), m_focusedElement));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,6 +11,7 @@ public:
|
||||
UIElement(type) { }
|
||||
virtual ~UIContainer() { }
|
||||
|
||||
static UIContainerPtr create() { return UIContainerPtr(new UIContainer); }
|
||||
virtual void destroy();
|
||||
virtual void onLoad();
|
||||
virtual void render();
|
||||
@@ -28,6 +29,8 @@ public:
|
||||
UIElementPtr recursiveGetChildById(const std::string& id);
|
||||
/// Find an element by position
|
||||
UIElementPtr getChildByPos(const Point& pos);
|
||||
UIElementPtr getChildBefore(const UIElementPtr& reference);
|
||||
UIElementPtr getChildAfter(const UIElementPtr& reference);
|
||||
/// Find an element in this container and in its children by position
|
||||
UIElementPtr recursiveGetChildByPos(const Point& pos);
|
||||
/// Get children
|
||||
@@ -37,6 +40,7 @@ public:
|
||||
/// Return number of children
|
||||
int getChildCount() const { return m_children.size(); }
|
||||
|
||||
|
||||
/// Disable all children except the specified element
|
||||
bool lockElement(const UIElementPtr& element);
|
||||
/// Renable all children
|
||||
@@ -50,9 +54,9 @@ public:
|
||||
UIElementPtr getFocusedElement() const { return m_focusedElement; }
|
||||
|
||||
virtual UI::ElementType getElementType() const { return UI::Container; }
|
||||
UIContainerPtr asUIContainer() { return boost::static_pointer_cast<UIContainer>(shared_from_this()); }
|
||||
UIContainerPtr asUIContainer() { return std::static_pointer_cast<UIContainer>(shared_from_this()); }
|
||||
|
||||
virtual const char *getScriptObjectType() const { return "UIContainer"; }
|
||||
virtual const char *getLuaTypeName() const { return "UIContainer"; }
|
||||
|
||||
/// Get root container (the container that contains everything)
|
||||
static UIContainerPtr& getRoot();
|
||||
|
@@ -6,13 +6,15 @@
|
||||
#include <ui/uielementskin.h>
|
||||
#include <ui/uicontainer.h>
|
||||
#include <ui/uianchorlayout.h>
|
||||
#include <script/luainterface.h>
|
||||
|
||||
UIElement::UIElement(UI::ElementType type) :
|
||||
ScriptObject(),
|
||||
LuaObject(),
|
||||
m_type(type),
|
||||
m_visible(true),
|
||||
m_enabled(true),
|
||||
m_mouseOver(false),
|
||||
m_destroyed(false),
|
||||
m_marginLeft(0),
|
||||
m_marginRight(0),
|
||||
m_marginTop(0),
|
||||
@@ -31,30 +33,33 @@ UIElement::~UIElement()
|
||||
void UIElement::destroyLater()
|
||||
{
|
||||
//logTraceDebug(getId());
|
||||
g_dispatcher.addTask(boost::bind(&UIElement::destroy, asUIElement()));
|
||||
if(!m_destroyed)
|
||||
g_dispatcher.addTask(std::bind(&UIElement::destroy, asUIElement()));
|
||||
}
|
||||
|
||||
void UIElement::destroy()
|
||||
{
|
||||
//logTraceDebug(getId());
|
||||
if(!m_destroyed) {
|
||||
UIElementPtr me = asUIElement();
|
||||
|
||||
UIElementPtr me = asUIElement();
|
||||
callScriptTableField("onDestroy");
|
||||
// remove from parent
|
||||
if(getParent())
|
||||
getParent()->removeChild(me);
|
||||
|
||||
// remove from parent
|
||||
if(getParent())
|
||||
getParent()->removeChild(me);
|
||||
g_dispatcher.addTask(std::bind(&UIElement::destroyCheck, me));
|
||||
|
||||
// free script stuff
|
||||
releaseScriptObject();
|
||||
|
||||
g_dispatcher.addTask(boost::bind(&UIElement::destroyCheck, me));
|
||||
m_destroyed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void UIElement::destroyCheck()
|
||||
{
|
||||
//logTraceDebug(getId());
|
||||
|
||||
for(int i=0;i<2;++i)
|
||||
g_lua.collectGarbage();
|
||||
|
||||
UIElementPtr me = asUIElement();
|
||||
// check for leaks, the number of references must be always 2 here
|
||||
if(me.use_count() != 2 && me != UIContainer::getRoot()) {
|
||||
@@ -99,7 +104,10 @@ void UIElement::setSkin(const UIElementSkinPtr& skin)
|
||||
|
||||
void UIElement::onLoad()
|
||||
{
|
||||
g_dispatcher.addTask(boost::bind(&ScriptObject::callScriptTableField, shared_from_this(), "onLoad", 0));
|
||||
UIElementPtr me = asUIElement();
|
||||
g_dispatcher.addTask([me] {
|
||||
me->callField("onLoad");
|
||||
});
|
||||
}
|
||||
|
||||
void UIElement::render()
|
||||
@@ -111,24 +119,29 @@ void UIElement::render()
|
||||
|
||||
UIElementPtr UIElement::backwardsGetElementById(const std::string& id)
|
||||
{
|
||||
if(getId() == id || id == "self")
|
||||
return asUIElement();
|
||||
|
||||
if(id == "parent")
|
||||
return getParent();
|
||||
|
||||
if(id == "root")
|
||||
return UIContainer::getRoot();
|
||||
|
||||
UIElementPtr element;
|
||||
if(asUIContainer()) {
|
||||
element = asUIContainer()->recursiveGetChildById(id);
|
||||
if(element)
|
||||
return element;
|
||||
}
|
||||
if(getId() == id || id == "self")
|
||||
element = asUIElement();
|
||||
else if(id == "parent")
|
||||
element = getParent();
|
||||
else if(id == "root")
|
||||
element = UIContainer::getRoot();
|
||||
else if(id == "prev") {
|
||||
if(UIContainerPtr parent = getParent())
|
||||
element = parent->getChildBefore(asUIElement());
|
||||
} else if(id == "next") {
|
||||
if(UIContainerPtr parent = getParent())
|
||||
element = parent->getChildAfter(asUIElement());
|
||||
} else {
|
||||
if(asUIContainer()) {
|
||||
element = asUIContainer()->recursiveGetChildById(id);
|
||||
if(element)
|
||||
return element;
|
||||
}
|
||||
|
||||
if(getParent())
|
||||
element = getParent()->backwardsGetElementById(id);
|
||||
if(getParent())
|
||||
element = getParent()->backwardsGetElementById(id);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
@@ -153,6 +166,17 @@ void UIElement::moveTo(Point pos)
|
||||
setRect(newRect);
|
||||
}
|
||||
|
||||
void UIElement::setLocked(bool locked)
|
||||
{
|
||||
UIContainerPtr parent = getParent();
|
||||
if(parent) {
|
||||
if(locked)
|
||||
parent->lockElement(asUIElement());
|
||||
else
|
||||
parent->unlockElement(asUIElement());
|
||||
}
|
||||
}
|
||||
|
||||
void UIElement::setParent(UIContainerPtr parent)
|
||||
{
|
||||
UIElementPtr me = asUIElement();
|
||||
@@ -197,17 +221,13 @@ UILayoutPtr UIElement::getLayout() const
|
||||
|
||||
void UIElement::centerIn(const std::string& targetId)
|
||||
{
|
||||
addAnchor(AnchorHorizontalCenter, AnchorLine(targetId, AnchorHorizontalCenter));
|
||||
addAnchor(AnchorVerticalCenter, AnchorLine(targetId, AnchorVerticalCenter));
|
||||
addAnchor(AnchorHorizontalCenter, targetId, AnchorHorizontalCenter);
|
||||
addAnchor(AnchorVerticalCenter, targetId, AnchorVerticalCenter);
|
||||
}
|
||||
|
||||
void UIElement::addAnchor(AnchorPoint anchoredEdge, AnchorLine anchorEdge)
|
||||
void UIElement::addAnchor(AnchorPoint edge, const std::string& targetId, AnchorPoint targetEdge)
|
||||
{
|
||||
UIElementPtr target = backwardsGetElementById(anchorEdge.getElementId());
|
||||
if(!target)
|
||||
logWarning("warning: element id '", anchorEdge.getElementId(), "' doesn't exist while anchoring element '", getId(), "'");
|
||||
|
||||
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(getLayout());
|
||||
UIAnchorLayoutPtr layout = std::dynamic_pointer_cast<UIAnchorLayout>(getLayout());
|
||||
if(layout)
|
||||
layout->addAnchor(asUIElement(), anchoredEdge, anchorEdge);
|
||||
layout->addAnchor(asUIElement(), edge, AnchorLine(targetId, targetEdge));
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <global.h>
|
||||
#include <core/input.h>
|
||||
#include <script/scriptobject.h>
|
||||
#include <script/luaobject.h>
|
||||
#include <ui/uianchorlayout.h>
|
||||
|
||||
namespace UI {
|
||||
@@ -21,22 +21,23 @@ namespace UI {
|
||||
}
|
||||
|
||||
class UIElementSkin;
|
||||
typedef boost::shared_ptr<UIElementSkin> UIElementSkinPtr;
|
||||
typedef std::shared_ptr<UIElementSkin> UIElementSkinPtr;
|
||||
|
||||
class UIContainer;
|
||||
typedef boost::shared_ptr<UIContainer> UIContainerPtr;
|
||||
typedef boost::weak_ptr<UIContainer> UIContainerWeakPtr;
|
||||
typedef std::shared_ptr<UIContainer> UIContainerPtr;
|
||||
typedef std::weak_ptr<UIContainer> UIContainerWeakPtr;
|
||||
|
||||
class UIElement;
|
||||
typedef boost::shared_ptr<UIElement> UIElementPtr;
|
||||
typedef boost::weak_ptr<UIElement> UIElementWeakPtr;
|
||||
typedef std::shared_ptr<UIElement> UIElementPtr;
|
||||
typedef std::weak_ptr<UIElement> UIElementWeakPtr;
|
||||
|
||||
class UIElement : public ScriptObject
|
||||
class UIElement : public LuaObject
|
||||
{
|
||||
public:
|
||||
UIElement(UI::ElementType type = UI::Element);
|
||||
virtual ~UIElement();
|
||||
|
||||
static UIElementPtr create() { return UIElementPtr(new UIElement); }
|
||||
void destroyLater();
|
||||
virtual void destroy();
|
||||
virtual void destroyCheck();
|
||||
@@ -54,6 +55,8 @@ public:
|
||||
|
||||
void moveTo(Point pos);
|
||||
|
||||
void setLocked(bool locked);
|
||||
|
||||
void setLayout(const UILayoutPtr& layout) { m_layout = layout; }
|
||||
UILayoutPtr getLayout() const;
|
||||
|
||||
@@ -82,9 +85,9 @@ public:
|
||||
virtual bool isFocusable() const { return false; }
|
||||
UI::ElementType getElementType() const { return m_type; }
|
||||
|
||||
UIElementPtr asUIElement() { return boost::static_pointer_cast<UIElement>(shared_from_this()); }
|
||||
UIElementPtr asUIElement() { return std::static_pointer_cast<UIElement>(shared_from_this()); }
|
||||
virtual UIContainerPtr asUIContainer() { return UIContainerPtr(); }
|
||||
virtual const char *getScriptObjectType() const { return "UIElement"; }
|
||||
virtual const char *getLuaTypeName() const { return "UIElement"; }
|
||||
|
||||
void setSize(const Size& size);
|
||||
void setSize(int width, int height) { setSize(Size(width, height)); }
|
||||
@@ -115,7 +118,7 @@ public:
|
||||
int getMarginBottom() const { return m_marginBottom; }
|
||||
|
||||
void centerIn(const std::string& targetId);
|
||||
void addAnchor(AnchorPoint anchoredEdge, AnchorLine anchorEdge);
|
||||
void addAnchor(AnchorPoint edge, const std::string& targetId, AnchorPoint targetEdge);
|
||||
|
||||
private:
|
||||
UI::ElementType m_type;
|
||||
@@ -126,6 +129,7 @@ private:
|
||||
bool m_visible;
|
||||
bool m_enabled;
|
||||
bool m_mouseOver;
|
||||
bool m_destroyed;
|
||||
|
||||
Rect m_rect;
|
||||
int m_marginLeft;
|
||||
|
@@ -43,6 +43,6 @@ private:
|
||||
Color m_fontColor;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UIElementSkin> UIElementSkinPtr;
|
||||
typedef std::shared_ptr<UIElementSkin> UIElementSkinPtr;
|
||||
|
||||
#endif // UIELEMENTSKIN_H
|
||||
|
@@ -5,6 +5,9 @@
|
||||
#include <ui/uielement.h>
|
||||
#include <graphics/font.h>
|
||||
|
||||
class UILabel;
|
||||
typedef std::shared_ptr<UILabel> UILabelPtr;
|
||||
|
||||
class UILabel : public UIElement
|
||||
{
|
||||
public:
|
||||
@@ -12,19 +15,21 @@ public:
|
||||
UIElement(UI::Label),
|
||||
m_align(AlignLeftCenter) { }
|
||||
|
||||
static UILabelPtr create() { return UILabelPtr(new UILabel); }
|
||||
|
||||
void setText(const std::string& text);
|
||||
std::string getText() const { return m_text; }
|
||||
|
||||
void setAlign(AlignmentFlag align) { m_align = align; }
|
||||
AlignmentFlag getAlign() const { return m_align; }
|
||||
|
||||
virtual const char *getScriptObjectType() const { return "UILabel"; }
|
||||
virtual const char *getLuaTypeName() const { return "UILabel"; }
|
||||
|
||||
private:
|
||||
std::string m_text;
|
||||
AlignmentFlag m_align;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UILabel> UILabelPtr;
|
||||
typedef std::shared_ptr<UILabel> UILabelPtr;
|
||||
|
||||
#endif // UILABEL_H
|
||||
|
@@ -4,12 +4,12 @@
|
||||
#include <global.h>
|
||||
|
||||
class UIElement;
|
||||
typedef boost::shared_ptr<UIElement> UIElementPtr;
|
||||
typedef std::shared_ptr<UIElement> UIElementPtr;
|
||||
|
||||
class UILayout;
|
||||
typedef boost::shared_ptr<UILayout> UILayoutPtr;
|
||||
typedef std::shared_ptr<UILayout> UILayoutPtr;
|
||||
|
||||
class UILayout : public boost::enable_shared_from_this<UILayout>
|
||||
class UILayout : public std::enable_shared_from_this<UILayout>
|
||||
{
|
||||
public:
|
||||
UILayout() { }
|
||||
|
@@ -2,8 +2,7 @@
|
||||
#include <core/resources.h>
|
||||
#include <ui/ui.h>
|
||||
#include <ui/uiloader.h>
|
||||
#include <script/scriptcontext.h>
|
||||
#include <script/scriptfunctions.h>
|
||||
#include <script/luainterface.h>
|
||||
#include <otml/otml.h>
|
||||
#include <ui/uianchorlayout.h>
|
||||
#include <util/translator.h>
|
||||
@@ -46,16 +45,19 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
|
||||
return element;
|
||||
}
|
||||
|
||||
UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr& parent)
|
||||
UIElementPtr UILoader::loadFromFile(std::string fileName, const UIContainerPtr& parent)
|
||||
{
|
||||
UIElementPtr element;
|
||||
|
||||
if(!boost::ends_with(".otml", fileName))
|
||||
fileName += ".otml";
|
||||
|
||||
std::stringstream fin;
|
||||
if(!g_resources.loadFile(filePath, fin))
|
||||
if(!g_resources.loadFile(fileName, fin))
|
||||
return element;
|
||||
|
||||
try {
|
||||
OTMLParser parser(fin, filePath);
|
||||
OTMLParser parser(fin, fileName);
|
||||
OTMLNode* doc = parser.getDocument();
|
||||
|
||||
// get element id
|
||||
@@ -82,7 +84,7 @@ UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr&
|
||||
// report onLoad events
|
||||
element->onLoad();
|
||||
} catch(OTMLException e) {
|
||||
logError("ERROR: Failed to load ui ",filePath,": ", e.what());
|
||||
logError("ERROR: Failed to load ui ", g_resources.resolvePath(fileName) ,": ", e.what());
|
||||
}
|
||||
|
||||
return element;
|
||||
@@ -139,9 +141,13 @@ void UILoader::loadElement(const UIElementPtr& element, OTMLNode* node)
|
||||
} else // apply default skin
|
||||
element->applyDefaultSkin();
|
||||
|
||||
// load elements common proprieties
|
||||
if(node->hasChild("size"))
|
||||
element->setSize(node->readAt<Size>("size"));
|
||||
// load size
|
||||
Size size = element->getSize();
|
||||
size = node->readAt("size", size);
|
||||
size.setWidth(node->readAt("width", size.width()));
|
||||
size.setHeight(node->readAt("height", size.height()));
|
||||
if(size.isValid())
|
||||
element->setSize(size);
|
||||
|
||||
// load margins
|
||||
element->setMarginLeft(node->readAtPath("margin/left", 0));
|
||||
@@ -159,18 +165,20 @@ void UILoader::loadElement(const UIElementPtr& element, OTMLNode* node)
|
||||
|
||||
// load basic element events
|
||||
loadElementScriptFunction(element, node->at("onLoad"));
|
||||
loadElementScriptFunction(element, node->at("onDestroy"));
|
||||
|
||||
// load specific element type
|
||||
switch(element->getElementType()) {
|
||||
case UI::Button:
|
||||
loadButton(boost::static_pointer_cast<UIButton>(element), node);
|
||||
loadButton(std::static_pointer_cast<UIButton>(element), node);
|
||||
break;
|
||||
case UI::Window:
|
||||
loadWindow(boost::static_pointer_cast<UIWindow>(element), node);
|
||||
loadWindow(std::static_pointer_cast<UIWindow>(element), node);
|
||||
break;
|
||||
case UI::Label:
|
||||
loadLabel(boost::static_pointer_cast<UILabel>(element), node);
|
||||
loadLabel(std::static_pointer_cast<UILabel>(element), node);
|
||||
break;
|
||||
case UI::TextEdit:
|
||||
loadTextEdit(std::static_pointer_cast<UITextEdit>(element), node);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -188,7 +196,7 @@ void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, AnchorPoin
|
||||
return;
|
||||
}
|
||||
|
||||
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
|
||||
UIAnchorLayoutPtr layout = std::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
|
||||
if(!layout) {
|
||||
logError(node->generateErrorMessage("could not add anchor, because this element does not participate of an anchor layout"));
|
||||
return;
|
||||
@@ -222,8 +230,9 @@ void UILoader::loadElementScriptFunction(const UIElementPtr& element, OTMLNode*
|
||||
functionDesc += g_resources.resolvePath(node->what()) + ":" + element->getId();
|
||||
functionDesc += "[" + node->tag() + "]";
|
||||
|
||||
if(g_lua.loadBufferAsFunction(node->value(), functionDesc))
|
||||
g_lua.setScriptObjectField(element, node->tag());
|
||||
LuaValuePtr function = g_lua.loadFunction(node->value(), functionDesc);
|
||||
if(function->isFunction())
|
||||
element->setField(node->tag(), function);
|
||||
else
|
||||
logError(node->generateErrorMessage("failed to parse inline lua script"));
|
||||
}
|
||||
@@ -244,3 +253,8 @@ void UILoader::loadLabel(const UILabelPtr& label, OTMLNode* node)
|
||||
label->setText(node->readAt("text", std::string()));
|
||||
label->setAlign(parseAlignment(node->readAt("align", std::string("left"))));
|
||||
}
|
||||
|
||||
void UILoader::loadTextEdit(const UITextEditPtr& textEdit, OTMLNode* node)
|
||||
{
|
||||
textEdit->setText(node->readAt("text", std::string()));
|
||||
}
|
||||
|
@@ -30,13 +30,14 @@
|
||||
#include <ui/uibutton.h>
|
||||
#include <ui/uiwindow.h>
|
||||
#include <ui/uilabel.h>
|
||||
#include <ui/uitextedit.h>
|
||||
#include <ui/uianchorlayout.h>
|
||||
|
||||
class UILoader
|
||||
{
|
||||
public:
|
||||
/// Loads an UIElement and it's children from a FML file
|
||||
UIElementPtr loadFromFile(std::string filePath, const UIContainerPtr& parent = UIContainer::getRoot());
|
||||
UIElementPtr loadFromFile(std::string fileName, const UIContainerPtr& parent = UIContainer::getRoot());
|
||||
|
||||
private:
|
||||
/// Detect element type and create it
|
||||
@@ -61,6 +62,7 @@ private:
|
||||
void loadButton(const UIButtonPtr& button, OTMLNode* node);
|
||||
void loadWindow(const UIWindowPtr& window, OTMLNode* node);
|
||||
void loadLabel(const UILabelPtr& label, OTMLNode* node);
|
||||
void loadTextEdit(const UITextEditPtr& textEdit, OTMLNode* node);
|
||||
};
|
||||
|
||||
extern UILoader g_uiLoader;
|
||||
|
@@ -53,9 +53,10 @@ void UITextEdit::onInputEvent(const InputEvent& event)
|
||||
else if(event.keycode == KC_TAB) // focus next parent element
|
||||
getParent()->focusNextElement();
|
||||
} else if(event.type == EV_MOUSE_LDOWN) {
|
||||
|
||||
int pos = m_textArea.getTextPos(event.mousePos);
|
||||
if(pos >= 0)
|
||||
m_textArea.setCursorPos(m_textArea.getTextPos(event.mousePos));
|
||||
} else if(event.type == EV_MOUSE_LUP && getRect().contains(event.mousePos)) {
|
||||
m_textArea.setCursorPos(m_textArea.getTextPos(event.mousePos));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,27 +1,3 @@
|
||||
/* 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 UITEXTEDIT_H
|
||||
#define UITEXTEDIT_H
|
||||
|
||||
@@ -31,11 +7,16 @@
|
||||
|
||||
class Font;
|
||||
|
||||
class UITextEdit;
|
||||
typedef std::shared_ptr<UITextEdit> UITextEditPtr;
|
||||
|
||||
class UITextEdit : public UIElement
|
||||
{
|
||||
public:
|
||||
UITextEdit();
|
||||
|
||||
static UITextEditPtr create() { return UITextEditPtr(new UITextEdit); }
|
||||
|
||||
void onInputEvent(const InputEvent& event);
|
||||
void onRectUpdate();
|
||||
void onFocusChange();
|
||||
@@ -46,12 +27,10 @@ public:
|
||||
|
||||
bool isFocusable() const { return true; }
|
||||
|
||||
virtual const char *getScriptObjectType() const { return "UITextEdit"; }
|
||||
virtual const char *getLuaTypeName() const { return "UITextEdit"; }
|
||||
|
||||
private:
|
||||
TextArea m_textArea;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UITextEdit> UITextEditPtr;
|
||||
|
||||
#endif // UITEXTEDIT_H
|
||||
|
@@ -1,33 +1,12 @@
|
||||
/* 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 UIWINDOW_H
|
||||
#define UIWINDOW_H
|
||||
|
||||
#include <global.h>
|
||||
#include <ui/uicontainer.h>
|
||||
|
||||
class UIWindow;
|
||||
typedef std::shared_ptr<UIWindow> UIWindowPtr;
|
||||
|
||||
class UIWindow : public UIContainer
|
||||
{
|
||||
public:
|
||||
@@ -35,12 +14,14 @@ public:
|
||||
UIContainer(UI::Window),
|
||||
m_moving(false) { }
|
||||
|
||||
static UIWindowPtr create() { return UIWindowPtr(new UIWindow); }
|
||||
|
||||
void onInputEvent(const InputEvent& event);
|
||||
|
||||
void setTitle(const std::string& title) { m_title = title; }
|
||||
std::string getTitle() const { return m_title; }
|
||||
|
||||
virtual const char *getScriptObjectType() const { return "UIWindow"; }
|
||||
virtual const char *getLuaTypeName() const { return "UIWindow"; }
|
||||
|
||||
virtual bool isFocusable() const { return true; }
|
||||
|
||||
@@ -50,6 +31,4 @@ private:
|
||||
Point m_movingReference;
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<UIWindow> UIWindowPtr;
|
||||
|
||||
#endif // UIWINDOW_H
|
||||
|
@@ -34,7 +34,7 @@ void UIWindowSkin::load(OTMLNode* node)
|
||||
OTMLNode* headNode = node->at("head");
|
||||
OTMLNode* bodyNode = node->at("body");
|
||||
|
||||
m_headImage = boost::dynamic_pointer_cast<BorderedImage>(loadImage(headNode));
|
||||
m_headImage = std::dynamic_pointer_cast<BorderedImage>(loadImage(headNode));
|
||||
m_headHeight = headNode->readAt("height", m_headImage->getDefaultSize().height());
|
||||
m_headMargin = headNode->readAt("margin", 0);
|
||||
m_titleAlign = parseAlignment(headNode->readAt("text align", std::string("center")));
|
||||
|
Reference in New Issue
Block a user