Logging for UI component events, tr

This commit is contained in:
Simon Robertshaw
2012-09-01 20:13:51 +01:00
parent 9bc06a2fc4
commit 6d991c10d7
8 changed files with 71 additions and 47 deletions

View File

@@ -7,6 +7,7 @@ extern "C"
#include <iostream> #include <iostream>
#include "LuaButton.h" #include "LuaButton.h"
#include "LuaScriptInterface.h"
#include "interface/Button.h" #include "interface/Button.h"
const char LuaButton::className[] = "Button"; const char LuaButton::className[] = "Button";
@@ -29,7 +30,12 @@ LuaButton::LuaButton(lua_State * l) :
int sizeX = luaL_optinteger(l, 3, 10); int sizeX = luaL_optinteger(l, 3, 10);
int sizeY = luaL_optinteger(l, 4, 10); int sizeY = luaL_optinteger(l, 4, 10);
std::string text = luaL_optstring(l, 5, ""); 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); button = new ui::Button(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, toolTip);
class ClickAction : public ui::ButtonAction class ClickAction : public ui::ButtonAction
@@ -116,12 +122,11 @@ void LuaButton::triggerAction()
{ {
if(actionFunction) if(actionFunction)
{ {
std::cout << actionFunction << std::endl;
lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction);
lua_pushinteger(l, 1); lua_rawgeti(l, LUA_REGISTRYINDEX, UserData);
if (lua_pcall(l, 1, 0, 0)) if (lua_pcall(l, 1, 0, 0))
{ {
//Log error somewhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }

View File

@@ -13,6 +13,8 @@ namespace ui
class Button; class Button;
} }
class LuaScriptInterface;
class LuaButton class LuaButton
{ {
ui::Button * button; ui::Button * button;
@@ -24,6 +26,8 @@ class LuaButton
int position(lua_State * l); int position(lua_State * l);
int size(lua_State * l); int size(lua_State * l);
public: public:
LuaScriptInterface * ci;
int UserData;
static const char className[]; static const char className[];
static Luna<LuaButton>::RegType methods[]; static Luna<LuaButton>::RegType methods[];

View File

@@ -6,6 +6,7 @@ extern "C"
} }
#include <iostream> #include <iostream>
#include "LuaScriptInterface.h"
#include "LuaLabel.h" #include "LuaLabel.h"
#include "interface/Label.h" #include "interface/Label.h"
@@ -28,6 +29,11 @@ LuaLabel::LuaLabel(lua_State * l)
int sizeY = luaL_optinteger(l, 4, 10); int sizeY = luaL_optinteger(l, 4, 10);
std::string text = luaL_optstring(l, 5, ""); 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); label = new ui::Label(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text);
} }

View File

@@ -13,6 +13,8 @@ namespace ui
class Label; class Label;
} }
class LuaScriptInterface;
class LuaLabel class LuaLabel
{ {
ui::Label * label; ui::Label * label;
@@ -21,6 +23,8 @@ class LuaLabel
int position(lua_State * l); int position(lua_State * l);
int size(lua_State * l); int size(lua_State * l);
public: public:
LuaScriptInterface * ci;
int UserData;
static const char className[]; static const char className[];
static Luna<LuaLabel>::RegType methods[]; static Luna<LuaLabel>::RegType methods[];

View File

@@ -126,9 +126,14 @@ private:
static int new_T(lua_State * L) static int new_T(lua_State * L)
{ {
lua_remove(L, 1); // use classname:new(), instead of classname.new() lua_remove(L, 1); // use classname:new(), instead of classname.new()
T *obj = new T(L); // call constructor for T objects T *obj = new T(L); // call constructor for T objects
userdataType *ud = static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType))); userdataType *ud = static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType)));
ud->pT = obj; // store pointer to object in userdata 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 luaL_getmetatable(L, T::className); // lookup metatable in Lua registry
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
return 1; // userdata containing pointer to T object return 1; // userdata containing pointer to T object

View File

@@ -12,6 +12,7 @@
#include <locale> #include <locale>
#include "Config.h" #include "Config.h"
#include "Format.h" #include "Format.h"
#include "LuaLuna.h"
#include "LuaScriptInterface.h" #include "LuaScriptInterface.h"
#include "TPTScriptInterface.h" #include "TPTScriptInterface.h"
#include "dialogues/ErrorMessage.h" #include "dialogues/ErrorMessage.h"
@@ -25,7 +26,6 @@
#include "LuaBit.h" #include "LuaBit.h"
#include "LuaLuna.h"
#include "LuaWindow.h" #include "LuaWindow.h"
#include "LuaButton.h" #include "LuaButton.h"
#include "LuaLabel.h" #include "LuaLabel.h"
@@ -48,11 +48,16 @@ LuaScriptInterface::LuaScriptInterface(GameController * c, GameModel * m):
luacon_ren = m->GetRenderer(); luacon_ren = m->GetRenderer();
luacon_ci = this; luacon_ci = this;
//New TPT API //New TPT API
l = lua_open(); l = lua_open();
luaL_openlibs(l); luaL_openlibs(l);
luaopen_bit(l); luaopen_bit(l);
lua_pushstring(l, "Luacon_ci");
lua_pushlightuserdata(l, this);
lua_settable(l, LUA_REGISTRYINDEX);
initInterfaceAPI(); initInterfaceAPI();
initRendererAPI(); initRendererAPI();
initElementsAPI(); initElementsAPI();

View File

@@ -6,6 +6,7 @@ extern "C"
} }
#include <iostream> #include <iostream>
#include "LuaScriptInterface.h"
#include "LuaWindow.h" #include "LuaWindow.h"
#include "LuaButton.h" #include "LuaButton.h"
#include "LuaLabel.h" #include "LuaLabel.h"
@@ -59,6 +60,11 @@ LuaWindow::LuaWindow(lua_State * l) :
int sizeX = luaL_optinteger(l, 3, 10); int sizeX = luaL_optinteger(l, 3, 10);
int sizeY = luaL_optinteger(l, 4, 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 class DrawnWindow : public ui::Window
{ {
LuaWindow * luaWindow; LuaWindow * luaWindow;
@@ -145,10 +151,9 @@ void LuaWindow::triggerOnInitialized()
if(onInitializedFunction) if(onInitializedFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onInitializedFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onInitializedFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 0, 0))
{ {
//Log error somwhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }
@@ -158,10 +163,9 @@ void LuaWindow::triggerOnExit()
if(onExitFunction) if(onExitFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onExitFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onExitFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 0, 0))
{ {
//Log error somwhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }
@@ -171,11 +175,10 @@ void LuaWindow::triggerOnTick(float dt)
if(onTickFunction) if(onTickFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onTickFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onTickFunction);
lua_pushinteger(l, 1); //Self placeholder
lua_pushnumber(l, dt); 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) if(onDrawFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onDrawFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onDrawFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 0, 0))
{ {
//Log error somwhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }
@@ -198,10 +200,9 @@ void LuaWindow::triggerOnFocus()
if(onFocusFunction) if(onFocusFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onFocusFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onFocusFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 0, 0))
{ {
//Log error somwhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }
@@ -211,10 +212,9 @@ void LuaWindow::triggerOnBlur()
if(onBlurFunction) if(onBlurFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onBlurFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onBlurFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 0, 0))
{ {
//Log error somwhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }
@@ -224,10 +224,9 @@ void LuaWindow::triggerOnTryExit()
if(onTryExitFunction) if(onTryExitFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onTryExitFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onTryExitFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 0, 0))
{ {
//Log error somwhere ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
} }
} }
} }
@@ -237,10 +236,9 @@ void LuaWindow::triggerOnTryOkay()
if(onTryOkayFunction) if(onTryOkayFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onTryOkayFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onTryOkayFunction);
lua_pushinteger(l, 1); //Self placeholder if(lua_pcall(l, 0, 0, 0))
if(lua_pcall(l, 1, 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) if(onMouseMoveFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseMoveFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseMoveFunction);
lua_pushinteger(l, 0); //Self placeholder
lua_pushinteger(l, x); lua_pushinteger(l, x);
lua_pushinteger(l, y); lua_pushinteger(l, y);
lua_pushinteger(l, dx); lua_pushinteger(l, dx);
lua_pushinteger(l, dy); 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) if(onMouseDownFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseDownFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseDownFunction);
lua_pushinteger(l, 0); //Self placeholder
lua_pushinteger(l, x); lua_pushinteger(l, x);
lua_pushinteger(l, y); lua_pushinteger(l, y);
lua_pushinteger(l, button); 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) if(onMouseUpFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseUpFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseUpFunction);
lua_pushinteger(l, 0); //Self placeholder
lua_pushinteger(l, x); lua_pushinteger(l, x);
lua_pushinteger(l, y); lua_pushinteger(l, y);
lua_pushinteger(l, button); 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) if(onMouseWheelFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseWheelFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onMouseWheelFunction);
lua_pushinteger(l, 0); //Self placeholder
lua_pushinteger(l, x); lua_pushinteger(l, x);
lua_pushinteger(l, y); lua_pushinteger(l, y);
lua_pushinteger(l, d); 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) if(onKeyPressFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyPressFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyPressFunction);
lua_pushinteger(l, 0); //Self placeholder
lua_pushinteger(l, key); lua_pushinteger(l, key);
lua_pushinteger(l, character); lua_pushinteger(l, character);
lua_pushboolean(l, shift); lua_pushboolean(l, shift);
lua_pushboolean(l, ctrl); lua_pushboolean(l, ctrl);
lua_pushboolean(l, alt); 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) if(onKeyReleaseFunction)
{ {
lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyReleaseFunction); lua_rawgeti(l, LUA_REGISTRYINDEX, onKeyReleaseFunction);
lua_pushinteger(l, 0); //Self placeholder
lua_pushinteger(l, key); lua_pushinteger(l, key);
lua_pushinteger(l, character); lua_pushinteger(l, character);
lua_pushboolean(l, shift); lua_pushboolean(l, shift);
lua_pushboolean(l, ctrl); lua_pushboolean(l, ctrl);
lua_pushboolean(l, alt); 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));
} }
} }
} }

View File

@@ -14,6 +14,7 @@ namespace ui
class Window; class Window;
} }
class LuaScriptInterface;
class LuaWindow class LuaWindow
{ {
int onInitializedFunction; int onInitializedFunction;
@@ -69,6 +70,8 @@ class LuaWindow
void triggerOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt); void triggerOnKeyRelease(int key, Uint16 character, bool shift, bool ctrl, bool alt);
public: public:
LuaScriptInterface * ci;
int UserData;
static const char className[]; static const char className[];
static Luna<LuaWindow>::RegType methods[]; static Luna<LuaWindow>::RegType methods[];