From 4e09a077a4d7494d1c1e1f494cbc36fa9abcb19c Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sun, 2 Sep 2012 23:55:08 +0100 Subject: Textbox component for Lua interface API 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::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::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 +#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::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::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 @@ -291,6 +292,7 @@ void LuaScriptInterface::initInterfaceAPI() Luna::Register(l); Luna::Register(l); Luna::Register(l); + Luna::Register(l); } int LuaScriptInterface::interface_addComponent(lua_State * l) @@ -301,6 +303,8 @@ int LuaScriptInterface::interface_addComponent(lua_State * l) component = Luna::get(luaComponent)->GetComponent(); else 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/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 +#include "LuaScriptInterface.h" +#include "LuaTextbox.h" +#include "interface/Textbox.h" + +const char LuaTextbox::className[] = "Textbox"; + +#define method(class, name) {#name, &class::name} +Luna::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::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::get(luaComponent)->GetComponent(); else 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