summaryrefslogtreecommitdiff
path: root/src/cat/LuaTextbox.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-09-02 22:55:08 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-09-02 22:55:08 (GMT)
commit4e09a077a4d7494d1c1e1f494cbc36fa9abcb19c (patch)
treef76cf74aa260678cd230dba8955d378c5fa392f0 /src/cat/LuaTextbox.cpp
parentb7616a91d860871a3e4ac46cba957633a93acb59 (diff)
downloadpowder-4e09a077a4d7494d1c1e1f494cbc36fa9abcb19c.zip
powder-4e09a077a4d7494d1c1e1f494cbc36fa9abcb19c.tar.gz
Textbox component for Lua interface API
Diffstat (limited to 'src/cat/LuaTextbox.cpp')
-rw-r--r--src/cat/LuaTextbox.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/cat/LuaTextbox.cpp b/src/cat/LuaTextbox.cpp
new file mode 100644
index 0000000..a8abf9e
--- /dev/null
+++ b/src/cat/LuaTextbox.cpp
@@ -0,0 +1,115 @@
+extern "C"
+{
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+}
+
+#include <iostream>
+#include "LuaScriptInterface.h"
+#include "LuaTextbox.h"
+#include "interface/Textbox.h"
+
+const char LuaTextbox::className[] = "Textbox";
+
+#define method(class, name) {#name, &class::name}
+Luna<LuaTextbox>::RegType LuaTextbox::methods[] = {
+ method(LuaTextbox, text),
+ method(LuaTextbox, readonly),
+ method(LuaTextbox, onTextChanged),
+ method(LuaTextbox, position),
+ method(LuaTextbox, size),
+ method(LuaTextbox, visible),
+ {0, 0}
+};
+
+LuaTextbox::LuaTextbox(lua_State * l) :
+ LuaComponent(l),
+ onTextChangedFunction(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 placeholder = luaL_optstring(l, 6, "");
+
+ textbox = new ui::Textbox(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, placeholder);
+ textbox->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
+ class TextChangedAction : public ui::TextboxAction
+ {
+ LuaTextbox * t;
+ public:
+ TextChangedAction(LuaTextbox * t) : t(t) {}
+ void TextChangedCallback(ui::Textbox * sender)
+ {
+ t->triggerOnTextChanged();
+ }
+ };
+ textbox->SetActionCallback(new TextChangedAction(this));
+ component = textbox;
+}
+
+int LuaTextbox::readonly(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TBOOLEAN);
+ textbox->ReadOnly = lua_toboolean(l, 1);
+ return 0;
+ }
+ else
+ {
+ lua_pushboolean(l, textbox->ReadOnly);
+ return 1;
+ }
+}
+
+int LuaTextbox::onTextChanged(lua_State * l)
+{
+ if(lua_type(l, 1) != LUA_TNIL)
+ {
+ luaL_checktype(l, 1, LUA_TFUNCTION);
+ lua_pushvalue(l, 1);
+ onTextChangedFunction = luaL_ref(l, LUA_REGISTRYINDEX);
+ }
+ else
+ {
+ onTextChangedFunction = 0;
+ }
+}
+
+void LuaTextbox::triggerOnTextChanged()
+{
+ if(onTextChangedFunction)
+ {
+ lua_rawgeti(l, LUA_REGISTRYINDEX, onTextChangedFunction);
+ lua_rawgeti(l, LUA_REGISTRYINDEX, UserData);
+ if (lua_pcall(l, 1, 0, 0))
+ {
+ ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
+ }
+ }
+}
+
+int LuaTextbox::text(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TSTRING);
+ textbox->SetText(lua_tostring(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushstring(l, textbox->GetText().c_str());
+ return 1;
+ }
+}
+
+LuaTextbox::~LuaTextbox()
+{
+} \ No newline at end of file