2017-08-12 04:22:28 +08:00
|
|
|
// Aseprite
|
2018-08-20 22:25:08 +08:00
|
|
|
// Copyright (C) 2017-2018 David Capello
|
2017-08-12 04:22:28 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2017-08-12 04:22:28 +08:00
|
|
|
#include "gfx/rect.h"
|
|
|
|
|
|
|
|
namespace app {
|
2018-08-20 22:25:08 +08:00
|
|
|
namespace script {
|
2017-08-12 04:22:28 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
gfx::Rect Rectangle_new(lua_State* L, int index)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
gfx::Rect rc(0, 0, 0, 0);
|
2017-08-12 04:22:28 +08:00
|
|
|
// Copy other rectangle
|
2018-08-20 22:25:08 +08:00
|
|
|
if (auto rc2 = may_get_obj<gfx::Rect>(L, index)) {
|
|
|
|
rc = *rc2;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
// Convert { x, y, width, height } into a Rectangle
|
2018-08-20 22:25:08 +08:00
|
|
|
else if (lua_istable(L, index)) {
|
|
|
|
lua_getfield(L, index, "x");
|
|
|
|
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);
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
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);
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
2018-08-20 22:25:08 +08:00
|
|
|
return rc;
|
|
|
|
}
|
2017-08-12 04:22:28 +08:00
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_new(lua_State* L)
|
|
|
|
{
|
|
|
|
push_obj(L, Rectangle_new(L, 1));
|
|
|
|
return 1;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-09-05 03:10:32 +08:00
|
|
|
int Rectangle_gc(lua_State* L)
|
|
|
|
{
|
|
|
|
get_obj<gfx::Rect>(L, 1)->~RectT();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_get_x(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
const auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
lua_pushinteger(L, rc->x);
|
|
|
|
return 1;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_get_y(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
const auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
lua_pushinteger(L, rc->y);
|
|
|
|
return 1;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_get_width(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
const auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
lua_pushinteger(L, rc->w);
|
|
|
|
return 1;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_get_height(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
const auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
lua_pushinteger(L, rc->h);
|
|
|
|
return 1;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_set_x(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
rc->x = lua_tointeger(L, 2);
|
|
|
|
return 0;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_set_y(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
rc->y = lua_tointeger(L, 2);
|
|
|
|
return 0;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_set_width(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
rc->w = lua_tointeger(L, 2);
|
|
|
|
return 0;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
int Rectangle_set_height(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
auto rc = get_obj<gfx::Rect>(L, 1);
|
|
|
|
rc->h = lua_tointeger(L, 2);
|
|
|
|
return 0;
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
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[] = {
|
2018-09-05 03:10:32 +08:00
|
|
|
{ "__gc", Rectangle_gc },
|
2018-08-20 22:25:08 +08:00
|
|
|
{ nullptr, nullptr }
|
2017-08-12 04:22:28 +08:00
|
|
|
};
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
const Property Rectangle_properties[] = {
|
2017-08-12 04:22:28 +08:00
|
|
|
{ "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 }
|
2017-08-12 04:22:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
DEF_MTNAME(gfx::Rect);
|
2017-08-12 04:22:28 +08:00
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
void register_rect_class(lua_State* L)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
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);
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
gfx::Rect convert_args_into_rect(lua_State* L, int index)
|
2017-08-12 04:22:28 +08:00
|
|
|
{
|
2018-08-20 22:25:08 +08:00
|
|
|
return Rectangle_new(L, index);
|
2017-08-12 04:22:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-20 22:25:08 +08:00
|
|
|
} // namespace script
|
2017-08-12 04:22:28 +08:00
|
|
|
} // namespace app
|