From 3f8e1955cb04ffdb4182d0bc5d5833f37a2f2b44 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 8 Jun 2010 21:44:25 +0100 Subject: [PATCH] Add Lua event hooks --- CMakeLists.txt | 2 +- include/ILua.hpp | 3 +++ scripts/uidemo.lua | 3 ++- src/LuaWrap.cpp | 47 +++++++++++++++++++++++++++++++++------------- src/UIDemo.cpp | 2 +- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0151451..e34e676 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,7 @@ add_executable (${PROJECT_NAME} WIN32 ${folder_source}) target_link_libraries (${PROJECT_NAME} ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${XERCES_LIBRARIES} ${Boost_LIBRARIES} - ${FREETYPE_LIBRARIES} ${GLEW_LIBRARY} ${LUA51_LIBRARIES}) + ${FREETYPE_LIBRARIES} ${GLEW_LIBRARY} ${LUA_LIBRARY}) # Profiling if (PROFILE) diff --git a/include/ILua.hpp b/include/ILua.hpp index c86756b..5648f12 100644 --- a/include/ILua.hpp +++ b/include/ILua.hpp @@ -26,6 +26,9 @@ struct ILua { virtual void eval(const string& code) = 0; virtual void loadFile(const string& file) = 0; + + virtual void addHook(const string& tag) = 0; + virtual void runHook(const string& tag) = 0; }; typedef shared_ptr ILuaPtr; diff --git a/scripts/uidemo.lua b/scripts/uidemo.lua index 22924f2..61d60d0 100644 --- a/scripts/uidemo.lua +++ b/scripts/uidemo.lua @@ -5,9 +5,10 @@ function btn2Click(btn) end function layoutLoaded(layout) + debug("layout loaded") layout:get("/wnd/btn2"):connect( gui.Widget.SIG_CLICK, btn2Click) end -addHook("layoutLoaded", layoutLoaded) +hook("layoutLoaded", layoutLoaded) diff --git a/src/LuaWrap.cpp b/src/LuaWrap.cpp index e4836ec..34f79a0 100644 --- a/src/LuaWrap.cpp +++ b/src/LuaWrap.cpp @@ -52,38 +52,43 @@ public: void eval(const string& code); void loadFile(const string& file); + + void addHook(const string& tag); + void runHook(const string& tag); private: - void pcall(); + void pcall(int nargs = 0, int nresults = 0); - lua_State* luaVM; + lua_State* L; }; LuaWrap::LuaWrap() { log() << "Creating Lua VM"; - if ((luaVM = lua_open()) == NULL) + if ((L = lua_open()) == NULL) throw runtime_error("Failed to initialise Lua"); - lua_pushvalue(luaVM, LUA_GLOBALSINDEX); - luaL_register(luaVM, NULL, luaFuncs); - lua_pop(luaVM, 1); + lua_pushvalue(L, LUA_GLOBALSINDEX); + luaL_register(L, NULL, luaFuncs); + lua_pop(L, 1); + + loadFile("scripts/init.lua"); } LuaWrap::~LuaWrap() { - lua_close(luaVM); + lua_close(L); } -void LuaWrap::pcall() +void LuaWrap::pcall(int nargs, int nresults) { - int result = lua_pcall(luaVM, 0, 0, 0); + int result = lua_pcall(L, nargs, nresults, 0); switch (result) { case LUA_ERRRUN: case LUA_ERRERR: - error() << lua_tolstring(luaVM, -1, NULL); - lua_pop(luaVM, 1); + error() << lua_tolstring(L, -1, NULL); + lua_pop(L, 1); break; case LUA_ERRMEM: error() << "Lua out of memory!"; @@ -93,7 +98,7 @@ void LuaWrap::pcall() void LuaWrap::eval(const string& code) { - int result = luaL_loadstring(luaVM, code.c_str()); + int result = luaL_loadstring(L, code.c_str()); switch (result) { case LUA_ERRSYNTAX: @@ -111,7 +116,7 @@ void LuaWrap::loadFile(const string& file) { log() << "Loading Lua script " << file; - int result = luaL_loadfile(luaVM, file.c_str()); + int result = luaL_loadfile(L, file.c_str()); switch (result) { case LUA_ERRSYNTAX: @@ -128,6 +133,22 @@ void LuaWrap::loadFile(const string& file) pcall(); } +void LuaWrap::addHook(const string& tag) +{ + lua_getglobal(L, "_hooks"); + lua_getglobal(L, "_default_hook"); + + lua_setfield(L, -2, tag.c_str()); +} + +void LuaWrap::runHook(const string& tag) +{ + lua_getglobal(L, "_hooks"); + lua_getfield(L, -1, tag.c_str()); + + pcall(0, 0); +} + ILuaPtr getLua() { static ILuaPtr lua; diff --git a/src/UIDemo.cpp b/src/UIDemo.cpp index a2040b9..232e023 100644 --- a/src/UIDemo.cpp +++ b/src/UIDemo.cpp @@ -56,7 +56,7 @@ UIDemo::UIDemo() bind(&UIDemo::btn1Click, this, _1)); ILuaPtr lua = getLua(); - lua->makeHook("layoutLoaded"); + lua->addHook("layoutLoaded"); lua->loadFile("scripts/uidemo.lua"); lua->runHook("layoutLoaded"); } -- 2.39.2