summaryrefslogtreecommitdiff
path: root/src
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
parentb7616a91d860871a3e4ac46cba957633a93acb59 (diff)
downloadpowder-4e09a077a4d7494d1c1e1f494cbc36fa9abcb19c.zip
powder-4e09a077a4d7494d1c1e1f494cbc36fa9abcb19c.tar.gz
Textbox component for Lua interface API
Diffstat (limited to 'src')
-rw-r--r--src/cat/LuaButton.cpp64
-rw-r--r--src/cat/LuaButton.h10
-rw-r--r--src/cat/LuaComponent.cpp82
-rw-r--r--src/cat/LuaComponent.h33
-rw-r--r--src/cat/LuaLabel.cpp50
-rw-r--r--src/cat/LuaLabel.h9
-rw-r--r--src/cat/LuaScriptInterface.cpp4
-rw-r--r--src/cat/LuaTextbox.cpp115
-rw-r--r--src/cat/LuaTextbox.h33
-rw-r--r--src/cat/LuaWindow.cpp3
10 files changed, 298 insertions, 105 deletions
diff --git a/src/cat/LuaButton.cpp b/src/cat/LuaButton.cpp
index 18001fd..59890f2 100644
--- a/src/cat/LuaButton.cpp
+++ b/src/cat/LuaButton.cpp
@@ -18,13 +18,15 @@ Luna<LuaButton>::RegType LuaButton::methods[] = {
method(LuaButton, text),
method(LuaButton, position),
method(LuaButton, size),
+ method(LuaButton, visible),
+ method(LuaButton, enabled),
{0, 0}
};
LuaButton::LuaButton(lua_State * l) :
+ LuaComponent(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);
@@ -32,12 +34,8 @@ LuaButton::LuaButton(lua_State * l) :
std::string text = luaL_optstring(l, 5, "");
std::string toolTip = luaL_optstring(l, 6, "");
- lua_pushstring(l, "Luacon_ci");
- lua_gettable(l, LUA_REGISTRYINDEX);
- ci = (LuaScriptInterface*)lua_touserdata(l, -1);
- lua_pop(l, 1);
-
button = new ui::Button(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text, toolTip);
+ component = button;
class ClickAction : public ui::ButtonAction
{
LuaButton * luaButton;
@@ -51,70 +49,49 @@ LuaButton::LuaButton(lua_State * l) :
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 LuaButton::enabled(lua_State * l)
{
int args = lua_gettop(l);
if(args)
{
- luaL_checktype(l, 1, LUA_TSTRING);
- button->SetText(lua_tostring(l, 1));
+ luaL_checktype(l, 1, LUA_TBOOLEAN);
+ button->Enabled = lua_toboolean(l, 1);
return 0;
}
else
{
- lua_pushstring(l, button->GetText().c_str());
+ lua_pushboolean(l, button->Enabled);
return 1;
}
}
-int LuaButton::position(lua_State * l)
+int LuaButton::action(lua_State * l)
{
- int args = lua_gettop(l);
- if(args)
+ if(lua_type(l, 1) != LUA_TNIL)
{
- 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;
+ luaL_checktype(l, 1, LUA_TFUNCTION);
+ lua_pushvalue(l, 1);
+ actionFunction = luaL_ref(l, LUA_REGISTRYINDEX);
}
else
{
- lua_pushinteger(l, button->Position.X);
- lua_pushinteger(l, button->Position.Y);
- return 2;
+ actionFunction = 0;
}
}
-int LuaButton::size(lua_State * l)
+int LuaButton::text(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();
+ luaL_checktype(l, 1, LUA_TSTRING);
+ button->SetText(lua_tostring(l, 1));
return 0;
}
else
{
- lua_pushinteger(l, button->Size.X);
- lua_pushinteger(l, button->Size.Y);
- return 2;
+ lua_pushstring(l, button->GetText().c_str());
+ return 1;
}
}
@@ -133,7 +110,4 @@ void LuaButton::triggerAction()
LuaButton::~LuaButton()
{
- if(button->GetParentWindow())
- button->GetParentWindow()->RemoveComponent(button);
- delete button;
} \ No newline at end of file
diff --git a/src/cat/LuaButton.h b/src/cat/LuaButton.h
index a092d07..012779d 100644
--- a/src/cat/LuaButton.h
+++ b/src/cat/LuaButton.h
@@ -7,6 +7,7 @@ extern "C" {
}
#include "LuaLuna.h"
+#include "LuaComponent.h"
namespace ui
{
@@ -15,23 +16,18 @@ namespace ui
class LuaScriptInterface;
-class LuaButton
+class LuaButton: public LuaComponent
{
ui::Button * button;
int actionFunction;
- lua_State * l;
void triggerAction();
int action(lua_State * l);
int text(lua_State * l);
- int position(lua_State * l);
- int size(lua_State * l);
+ int enabled(lua_State * l);
public:
- LuaScriptInterface * ci;
- int UserData;
static const char className[];
static Luna<LuaButton>::RegType methods[];
- ui::Button * GetComponent() { return button; }
LuaButton(lua_State * l);
~LuaButton();
}; \ No newline at end of file
diff --git a/src/cat/LuaComponent.cpp b/src/cat/LuaComponent.cpp
new file mode 100644
index 0000000..8c2d3a4
--- /dev/null
+++ b/src/cat/LuaComponent.cpp
@@ -0,0 +1,82 @@
+extern "C"
+{
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+}
+
+#include <iostream>
+#include "LuaComponent.h"
+#include "LuaScriptInterface.h"
+#include "interface/Component.h"
+
+
+LuaComponent::LuaComponent(lua_State * l)
+{
+ this->l = l;
+
+ lua_pushstring(l, "Luacon_ci");
+ lua_gettable(l, LUA_REGISTRYINDEX);
+ ci = (LuaScriptInterface*)lua_touserdata(l, -1);
+ lua_pop(l, 1);
+}
+
+int LuaComponent::position(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TNUMBER);
+ luaL_checktype(l, 2, LUA_TNUMBER);
+ component->Position = ui::Point(lua_tointeger(l, 1), lua_tointeger(l, 2));
+ return 0;
+ }
+ else
+ {
+ lua_pushinteger(l, component->Position.X);
+ lua_pushinteger(l, component->Position.Y);
+ return 2;
+ }
+}
+
+int LuaComponent::size(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TNUMBER);
+ luaL_checktype(l, 2, LUA_TNUMBER);
+ component->Size = ui::Point(lua_tointeger(l, 1), lua_tointeger(l, 2));
+ component->Invalidate();
+ return 0;
+ }
+ else
+ {
+ lua_pushinteger(l, component->Size.X);
+ lua_pushinteger(l, component->Size.Y);
+ return 2;
+ }
+}
+
+int LuaComponent::visible(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TBOOLEAN);
+ component->Visible = lua_toboolean(l, 1);
+ return 0;
+ }
+ else
+ {
+ lua_pushboolean(l, component->Visible);
+ return 1;
+ }
+}
+
+LuaComponent::~LuaComponent()
+{
+ if(component->GetParentWindow())
+ component->GetParentWindow()->RemoveComponent(component);
+ delete component;
+} \ No newline at end of file
diff --git a/src/cat/LuaComponent.h b/src/cat/LuaComponent.h
new file mode 100644
index 0000000..9e11b12
--- /dev/null
+++ b/src/cat/LuaComponent.h
@@ -0,0 +1,33 @@
+#pragma once
+
+extern "C" {
+ #include "lua.h"
+ #include "lauxlib.h"
+ #include "lualib.h"
+}
+
+#include "LuaLuna.h"
+
+namespace ui
+{
+ class Component;
+}
+
+class LuaScriptInterface;
+
+class LuaComponent
+{
+protected:
+ ui::Component * component;
+ lua_State * l;
+ int position(lua_State * l);
+ int size(lua_State * l);
+ int visible(lua_State * l);
+public:
+ LuaScriptInterface * ci;
+ int UserData;
+
+ ui::Component * GetComponent() { return component; }
+ LuaComponent(lua_State * l);
+ ~LuaComponent();
+}; \ No newline at end of file
diff --git a/src/cat/LuaLabel.cpp b/src/cat/LuaLabel.cpp
index c1d9497..e2ca56d 100644
--- a/src/cat/LuaLabel.cpp
+++ b/src/cat/LuaLabel.cpp
@@ -17,10 +17,12 @@ Luna<LuaLabel>::RegType LuaLabel::methods[] = {
method(LuaLabel, text),
method(LuaLabel, position),
method(LuaLabel, size),
+ method(LuaLabel, visible),
{0, 0}
};
-LuaLabel::LuaLabel(lua_State * l)
+LuaLabel::LuaLabel(lua_State * l) :
+ LuaComponent(l)
{
this->l = l;
int posX = luaL_optinteger(l, 1, 0);
@@ -29,12 +31,8 @@ LuaLabel::LuaLabel(lua_State * l)
int sizeY = luaL_optinteger(l, 4, 10);
std::string text = luaL_optstring(l, 5, "");
- lua_pushstring(l, "Luacon_ci");
- lua_gettable(l, LUA_REGISTRYINDEX);
- ci = (LuaScriptInterface*)lua_touserdata(l, -1);
- lua_pop(l, 1);
-
label = new ui::Label(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text);
+ component = label;
}
int LuaLabel::text(lua_State * l)
@@ -53,46 +51,6 @@ int LuaLabel::text(lua_State * l)
}
}
-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
index 7e1f808..a80ea4f 100644
--- a/src/cat/LuaLabel.h
+++ b/src/cat/LuaLabel.h
@@ -7,6 +7,7 @@ extern "C" {
}
#include "LuaLuna.h"
+#include "LuaComponent.h"
namespace ui
{
@@ -15,20 +16,14 @@ namespace ui
class LuaScriptInterface;
-class LuaLabel
+class LuaLabel: public LuaComponent
{
ui::Label * label;
- lua_State * l;
int text(lua_State * l);
- int position(lua_State * l);
- int size(lua_State * l);
public:
- LuaScriptInterface * ci;
- int UserData;
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/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp
index 618319d..78a6de5 100644
--- a/src/cat/LuaScriptInterface.cpp
+++ b/src/cat/LuaScriptInterface.cpp
@@ -29,6 +29,7 @@
#include "LuaWindow.h"
#include "LuaButton.h"
#include "LuaLabel.h"
+#include "LuaTextbox.h"
#ifdef WIN
#include <direct.h>
@@ -291,6 +292,7 @@ void LuaScriptInterface::initInterfaceAPI()
Luna<LuaWindow>::Register(l);
Luna<LuaButton>::Register(l);
Luna<LuaLabel>::Register(l);
+ Luna<LuaTextbox>::Register(l);
}
int LuaScriptInterface::interface_addComponent(lua_State * l)
@@ -301,6 +303,8 @@ int LuaScriptInterface::interface_addComponent(lua_State * l)
component = Luna<LuaButton>::get(luaComponent)->GetComponent();
else if(luaComponent = Luna<LuaLabel>::tryGet(l, 1))
component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaTextbox>::tryGet(l, 1))
+ component = Luna<LuaTextbox>::get(luaComponent)->GetComponent();
else
luaL_typerror(l, 1, "Component");
if(luacon_ci->Window && component)
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
diff --git a/src/cat/LuaTextbox.h b/src/cat/LuaTextbox.h
new file mode 100644
index 0000000..437875f
--- /dev/null
+++ b/src/cat/LuaTextbox.h
@@ -0,0 +1,33 @@
+#pragma once
+
+extern "C" {
+ #include "lua.h"
+ #include "lauxlib.h"
+ #include "lualib.h"
+}
+
+#include "LuaLuna.h"
+#include "LuaComponent.h"
+
+namespace ui
+{
+ class Textbox;
+}
+
+class LuaScriptInterface;
+
+class LuaTextbox: public LuaComponent
+{
+ int onTextChangedFunction;
+ ui::Textbox * textbox;
+ int text(lua_State * l);
+ int readonly(lua_State * l);
+ int onTextChanged(lua_State * l);
+ void triggerOnTextChanged();
+public:
+ static const char className[];
+ static Luna<LuaTextbox>::RegType methods[];
+
+ LuaTextbox(lua_State * l);
+ ~LuaTextbox();
+}; \ No newline at end of file
diff --git a/src/cat/LuaWindow.cpp b/src/cat/LuaWindow.cpp
index c837170..06b29c8 100644
--- a/src/cat/LuaWindow.cpp
+++ b/src/cat/LuaWindow.cpp
@@ -10,6 +10,7 @@ extern "C"
#include "LuaWindow.h"
#include "LuaButton.h"
#include "LuaLabel.h"
+#include "LuaTextbox.h"
#include "interface/Button.h"
#include "interface/Label.h"
#include "interface/Window.h"
@@ -103,6 +104,8 @@ int LuaWindow::addComponent(lua_State * l)
component = Luna<LuaButton>::get(luaComponent)->GetComponent();
else if(luaComponent = Luna<LuaLabel>::tryGet(l, 1))
component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaTextbox>::tryGet(l, 1))
+ component = Luna<LuaTextbox>::get(luaComponent)->GetComponent();
else
luaL_typerror(l, 1, "Component");
if(component)