diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-08-31 18:39:11 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-08-31 18:39:11 (GMT) |
| commit | 493a32a1b28f15cf02bb69c3735b9a411448c523 (patch) | |
| tree | 838b39db197c1f9cab5740703bb03f9b24702c56 /src/cat/LuaButton.cpp | |
| parent | 322d224fa7a1d5f155c87596bb2f5927989e9933 (diff) | |
| download | powder-493a32a1b28f15cf02bb69c3735b9a411448c523.zip powder-493a32a1b28f15cf02bb69c3735b9a411448c523.tar.gz | |
Some Lua interface API stuff
Diffstat (limited to 'src/cat/LuaButton.cpp')
| -rw-r--r-- | src/cat/LuaButton.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/cat/LuaButton.cpp b/src/cat/LuaButton.cpp new file mode 100644 index 0000000..8a3c92d --- /dev/null +++ b/src/cat/LuaButton.cpp @@ -0,0 +1,134 @@ +extern "C" +{ +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +} + +#include <iostream> +#include "LuaButton.h" +#include "interface/Button.h" + +const char LuaButton::className[] = "Button"; + +#define method(class, name) {#name, &class::name} +Luna<LuaButton>::RegType LuaButton::methods[] = { + method(LuaButton, action), + method(LuaButton, text), + method(LuaButton, position), + method(LuaButton, size), + {0, 0} +}; + +LuaButton::LuaButton(lua_State * l) : + actionFunction(0) +{ + this->l = l; + int posX = luaL_optinteger(l, 1, 0); + int posY = luaL_optinteger(l, 2, 0); + 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, "");; + + button = new ui::Button(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, toolTip); + class ClickAction : public ui::ButtonAction + { + LuaButton * luaButton; + public: + ClickAction(LuaButton * luaButton) : luaButton(luaButton) {} + void ActionCallback(ui::Button * sender) + { + luaButton->triggerAction(); + } + }; + button->SetActionCallback(new ClickAction(this)); +} + +int LuaButton::action(lua_State * l) +{ + if(lua_type(l, 1) != LUA_TNIL) + { + luaL_checktype(l, 1, LUA_TFUNCTION); + lua_pushvalue(l, 1); + actionFunction = luaL_ref(l, LUA_REGISTRYINDEX); + } + else + { + actionFunction = 0; + } +} + +int LuaButton::text(lua_State * l) +{ + int args = lua_gettop(l); + if(args) + { + luaL_checktype(l, 1, LUA_TSTRING); + button->SetText(lua_tostring(l, 1)); + return 0; + } + else + { + lua_pushstring(l, button->GetText().c_str()); + return 1; + } +} + +int LuaButton::position(lua_State * l) +{ + int args = lua_gettop(l); + if(args) + { + luaL_checktype(l, 1, LUA_TNUMBER); + luaL_checktype(l, 2, LUA_TNUMBER); + button->Position = ui::Point(lua_tointeger(l, 1), lua_tointeger(l, 2)); + return 0; + } + else + { + lua_pushinteger(l, button->Position.X); + lua_pushinteger(l, button->Position.Y); + return 2; + } +} + +int LuaButton::size(lua_State * l) +{ + int args = lua_gettop(l); + if(args) + { + luaL_checktype(l, 1, LUA_TNUMBER); + luaL_checktype(l, 2, LUA_TNUMBER); + button->Size = ui::Point(lua_tointeger(l, 1), lua_tointeger(l, 2)); + button->Invalidate(); + return 0; + } + else + { + lua_pushinteger(l, button->Size.X); + lua_pushinteger(l, button->Size.Y); + return 2; + } +} + +void LuaButton::triggerAction() +{ + if(actionFunction) + { + std::cout << actionFunction << std::endl; + lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction); + lua_pushinteger(l, 1); + if (lua_pcall(l, 1, 1, 0)) + { + //Log error somewhere + } + } +} + +LuaButton::~LuaButton() +{ + if(button->GetParentWindow()) + button->GetParentWindow()->RemoveComponent(button); + delete button; +}
\ No newline at end of file |
