summaryrefslogtreecommitdiff
path: root/src/cat/LuaCheckbox.cpp
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-09-17 11:20:58 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-09-17 11:20:58 (GMT)
commit6e44ebc358d1206c147f514225373da07b43c015 (patch)
tree35e97c28991c4aff46a9e5a4182b53dedb26e7ee /src/cat/LuaCheckbox.cpp
parente52e9ce91ccca13115fec0fdb0111e7e5d39d10d (diff)
downloadpowder-6e44ebc358d1206c147f514225373da07b43c015.zip
powder-6e44ebc358d1206c147f514225373da07b43c015.tar.gz
Checkbox, Slider and ProgressBar components for ui API
Diffstat (limited to 'src/cat/LuaCheckbox.cpp')
-rw-r--r--src/cat/LuaCheckbox.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/cat/LuaCheckbox.cpp b/src/cat/LuaCheckbox.cpp
new file mode 100644
index 0000000..1ddc0c2
--- /dev/null
+++ b/src/cat/LuaCheckbox.cpp
@@ -0,0 +1,114 @@
+extern "C"
+{
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+}
+
+#include <iostream>
+#include "LuaCheckbox.h"
+#include "LuaScriptInterface.h"
+#include "interface/Checkbox.h"
+
+const char LuaCheckbox::className[] = "Checkbox";
+
+#define method(class, name) {#name, &class::name}
+Luna<LuaCheckbox>::RegType LuaCheckbox::methods[] = {
+ method(LuaCheckbox, action),
+ method(LuaCheckbox, text),
+ method(LuaCheckbox, position),
+ method(LuaCheckbox, size),
+ method(LuaCheckbox, visible),
+ method(LuaCheckbox, checked),
+ {0, 0}
+};
+
+LuaCheckbox::LuaCheckbox(lua_State * l) :
+ LuaComponent(l),
+ actionFunction(0)
+{
+ 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, "");
+
+ checkbox = new ui::Checkbox(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text);
+ component = checkbox;
+ class ClickAction : public ui::CheckboxAction
+ {
+ LuaCheckbox * luaCheckbox;
+ public:
+ ClickAction(LuaCheckbox * luaCheckbox) : luaCheckbox(luaCheckbox) {}
+ void ActionCallback(ui::Checkbox * sender)
+ {
+ luaCheckbox->triggerAction();
+ }
+ };
+ checkbox->SetActionCallback(new ClickAction(this));
+}
+
+int LuaCheckbox::checked(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TBOOLEAN);
+ checkbox->SetChecked(lua_toboolean(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushboolean(l, checkbox->GetChecked());
+ return 1;
+ }
+}
+
+int LuaCheckbox::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;
+ }
+ return 0;
+}
+
+int LuaCheckbox::text(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TSTRING);
+ checkbox->SetText(lua_tostring(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushstring(l, checkbox->GetText().c_str());
+ return 1;
+ }
+}
+
+void LuaCheckbox::triggerAction()
+{
+ if(actionFunction)
+ {
+ lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction);
+ lua_rawgeti(l, LUA_REGISTRYINDEX, UserData);
+ lua_pushboolean(l, checkbox->GetChecked());
+ if (lua_pcall(l, 2, 0, 0))
+ {
+ ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
+ }
+ }
+}
+
+LuaCheckbox::~LuaCheckbox()
+{
+} \ No newline at end of file