scripting and UI improvements

master
Eduardo Bart 13 years ago
parent 3960240b8e
commit 32a8ed3871

@ -5,7 +5,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
# find needed packages
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(OpenGL REQUIRED)
FIND_PACKAGE(Lua51 REQUIRED)

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

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

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

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

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

@ -1,12 +1,12 @@
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 messageBoxOkButton = messageBoxWindow:getChildByID("messageBoxOkButton")
local uiRoot = messageBoxWindow:getParent()
uiRoot:lock(messageBox)
messageBoxWindow:setTitle(text)
local uiRoot = UI.getRootContainer()
uiRoot:lock(messageBoxWindow)
messageBoxWindow:setTitle(title)
messageBoxLabel:setText(text)
messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20})
messageBox:setOnDestroy(function() uiRoot:unlock() end)
--messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20})
messageBoxWindow:setOnDestroy(function() uiRoot:unlock(self) end)
messageBoxOkButton:setOnClick(function() messageBoxWindow:destroy() end)
end
end

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

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

@ -61,6 +61,11 @@ void Engine::poll()
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;
Size fpsTextSize;
Font *defaultFont = g_fonts.getDefaultFont();
@ -95,7 +100,7 @@ void Engine::run()
// render
g_graphics.beginRender();
UIContainer::getRootContainer()->render();
rootContainer->render();
// render fps
if(m_calculateFps)
@ -110,6 +115,12 @@ void Engine::run()
m_stopping = false;
m_running = false;
// destroy root ui
rootContainer->destroy();
// poll remaning ui events
g_engine.poll();
}
void Engine::stop()
@ -126,10 +137,10 @@ void Engine::onClose()
void Engine::onResize(const Size& size)
{
g_graphics.resize(size);
UIContainer::getRootContainer()->setSize(size);
UIContainer::getRoot()->setSize(size);
}
void Engine::onInputEvent(const InputEvent& event)
{
UIContainer::getRootContainer()->onInputEvent(event);
UIContainer::getRoot()->onInputEvent(event);
}

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

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

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

@ -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

@ -24,6 +24,7 @@
#include <prerequisites.h>
#include <script/luascript.h>
#include <script/luafunctions.h>
#include <core/resources.h>
LuaScript g_lua;
@ -40,8 +41,7 @@ LuaScript::LuaScript()
// setup custom package loader
setupPackageLoader();
// register classes and functions
registerFunctions();
registerLuaFunctions();
}
LuaScript::~LuaScript()
@ -250,6 +250,23 @@ void LuaScript::callFunction(int numArgs)
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)
{
lua_getfenv(L, envIndex);
@ -352,6 +369,13 @@ void LuaScript::registerGlobalFunction(const std::string& functionName, LuaCFunc
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)
{
std::string fileName = lua_tostring(L, -1);
@ -395,7 +419,7 @@ int LuaScript::luaFunctionCallback(lua_State* L)
// look for function id
int id = lua_tonumber(L, lua_upvalueindex(1));
// call the function
return (g_lua.*(g_lua.m_functions[id]))();
return (*(g_lua.m_functions[id]))();
}
int LuaScript::luaErrorHandler(lua_State *L)

@ -64,17 +64,21 @@ public:
void releaseFunction(int functionRef);
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 pushClassInstance(const ScriptablePtr& object);
ScriptablePtr popClassInstance();
typedef int (LuaScript::*LuaCFunction)();
typedef int (*LuaCFunction)();
void setupPackageLoader();
void registerClass(const std::string& klass, const std::string& baseClass = "");
void registerMemberFunction(const std::string& klass, 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 luaPackageLoader(lua_State* L);
@ -82,26 +86,6 @@ public:
static int luaCompareClassInstances(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:
std::vector<LuaCFunction> m_functions;
lua_State *L;

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

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

@ -40,24 +40,34 @@ UIElement::UIElement(UI::EElementType type) :
}
UIElement::~UIElement()
{
//logTraceDebug(getId());
}
void UIElement::destroy()
{
if(m_onDestroyCallback)
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
//logTraceDebug(getId());
g_dispatcher.addTask(boost::bind(&UIElement::internalOnDestroy, asUIElement()));
}
void UIElement::internalOnDestroy()
{
setVisible(false);
setEnabled(false);
//logTraceDebug(getId());
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
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)

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

@ -27,6 +27,7 @@
#include <ui/ui.h>
#include <ui/uiloader.h>
#include <script/luascript.h>
#include <script/luafunctions.h>
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) {
g_lua.pushClassInstance(element);
g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnLoad();
lua_UIElement_setOnLoad();
} else
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) {
g_lua.pushClassInstance(element);
g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnDestroy();
lua_UIElement_setOnDestroy();
} else
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()) {
relativeElement = element->getParent()->asUILayout();
} else if(relativeElementId == "root") {
relativeElement = UIContainer::getRootContainer();
relativeElement = UIContainer::getRoot();
} else {
UIElementPtr tmp = element->backwardsGetElementById(relativeElementId);
if(tmp)
@ -311,7 +312,7 @@ void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
if(funcRef != LUA_REFNIL) {
g_lua.pushClassInstance(button);
g_lua.pushFunction(funcRef);
g_lua.lua_UIButton_setOnClick();
lua_UIButton_setOnClick();
} else {
throw YAML::Exception(node["onClick"].GetMark(), "failed to parse lua script");
}

@ -34,7 +34,7 @@ class UILoader
{
public:
/// 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:
/// Detect element type and create it

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

Loading…
Cancel
Save