lua: Add support to construct Point/Size/Rectangles from array of integers

This commit is contained in:
David Capello 2019-07-19 18:39:06 -03:00
parent 1ef67cada4
commit ccef22f187
3 changed files with 58 additions and 21 deletions

View File

@ -25,14 +25,25 @@ gfx::Point Point_new(lua_State* L, int index)
if (auto pt2 = may_get_obj<gfx::Point>(L, index)) { if (auto pt2 = may_get_obj<gfx::Point>(L, index)) {
pt = *pt2; pt = *pt2;
} }
// Convert { x, y } into a Point // Convert {x=int,y=int} or {int,int} into a Point
else if (lua_istable(L, index)) { else if (lua_istable(L, index)) {
lua_getfield(L, index, "x"); const int type = lua_getfield(L, index, "x");
if (type != LUA_TNONE &&
type != LUA_TNIL) {
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
pt.x = lua_tointeger(L, -2); pt.x = lua_tointeger(L, -2);
pt.y = lua_tointeger(L, -1); pt.y = lua_tointeger(L, -1);
lua_pop(L, 2); lua_pop(L, 2);
} }
else {
lua_pop(L, 1);
lua_geti(L, index, 1);
lua_geti(L, index, 2);
pt.x = lua_tointeger(L, -2);
pt.y = lua_tointeger(L, -1);
lua_pop(L, 2);
}
}
else { else {
pt.x = lua_tointeger(L, index); pt.x = lua_tointeger(L, index);
pt.y = lua_tointeger(L, index+1); pt.y = lua_tointeger(L, index+1);

View File

@ -28,7 +28,9 @@ gfx::Rect Rectangle_new(lua_State* L, int index)
} }
// 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)) {
lua_getfield(L, index, "x"); const int type = lua_getfield(L, index, "x");
if (type != LUA_TNONE &&
type != LUA_TNIL) {
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
lua_getfield(L, index, "width"); lua_getfield(L, index, "width");
lua_getfield(L, index, "height"); lua_getfield(L, index, "height");
@ -38,6 +40,19 @@ 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);
} }
else {
lua_pop(L, 1);
lua_geti(L, index, 1);
lua_geti(L, index, 2);
lua_geti(L, index, 3);
lua_geti(L, index, 4);
rc.x = lua_tointeger(L, -4);
rc.y = lua_tointeger(L, -3);
rc.w = lua_tointeger(L, -2);
rc.h = lua_tointeger(L, -1);
lua_pop(L, 4);
}
}
else { else {
rc.x = lua_tointeger(L, index); rc.x = lua_tointeger(L, index);
rc.y = lua_tointeger(L, index+1); rc.y = lua_tointeger(L, index+1);

View File

@ -25,14 +25,25 @@ gfx::Size Size_new(lua_State* L, int index)
if (auto sz2 = may_get_obj<gfx::Size>(L, index)) { if (auto sz2 = may_get_obj<gfx::Size>(L, index)) {
sz = *sz2; sz = *sz2;
} }
// Convert { width, height } into a Size // Convert {x=int,y=int} or {int,int} into a Size
else if (lua_istable(L, index)) { else if (lua_istable(L, index)) {
lua_getfield(L, index, "width"); const int type = lua_getfield(L, index, "width");
if (type != LUA_TNONE &&
type != LUA_TNIL) {
lua_getfield(L, index, "height"); lua_getfield(L, index, "height");
sz.w = lua_tointeger(L, -2); sz.w = lua_tointeger(L, -2);
sz.h = lua_tointeger(L, -1); sz.h = lua_tointeger(L, -1);
lua_pop(L, 2); lua_pop(L, 2);
} }
else {
lua_pop(L, 1);
lua_geti(L, index, 1);
lua_geti(L, index, 2);
sz.w = lua_tointeger(L, -2);
sz.h = lua_tointeger(L, -1);
lua_pop(L, 2);
}
}
else { else {
sz.w = lua_tointeger(L, index); sz.w = lua_tointeger(L, index);
sz.h = lua_tointeger(L, index+1); sz.h = lua_tointeger(L, index+1);