From 355c43723fd7aa5c412649924c1aa9238657b8ca Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 31 Aug 2012 22:02:02 +0100 Subject: More interface API diff --git a/src/cat/LuaLabel.cpp b/src/cat/LuaLabel.cpp new file mode 100644 index 0000000..5ab3d1c --- /dev/null +++ b/src/cat/LuaLabel.cpp @@ -0,0 +1,92 @@ +extern "C" +{ +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +} + +#include +#include "LuaLabel.h" +#include "interface/Label.h" + +const char LuaLabel::className[] = "Label"; + +#define method(class, name) {#name, &class::name} +Luna::RegType LuaLabel::methods[] = { + method(LuaLabel, text), + method(LuaLabel, position), + method(LuaLabel, size), + {0, 0} +}; + +LuaLabel::LuaLabel(lua_State * l) +{ + 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, ""); + + label = new ui::Label(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text); +} + +int LuaLabel::text(lua_State * l) +{ + int args = lua_gettop(l); + if(args) + { + luaL_checktype(l, 1, LUA_TSTRING); + label->SetText(lua_tostring(l, 1)); + return 0; + } + else + { + lua_pushstring(l, label->GetText().c_str()); + return 1; + } +} + +int LuaLabel::position(lua_State * l) +{ + int args = lua_gettop(l); + if(args) + { + luaL_checktype(l, 1, LUA_TNUMBER); + luaL_checktype(l, 2, LUA_TNUMBER); + label->Position = ui::Point(lua_tointeger(l, 1), lua_tointeger(l, 2)); + return 0; + } + else + { + lua_pushinteger(l, label->Position.X); + lua_pushinteger(l, label->Position.Y); + return 2; + } +} + +int LuaLabel::size(lua_State * l) +{ + int args = lua_gettop(l); + if(args) + { + luaL_checktype(l, 1, LUA_TNUMBER); + luaL_checktype(l, 2, LUA_TNUMBER); + label->Size = ui::Point(lua_tointeger(l, 1), lua_tointeger(l, 2)); + label->Invalidate(); + return 0; + } + else + { + lua_pushinteger(l, label->Size.X); + lua_pushinteger(l, label->Size.Y); + return 2; + } +} + +LuaLabel::~LuaLabel() +{ + if(label->GetParentWindow()) + label->GetParentWindow()->RemoveComponent(label); + delete label; +} \ No newline at end of file diff --git a/src/cat/LuaLabel.h b/src/cat/LuaLabel.h new file mode 100644 index 0000000..bfaaabf --- /dev/null +++ b/src/cat/LuaLabel.h @@ -0,0 +1,30 @@ +#pragma once + +extern "C" { + #include "lua.h" + #include "lauxlib.h" + #include "lualib.h" +} + +#include "LuaLuna.h" + +namespace ui +{ + class Label; +} + +class LuaLabel +{ + ui::Label * label; + lua_State * l; + int text(lua_State * l); + int position(lua_State * l); + int size(lua_State * l); +public: + static const char className[]; + static Luna::RegType methods[]; + + ui::Label * GetComponent() { return label; } + LuaLabel(lua_State * l); + ~LuaLabel(); +}; \ No newline at end of file diff --git a/src/cat/LuaLuna.h b/src/cat/LuaLuna.h index d15a7c6..591dad2 100644 --- a/src/cat/LuaLuna.h +++ b/src/cat/LuaLuna.h @@ -75,6 +75,33 @@ public: return ud->pT; // pointer to T object } + static void * tryGet(lua_State * L, int narg) + { + if(checkType(L, narg, T::className)) + { + userdataType *ud = static_cast(luaL_checkudata(L, narg, T::className)); + if(!ud) + luaL_typerror(L, narg, T::className); + return ud; // pointer to T object + } + else + { + return NULL; + } + } + + static bool checkType (lua_State * L, int idx, const char *name) + { + // returns true if a userdata is of a certain type + int res; + if (lua_type(L, idx) != LUA_TUSERDATA) return false; + lua_getmetatable(L, idx); + luaL_newmetatable (L, name); + res = lua_equal(L, -2, -1); + lua_pop(L, 2); // pop both tables (metatables) off + return res; + } + static inline T * get(void * userData) { return ((userdataType*)userData)->pT; diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index fe098a0..54e2292 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -28,6 +28,7 @@ #include "LuaLuna.h" #include "LuaWindow.h" #include "LuaButton.h" +#include "LuaLabel.h" #ifdef WIN #include @@ -284,14 +285,17 @@ void LuaScriptInterface::initInterfaceAPI() luaL_register(l, "interface", interfaceAPIMethods); Luna::Register(l); Luna::Register(l); + Luna::Register(l); } int LuaScriptInterface::interface_addComponent(lua_State * l) { void * luaComponent = NULL; ui::Component * component = NULL; - if(luaComponent = luaL_checkudata(l, 1, "Button")) + if(luaComponent = Luna::tryGet(l, 1)) component = Luna::get(luaComponent)->GetComponent(); + else if(luaComponent = Luna::tryGet(l, 1)) + component = Luna::get(luaComponent)->GetComponent(); else luaL_typerror(l, 1, "Component"); if(luacon_ci->Window && component) diff --git a/src/cat/LuaWindow.cpp b/src/cat/LuaWindow.cpp index f14d0b9..e0a5298 100644 --- a/src/cat/LuaWindow.cpp +++ b/src/cat/LuaWindow.cpp @@ -8,7 +8,9 @@ extern "C" #include #include "LuaWindow.h" #include "LuaButton.h" +#include "LuaLabel.h" #include "interface/Button.h" +#include "interface/Label.h" #include "interface/Window.h" const char LuaWindow::className[] = "Window"; @@ -91,8 +93,10 @@ int LuaWindow::addComponent(lua_State * l) { void * luaComponent = NULL; ui::Component * component = NULL; - if(luaComponent = luaL_checkudata(l, 1, "Button")) + if(luaComponent = Luna::tryGet(l, 1)) component = Luna::get(luaComponent)->GetComponent(); + else if(luaComponent = Luna::tryGet(l, 1)) + component = Luna::get(luaComponent)->GetComponent(); else luaL_typerror(l, 1, "Component"); if(component) -- cgit v0.9.2-21-gd62e