summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-08-31 21:02:02 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-08-31 21:02:02 (GMT)
commit355c43723fd7aa5c412649924c1aa9238657b8ca (patch)
tree6860645c9f9dc5e224471af5170c578a2d13435e /src
parente8628274ada57b6a526e7cdb261875ac95f05db5 (diff)
downloadpowder-355c43723fd7aa5c412649924c1aa9238657b8ca.zip
powder-355c43723fd7aa5c412649924c1aa9238657b8ca.tar.gz
More interface API
Diffstat (limited to 'src')
-rw-r--r--src/cat/LuaLabel.cpp92
-rw-r--r--src/cat/LuaLabel.h30
-rw-r--r--src/cat/LuaLuna.h27
-rw-r--r--src/cat/LuaScriptInterface.cpp6
-rw-r--r--src/cat/LuaWindow.cpp6
5 files changed, 159 insertions, 2 deletions
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 <iostream>
+#include "LuaLabel.h"
+#include "interface/Label.h"
+
+const char LuaLabel::className[] = "Label";
+
+#define method(class, name) {#name, &class::name}
+Luna<LuaLabel>::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<LuaLabel>::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<userdataType*>(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 <direct.h>
@@ -284,14 +285,17 @@ void LuaScriptInterface::initInterfaceAPI()
luaL_register(l, "interface", interfaceAPIMethods);
Luna<LuaWindow>::Register(l);
Luna<LuaButton>::Register(l);
+ Luna<LuaLabel>::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<LuaButton>::tryGet(l, 1))
component = Luna<LuaButton>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaLabel>::tryGet(l, 1))
+ component = Luna<LuaLabel>::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 <iostream>
#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<LuaButton>::tryGet(l, 1))
component = Luna<LuaButton>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaLabel>::tryGet(l, 1))
+ component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
else
luaL_typerror(l, 1, "Component");
if(component)