scripting and UI improvements

This commit is contained in:
Eduardo Bart 2011-05-01 15:47:35 -03:00
parent 3960240b8e
commit 32a8ed3871
23 changed files with 424 additions and 293 deletions

View File

@ -5,7 +5,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
# find needed packages # find needed packages
SET(Boost_USE_STATIC_LIBS ON) SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_USE_MULTITHREADED ON) SET(Boost_USE_MULTITHREADED OFF)
FIND_PACKAGE(Boost COMPONENTS system signals REQUIRED) FIND_PACKAGE(Boost COMPONENTS system signals REQUIRED)
FIND_PACKAGE(OpenGL REQUIRED) FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(Lua51 REQUIRED) FIND_PACKAGE(Lua51 REQUIRED)

View File

@ -4,22 +4,22 @@ window#enterGameWindow:
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onLoad: self:getParent():lock(self) onLoad: self:getParent():lock(self)
onDestroy: self:getParent():unlock() onDestroy: self:getParent():unlock(self)
label#accountNameLabel: label#accountNameLabel:
text: Account name text: Account name
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 33 margin.top: 33
label#passwordLabel: label#passwordLabel:
text: "Password:" text: "Password:"
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 62 margin.top: 62
label#createAccountLabel: label#createAccountLabel:
text: | text: |
If you don't have If you don't have
@ -35,6 +35,7 @@ window#enterGameWindow:
anchors.top: parent.top anchors.top: parent.top
margin.top: 94 margin.top: 94
margin.left: 132 margin.left: 132
onClick: messageBox("Error", "Not implemented yet")
button#okButton: button#okButton:
text: Ok text: Ok
@ -52,7 +53,7 @@ window#enterGameWindow:
margin.bottom: 10 margin.bottom: 10
margin.right: 13 margin.right: 13
onClick: self:getParent():destroy() onClick: self:getParent():destroy()
textEdit#accountNameTextEdit: textEdit#accountNameTextEdit:
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
@ -63,4 +64,4 @@ window#enterGameWindow:
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
margin.top: 61 margin.top: 61
margin.right: 18 margin.right: 18

View File

@ -4,8 +4,8 @@ window#infoWindow:
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onLoad: self:getParent():lock(self) onLoad: self:getParent():lock(self)
onDestroy: self:getParent():unlock() onDestroy: self:getParent():unlock(self)
panel#infoPanel: panel#infoPanel:
skin: flatPanel skin: flatPanel
size: [208, 129] size: [208, 129]
@ -13,7 +13,7 @@ window#infoWindow:
anchors.top: parent.top anchors.top: parent.top
margin.top: 32 margin.top: 32
margin.left: 18 margin.left: 18
label#infoLabel: label#infoLabel:
align: center align: center
text: |- text: |-
@ -30,21 +30,21 @@ window#infoWindow:
anchors.top: parent.top anchors.top: parent.top
margin.top: 83 margin.top: 83
margin.left: 9 margin.left: 9
label#websiteLabel: label#websiteLabel:
text: Official Website text: Official Website
anchors.left: parent.left anchors.left: parent.left
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.bottom: 14 margin.bottom: 14
margin.left: 9 margin.left: 9
button#websiteButton: button#websiteButton:
text: Github Page text: Github Page
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.bottom: 9 margin.bottom: 9
margin.right: 9 margin.right: 9
lineDecoration#bottomSeparator: lineDecoration#bottomSeparator:
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
@ -52,7 +52,7 @@ window#infoWindow:
margin.bottom: 40 margin.bottom: 40
margin.left: 13 margin.left: 13
margin.right: 13 margin.right: 13
button#okButton: button#okButton:
text: Ok text: Ok
size: [43, 20] size: [43, 20]
@ -60,4 +60,4 @@ window#infoWindow:
anchors.top: parent.top anchors.top: parent.top
margin.top: 191 margin.top: 191
margin.left: 188 margin.left: 188
onClick: self:getParent():destroy() onClick: self:getParent():destroy()

View File

@ -7,7 +7,7 @@ panel#background:
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
panel#mainMenu: panel#mainMenu:
skin: roundedGridPanel skin: roundedGridPanel
size: [117, 171] size: [117, 171]
@ -15,13 +15,13 @@ panel#background:
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.left: 60 margin.left: 60
margin.bottom: 70 margin.bottom: 70
button#enterGameButton: button#enterGameButton:
text: Enter Game text: Enter Game
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 16 margin.top: 16
onClick: loadUI("modules/mainmenu/entergamewindow.yml") onClick: UI.load("modules/mainmenu/entergamewindow.yml")
button#accessAccountButton: button#accessAccountButton:
text: Access Account text: Access Account
@ -35,18 +35,18 @@ panel#background:
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 76 margin.top: 76
onClick: loadUI("modules/mainmenu/optionswindow.yml") onClick: UI.load("modules/mainmenu/optionswindow.yml")
button#infoButton: button#infoButton:
text: Info text: Info
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 106 margin.top: 106
onClick: loadUI("modules/mainmenu/infowindow.yml") onClick: UI.load("modules/mainmenu/infowindow.yml")
button#exitGameButton: button#exitGameButton:
text: Exit text: Exit
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 136 margin.top: 136
onClick: exitGame() onClick: App.exit()

View File

@ -1,6 +1,6 @@
-- menu state -- menu state
function onEnterMenuState() function onEnterMenuState()
mainMenu = loadUI("modules/mainmenu/mainmenu.yml") mainMenu = UI.load("modules/mainmenu/mainmenu.yml")
end end
function onLeaveMenuState() function onLeaveMenuState()
@ -15,6 +15,6 @@ end
-- here is where everything starts -- here is where everything starts
if not initialStateLoaded then if not initialStateLoaded then
onEnterMenuState() onEnterMenuState()
setOnApplicationClose(onApplicationClose) App.setOnClose(onApplicationClose)
initialStateLoaded = true initialStateLoaded = true
end end

View File

@ -4,8 +4,8 @@ window#optionsWindow:
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onLoad: self:getParent():lock(self) onLoad: self:getParent():lock(self)
onDestroy: self:getParent():unlock() onDestroy: self:getParent():unlock(self)
# general # general
button#generalButton: button#generalButton:
text: General text: General
@ -22,7 +22,7 @@ window#optionsWindow:
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 29 margin.top: 29
# graphics # graphics
button#graphicsButton: button#graphicsButton:
text: Graphics text: Graphics
@ -39,7 +39,7 @@ window#optionsWindow:
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 62 margin.top: 62
# console # console
button#consoleButton: button#consoleButton:
text: Console text: Console
@ -54,7 +54,7 @@ window#optionsWindow:
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 95 margin.top: 95
# hotkeys # hotkeys
button#hotkeysButton: button#hotkeysButton:
text: Hotkeys text: Hotkeys
@ -69,7 +69,7 @@ window#optionsWindow:
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 128 margin.top: 128
lineDecoration#middleSeparator: lineDecoration#middleSeparator:
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
@ -77,7 +77,7 @@ window#optionsWindow:
margin.bottom: 97 margin.bottom: 97
margin.left: 18 margin.left: 18
margin.right: 18 margin.right: 18
# motd # motd
button#motdButton: button#motdButton:
text: Motd text: Motd
@ -94,7 +94,7 @@ window#optionsWindow:
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.left: 117 margin.left: 117
margin.bottom: 56 margin.bottom: 56
lineDecoration#bottomSeparator: lineDecoration#bottomSeparator:
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
@ -102,7 +102,7 @@ window#optionsWindow:
margin.bottom: 40 margin.bottom: 40
margin.left: 13 margin.left: 13
margin.right: 13 margin.right: 13
# ok button # ok button
button#okButton: button#okButton:
text: Ok text: Ok
@ -111,4 +111,4 @@ window#optionsWindow:
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.right: 10 margin.right: 10
margin.bottom: 13 margin.bottom: 13
onClick: self:getParent():destroy() onClick: self:getParent():destroy()

View File

@ -1,12 +1,12 @@
function messageBox(title, text) function messageBox(title, text)
local messageBoxWindow = loadUI("modules/messagebox/messagebox.yml") local messageBoxWindow = UI.load("modules/messagebox/messagebox.yml")
local messageBoxLabel = messageBoxWindow:getChildByID("messageBoxLabel") local messageBoxLabel = messageBoxWindow:getChildByID("messageBoxLabel")
local messageBoxOkButton = messageBoxWindow:getChildByID("messageBoxOkButton") local messageBoxOkButton = messageBoxWindow:getChildByID("messageBoxOkButton")
local uiRoot = messageBoxWindow:getParent() local uiRoot = UI.getRootContainer()
uiRoot:lock(messageBox) uiRoot:lock(messageBoxWindow)
messageBoxWindow:setTitle(text) messageBoxWindow:setTitle(title)
messageBoxLabel:setText(text) messageBoxLabel:setText(text)
messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20}) --messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20})
messageBox:setOnDestroy(function() uiRoot:unlock() end) messageBoxWindow:setOnDestroy(function() uiRoot:unlock(self) end)
messageBoxOkButton:setOnClick(function() messageBoxWindow:destroy() end) messageBoxOkButton:setOnClick(function() messageBoxWindow:destroy() end)
end end

View File

@ -3,7 +3,7 @@ window#messageBoxWindow:
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
label#messageBoxLabel: label#messageBoxLabel:
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top anchors.top: parent.top

View File

@ -16,7 +16,7 @@ buttons:
bottom left corner: [45,157,1,1] bottom left corner: [45,157,1,1]
bottom right corner: [130,157,1,1] bottom right corner: [130,157,1,1]
center: [46,139,84,18] center: [46,139,84,18]
down state: down state:
text translate: [1, 1] text translate: [1, 1]
bordered image: bordered image:
@ -45,7 +45,7 @@ panels:
bottom left corner: [43,230,5,5] bottom left corner: [43,230,5,5]
bottom right corner: [48,231,5,5] bottom right corner: [48,231,5,5]
center: [11,214,32,32] center: [11,214,32,32]
flatPanel: flatPanel:
bordered image: bordered image:
left border: [275,0,1,96] left border: [275,0,1,96]
@ -57,7 +57,7 @@ panels:
bottom left corner: [2,210,1,1] bottom left corner: [2,210,1,1]
bottom right corner: [276,95,1,1] bottom right corner: [276,95,1,1]
center: [0, 0, 96, 96] center: [0, 0, 96, 96]
labels: labels:
default: default:
font: tibia-10px-antialised font: tibia-10px-antialised
@ -87,7 +87,7 @@ windows:
bottom left corner: [98,193,4,4] bottom left corner: [98,193,4,4]
bottom right corner: [102,193,4,4] bottom right corner: [102,193,4,4]
center: [0, 0, 96, 96] center: [0, 0, 96, 96]
text edits: text edits:
default: default:
default size: [86, 16] default size: [86, 16]
@ -103,9 +103,8 @@ text edits:
bottom left corner: [308,107,1,1] bottom left corner: [308,107,1,1]
bottom right corner: [319,107,1,1] bottom right corner: [319,107,1,1]
center: [309,97,10,10] center: [309,97,10,10]
line decorations: line decorations:
default: default:
bordered image: bordered image:
top border: [2,210,96,2] top border: [2,210,96,2]

View File

@ -61,6 +61,11 @@ void Engine::poll()
void Engine::run() void Engine::run()
{ {
// check if root container has elements
const UIContainerPtr& rootContainer = UIContainer::getRoot();
if(rootContainer->getChildCount() == 0)
logFatal("FATAL ERROR: no ui loaded at all, no reason to continue running");
std::string fpsText; std::string fpsText;
Size fpsTextSize; Size fpsTextSize;
Font *defaultFont = g_fonts.getDefaultFont(); Font *defaultFont = g_fonts.getDefaultFont();
@ -95,7 +100,7 @@ void Engine::run()
// render // render
g_graphics.beginRender(); g_graphics.beginRender();
UIContainer::getRootContainer()->render(); rootContainer->render();
// render fps // render fps
if(m_calculateFps) if(m_calculateFps)
@ -110,6 +115,12 @@ void Engine::run()
m_stopping = false; m_stopping = false;
m_running = false; m_running = false;
// destroy root ui
rootContainer->destroy();
// poll remaning ui events
g_engine.poll();
} }
void Engine::stop() void Engine::stop()
@ -126,10 +137,10 @@ void Engine::onClose()
void Engine::onResize(const Size& size) void Engine::onResize(const Size& size)
{ {
g_graphics.resize(size); g_graphics.resize(size);
UIContainer::getRootContainer()->setSize(size); UIContainer::getRoot()->setSize(size);
} }
void Engine::onInputEvent(const InputEvent& event) void Engine::onInputEvent(const InputEvent& event)
{ {
UIContainer::getRootContainer()->onInputEvent(event); UIContainer::getRoot()->onInputEvent(event);
} }

View File

@ -49,7 +49,7 @@ void Graphics::init()
void Graphics::terminate() void Graphics::terminate()
{ {
m_bindedTexture = TexturePtr(); m_bindedTexture.reset();
} }
bool Graphics::isExtensionSupported(const char *extension) bool Graphics::isExtensionSupported(const char *extension)

View File

@ -59,6 +59,7 @@ typedef int8_t int8;
#include <string> #include <string>
#include <list> #include <list>
#include <queue> #include <queue>
#include <iterator>
// boost utilities // boost utilities
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View File

@ -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();
}
if(container) {
container->lockElement(element);
}
return 1; return 1;
} }
int LuaScript::lua_UIContainer_unlock()
////////////////////////////////////////////////////////////////////////////////
// UIWindow
int lua_UIWindow_setTitle()
{ {
UIContainerPtr container = boost::dynamic_pointer_cast<UIContainer>(popClassInstance()); std::string title = g_lua.popString();
if(container) { if(UIWindowPtr window = boost::dynamic_pointer_cast<UIWindow>(g_lua.popClassInstance()))
container->unlockElement(); window->setTitle(title);
} return 1;
}
int lua_UIWindow_getTitle()
{
if(UIWindowPtr window = boost::dynamic_pointer_cast<UIWindow>(g_lua.popClassInstance()))
g_lua.pushString(window->getTitle());
else
g_lua.pushNil();
return 1; return 1;
} }

View File

@ -0,0 +1,62 @@
/* 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 LUAFUNCTIONS_H
#define LUAFUNCTIONS_H
#include <prerequisites.h>
#include <script/scriptable.h>
void registerLuaFunctions();
// global functions
int lua_App_exit();
int lua_App_setOnClose();
int lua_UI_load();
int lua_UI_getRootContainer();
// UIElement
int lua_UIElement_setOnLoad();
int lua_UIElement_setOnDestroy();
int lua_UIElement_getParent();
int lua_UIElement_destroy();
// UIContainer
int lua_UIContainer_getChildByID();
int lua_UIContainer_lock();
int lua_UIContainer_unlock();
// UILabel
int lua_UILabel_setText();
int lua_UILabel_getText();
// UIButton
int lua_UIButton_setOnClick();
// UIWindow
int lua_UIWindow_setTitle();
int lua_UIWindow_getTitle();
#endif // LUAFUNCTIONS_H

View File

@ -24,6 +24,7 @@
#include <prerequisites.h> #include <prerequisites.h>
#include <script/luascript.h> #include <script/luascript.h>
#include <script/luafunctions.h>
#include <core/resources.h> #include <core/resources.h>
LuaScript g_lua; LuaScript g_lua;
@ -40,8 +41,7 @@ LuaScript::LuaScript()
// setup custom package loader // setup custom package loader
setupPackageLoader(); setupPackageLoader();
// register classes and functions registerLuaFunctions();
registerFunctions();
} }
LuaScript::~LuaScript() LuaScript::~LuaScript()
@ -250,6 +250,23 @@ void LuaScript::callFunction(int numArgs)
reportError("stack size changed!"); reportError("stack size changed!");
} }
SimpleCallback LuaScript::createSimpleFuncCallback(int funcRef)
{
return [this, funcRef]() {
pushFunction(funcRef);
callFunction();
};
}
boost::function<void(ScriptablePtr)> LuaScript::createScriptableSelfFuncCallback(int funcRef)
{
return [this, funcRef](ScriptablePtr scriptable) {
pushFunction(funcRef);
setLocal(scriptable, "self");
callFunction();
};
}
void LuaScript::setLocal(const ScriptablePtr& scriptable, const char *varName, int envIndex) void LuaScript::setLocal(const ScriptablePtr& scriptable, const char *varName, int envIndex)
{ {
lua_getfenv(L, envIndex); lua_getfenv(L, envIndex);
@ -352,6 +369,13 @@ void LuaScript::registerGlobalFunction(const std::string& functionName, LuaCFunc
lua_setfield(L, LUA_GLOBALSINDEX, functionName.c_str()); lua_setfield(L, LUA_GLOBALSINDEX, functionName.c_str());
} }
void LuaScript::registerModule(const std::string& module)
{
// module = {}
lua_newtable(L); // module table
lua_setfield(L, LUA_GLOBALSINDEX, module.c_str()); // register at globals index
}
int LuaScript::luaPackageLoader(lua_State* L) int LuaScript::luaPackageLoader(lua_State* L)
{ {
std::string fileName = lua_tostring(L, -1); std::string fileName = lua_tostring(L, -1);
@ -395,7 +419,7 @@ int LuaScript::luaFunctionCallback(lua_State* L)
// look for function id // look for function id
int id = lua_tonumber(L, lua_upvalueindex(1)); int id = lua_tonumber(L, lua_upvalueindex(1));
// call the function // call the function
return (g_lua.*(g_lua.m_functions[id]))(); return (*(g_lua.m_functions[id]))();
} }
int LuaScript::luaErrorHandler(lua_State *L) int LuaScript::luaErrorHandler(lua_State *L)

View File

@ -64,17 +64,21 @@ public:
void releaseFunction(int functionRef); void releaseFunction(int functionRef);
void callFunction(int numArgs = 0); void callFunction(int numArgs = 0);
SimpleCallback createSimpleFuncCallback(int funcRef);
boost::function<void(ScriptablePtr)> createScriptableSelfFuncCallback(int funcRef);
void setLocal(const ScriptablePtr& scriptable, const char *varName, int envIndex = -1); void setLocal(const ScriptablePtr& scriptable, const char *varName, int envIndex = -1);
void pushClassInstance(const ScriptablePtr& object); void pushClassInstance(const ScriptablePtr& object);
ScriptablePtr popClassInstance(); ScriptablePtr popClassInstance();
typedef int (LuaScript::*LuaCFunction)(); typedef int (*LuaCFunction)();
void setupPackageLoader(); void setupPackageLoader();
void registerClass(const std::string& klass, const std::string& baseClass = ""); void registerClass(const std::string& klass, const std::string& baseClass = "");
void registerMemberFunction(const std::string& klass, const std::string& functionName, LuaCFunction function); void registerMemberFunction(const std::string& klass, const std::string& functionName, LuaCFunction function);
void registerGlobalFunction(const std::string& functionName, LuaCFunction function); void registerGlobalFunction(const std::string& functionName, LuaCFunction function);
void registerModule(const std::string& module);
static int luaFunctionCallback(lua_State* L); static int luaFunctionCallback(lua_State* L);
static int luaPackageLoader(lua_State* L); static int luaPackageLoader(lua_State* L);
@ -82,26 +86,6 @@ public:
static int luaCompareClassInstances(lua_State* L); static int luaCompareClassInstances(lua_State* L);
static int luaErrorHandler(lua_State *L); static int luaErrorHandler(lua_State *L);
void registerFunctions();
int lua_UIElement_setOnLoad();
int lua_UIElement_setOnDestroy();
int lua_UIElement_getParent();
int lua_UIElement_destroy();
int lua_UIButton_setOnClick();
// container functions
int lua_UIContainer_getChildByID();
int lua_UIContainer_lock();
int lua_UIContainer_unlock();
// global functions
int lua_exitGame();
int lua_loadUI();
int lua_getUIRootContainer();
int lua_setOnApplicationClose();
private: private:
std::vector<LuaCFunction> m_functions; std::vector<LuaCFunction> m_functions;
lua_State *L; lua_State *L;

View File

@ -29,19 +29,22 @@
void UIContainer::internalOnDestroy() void UIContainer::internalOnDestroy()
{ {
// destroy children //logTraceDebug(getId());
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
(*it)->setParent(UIContainerPtr());
(*it)->destroy();
}
m_children.clear();
// root container must not call internalDestroy // clear additional references
if(asUIContainer() != getRootContainer()) m_lockedElements.clear();
UIElement::internalOnDestroy(); m_focusedElement.reset();
// destroy children
while(m_children.size() > 0) {
UIElementPtr element = m_children.back(); //hold reference
element->internalOnDestroy();
}
UIElement::internalOnDestroy();
} }
UIContainerPtr& UIContainer::getRootContainer() UIContainerPtr& UIContainer::getRoot()
{ {
static UIContainerPtr rootContainer; static UIContainerPtr rootContainer;
if(!rootContainer) { if(!rootContainer) {
@ -59,22 +62,14 @@ void UIContainer::addChild(UIElementPtr child)
void UIContainer::removeChild(UIElementPtr child) void UIContainer::removeChild(UIElementPtr child)
{ {
// first check if its really a child // defocus if needed
bool removed = false;
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) == child) {
removed = true;
m_children.erase(it);
break;
}
}
assert(removed);
if(m_focusedElement == child) if(m_focusedElement == child)
setFocusedElement(UIElementPtr()); setFocusedElement(UIElementPtr());
// remove from children list
m_children.remove(child);
// child must have this container as parent // child must have this container as parent
assert(child->getParent() == asUIContainer());
child->setParent(UIContainerPtr()); child->setParent(UIContainerPtr());
} }
@ -82,19 +77,23 @@ UIElementPtr UIContainer::getChildById(const std::string& id)
{ {
if(getId() == id) if(getId() == id)
return asUIElement(); return asUIElement();
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->getId() == id) foreach(const UIElementPtr& child, m_children) {
return (*it); if(child->getId() == id)
return child;
} }
return UIElementPtr(); return UIElementPtr();
} }
UIElementPtr UIContainer::getChildByPos(const Point& pos) UIElementPtr UIContainer::getChildByPos(const Point& pos)
{ {
for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) { for(auto it = m_children.rbegin(); it != m_children.rend(); ++it) {
if((*it)->getRect().contains(pos)) const UIElementPtr& element = (*it);
return (*it); if(element->getRect().contains(pos))
return element;
} }
return UIElementPtr(); return UIElementPtr();
} }
@ -103,50 +102,42 @@ UIElementPtr UIContainer::recursiveGetChildById(const std::string& id)
if(getId() == id) if(getId() == id)
return asUIElement(); return asUIElement();
UIElementPtr element; foreach(const UIElementPtr& element, m_children) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) { if(element->getId() == id)
element = (*it);
if(element->getId() == id) {
return element; return element;
} else { else {
UIContainerPtr container = element->asUIContainer(); UIContainerPtr container = element->asUIContainer();
if(container) { if(container) {
element = container->recursiveGetChildById(id); UIElementPtr element2 = container->recursiveGetChildById(id);
if(element) if(element2)
return element; return element2;
} }
} }
} }
return UIElementPtr(); return UIElementPtr();
} }
void UIContainer::pushChildToTop(const UIElementPtr& child) void UIContainer::pushChildToTop(const UIElementPtr& child)
{ {
bool removed = false; auto it = std::find(m_children.begin(), m_children.end(), child);
for(auto it = m_children.begin(); it != m_children.end(); ++it) { if(it != m_children.end()) {
if((*it) == child) { m_children.erase(it);
removed = true;
m_children.erase(it);
break;
}
}
if(removed) {
m_children.push_back(child); m_children.push_back(child);
} }
} }
void UIContainer::onLoad() void UIContainer::onLoad()
{ {
for(auto it = m_children.begin(); it != m_children.end(); ++it) foreach(const UIElementPtr& child, m_children)
(*it)->onLoad(); child->onLoad();
UIElement::onLoad(); UIElement::onLoad();
} }
void UIContainer::render() void UIContainer::render()
{ {
UIElement::render(); UIElement::render();
for(auto it = m_children.begin(); it != m_children.end(); ++it) { foreach(const UIElementPtr& child, m_children) {
const UIElementPtr& child = (*it);
if(child->isVisible()) if(child->isVisible())
child->render(); child->render();
} }
@ -155,8 +146,7 @@ void UIContainer::render()
void UIContainer::onInputEvent(const InputEvent& event) void UIContainer::onInputEvent(const InputEvent& event)
{ {
UIElementPtr focusedElement = m_focusedElement; UIElementPtr focusedElement = m_focusedElement;
for(auto it = m_children.begin(); it != m_children.end(); ++it) { foreach(const UIElementPtr& child, m_children) {
const UIElementPtr& child = (*it);
bool shouldFire = false; bool shouldFire = false;
// events should pass only when element is visible and enabled // events should pass only when element is visible and enabled
@ -191,25 +181,19 @@ void UIContainer::onInputEvent(const InputEvent& event)
void UIContainer::focusNextElement() void UIContainer::focusNextElement()
{ {
UIElementPtr element; UIElementPtr element;
auto focusedIt = std::find(m_children.begin(), m_children.end(), m_focusedElement); std::list<UIElementPtr> rotatedChildren(m_children);
if(focusedIt != m_children.end()) { auto focusedIt = std::find(rotatedChildren.begin(), rotatedChildren.end(), m_focusedElement);
for(auto it = ++focusedIt; it != m_children.end(); ++it) { if(focusedIt != rotatedChildren.end()) {
const UIElementPtr& child = (*it); std::rotate(rotatedChildren.begin(), focusedIt, rotatedChildren.end());
if(child->isFocusable()) { rotatedChildren.pop_front();
element = child; foreach(const UIElementPtr& child, rotatedChildren) {
break;
}
}
}
if(!element) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
if(child->isFocusable()) { if(child->isFocusable()) {
element = child; element = child;
break; break;
} }
} }
} }
if(element) if(element)
setFocusedElement(element); setFocusedElement(element);
} }
@ -237,28 +221,38 @@ void UIContainer::setFocusedElement(UIElementPtr focusedElement)
bool UIContainer::lockElement(UIElementPtr element) bool UIContainer::lockElement(UIElementPtr element)
{ {
bool found = false; if(std::find(m_children.begin(), m_children.end(), element) != m_children.end()) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) { m_lockedElements.remove(element);
if((*it) == element) { m_lockedElements.push_front(element);
(*it)->setEnabled(true); foreach(const UIElementPtr& child, m_children) {
found = true; if(child != element)
break; child->setEnabled(false);
} else
} child->setEnabled(true);
if(found) {
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
if((*it) != element)
(*it)->setEnabled(false);
} }
return true; return true;
} }
return false; return false;
} }
void UIContainer::unlockElement() bool UIContainer::unlockElement(UIElementPtr element)
{ {
for(auto it = m_children.begin(); it != m_children.end(); ++it) { auto it = std::find(m_lockedElements.begin(), m_lockedElements.end(), element);
(*it)->setEnabled(true); if(it != m_lockedElements.end()) {
m_lockedElements.erase(it);
UIElementPtr newLockedElement;
if(m_lockedElements.size() > 0)
newLockedElement = m_lockedElements.front();
foreach(const UIElementPtr& child, m_children) {
if(newLockedElement) {
if(child == newLockedElement)
child->setEnabled(true);
else
child->setEnabled(false);
} else
child->setEnabled(true);
}
return true;
} }
return false;
} }

View File

@ -57,7 +57,7 @@ public:
/// Disable all children except the specified element /// Disable all children except the specified element
bool lockElement(UIElementPtr element); bool lockElement(UIElementPtr element);
/// Renable all children /// Renable all children
void unlockElement(); bool unlockElement(UIElementPtr element);
/// Focus next element /// Focus next element
void focusNextElement(); void focusNextElement();
@ -72,13 +72,14 @@ public:
virtual const char *getScriptableName() const { return "UIContainer"; } virtual const char *getScriptableName() const { return "UIContainer"; }
/// Get root container (the container that contains everything) /// Get root container (the container that contains everything)
static UIContainerPtr& getRootContainer(); static UIContainerPtr& getRoot();
protected: protected:
virtual void internalOnDestroy(); virtual void internalOnDestroy();
private: private:
std::list<UIElementPtr> m_children; std::list<UIElementPtr> m_children;
std::list<UIElementPtr> m_lockedElements;
UIElementPtr m_focusedElement; UIElementPtr m_focusedElement;
}; };

View File

@ -40,24 +40,34 @@ UIElement::UIElement(UI::EElementType type) :
} }
UIElement::~UIElement()
{
//logTraceDebug(getId());
}
void UIElement::destroy() void UIElement::destroy()
{ {
if(m_onDestroyCallback) //logTraceDebug(getId());
g_dispatcher.addTask(boost::bind(m_onDestroyCallback, asUIElement()));
if(getParent()) {
// schedule removal from parent
g_dispatcher.addTask(boost::bind(&UIContainer::removeChild, getParent(), asUIElement()));
}
// schedule internal destroy
g_dispatcher.addTask(boost::bind(&UIElement::internalOnDestroy, asUIElement())); g_dispatcher.addTask(boost::bind(&UIElement::internalOnDestroy, asUIElement()));
} }
void UIElement::internalOnDestroy() void UIElement::internalOnDestroy()
{ {
setVisible(false); //logTraceDebug(getId());
setEnabled(false);
UIElementPtr me = asUIElement();
if(m_onDestroyCallback)
m_onDestroyCallback(me);
// remove from parent
if(getParent()) {
getParent()->removeChild(me);
}
// check for leaks, the number of references must be always 2 here // check for leaks, the number of references must be always 2 here
assert(asUIElement().use_count() == 2); if(me.use_count() != 2 && me != UIContainer::getRoot()) {
flogWarning("destroyed element with id '%s', but it still have %d references left", getId() % (me.use_count()-2));
}
} }
void UIElement::setSkin(const UIElementSkinPtr& skin) void UIElement::setSkin(const UIElementSkinPtr& skin)

View File

@ -47,10 +47,11 @@ class UIElement : public UILayout
{ {
public: public:
UIElement(UI::EElementType type = UI::Element); UIElement(UI::EElementType type = UI::Element);
virtual ~UIElement() { } virtual ~UIElement();
/// Destroy this element by removing it from its parent /// Destroy this element by removing it from its parent
void destroy(); void destroy();
virtual void internalOnDestroy();
/// Draw element /// Draw element
virtual void render(); virtual void render();
@ -92,11 +93,7 @@ public:
void setOnDestroy(const UIElementCallback& onDestroyCallback) { m_onDestroyCallback = onDestroyCallback; } void setOnDestroy(const UIElementCallback& onDestroyCallback) { m_onDestroyCallback = onDestroyCallback; }
void setOnLoad(const UIElementCallback& onLoadCallback) { m_onLoadCallback = onLoadCallback; } void setOnLoad(const UIElementCallback& onLoadCallback) { m_onLoadCallback = onLoadCallback; }
protected:
virtual void internalOnDestroy();
private: private:
UI::EElementType m_type; UI::EElementType m_type;
UIContainerWeakPtr m_parent; UIContainerWeakPtr m_parent;
UIElementSkinPtr m_skin; UIElementSkinPtr m_skin;

View File

@ -27,6 +27,7 @@
#include <ui/ui.h> #include <ui/ui.h>
#include <ui/uiloader.h> #include <ui/uiloader.h>
#include <script/luascript.h> #include <script/luascript.h>
#include <script/luafunctions.h>
UIElementPtr UILoader::createElementFromId(const std::string& id) UIElementPtr UILoader::createElementFromId(const std::string& id)
{ {
@ -216,7 +217,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
if(funcRef != LUA_REFNIL) { if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(element); g_lua.pushClassInstance(element);
g_lua.pushFunction(funcRef); g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnLoad(); lua_UIElement_setOnLoad();
} else } else
throw YAML::Exception(cnode.GetMark(), "failed to parse lua script"); throw YAML::Exception(cnode.GetMark(), "failed to parse lua script");
} }
@ -227,7 +228,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
if(funcRef != LUA_REFNIL) { if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(element); g_lua.pushClassInstance(element);
g_lua.pushFunction(funcRef); g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnDestroy(); lua_UIElement_setOnDestroy();
} else } else
throw YAML::Exception(cnode.GetMark(), "failed to parse lua script"); throw YAML::Exception(cnode.GetMark(), "failed to parse lua script");
} }
@ -286,7 +287,7 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type,
if(relativeElementId == "parent" && element->getParent()) { if(relativeElementId == "parent" && element->getParent()) {
relativeElement = element->getParent()->asUILayout(); relativeElement = element->getParent()->asUILayout();
} else if(relativeElementId == "root") { } else if(relativeElementId == "root") {
relativeElement = UIContainer::getRootContainer(); relativeElement = UIContainer::getRoot();
} else { } else {
UIElementPtr tmp = element->backwardsGetElementById(relativeElementId); UIElementPtr tmp = element->backwardsGetElementById(relativeElementId);
if(tmp) if(tmp)
@ -311,7 +312,7 @@ void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
if(funcRef != LUA_REFNIL) { if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(button); g_lua.pushClassInstance(button);
g_lua.pushFunction(funcRef); g_lua.pushFunction(funcRef);
g_lua.lua_UIButton_setOnClick(); lua_UIButton_setOnClick();
} else { } else {
throw YAML::Exception(node["onClick"].GetMark(), "failed to parse lua script"); throw YAML::Exception(node["onClick"].GetMark(), "failed to parse lua script");
} }

View File

@ -34,7 +34,7 @@ class UILoader
{ {
public: public:
/// Loads an UIElement and it's children from a YAML file /// Loads an UIElement and it's children from a YAML file
static UIElementPtr loadFile(const std::string& file, const UIContainerPtr& parent = UIContainer::getRootContainer()); static UIElementPtr loadFile(const std::string& file, const UIContainerPtr& parent = UIContainer::getRoot());
private: private:
/// Detect element type and create it /// Detect element type and create it

View File

@ -85,7 +85,7 @@ int main(int argc, const char *argv[])
Platform::init("OTClient"); Platform::init("OTClient");
// init random numbers // init random numbers
srand(time(NULL)); std::srand(std::time(NULL));
// init resources // init resources
g_resources.init(argv[0]); g_resources.init(argv[0]);
@ -120,21 +120,12 @@ int main(int argc, const char *argv[])
// load script modules // load script modules
g_lua.loadAllModules(); g_lua.loadAllModules();
if(!UIContainer::getRootContainer()->getChildCount())
logFatal("FATAL ERROR: no ui loaded at all, no reason to continue running");
Platform::showWindow(); Platform::showWindow();
//Platform::hideMouseCursor(); //Platform::hideMouseCursor();
// main loop, run everything // main loop, run everything
g_engine.run(); g_engine.run();
// destroy root ui
UIContainer::getRootContainer()->destroy();
// poll remaning events
g_engine.poll();
// terminate stuff // terminate stuff
g_engine.terminate(); g_engine.terminate();