mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-28 05:37:19 +00:00
[lua] Add some operations (add,sub,mul,etc.) to points/size/rectangles
This commit is contained in:
parent
d43ff9b32b
commit
1748e4f59c
@ -13,6 +13,8 @@
|
||||
#include "fmt/format.h"
|
||||
#include "gfx/point.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace app {
|
||||
namespace script {
|
||||
|
||||
@ -93,6 +95,73 @@ int Point_unm(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_add(lua_State* L)
|
||||
{
|
||||
gfx::Point result(0, 0);
|
||||
if (lua_isuserdata(L, 1))
|
||||
result += *get_obj<gfx::Point>(L, 1);
|
||||
else
|
||||
result += lua_tointeger(L, 1);
|
||||
if (lua_isuserdata(L, 2))
|
||||
result += *get_obj<gfx::Point>(L, 2);
|
||||
else
|
||||
result += lua_tointeger(L, 2);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_sub(lua_State* L)
|
||||
{
|
||||
gfx::Point result = *get_obj<gfx::Point>(L, 1);
|
||||
if (lua_isuserdata(L, 2))
|
||||
result -= *get_obj<gfx::Point>(L, 2);
|
||||
else
|
||||
result -= lua_tointeger(L, 2);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_mul(lua_State* L)
|
||||
{
|
||||
gfx::Point result = *get_obj<gfx::Point>(L, 1);
|
||||
result *= lua_tointeger(L, 2);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_div(lua_State* L)
|
||||
{
|
||||
gfx::Point result = *get_obj<gfx::Point>(L, 1);
|
||||
const int value = lua_tointeger(L, 2);
|
||||
if (value == 0)
|
||||
return luaL_error(L, "attempt to divide by zero");
|
||||
result /= value;
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_mod(lua_State* L)
|
||||
{
|
||||
gfx::Point result = *get_obj<gfx::Point>(L, 1);
|
||||
const int value = lua_tointeger(L, 2);
|
||||
if (value == 0)
|
||||
return luaL_error(L, "attempt to divide by zero");
|
||||
result.x %= value;
|
||||
result.y %= value;
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_pow(lua_State* L)
|
||||
{
|
||||
gfx::Point result = *get_obj<gfx::Point>(L, 1);
|
||||
const int value = lua_tointeger(L, 2);
|
||||
result.x = std::pow(result.x, value);
|
||||
result.y = std::pow(result.y, value);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Point_get_x(lua_State* L)
|
||||
{
|
||||
const auto pt = get_obj<gfx::Point>(L, 1);
|
||||
@ -126,6 +195,13 @@ const luaL_Reg Point_methods[] = {
|
||||
{ "__eq", Point_eq },
|
||||
{ "__tostring", Point_tostring },
|
||||
{ "__unm", Point_unm },
|
||||
{ "__add", Point_add },
|
||||
{ "__sub", Point_sub },
|
||||
{ "__mul", Point_mul },
|
||||
{ "__div", Point_div },
|
||||
{ "__mod", Point_mod },
|
||||
{ "__pow", Point_pow },
|
||||
{ "__idiv", Point_div },
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
|
||||
|
@ -189,6 +189,8 @@ const luaL_Reg Rectangle_methods[] = {
|
||||
{ "__gc", Rectangle_gc },
|
||||
{ "__eq", Rectangle_eq },
|
||||
{ "__tostring", Rectangle_tostring },
|
||||
{ "__band", Rectangle_intersect },
|
||||
{ "__bor", Rectangle_union },
|
||||
{ "contains", Rectangle_contains },
|
||||
{ "intersects", Rectangle_intersects },
|
||||
{ "union", Rectangle_union },
|
||||
|
@ -13,6 +13,8 @@
|
||||
#include "fmt/format.h"
|
||||
#include "gfx/size.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace app {
|
||||
namespace script {
|
||||
|
||||
@ -79,6 +81,80 @@ int Size_tostring(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_unm(lua_State* L)
|
||||
{
|
||||
const auto sz = get_obj<gfx::Size>(L, 1);
|
||||
push_obj(L, -(*sz));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_add(lua_State* L)
|
||||
{
|
||||
gfx::Size result(0, 0);
|
||||
if (lua_isuserdata(L, 1))
|
||||
result += *get_obj<gfx::Size>(L, 1);
|
||||
else
|
||||
result += lua_tointeger(L, 1);
|
||||
if (lua_isuserdata(L, 2))
|
||||
result += *get_obj<gfx::Size>(L, 2);
|
||||
else
|
||||
result += lua_tointeger(L, 2);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_sub(lua_State* L)
|
||||
{
|
||||
gfx::Size result = *get_obj<gfx::Size>(L, 1);
|
||||
if (lua_isuserdata(L, 2))
|
||||
result -= *get_obj<gfx::Size>(L, 2);
|
||||
else
|
||||
result -= lua_tointeger(L, 2);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_mul(lua_State* L)
|
||||
{
|
||||
gfx::Size result = *get_obj<gfx::Size>(L, 1);
|
||||
result *= lua_tointeger(L, 2);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_div(lua_State* L)
|
||||
{
|
||||
gfx::Size result = *get_obj<gfx::Size>(L, 1);
|
||||
const int value = lua_tointeger(L, 2);
|
||||
if (value == 0)
|
||||
return luaL_error(L, "attempt to divide by zero");
|
||||
result /= value;
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_mod(lua_State* L)
|
||||
{
|
||||
gfx::Size result = *get_obj<gfx::Size>(L, 1);
|
||||
const int value = lua_tointeger(L, 2);
|
||||
if (value == 0)
|
||||
return luaL_error(L, "attempt to divide by zero");
|
||||
result.w %= value;
|
||||
result.h %= value;
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_pow(lua_State* L)
|
||||
{
|
||||
gfx::Size result = *get_obj<gfx::Size>(L, 1);
|
||||
const int value = lua_tointeger(L, 2);
|
||||
result.w = std::pow(result.w, value);
|
||||
result.h = std::pow(result.h, value);
|
||||
push_obj(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int Size_get_width(lua_State* L)
|
||||
{
|
||||
const auto sz = get_obj<gfx::Size>(L, 1);
|
||||
@ -115,6 +191,14 @@ const luaL_Reg Size_methods[] = {
|
||||
{ "__gc", Size_gc },
|
||||
{ "__eq", Size_eq },
|
||||
{ "__tostring", Size_tostring },
|
||||
{ "__unm", Size_unm },
|
||||
{ "__add", Size_add },
|
||||
{ "__sub", Size_sub },
|
||||
{ "__mul", Size_mul },
|
||||
{ "__div", Size_div },
|
||||
{ "__mod", Size_mod },
|
||||
{ "__pow", Size_pow },
|
||||
{ "__idiv", Size_div },
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user