lua scripting

This commit is contained in:
Eduardo Bart
2011-04-21 19:44:30 -03:00
parent bb05338190
commit 50b99a75b9
27 changed files with 674 additions and 145 deletions

View File

@@ -45,6 +45,8 @@ public:
void setOnClick(const Callback& callback) { m_buttonClickCallback = callback; }
virtual const char *getScriptableName() const { return "UIButton"; }
private:
std::string m_text;
UI::EButtonState m_state;

View File

@@ -30,9 +30,10 @@
class UICheckBox : public UIElement
{
public:
UICheckBox(UI::EElementType type = UI::Element);
UICheckBox(UI::EElementType type = UI::Element);
virtual const char *getScriptableName() const { return "UICheckBox"; }
};
#endif // UICHECKBOX_H

View File

@@ -62,6 +62,8 @@ public:
virtual UI::EElementType getElementType() const { return UI::Container; }
UIContainerPtr asUIContainer() { return boost::static_pointer_cast<UIContainer>(shared_from_this()); }
virtual const char *getScriptableName() const { return "UIContainer"; }
/// Get root container (the container that contains everything)
static UIContainerPtr& getRootContainer();

View File

@@ -84,6 +84,7 @@ public:
UIElementPtr asUIElement() { return boost::static_pointer_cast<UIElement>(shared_from_this()); }
virtual UIContainerPtr asUIContainer() { return UIContainerPtr(); }
virtual const char *getScriptableName() const { return "UIElement"; }
friend class UIContainer;

View File

@@ -43,6 +43,8 @@ public:
void setAlign(int align) { m_align = align; }
int getAlign() const { return m_align; }
virtual const char *getScriptableName() const { return "UILabel"; }
private:
std::string m_text;
int m_align;

View File

@@ -27,6 +27,7 @@
#include <prerequisites.h>
#include <ui/uiconstants.h>
#include <script/scriptable.h>
enum EAnchorType {
ANCHOR_LEFT = 0,
@@ -62,7 +63,7 @@ private:
EAnchorType m_anchorType;
};
class UILayout : public boost::enable_shared_from_this<UILayout>
class UILayout : public Scriptable
{
public:
UILayout() :
@@ -106,7 +107,9 @@ public:
void setMarginTop(int margin) { m_marginTop = margin; recalculateLayout(); }
void setMarginBottom(int margin) { m_marginBottom = margin; recalculateLayout(); }
UILayoutPtr asUILayout() { return shared_from_this(); }
UILayoutPtr asUILayout() { return boost::static_pointer_cast<UILayout>(shared_from_this()); }
virtual const char *getScriptableName() const { return "UILayout"; }
protected:
virtual void onLayoutRectChange(const Rect& newRect) { }

View File

@@ -26,6 +26,7 @@
#include <core/resources.h>
#include <ui/ui.h>
#include <ui/uiloader.h>
#include <script/luascript.h>
UIElementPtr UILoader::createElementFromId(const std::string& id)
{
@@ -149,10 +150,8 @@ void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
{
// load specific element type
if(element->getElementType() == UI::Button) {
UIButtonPtr button = boost::static_pointer_cast<UIButton>(element);
button->setText(node["text"].Read<std::string>());
}
if(element->getElementType() == UI::Button)
loadButton(boost::static_pointer_cast<UIButton>(element), node);
else if(element->getElementType() == UI::Window) {
UIWindowPtr window = boost::static_pointer_cast<UIWindow>(element);
window->setTitle(node["title"].Read<std::string>());
@@ -266,3 +265,18 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type,
throw YAML::Exception(node.GetMark(), "anchoring failed, does the relative element really exists?");
}
}
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")) {
int funcRef = g_lua.loadBufferAsFunction(node["onClick"].Read<std::string>());
if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(button);
g_lua.pushFunction(funcRef);
g_lua.lua_UIButton_setOnClick();
}
}
}

View File

@@ -28,6 +28,7 @@
#include <prerequisites.h>
#include <ui/uiconstants.h>
#include <ui/uicontainer.h>
#include <ui/uibutton.h>
class UILoader
{
@@ -50,6 +51,9 @@ private:
/// Load anchor from a YAML node
static void loadElementAnchor(const UIElementPtr& element, EAnchorType type, const YAML::Node& node);
// specific elements loading
static void loadButton(const UIButtonPtr& button, const YAML::Node& node);
};
#endif // UILOADER_H

View File

@@ -46,6 +46,8 @@ public:
bool isFocusable() const { return true; }
virtual const char *getScriptableName() const { return "UITextEdit"; }
private:
TextArea m_textArea;
};

View File

@@ -40,6 +40,8 @@ public:
void setTitle(const std::string& title) { m_title = title; }
const std::string& getTitle() const { return m_title; }
virtual const char *getScriptableName() const { return "UIWindow"; }
private:
std::string m_title;
bool m_moving;