|
|
@ -23,182 +23,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <prerequisites.h>
|
|
|
|
#include <prerequisites.h>
|
|
|
|
|
|
|
|
#include <script/luafunctions.h>
|
|
|
|
#include <script/luascript.h>
|
|
|
|
#include <script/luascript.h>
|
|
|
|
#include <core/engine.h>
|
|
|
|
#include <core/engine.h>
|
|
|
|
#include <core/resources.h>
|
|
|
|
#include <core/resources.h>
|
|
|
|
#include <ui/ui.h>
|
|
|
|
#include <ui/ui.h>
|
|
|
|
|
|
|
|
|
|
|
|
void LuaScript::registerFunctions()
|
|
|
|
void registerLuaFunctions()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
registerGlobalFunction("exitGame", &LuaScript::lua_exitGame);
|
|
|
|
// App
|
|
|
|
registerGlobalFunction("loadUI", &LuaScript::lua_loadUI);
|
|
|
|
g_lua.registerModule("App");
|
|
|
|
registerGlobalFunction("getUIRootContainer", &LuaScript::lua_getUIRootContainer);
|
|
|
|
g_lua.registerMemberFunction("App", "exit", &lua_App_exit);
|
|
|
|
registerGlobalFunction("setOnApplicationClose", &LuaScript::lua_setOnApplicationClose);
|
|
|
|
g_lua.registerMemberFunction("App", "setOnClose", &lua_App_setOnClose);
|
|
|
|
|
|
|
|
|
|
|
|
registerClass("UILayout");
|
|
|
|
// UI
|
|
|
|
registerClass("UIElement", "UILayout");
|
|
|
|
g_lua.registerModule("UI");
|
|
|
|
registerClass("UIButton", "UIElement");
|
|
|
|
g_lua.registerMemberFunction("UI", "load", &lua_UI_load);
|
|
|
|
registerClass("UITextEdit", "UIElement");
|
|
|
|
g_lua.registerMemberFunction("UI", "getRootContainer", &lua_UI_getRootContainer);
|
|
|
|
registerClass("UICheckBox", "UIElement");
|
|
|
|
|
|
|
|
registerClass("UIContainer", "UIElement");
|
|
|
|
|
|
|
|
registerClass("UIWindow", "UIContainer");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
registerMemberFunction("UIElement", "setOnLoad", &LuaScript::lua_UIElement_setOnLoad);
|
|
|
|
// UILayout
|
|
|
|
registerMemberFunction("UIElement", "setOnDestroy", &LuaScript::lua_UIElement_setOnDestroy);
|
|
|
|
g_lua.registerClass("UILayout");
|
|
|
|
registerMemberFunction("UIElement", "getParent", &LuaScript::lua_UIElement_getParent);
|
|
|
|
|
|
|
|
registerMemberFunction("UIElement", "destroy", &LuaScript::lua_UIElement_destroy);
|
|
|
|
// UIElement
|
|
|
|
registerMemberFunction("UIContainer", "getChildByID", &LuaScript::lua_UIContainer_getChildByID);
|
|
|
|
g_lua.registerClass("UIElement", "UILayout");
|
|
|
|
registerMemberFunction("UIContainer", "lock", &LuaScript::lua_UIContainer_lock);
|
|
|
|
g_lua.registerMemberFunction("UIElement", "setOnLoad", &lua_UIElement_setOnLoad);
|
|
|
|
registerMemberFunction("UIContainer", "unlock", &LuaScript::lua_UIContainer_unlock);
|
|
|
|
g_lua.registerMemberFunction("UIElement", "setOnDestroy", &lua_UIElement_setOnDestroy);
|
|
|
|
registerMemberFunction("UIButton", "setOnClick", &LuaScript::lua_UIButton_setOnClick);
|
|
|
|
g_lua.registerMemberFunction("UIElement", "getParent", &lua_UIElement_getParent);
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIElement", "destroy", &lua_UIElement_destroy);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UIContainer
|
|
|
|
|
|
|
|
g_lua.registerClass("UIContainer", "UIElement");
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIContainer", "getChildByID", &lua_UIContainer_getChildByID);
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIContainer", "lock", &lua_UIContainer_lock);
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIContainer", "unlock", &lua_UIContainer_unlock);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UILabel
|
|
|
|
|
|
|
|
g_lua.registerClass("UILabel", "UIElement");
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UILabel", "setText", &lua_UILabel_setText);
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UILabel", "getText", &lua_UILabel_getText);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UIButton
|
|
|
|
|
|
|
|
g_lua.registerClass("UIButton", "UIElement");
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIButton", "setOnClick", &lua_UIButton_setOnClick);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UIWindow
|
|
|
|
|
|
|
|
g_lua.registerClass("UIWindow", "UIContainer");
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIWindow", "setTitle", &lua_UIWindow_setTitle);
|
|
|
|
|
|
|
|
g_lua.registerMemberFunction("UIWindow", "getTitle", &lua_UIWindow_getTitle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_exitGame()
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// App module
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_App_exit()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
g_engine.stop();
|
|
|
|
g_engine.stop();
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_loadUI()
|
|
|
|
int lua_App_setOnClose()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
g_engine.setOnClose(g_lua.createSimpleFuncCallback(g_lua.popFunction()));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// UI module
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UI_load()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UIContainerPtr parent;
|
|
|
|
UIContainerPtr parent;
|
|
|
|
if(getStackSize() > 1) {
|
|
|
|
if(g_lua.getStackSize() > 1) {
|
|
|
|
parent = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
|
|
|
parent = boost::dynamic_pointer_cast<UIContainer>(g_lua.popClassInstance());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
parent = UIContainer::getRootContainer();
|
|
|
|
parent = UIContainer::getRoot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string uiFile = popString();
|
|
|
|
std::string uiFile = g_lua.popString();
|
|
|
|
|
|
|
|
|
|
|
|
UIElementPtr element;
|
|
|
|
UIElementPtr element;
|
|
|
|
if(parent)
|
|
|
|
if(parent)
|
|
|
|
element = UILoader::loadFile(uiFile.c_str(), parent);
|
|
|
|
element = UILoader::loadFile(uiFile.c_str(), parent);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
reportErrorWithTraceback("invalid parent container");
|
|
|
|
g_lua.reportErrorWithTraceback("invalid parent container");
|
|
|
|
|
|
|
|
|
|
|
|
pushClassInstance(element);
|
|
|
|
g_lua.pushClassInstance(element);
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_getUIRootContainer()
|
|
|
|
int lua_UI_getRootContainer()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UIContainerPtr rootContainer = UIContainer::getRootContainer();
|
|
|
|
UIContainerPtr rootContainer = UIContainer::getRoot();
|
|
|
|
pushClassInstance(rootContainer);
|
|
|
|
g_lua.pushClassInstance(rootContainer);
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_setOnApplicationClose()
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// UIElement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UIElement_setOnLoad()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int funcRef = popFunction();
|
|
|
|
g_lua.moveTop(-2);
|
|
|
|
g_engine.setOnClose([this, funcRef] {
|
|
|
|
if(UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(g_lua.popClassInstance()))
|
|
|
|
pushFunction(funcRef);
|
|
|
|
element->setOnLoad(g_lua.createScriptableSelfFuncCallback(g_lua.popFunction()));
|
|
|
|
callFunction();
|
|
|
|
else
|
|
|
|
});
|
|
|
|
g_lua.pop();
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIElement_setOnLoad()
|
|
|
|
int lua_UIElement_setOnDestroy()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
moveTop(-2);
|
|
|
|
g_lua.moveTop(-2);
|
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
|
|
|
if(UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(g_lua.popClassInstance()))
|
|
|
|
if(element) {
|
|
|
|
element->setOnDestroy(g_lua.createScriptableSelfFuncCallback(g_lua.popFunction()));
|
|
|
|
int funcRef = popFunction();
|
|
|
|
else
|
|
|
|
element->setOnLoad([this, funcRef](UIElementPtr element) {
|
|
|
|
g_lua.pop();
|
|
|
|
pushFunction(funcRef);
|
|
|
|
|
|
|
|
setLocal(element, "self");
|
|
|
|
|
|
|
|
callFunction();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
pop();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIElement_setOnDestroy()
|
|
|
|
int lua_UIElement_getParent()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
moveTop(-2);
|
|
|
|
if(UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(g_lua.popClassInstance()))
|
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
|
|
|
g_lua.pushClassInstance(element->getParent());
|
|
|
|
if(element) {
|
|
|
|
else
|
|
|
|
int funcRef = popFunction();
|
|
|
|
g_lua.pushNil();
|
|
|
|
element->setOnDestroy([this, funcRef](UIElementPtr element) {
|
|
|
|
|
|
|
|
pushFunction(funcRef);
|
|
|
|
|
|
|
|
setLocal(element, "self");
|
|
|
|
|
|
|
|
callFunction();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
pop();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIElement_destroy()
|
|
|
|
int lua_UIElement_destroy()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
|
|
|
if(UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(g_lua.popClassInstance()))
|
|
|
|
if(element)
|
|
|
|
|
|
|
|
element->destroy();
|
|
|
|
element->destroy();
|
|
|
|
else
|
|
|
|
|
|
|
|
pushNil();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIElement_getParent()
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// UIContainer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UIContainer_getChildByID()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
|
|
|
std::string id = g_lua.popString();
|
|
|
|
if(element)
|
|
|
|
if(UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(g_lua.popClassInstance()))
|
|
|
|
pushClassInstance(element->getParent());
|
|
|
|
g_lua.pushClassInstance(container->getChildById(id));
|
|
|
|
else
|
|
|
|
else
|
|
|
|
pushNil();
|
|
|
|
g_lua.pushNil();
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIButton_setOnClick()
|
|
|
|
int lua_UIContainer_lock()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
moveTop(-2);
|
|
|
|
if(UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(g_lua.popClassInstance())) {
|
|
|
|
UIButtonPtr button = boost::dynamic_pointer_cast<UIButton>(popClassInstance());
|
|
|
|
if(UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(g_lua.popClassInstance()))
|
|
|
|
if(button) {
|
|
|
|
g_lua.pushBoolean(container->lockElement(element));
|
|
|
|
int funcRef = popFunction();
|
|
|
|
else
|
|
|
|
button->setOnClick([this, funcRef](UIElementPtr button) {
|
|
|
|
g_lua.pushBoolean(false);
|
|
|
|
pushFunction(funcRef);
|
|
|
|
|
|
|
|
setLocal(button, "self");
|
|
|
|
|
|
|
|
callFunction();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
pop();
|
|
|
|
g_lua.reportFuncErrorWithTraceback("invalid element");
|
|
|
|
|
|
|
|
g_lua.pushBoolean(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIContainer_getChildByID()
|
|
|
|
int lua_UIContainer_unlock()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::string id = popString();
|
|
|
|
if(UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(g_lua.popClassInstance())) {
|
|
|
|
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
|
|
|
if(UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(g_lua.popClassInstance()))
|
|
|
|
if(container)
|
|
|
|
g_lua.pushBoolean(container->unlockElement(element));
|
|
|
|
pushClassInstance(container->getChildById(id));
|
|
|
|
else
|
|
|
|
|
|
|
|
g_lua.pushBoolean(false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
g_lua.reportFuncErrorWithTraceback("invalid element");
|
|
|
|
|
|
|
|
g_lua.pushBoolean(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// UILabel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UILabel_setText()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string text = g_lua.popString();
|
|
|
|
|
|
|
|
if(UILabelPtr label = boost::dynamic_pointer_cast<UILabel>(g_lua.popClassInstance()))
|
|
|
|
|
|
|
|
label->setText(text);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UILabel_getText()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if(UILabelPtr label = boost::dynamic_pointer_cast<UILabel>(g_lua.popClassInstance()))
|
|
|
|
|
|
|
|
g_lua.pushString(label->getText());
|
|
|
|
else
|
|
|
|
else
|
|
|
|
pushNil();
|
|
|
|
g_lua.pushNil();
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIContainer_lock()
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// UIButton
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UIButton_setOnClick()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
|
|
|
g_lua.moveTop(-2);
|
|
|
|
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
|
|
|
if(UIButtonPtr button = boost::dynamic_pointer_cast<UIButton>(g_lua.popClassInstance()))
|
|
|
|
if(!element) {
|
|
|
|
button->setOnClick(g_lua.createScriptableSelfFuncCallback(g_lua.popFunction()));
|
|
|
|
reportFuncErrorWithTraceback("invalid lock element");
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
g_lua.pop();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(container) {
|
|
|
|
|
|
|
|
container->lockElement(element);
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
}
|
|
|
|
// UIWindow
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int lua_UIWindow_setTitle()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string title = g_lua.popString();
|
|
|
|
|
|
|
|
if(UIWindowPtr window = boost::dynamic_pointer_cast<UIWindow>(g_lua.popClassInstance()))
|
|
|
|
|
|
|
|
window->setTitle(title);
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int LuaScript::lua_UIContainer_unlock()
|
|
|
|
int lua_UIWindow_getTitle()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
|
|
|
if(UIWindowPtr window = boost::dynamic_pointer_cast<UIWindow>(g_lua.popClassInstance()))
|
|
|
|
if(container) {
|
|
|
|
g_lua.pushString(window->getTitle());
|
|
|
|
container->unlockElement();
|
|
|
|
else
|
|
|
|
}
|
|
|
|
g_lua.pushNil();
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|