Textbox component for Lua interface API
This commit is contained in:
parent
b7616a91d8
commit
4e09a077a4
@ -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,6 +49,22 @@ LuaButton::LuaButton(lua_State * l) :
|
||||
button->SetActionCallback(new ClickAction(this));
|
||||
}
|
||||
|
||||
int LuaButton::enabled(lua_State * l)
|
||||
{
|
||||
int args = lua_gettop(l);
|
||||
if(args)
|
||||
{
|
||||
luaL_checktype(l, 1, LUA_TBOOLEAN);
|
||||
button->Enabled = lua_toboolean(l, 1);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushboolean(l, button->Enabled);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int LuaButton::action(lua_State * l)
|
||||
{
|
||||
if(lua_type(l, 1) != LUA_TNIL)
|
||||
@ -81,43 +95,6 @@ int LuaButton::text(lua_State * l)
|
||||
}
|
||||
}
|
||||
|
||||
int LuaButton::position(lua_State * l)
|
||||
{
|
||||
int args = lua_gettop(l);
|
||||
if(args)
|
||||
{
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushinteger(l, button->Position.X);
|
||||
lua_pushinteger(l, button->Position.Y);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
int LuaButton::size(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();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushinteger(l, button->Size.X);
|
||||
lua_pushinteger(l, button->Size.Y);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
void LuaButton::triggerAction()
|
||||
{
|
||||
if(actionFunction)
|
||||
@ -133,7 +110,4 @@ void LuaButton::triggerAction()
|
||||
|
||||
LuaButton::~LuaButton()
|
||||
{
|
||||
if(button->GetParentWindow())
|
||||
button->GetParentWindow()->RemoveComponent(button);
|
||||
delete button;
|
||||
}
|
@ -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();
|
||||
};
|
82
src/cat/LuaComponent.cpp
Normal file
82
src/cat/LuaComponent.cpp
Normal file
@ -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;
|
||||
}
|
33
src/cat/LuaComponent.h
Normal file
33
src/cat/LuaComponent.h
Normal file
@ -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();
|
||||
};
|
@ -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;
|
||||
}
|
@ -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();
|
||||
};
|
@ -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)
|
||||
|
115
src/cat/LuaTextbox.cpp
Normal file
115
src/cat/LuaTextbox.cpp
Normal file
@ -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()
|
||||
{
|
||||
}
|
33
src/cat/LuaTextbox.h
Normal file
33
src/cat/LuaTextbox.h
Normal file
@ -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();
|
||||
};
|
@ -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)
|
||||
|
Reference in New Issue
Block a user