aseprite/src/app/script/rectangle_class.cpp

228 lines
4.9 KiB
C++
Raw Normal View History

// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
2018-08-20 22:25:08 +08:00
// Copyright (C) 2017-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2018-08-20 22:25:08 +08:00
#include "app/script/luacpp.h"
#include "fmt/format.h"
#include "gfx/point.h"
#include "gfx/rect.h"
namespace app {
2018-08-20 22:25:08 +08:00
namespace script {
namespace {
2018-08-20 22:25:08 +08:00
gfx::Rect Rectangle_new(lua_State* L, int index)
{
2018-08-20 22:25:08 +08:00
gfx::Rect rc(0, 0, 0, 0);
// Copy other rectangle
2018-08-20 22:25:08 +08:00
if (auto rc2 = may_get_obj<gfx::Rect>(L, index)) {
rc = *rc2;
}
// Convert { x, y, width, height } into a Rectangle
2018-08-20 22:25:08 +08:00
else if (lua_istable(L, index)) {
const int type = lua_getfield(L, index, "x");
2020-07-29 04:40:43 +08:00
if (VALID_LUATYPE(type)) {
lua_getfield(L, index, "y");
lua_getfield(L, index, "width");
lua_getfield(L, index, "height");
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 {
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);
}
}
2018-08-20 22:25:08 +08:00
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);
}
2018-08-20 22:25:08 +08:00
return rc;
}
2018-08-20 22:25:08 +08:00
int Rectangle_new(lua_State* L)
{
push_obj(L, Rectangle_new(L, 1));
return 1;
}
int Rectangle_gc(lua_State* L)
{
get_obj<gfx::Rect>(L, 1)->~RectT();
return 0;
}
2018-09-05 20:41:44 +08:00
int Rectangle_eq(lua_State* L)
{
const auto a = get_obj<gfx::Rect>(L, 1);
const auto b = get_obj<gfx::Rect>(L, 2);
lua_pushboolean(L, *a == *b);
return 1;
}
int Rectangle_tostring(lua_State* L)
{
const auto rc = get_obj<gfx::Rect>(L, 1);
lua_pushstring(L, fmt::format("Rectangle{{ x={}, y={}, width={}, height={} }}",
rc->x, rc->y, rc->w, rc->h).c_str());
return 1;
}
int Rectangle_contains(lua_State* L)
{
const auto a = get_obj<gfx::Rect>(L, 1);
const auto b = get_obj<gfx::Rect>(L, 2);
lua_pushboolean(L, a->contains(*b));
return 1;
}
int Rectangle_intersects(lua_State* L)
{
const auto a = get_obj<gfx::Rect>(L, 1);
const auto b = get_obj<gfx::Rect>(L, 2);
lua_pushboolean(L, a->intersects(*b));
return 1;
}
int Rectangle_union(lua_State* L)
{
const auto a = get_obj<gfx::Rect>(L, 1);
const auto b = get_obj<gfx::Rect>(L, 2);
push_obj(L, a->createUnion(*b));
return 1;
}
int Rectangle_intersect(lua_State* L)
{
const auto a = get_obj<gfx::Rect>(L, 1);
const auto b = get_obj<gfx::Rect>(L, 2);
push_obj(L, a->createIntersection(*b));
return 1;
}
2018-08-20 22:25:08 +08:00
int Rectangle_get_x(lua_State* L)
{
2018-08-20 22:25:08 +08:00
const auto rc = get_obj<gfx::Rect>(L, 1);
lua_pushinteger(L, rc->x);
return 1;
}
2018-08-20 22:25:08 +08:00
int Rectangle_get_y(lua_State* L)
{
2018-08-20 22:25:08 +08:00
const auto rc = get_obj<gfx::Rect>(L, 1);
lua_pushinteger(L, rc->y);
return 1;
}
2018-08-20 22:25:08 +08:00
int Rectangle_get_width(lua_State* L)
{
2018-08-20 22:25:08 +08:00
const auto rc = get_obj<gfx::Rect>(L, 1);
lua_pushinteger(L, rc->w);
return 1;
}
2018-08-20 22:25:08 +08:00
int Rectangle_get_height(lua_State* L)
{
2018-08-20 22:25:08 +08:00
const auto rc = get_obj<gfx::Rect>(L, 1);
lua_pushinteger(L, rc->h);
return 1;
}
2018-08-20 22:25:08 +08:00
int Rectangle_set_x(lua_State* L)
{
2018-08-20 22:25:08 +08:00
auto rc = get_obj<gfx::Rect>(L, 1);
rc->x = lua_tointeger(L, 2);
return 0;
}
2018-08-20 22:25:08 +08:00
int Rectangle_set_y(lua_State* L)
{
2018-08-20 22:25:08 +08:00
auto rc = get_obj<gfx::Rect>(L, 1);
rc->y = lua_tointeger(L, 2);
return 0;
}
2018-08-20 22:25:08 +08:00
int Rectangle_set_width(lua_State* L)
{
2018-08-20 22:25:08 +08:00
auto rc = get_obj<gfx::Rect>(L, 1);
rc->w = lua_tointeger(L, 2);
return 0;
}
2018-08-20 22:25:08 +08:00
int Rectangle_set_height(lua_State* L)
{
2018-08-20 22:25:08 +08:00
auto rc = get_obj<gfx::Rect>(L, 1);
rc->h = lua_tointeger(L, 2);
return 0;
}
2018-09-01 02:05:27 +08:00
int Rectangle_get_isEmpty(lua_State* L)
{
const auto rc = get_obj<gfx::Rect>(L, 1);
lua_pushboolean(L, rc->isEmpty());
return 1;
}
2018-08-20 22:25:08 +08:00
const luaL_Reg Rectangle_methods[] = {
{ "__gc", Rectangle_gc },
2018-09-05 20:41:44 +08:00
{ "__eq", Rectangle_eq },
{ "__tostring", Rectangle_tostring },
{ "__band", Rectangle_intersect },
{ "__bor", Rectangle_union },
{ "contains", Rectangle_contains },
{ "intersects", Rectangle_intersects },
{ "union", Rectangle_union },
{ "intersect", Rectangle_intersect },
2018-08-20 22:25:08 +08:00
{ nullptr, nullptr }
};
2018-08-20 22:25:08 +08:00
const Property Rectangle_properties[] = {
{ "x", Rectangle_get_x, Rectangle_set_x },
{ "y", Rectangle_get_y, Rectangle_set_y },
{ "width", Rectangle_get_width, Rectangle_set_width },
{ "height", Rectangle_get_height, Rectangle_set_height },
2018-09-01 02:05:27 +08:00
{ "isEmpty", Rectangle_get_isEmpty, nullptr },
2018-08-20 22:25:08 +08:00
{ nullptr, nullptr, nullptr }
};
} // anonymous namespace
2018-08-20 22:25:08 +08:00
DEF_MTNAME(gfx::Rect);
2018-08-20 22:25:08 +08:00
void register_rect_class(lua_State* L)
{
2018-08-20 22:25:08 +08:00
using Rectangle = gfx::Rect;
REG_CLASS(L, Rectangle);
REG_CLASS_NEW(L, Rectangle);
REG_CLASS_PROPERTIES(L, Rectangle);
}
2018-08-20 22:25:08 +08:00
gfx::Rect convert_args_into_rect(lua_State* L, int index)
{
2018-08-20 22:25:08 +08:00
return Rectangle_new(L, index);
}
2018-08-20 22:25:08 +08:00
} // namespace script
} // namespace app