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

View File

@ -60,6 +60,12 @@ assert(rc.y == 7)
assert(rc.width == 8) assert(rc.width == 8)
assert(rc.height == 9) 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 -- Rectangle:contains
local a = Rectangle{x=2, y=3, width=4, height=5} local a = Rectangle{x=2, y=3, width=4, height=5}