[lua] New Rectangle(point, size) constructor

This commit is contained in:
David Capello 2022-12-21 13:01:03 -03:00
parent 4d880bd5b7
commit 909ebd6dcb
2 changed files with 22 additions and 8 deletions

View File

@ -23,13 +23,13 @@ namespace {
gfx::Rect Rectangle_new(lua_State* L, int index)
{
gfx::Rect rc(0, 0, 0, 0);
// Copy other rectangle
if (auto rc2 = may_get_obj<gfx::Rect>(L, index)) {
rc = *rc2;
return *rc2;
}
// Convert { x, y, width, height } into a Rectangle
else if (lua_istable(L, index)) {
gfx::Rect rc(0, 0, 0, 0);
const int type = lua_getfield(L, index, "x");
if (VALID_LUATYPE(type)) {
lua_getfield(L, index, "y");
@ -53,15 +53,23 @@ gfx::Rect Rectangle_new(lua_State* L, int index)
rc.h = lua_tointeger(L, -1);
lua_pop(L, 4);
}
}
else {
rc.x = lua_tointeger(L, index);
rc.y = lua_tointeger(L, index+1);
rc.w = lua_tointeger(L, index+2);
rc.h = lua_tointeger(L, index+3);
}
return rc;
}
else if (index > 0) {
if (lua_gettop(L) >= index+1) {
const auto pt = may_get_obj<gfx::Point>(L, index);
const auto sz = may_get_obj<gfx::Size>(L, index+1);
if (pt && sz)
return gfx::Rect(*pt, *sz);
}
return gfx::Rect(lua_tointeger(L, index),
lua_tointeger(L, index + 1),
lua_tointeger(L, index + 2),
lua_tointeger(L, index + 3));
}
else
return gfx::Rect();
}
int Rectangle_new(lua_State* L)
{

View File

@ -60,6 +60,12 @@ assert(rc.y == 7)
assert(rc.width == 8)
assert(rc.height == 9)
rc = Rectangle(Point(2, 3), Size(4, 5))
assert(rc.x == 2)
assert(rc.y == 3)
assert(rc.width == 4)
assert(rc.height == 5)
-- Rectangle:contains
local a = Rectangle{x=2, y=3, width=4, height=5}