mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 05:53:26 +02:00
lua scripting
This commit is contained in:
@@ -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;
|
||||
|
@@ -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
|
||||
|
@@ -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();
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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) { }
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -46,6 +46,8 @@ public:
|
||||
|
||||
bool isFocusable() const { return true; }
|
||||
|
||||
virtual const char *getScriptableName() const { return "UITextEdit"; }
|
||||
|
||||
private:
|
||||
TextArea m_textArea;
|
||||
};
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user