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 "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));
}
}
}

View File

@ -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<LuaButton>::RegType methods[];

View File

@ -6,6 +6,7 @@ extern "C"
}
#include <iostream>
#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);
}

View File

@ -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<LuaLabel>::RegType methods[];

View File

@ -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<userdataType*>(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

View File

@ -12,6 +12,7 @@
#include <locale>
#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();

View File

@ -6,6 +6,7 @@ extern "C"
}
#include <iostream>
#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));
}
}
}

View File

@ -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<LuaWindow>::RegType methods[];