2011-04-22 00:44:30 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <prerequisites.h>
|
|
|
|
#include <script/luascript.h>
|
|
|
|
#include <core/engine.h>
|
|
|
|
#include <core/resources.h>
|
|
|
|
#include <ui/ui.h>
|
|
|
|
|
|
|
|
void LuaScript::registerFunctions()
|
|
|
|
{
|
|
|
|
registerGlobalFunction("exitGame", &LuaScript::lua_exitGame);
|
|
|
|
registerGlobalFunction("loadUI", &LuaScript::lua_loadUI);
|
|
|
|
registerGlobalFunction("getUIRootContainer", &LuaScript::lua_getUIRootContainer);
|
2011-04-22 15:49:46 +02:00
|
|
|
registerGlobalFunction("setOnApplicationClose", &LuaScript::lua_setOnApplicationClose);
|
2011-04-22 00:44:30 +02:00
|
|
|
|
|
|
|
registerClass("UILayout");
|
|
|
|
registerClass("UIElement", "UILayout");
|
|
|
|
registerClass("UIButton", "UIElement");
|
|
|
|
registerClass("UITextEdit", "UIElement");
|
|
|
|
registerClass("UICheckBox", "UIElement");
|
|
|
|
registerClass("UIContainer", "UIElement");
|
|
|
|
registerClass("UIWindow", "UIContainer");
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
registerMemberFunction("UIElement", "getParent", &LuaScript::lua_UIElement_getParent);
|
2011-04-22 00:44:30 +02:00
|
|
|
registerMemberFunction("UIElement", "destroy", &LuaScript::lua_UIElement_destroy);
|
|
|
|
registerMemberFunction("UIContainer", "getChildByID", &LuaScript::lua_UIContainer_getChildByID);
|
2011-04-22 15:49:46 +02:00
|
|
|
registerMemberFunction("UIContainer", "lock", &LuaScript::lua_UIContainer_lock);
|
|
|
|
registerMemberFunction("UIContainer", "unlock", &LuaScript::lua_UIContainer_unlock);
|
2011-04-22 00:44:30 +02:00
|
|
|
registerMemberFunction("UIButton", "setOnClick", &LuaScript::lua_UIButton_setOnClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_exitGame()
|
|
|
|
{
|
|
|
|
g_engine.stop();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_loadUI()
|
|
|
|
{
|
|
|
|
UIContainerPtr parent;
|
|
|
|
if(getStackSize() > 1) {
|
2011-04-22 20:48:02 +02:00
|
|
|
parent = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
2011-04-22 00:44:30 +02:00
|
|
|
} else {
|
|
|
|
parent = UIContainer::getRootContainer();
|
|
|
|
}
|
2011-04-22 20:48:02 +02:00
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
std::string uiFile = popString();
|
2011-04-22 20:48:02 +02:00
|
|
|
|
|
|
|
UIElementPtr element;
|
|
|
|
if(parent)
|
|
|
|
element = UILoader::loadFile(uiFile.c_str(), parent);
|
|
|
|
else
|
|
|
|
reportErrorWithTraceback("invalid parent container");
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
pushClassInstance(element);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_getUIRootContainer()
|
|
|
|
{
|
|
|
|
UIContainerPtr rootContainer = UIContainer::getRootContainer();
|
|
|
|
pushClassInstance(rootContainer);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
int LuaScript::lua_setOnApplicationClose()
|
|
|
|
{
|
|
|
|
int funcRef = popFunction();
|
|
|
|
g_engine.setOnClose([this, funcRef] {
|
|
|
|
pushFunction(funcRef);
|
|
|
|
callFunction();
|
|
|
|
});
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-22 00:44:30 +02:00
|
|
|
int LuaScript::lua_UIElement_destroy()
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
2011-04-22 15:49:46 +02:00
|
|
|
if(element)
|
|
|
|
element->destroy();
|
|
|
|
else
|
|
|
|
pushNil();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_UIElement_getParent()
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
2011-04-22 15:49:46 +02:00
|
|
|
if(element)
|
|
|
|
pushClassInstance(element->getParent());
|
|
|
|
else
|
|
|
|
pushNil();
|
2011-04-22 00:44:30 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_UIButton_setOnClick()
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
moveTop(-2);
|
|
|
|
UIButtonPtr button = boost::dynamic_pointer_cast<UIButton>(popClassInstance());
|
2011-04-22 00:44:30 +02:00
|
|
|
if(button) {
|
|
|
|
int funcRef = popFunction();
|
2011-04-22 15:49:46 +02:00
|
|
|
button->setOnClick([this, funcRef](UIButtonPtr button) {
|
2011-04-22 00:44:30 +02:00
|
|
|
pushFunction(funcRef);
|
2011-04-22 20:48:02 +02:00
|
|
|
setLocal(button, "self");
|
2011-04-22 00:44:30 +02:00
|
|
|
callFunction();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
pop();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_UIContainer_getChildByID()
|
|
|
|
{
|
|
|
|
std::string id = popString();
|
2011-04-22 20:48:02 +02:00
|
|
|
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
2011-04-22 15:49:46 +02:00
|
|
|
if(container)
|
2011-04-22 00:44:30 +02:00
|
|
|
pushClassInstance(container->getChildById(id));
|
2011-04-22 15:49:46 +02:00
|
|
|
else
|
2011-04-22 00:44:30 +02:00
|
|
|
pushNil();
|
2011-04-22 15:49:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaScript::lua_UIContainer_lock()
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
UIElementPtr element = boost::dynamic_pointer_cast<UIElement>(popClassInstance());
|
|
|
|
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
|
|
|
if(!element) {
|
|
|
|
reportFuncErrorWithTraceback("invalid lock element");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(container) {
|
2011-04-22 15:49:46 +02:00
|
|
|
container->lockElement(element);
|
2011-04-22 00:44:30 +02:00
|
|
|
}
|
2011-04-22 15:49:46 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-04-22 00:44:30 +02:00
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
int LuaScript::lua_UIContainer_unlock()
|
|
|
|
{
|
2011-04-22 20:48:02 +02:00
|
|
|
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance());
|
2011-04-22 15:49:46 +02:00
|
|
|
if(container) {
|
|
|
|
container->unlockElement();
|
|
|
|
}
|
2011-04-22 00:44:30 +02:00
|
|
|
return 1;
|
2011-04-22 15:49:46 +02:00
|
|
|
}
|