More interface API
This commit is contained in:
parent
e8628274ad
commit
355c43723f
92
src/cat/LuaLabel.cpp
Normal file
92
src/cat/LuaLabel.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
extern "C"
|
||||
{
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
#include "LuaLabel.h"
|
||||
#include "interface/Label.h"
|
||||
|
||||
const char LuaLabel::className[] = "Label";
|
||||
|
||||
#define method(class, name) {#name, &class::name}
|
||||
Luna<LuaLabel>::RegType LuaLabel::methods[] = {
|
||||
method(LuaLabel, text),
|
||||
method(LuaLabel, position),
|
||||
method(LuaLabel, size),
|
||||
{0, 0}
|
||||
};
|
||||
|
||||
LuaLabel::LuaLabel(lua_State * l)
|
||||
{
|
||||
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, "");
|
||||
|
||||
label = new ui::Label(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text);
|
||||
}
|
||||
|
||||
int LuaLabel::text(lua_State * l)
|
||||
{
|
||||
int args = lua_gettop(l);
|
||||
if(args)
|
||||
{
|
||||
luaL_checktype(l, 1, LUA_TSTRING);
|
||||
label->SetText(lua_tostring(l, 1));
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
lua_pushstring(l, label->GetText().c_str());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
30
src/cat/LuaLabel.h
Normal file
30
src/cat/LuaLabel.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
extern "C" {
|
||||
#include "lua.h"
|
||||
#include "lauxlib.h"
|
||||
#include "lualib.h"
|
||||
}
|
||||
|
||||
#include "LuaLuna.h"
|
||||
|
||||
namespace ui
|
||||
{
|
||||
class Label;
|
||||
}
|
||||
|
||||
class LuaLabel
|
||||
{
|
||||
ui::Label * label;
|
||||
lua_State * l;
|
||||
int text(lua_State * l);
|
||||
int position(lua_State * l);
|
||||
int size(lua_State * l);
|
||||
public:
|
||||
static const char className[];
|
||||
static Luna<LuaLabel>::RegType methods[];
|
||||
|
||||
ui::Label * GetComponent() { return label; }
|
||||
LuaLabel(lua_State * l);
|
||||
~LuaLabel();
|
||||
};
|
@ -75,6 +75,33 @@ public:
|
||||
return ud->pT; // pointer to T object
|
||||
}
|
||||
|
||||
static void * tryGet(lua_State * L, int narg)
|
||||
{
|
||||
if(checkType(L, narg, T::className))
|
||||
{
|
||||
userdataType *ud = static_cast<userdataType*>(luaL_checkudata(L, narg, T::className));
|
||||
if(!ud)
|
||||
luaL_typerror(L, narg, T::className);
|
||||
return ud; // pointer to T object
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static bool checkType (lua_State * L, int idx, const char *name)
|
||||
{
|
||||
// returns true if a userdata is of a certain type
|
||||
int res;
|
||||
if (lua_type(L, idx) != LUA_TUSERDATA) return false;
|
||||
lua_getmetatable(L, idx);
|
||||
luaL_newmetatable (L, name);
|
||||
res = lua_equal(L, -2, -1);
|
||||
lua_pop(L, 2); // pop both tables (metatables) off
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline T * get(void * userData)
|
||||
{
|
||||
return ((userdataType*)userData)->pT;
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "LuaLuna.h"
|
||||
#include "LuaWindow.h"
|
||||
#include "LuaButton.h"
|
||||
#include "LuaLabel.h"
|
||||
|
||||
#ifdef WIN
|
||||
#include <direct.h>
|
||||
@ -284,14 +285,17 @@ void LuaScriptInterface::initInterfaceAPI()
|
||||
luaL_register(l, "interface", interfaceAPIMethods);
|
||||
Luna<LuaWindow>::Register(l);
|
||||
Luna<LuaButton>::Register(l);
|
||||
Luna<LuaLabel>::Register(l);
|
||||
}
|
||||
|
||||
int LuaScriptInterface::interface_addComponent(lua_State * l)
|
||||
{
|
||||
void * luaComponent = NULL;
|
||||
ui::Component * component = NULL;
|
||||
if(luaComponent = luaL_checkudata(l, 1, "Button"))
|
||||
if(luaComponent = Luna<LuaButton>::tryGet(l, 1))
|
||||
component = Luna<LuaButton>::get(luaComponent)->GetComponent();
|
||||
else if(luaComponent = Luna<LuaLabel>::tryGet(l, 1))
|
||||
component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
|
||||
else
|
||||
luaL_typerror(l, 1, "Component");
|
||||
if(luacon_ci->Window && component)
|
||||
|
@ -8,7 +8,9 @@ extern "C"
|
||||
#include <iostream>
|
||||
#include "LuaWindow.h"
|
||||
#include "LuaButton.h"
|
||||
#include "LuaLabel.h"
|
||||
#include "interface/Button.h"
|
||||
#include "interface/Label.h"
|
||||
#include "interface/Window.h"
|
||||
|
||||
const char LuaWindow::className[] = "Window";
|
||||
@ -91,8 +93,10 @@ int LuaWindow::addComponent(lua_State * l)
|
||||
{
|
||||
void * luaComponent = NULL;
|
||||
ui::Component * component = NULL;
|
||||
if(luaComponent = luaL_checkudata(l, 1, "Button"))
|
||||
if(luaComponent = Luna<LuaButton>::tryGet(l, 1))
|
||||
component = Luna<LuaButton>::get(luaComponent)->GetComponent();
|
||||
else if(luaComponent = Luna<LuaLabel>::tryGet(l, 1))
|
||||
component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
|
||||
else
|
||||
luaL_typerror(l, 1, "Component");
|
||||
if(component)
|
||||
|
Reference in New Issue
Block a user