From c945996fd6987c0f5836ea74f040f7349fd1c3fd Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 29 Mar 2019 15:54:20 -0300 Subject: [PATCH] lua: Add tostring support for Point/Size/Rectangle --- src/app/script/point_class.cpp | 11 +++++++++++ src/app/script/rectangle_class.cpp | 11 +++++++++++ src/app/script/size_class.cpp | 11 +++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/app/script/point_class.cpp b/src/app/script/point_class.cpp index d9c51647f..aaf6bfec0 100644 --- a/src/app/script/point_class.cpp +++ b/src/app/script/point_class.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2017-2018 David Capello // // This program is distributed under the terms of @@ -9,6 +10,7 @@ #endif #include "app/script/luacpp.h" +#include "fmt/format.h" #include "gfx/point.h" namespace app { @@ -58,6 +60,14 @@ int Point_eq(lua_State* L) return 1; } +int Point_tostring(lua_State* L) +{ + const auto pt = get_obj(L, 1); + lua_pushstring(L, fmt::format("Point{{ x={}, y={} }}", + pt->x, pt->y).c_str()); + return 1; +} + int Point_get_x(lua_State* L) { const auto pt = get_obj(L, 1); @@ -89,6 +99,7 @@ int Point_set_y(lua_State* L) const luaL_Reg Point_methods[] = { { "__gc", Point_gc }, { "__eq", Point_eq }, + { "__tostring", Point_tostring }, { nullptr, nullptr } }; diff --git a/src/app/script/rectangle_class.cpp b/src/app/script/rectangle_class.cpp index 4bca7a300..70940b3cd 100644 --- a/src/app/script/rectangle_class.cpp +++ b/src/app/script/rectangle_class.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2017-2018 David Capello // // This program is distributed under the terms of @@ -9,6 +10,7 @@ #endif #include "app/script/luacpp.h" +#include "fmt/format.h" #include "gfx/point.h" #include "gfx/rect.h" @@ -65,6 +67,14 @@ int Rectangle_eq(lua_State* L) return 1; } +int Rectangle_tostring(lua_State* L) +{ + const auto rc = get_obj(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(L, 1); @@ -163,6 +173,7 @@ int Rectangle_get_isEmpty(lua_State* L) const luaL_Reg Rectangle_methods[] = { { "__gc", Rectangle_gc }, { "__eq", Rectangle_eq }, + { "__tostring", Rectangle_tostring }, { "contains", Rectangle_contains }, { "intersects", Rectangle_intersects }, { "union", Rectangle_union }, diff --git a/src/app/script/size_class.cpp b/src/app/script/size_class.cpp index 66a55d808..160bbc642 100644 --- a/src/app/script/size_class.cpp +++ b/src/app/script/size_class.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2019 Igara Studio S.A. // Copyright (C) 2017-2018 David Capello // // This program is distributed under the terms of @@ -9,6 +10,7 @@ #endif #include "app/script/luacpp.h" +#include "fmt/format.h" #include "gfx/size.h" namespace app { @@ -58,6 +60,14 @@ int Size_eq(lua_State* L) return 1; } +int Size_tostring(lua_State* L) +{ + const auto sz = get_obj(L, 1); + lua_pushstring(L, fmt::format("Size{{ width={}, height={} }}", + sz->w, sz->h).c_str()); + return 1; +} + int Size_get_width(lua_State* L) { const auto sz = get_obj(L, 1); @@ -93,6 +103,7 @@ int Size_set_height(lua_State* L) const luaL_Reg Size_methods[] = { { "__gc", Size_gc }, { "__eq", Size_eq }, + { "__tostring", Size_tostring }, { nullptr, nullptr } };