From 6d991c10d7800859a39e447ec7f95b861d5fb69f Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 1 Sep 2012 20:13:51 +0100 Subject: [PATCH] Logging for UI component events, tr --- src/cat/LuaButton.cpp | 13 ++++-- src/cat/LuaButton.h | 4 ++ src/cat/LuaLabel.cpp | 6 +++ src/cat/LuaLabel.h | 4 ++ src/cat/LuaLuna.h | 5 +++ src/cat/LuaScriptInterface.cpp | 7 +++- src/cat/LuaWindow.cpp | 76 +++++++++++++++------------------- src/cat/LuaWindow.h | 3 ++ 8 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/cat/LuaButton.cpp b/src/cat/LuaButton.cpp index 9ba5bd1c0..18001fd4a 100644 --- a/src/cat/LuaButton.cpp +++ b/src/cat/LuaButton.cpp @@ -7,6 +7,7 @@ extern "C" #include #include "LuaButton.h" +#include "LuaScriptInterface.h" #include "interface/Button.h" const char LuaButton::className[] = "Button"; @@ -29,7 +30,12 @@ LuaButton::LuaButton(lua_State * l) : int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); std::string text = luaL_optstring(l, 5, ""); - std::string toolTip = luaL_optstring(l, 6, "");; + std::string toolTip = luaL_optstring(l, 6, ""); + + lua_pushstring(l, "Luacon_ci"); + lua_gettable(l, LUA_REGISTRYINDEX); + ci = (LuaScriptInterface*)lua_touserdata(l, -1); + lua_pop(l, 1); button = new ui::Button(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, toolTip); class ClickAction : public ui::ButtonAction @@ -116,12 +122,11 @@ void LuaButton::triggerAction() { if(actionFunction) { - std::cout << actionFunction << std::endl; lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction); - lua_pushinteger(l, 1); + lua_rawgeti(l, LUA_REGISTRYINDEX, UserData); if (lua_pcall(l, 1, 0, 0)) { - //Log error somewhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } diff --git a/src/cat/LuaButton.h b/src/cat/LuaButton.h index ef5bb54f7..a092d078c 100644 --- a/src/cat/LuaButton.h +++ b/src/cat/LuaButton.h @@ -13,6 +13,8 @@ namespace ui class Button; } +class LuaScriptInterface; + class LuaButton { ui::Button * button; @@ -24,6 +26,8 @@ class LuaButton int position(lua_State * l); int size(lua_State * l); public: + LuaScriptInterface * ci; + int UserData; static const char className[]; static Luna::RegType methods[]; diff --git a/src/cat/LuaLabel.cpp b/src/cat/LuaLabel.cpp index 5ab3d1c12..c1d949705 100644 --- a/src/cat/LuaLabel.cpp +++ b/src/cat/LuaLabel.cpp @@ -6,6 +6,7 @@ extern "C" } #include +#include "LuaScriptInterface.h" #include "LuaLabel.h" #include "interface/Label.h" @@ -28,6 +29,11 @@ LuaLabel::LuaLabel(lua_State * l) int sizeY = luaL_optinteger(l, 4, 10); std::string text = luaL_optstring(l, 5, ""); + lua_pushstring(l, "Luacon_ci"); + lua_gettable(l, LUA_REGISTRYINDEX); + ci = (LuaScriptInterface*)lua_touserdata(l, -1); + lua_pop(l, 1); + label = new ui::Label(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text); } diff --git a/src/cat/LuaLabel.h b/src/cat/LuaLabel.h index bfaaabf2b..7e1f80893 100644 --- a/src/cat/LuaLabel.h +++ b/src/cat/LuaLabel.h @@ -13,6 +13,8 @@ namespace ui class Label; } +class LuaScriptInterface; + class LuaLabel { ui::Label * label; @@ -21,6 +23,8 @@ class LuaLabel int position(lua_State * l); int size(lua_State * l); public: + LuaScriptInterface * ci; + int UserData; static const char className[]; static Luna::RegType methods[]; diff --git a/src/cat/LuaLuna.h b/src/cat/LuaLuna.h index 591dad258..1d5d937a5 100644 --- a/src/cat/LuaLuna.h +++ b/src/cat/LuaLuna.h @@ -126,9 +126,14 @@ private: static int new_T(lua_State * L) { lua_remove(L, 1); // use classname:new(), instead of classname.new() + T *obj = new T(L); // call constructor for T objects userdataType *ud = static_cast(lua_newuserdata(L, sizeof(userdataType))); ud->pT = obj; // store pointer to object in userdata + + obj->UserData = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, LUA_REGISTRYINDEX, obj->UserData); + luaL_getmetatable(L, T::className); // lookup metatable in Lua registry lua_setmetatable(L, -2); return 1; // userdata containing pointer to T object diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 18a988613..618319db2 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -12,6 +12,7 @@ #include #include "Config.h" #include "Format.h" +#include "LuaLuna.h" #include "LuaScriptInterface.h" #include "TPTScriptInterface.h" #include "dialogues/ErrorMessage.h" @@ -25,7 +26,6 @@ #include "LuaBit.h" -#include "LuaLuna.h" #include "LuaWindow.h" #include "LuaButton.h" #include "LuaLabel.h" @@ -48,11 +48,16 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m): luacon_ren = m->GetRenderer(); luacon_ci = this; + //New TPT API l = lua_open(); luaL_openlibs(l); luaopen_bit(l); + lua_pushstring(l, "Luacon_ci"); + lua_pushlightuserdata(l, this); + lua_settable(l, LUA_REGISTRYINDEX); + initInterfaceAPI(); initRendererAPI(); initElementsAPI(); diff --git a/src/cat/LuaWindow.cpp b/src/cat/LuaWindow.cpp index e0a52982d..c83717092 100644 --- a/src/cat/LuaWindow.cpp +++ b/src/cat/LuaWindow.cpp @@ -6,6 +6,7 @@ extern "C" } #include +#include "LuaScriptInterface.h" #include "LuaWindow.h" #include "LuaButton.h" #include "LuaLabel.h" @@ -59,6 +60,11 @@ LuaWindow::LuaWindow(lua_State * l) : int sizeX = luaL_optinteger(l, 3, 10); int sizeY = luaL_optinteger(l, 4, 10); + lua_pushstring(l, "Luacon_ci"); + lua_gettable(l, LUA_REGISTRYINDEX); + ci = (LuaScriptInterface*)lua_touserdata(l, -1); + lua_pop(l, 1); + class DrawnWindow : public ui::Window { LuaWindow * luaWindow; @@ -145,10 +151,9 @@ void LuaWindow::triggerOnInitialized() if(onInitializedFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onInitializedFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -158,10 +163,9 @@ void LuaWindow::triggerOnExit() if(onExitFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onExitFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -171,11 +175,10 @@ void LuaWindow::triggerOnTick(float dt) if(onTickFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onTickFunction); - lua_pushinteger(l, 1); //Self placeholder lua_pushnumber(l, dt); - if(lua_pcall(l, 2, 0, 0)) + if(lua_pcall(l, 1, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -185,10 +188,9 @@ void LuaWindow::triggerOnDraw() if(onDrawFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onDrawFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -198,10 +200,9 @@ void LuaWindow::triggerOnFocus() if(onFocusFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onFocusFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -211,10 +212,9 @@ void LuaWindow::triggerOnBlur() if(onBlurFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onBlurFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -224,10 +224,9 @@ void LuaWindow::triggerOnTryExit() if(onTryExitFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onTryExitFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -237,10 +236,9 @@ void LuaWindow::triggerOnTryOkay() if(onTryOkayFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onTryOkayFunction); - lua_pushinteger(l, 1); //Self placeholder - if(lua_pcall(l, 1, 0, 0)) + if(lua_pcall(l, 0, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -250,14 +248,13 @@ void LuaWindow::triggerOnMouseMove(int x, int y, int dx, int dy) if(onMouseMoveFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseMoveFunction); - lua_pushinteger(l, 0); //Self placeholder lua_pushinteger(l, x); lua_pushinteger(l, y); lua_pushinteger(l, dx); lua_pushinteger(l, dy); - if(lua_pcall(l, 5, 0, 0)) + if(lua_pcall(l, 4, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -267,13 +264,12 @@ void LuaWindow::triggerOnMouseDown(int x, int y, unsigned button) if(onMouseDownFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseDownFunction); - lua_pushinteger(l, 0); //Self placeholder lua_pushinteger(l, x); lua_pushinteger(l, y); lua_pushinteger(l, button); - if(lua_pcall(l, 4, 0, 0)) + if(lua_pcall(l, 3, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -283,13 +279,12 @@ void LuaWindow::triggerOnMouseUp(int x, int y, unsigned button) if(onMouseUpFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseUpFunction); - lua_pushinteger(l, 0); //Self placeholder lua_pushinteger(l, x); lua_pushinteger(l, y); lua_pushinteger(l, button); - if(lua_pcall(l, 4, 0, 0)) + if(lua_pcall(l, 3, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -299,13 +294,12 @@ void LuaWindow::triggerOnMouseWheel(int x, int y, int d) if(onMouseWheelFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseWheelFunction); - lua_pushinteger(l, 0); //Self placeholder lua_pushinteger(l, x); lua_pushinteger(l, y); lua_pushinteger(l, d); - if(lua_pcall(l, 4, 0, 0)) + if(lua_pcall(l, 3, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -315,15 +309,14 @@ void LuaWindow::triggerOnKeyPress(int key, Uint16 character, bool shift, bool ct if(onKeyPressFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyPressFunction); - lua_pushinteger(l, 0); //Self placeholder lua_pushinteger(l, key); lua_pushinteger(l, character); lua_pushboolean(l, shift); lua_pushboolean(l, ctrl); lua_pushboolean(l, alt); - if(lua_pcall(l, 6, 0, 0)) + if(lua_pcall(l, 5, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } @@ -333,15 +326,14 @@ void LuaWindow::triggerOnKeyRelease(int key, Uint16 character, bool shift, bool if(onKeyReleaseFunction) { lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyReleaseFunction); - lua_pushinteger(l, 0); //Self placeholder lua_pushinteger(l, key); lua_pushinteger(l, character); lua_pushboolean(l, shift); lua_pushboolean(l, ctrl); lua_pushboolean(l, alt); - if(lua_pcall(l, 6, 0, 0)) + if(lua_pcall(l, 5, 0, 0)) { - //Log error somwhere + ci->Log(CommandInterface::LogError, lua_tostring(l, -1)); } } } diff --git a/src/cat/LuaWindow.h b/src/cat/LuaWindow.h index 0c4a30561..be6af9266 100644 --- a/src/cat/LuaWindow.h +++ b/src/cat/LuaWindow.h @@ -14,6 +14,7 @@ namespace ui class Window; } +class LuaScriptInterface; class LuaWindow { int onInitializedFunction; @@ -69,6 +70,8 @@ class LuaWindow void triggerOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); public: + LuaScriptInterface * ci; + int UserData; static const char className[]; static Luna::RegType methods[];