[lua] Add Rectangle.origin and Rectangle.size properties

This commit is contained in:
David Capello 2022-12-20 18:04:51 -03:00
parent f50bbff5d4
commit 4d880bd5b7
2 changed files with 55 additions and 3 deletions

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2017-2018 David Capello // Copyright (C) 2017-2018 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
@ -9,10 +9,12 @@
#include "config.h" #include "config.h"
#endif #endif
#include "app/script/engine.h"
#include "app/script/luacpp.h" #include "app/script/luacpp.h"
#include "fmt/format.h" #include "fmt/format.h"
#include "gfx/point.h" #include "gfx/point.h"
#include "gfx/rect.h" #include "gfx/rect.h"
#include "gfx/size.h"
namespace app { namespace app {
namespace script { namespace script {
@ -92,8 +94,12 @@ int Rectangle_tostring(lua_State* L)
int Rectangle_contains(lua_State* L) int Rectangle_contains(lua_State* L)
{ {
const auto a = get_obj<gfx::Rect>(L, 1); const auto a = get_obj<gfx::Rect>(L, 1);
const auto b = get_obj<gfx::Rect>(L, 2); if (const auto b = may_get_obj<gfx::Rect>(L, 2))
lua_pushboolean(L, a->contains(*b)); lua_pushboolean(L, a->contains(*b));
else if (const auto b = may_get_obj<gfx::Point>(L, 2))
lua_pushboolean(L, a->contains(*b));
else
lua_pushboolean(L, false);
return 1; return 1;
} }
@ -149,6 +155,20 @@ int Rectangle_get_height(lua_State* L)
return 1; return 1;
} }
int Rectangle_get_origin(lua_State* L)
{
const auto rc = get_obj<gfx::Rect>(L, 1);
push_obj(L, rc->origin());
return 1;
}
int Rectangle_get_size(lua_State* L)
{
const auto rc = get_obj<gfx::Rect>(L, 1);
push_obj(L, rc->size());
return 1;
}
int Rectangle_set_x(lua_State* L) int Rectangle_set_x(lua_State* L)
{ {
auto rc = get_obj<gfx::Rect>(L, 1); auto rc = get_obj<gfx::Rect>(L, 1);
@ -177,6 +197,22 @@ int Rectangle_set_height(lua_State* L)
return 0; return 0;
} }
int Rectangle_set_origin(lua_State* L)
{
const auto rc = get_obj<gfx::Rect>(L, 1);
const auto pt = convert_args_into_point(L, 2);
rc->setOrigin(pt);
return 0;
}
int Rectangle_set_size(lua_State* L)
{
const auto rc = get_obj<gfx::Rect>(L, 1);
const auto sz = convert_args_into_size(L, 2);
rc->setSize(sz);
return 0;
}
int Rectangle_get_isEmpty(lua_State* L) int Rectangle_get_isEmpty(lua_State* L)
{ {
const auto rc = get_obj<gfx::Rect>(L, 1); const auto rc = get_obj<gfx::Rect>(L, 1);
@ -202,6 +238,8 @@ const Property Rectangle_properties[] = {
{ "y", Rectangle_get_y, Rectangle_set_y }, { "y", Rectangle_get_y, Rectangle_set_y },
{ "width", Rectangle_get_width, Rectangle_set_width }, { "width", Rectangle_get_width, Rectangle_set_width },
{ "height", Rectangle_get_height, Rectangle_set_height }, { "height", Rectangle_get_height, Rectangle_set_height },
{ "origin", Rectangle_get_origin, Rectangle_set_origin },
{ "size", Rectangle_get_size, Rectangle_set_size },
{ "isEmpty", Rectangle_get_isEmpty, nullptr }, { "isEmpty", Rectangle_get_isEmpty, nullptr },
{ nullptr, nullptr, nullptr } { nullptr, nullptr, nullptr }
}; };

View File

@ -16,6 +16,8 @@ assert(rc.x == 1)
assert(rc.y == 2) assert(rc.y == 2)
assert(rc.width == 3) assert(rc.width == 3)
assert(rc.height == 4) assert(rc.height == 4)
assert(rc.origin == Point(1, 2))
assert(rc.size == Size(3, 4))
assert(not rc.isEmpty) assert(not rc.isEmpty)
assert("Rectangle{ x=1, y=2, width=3, height=4 }" == tostring(rc)) assert("Rectangle{ x=1, y=2, width=3, height=4 }" == tostring(rc))
@ -34,6 +36,18 @@ assert(rc.y == 6)
assert(rc.width == 7) assert(rc.width == 7)
assert(rc.height == 8) assert(rc.height == 8)
rc.origin = Point(10, 12)
assert(rc.x == 10)
assert(rc.y == 12)
assert(rc.width == 7)
assert(rc.height == 8)
rc.size = Size(32, 64)
assert(rc.x == 10)
assert(rc.y == 12)
assert(rc.width == 32)
assert(rc.height == 64)
rc = Rectangle{x=2, y=3, width=4, height=5} rc = Rectangle{x=2, y=3, width=4, height=5}
assert(rc.x == 2) assert(rc.x == 2)
assert(rc.y == 3) assert(rc.y == 3)