From dbab8458dcce186e7eb7a114a83f759d7db5445a Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 3 Aug 2010 12:30:27 +0000 Subject: New scripting interface part1. --- client/lua.cc | 193 ++++++++-------------------------------------------------- 1 file changed, 24 insertions(+), 169 deletions(-) (limited to 'client/lua.cc') diff --git a/client/lua.cc b/client/lua.cc index 5e6850b..19b94c0 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -30,14 +30,16 @@ #include "widgets/widget.h" +#include "luawidget.h" + #define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten" -static int _enable(lua_State *L) +static int get_widget(lua_State *L) { int n = lua_gettop(L); // number of arguments if(n != 1) { char errstr[512]; - sprintf(errstr, "Number of args expected 0, got %d", n); + sprintf(errstr, "Number of args expected 1, got %d", n); lua_pushstring(L, errstr); lua_error(L); return 1; @@ -54,127 +56,21 @@ static int _enable(lua_State *L) return 1; } - lua->enable(name); + Widget *widget = lua->getWidget(name); - return 0; -} + printf("FIND: %s (%p)\n", name.toStdString().c_str(), widget); -static int _disable(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 1) { - char errstr[512]; - sprintf(errstr, "Number of args expected 0, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 1; + if(widget) { + wdg_make_widget(L, widget); + } else { + lua_pushnil(L); } - QString name = lua_tostring(L, lua_gettop(L)); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - lua->disable(name); - - return 0; -} - -static int _getValue(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 1) { - char errstr[512]; - sprintf(errstr, "Number of args expected 0, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 1; - } - - QString name = lua_tostring(L, lua_gettop(L)); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - QString value = lua->getValue(name); - lua_pushstring(L, value.toStdString().c_str()); + printf("DONE\n"); return 1; } -static int _setValue(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 2) { - char errstr[512]; - sprintf(errstr, "Number of args expected 2, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - QString value = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - QString name = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - lua->setValue(name, value); - - return 0; -} - -static int _setVisible(lua_State *L) -{ - int n = lua_gettop(L); // number of arguments - if(n != 2) { - char errstr[512]; - sprintf(errstr, "Number of args expected 2, got %d", n); - lua_pushstring(L, errstr); - lua_error(L); - return 0; - } - - bool value = lua_toboolean(L, lua_gettop(L)); - lua_pop(L, 1); - QString name = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - lua_getglobal(L, GLOBAL_POINTER); - LUA *lua = (LUA*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - lua->setVisible(name, value); - - return 0; -} - - LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets) { this->widgets = widgets; @@ -191,11 +87,9 @@ LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets) lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. - lua_register(L, "getValue", _getValue); - lua_register(L, "setValue", _setValue); - lua_register(L, "enable", _enable); - lua_register(L, "disable", _disable); - lua_register(L, "setVisible", _setVisible); + lua_register(L, "widget", get_widget); + + register_widget(L); } LUA::~LUA() @@ -203,38 +97,7 @@ LUA::~LUA() lua_close(L); } -QString LUA::getValue(QString name) -{ - Widget *widget = getWidget(name); - if(widget) return widget->getValue(); - return ""; -} - -void LUA::setValue(QString name, QString value) -{ - Widget *widget = getWidget(name); - if(widget) widget->setValue(value); -} - -void LUA::enable(QString name) -{ - Widget *widget = getWidget(name); - if(widget) widget->enable(); -} - -void LUA::disable(QString name) -{ - Widget *widget = getWidget(name); - if(widget) widget->disable(); -} - -void LUA::setVisible(QString name, bool value) -{ - Widget *widget = getWidget(name); - if(widget) widget->setVisibility(value); -} - -bool LUA::runValidator(QString program, QString name, QString value) +bool LUA::runValidator(QString program, Widget *widget, QString name, QString value) { if(L == NULL) { error("LUA state not initialized!"); @@ -252,7 +115,12 @@ bool LUA::runValidator(QString program, QString name, QString value) lua_pushstring(L, name.toStdString().c_str()); lua_setglobal(L, "name"); - int top = lua_gettop(L); + if(widget) { + wdg_make_widget(L, widget); + lua_setglobal(L, "this"); + } + + // int top = lua_gettop(L); if(luaL_loadbuffer(L, program.toStdString().c_str(), @@ -268,20 +136,7 @@ bool LUA::runValidator(QString program, QString name, QString value) return false; } - if(top != lua_gettop(L) - 1) { - error("Program did not return a single value.\n"); - return false; - } - - if(lua_isboolean(L, lua_gettop(L)) == false) { - error("Program did not return a boolean value.\n"); - return false; - } - - bool res = lua_toboolean(L, lua_gettop(L)); - lua_pop(L, 1); - - return res; + return true; } QString LUA::runParser(QString program) @@ -335,7 +190,7 @@ Widget *LUA::getWidget(QString name) QVector< Widget* >::iterator i = widgets->begin(); while (i != widgets->end()) { Widget* w = *i; - if(name == w->getName()) return w; + if(name == w->name()) return w; i++; } @@ -343,7 +198,7 @@ Widget *LUA::getWidget(QString name) QVector< Widget* >::iterator j = auxwidgets->begin(); while (j != auxwidgets->end()) { Widget* w = *j; - if(name == w->getName()) return w; + if(name == w->name()) return w; j++; } } -- cgit v1.2.3