[lua] Add short field names (fix #3815, fix #3816)

This commit is contained in:
David Capello 2023-04-19 13:41:11 -03:00
parent 6cbde57470
commit 622b02294a
9 changed files with 113 additions and 58 deletions

View File

@ -510,7 +510,7 @@ int App_get_uiScale(lua_State* L)
return 1; return 1;
} }
int App_get_activeSprite(lua_State* L) int App_get_sprite(lua_State* L)
{ {
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
Doc* doc = ctx->activeDocument(); Doc* doc = ctx->activeDocument();
@ -521,7 +521,7 @@ int App_get_activeSprite(lua_State* L)
return 1; return 1;
} }
int App_get_activeLayer(lua_State* L) int App_get_layer(lua_State* L)
{ {
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite(); Site site = ctx->activeSite();
@ -532,7 +532,7 @@ int App_get_activeLayer(lua_State* L)
return 1; return 1;
} }
int App_get_activeFrame(lua_State* L) int App_get_frame(lua_State* L)
{ {
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite(); Site site = ctx->activeSite();
@ -543,7 +543,7 @@ int App_get_activeFrame(lua_State* L)
return 1; return 1;
} }
int App_get_activeCel(lua_State* L) int App_get_cel(lua_State* L)
{ {
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite(); Site site = ctx->activeSite();
@ -554,7 +554,7 @@ int App_get_activeCel(lua_State* L)
return 1; return 1;
} }
int App_get_activeImage(lua_State* L) int App_get_image(lua_State* L)
{ {
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
Site site = ctx->activeSite(); Site site = ctx->activeSite();
@ -565,7 +565,7 @@ int App_get_activeImage(lua_State* L)
return 1; return 1;
} }
int App_get_activeTag(lua_State* L) int App_get_tag(lua_State* L)
{ {
Tag* tag = nullptr; Tag* tag = nullptr;
@ -657,14 +657,14 @@ int App_get_apiVersion(lua_State* L)
return 1; return 1;
} }
int App_get_activeTool(lua_State* L) int App_get_tool(lua_State* L)
{ {
tools::Tool* tool = App::instance()->activeToolManager()->activeTool(); tools::Tool* tool = App::instance()->activeToolManager()->activeTool();
push_tool(L, tool); push_tool(L, tool);
return 1; return 1;
} }
int App_get_activeBrush(lua_State* L) int App_get_brush(lua_State* L)
{ {
#if ENABLE_UI #if ENABLE_UI
App* app = App::instance(); App* app = App::instance();
@ -688,7 +688,7 @@ int App_get_defaultPalette(lua_State* L)
return 1; return 1;
} }
int App_set_activeSprite(lua_State* L) int App_set_sprite(lua_State* L)
{ {
auto sprite = get_docobj<Sprite>(L, 2); auto sprite = get_docobj<Sprite>(L, 2);
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
@ -697,7 +697,7 @@ int App_set_activeSprite(lua_State* L)
return 0; return 0;
} }
int App_set_activeLayer(lua_State* L) int App_set_layer(lua_State* L)
{ {
auto layer = get_docobj<Layer>(L, 2); auto layer = get_docobj<Layer>(L, 2);
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
@ -705,7 +705,7 @@ int App_set_activeLayer(lua_State* L)
return 0; return 0;
} }
int App_set_activeFrame(lua_State* L) int App_set_frame(lua_State* L)
{ {
const doc::frame_t frame = get_frame_number_from_arg(L, 2); const doc::frame_t frame = get_frame_number_from_arg(L, 2);
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
@ -713,7 +713,7 @@ int App_set_activeFrame(lua_State* L)
return 0; return 0;
} }
int App_set_activeCel(lua_State* L) int App_set_cel(lua_State* L)
{ {
const auto cel = get_docobj<Cel>(L, 2); const auto cel = get_docobj<Cel>(L, 2);
app::Context* ctx = App::instance()->context(); app::Context* ctx = App::instance()->context();
@ -722,7 +722,7 @@ int App_set_activeCel(lua_State* L)
return 0; return 0;
} }
int App_set_activeImage(lua_State* L) int App_set_image(lua_State* L)
{ {
const auto cel = get_image_cel_from_arg(L, 2); const auto cel = get_image_cel_from_arg(L, 2);
if (!cel) if (!cel)
@ -734,14 +734,14 @@ int App_set_activeImage(lua_State* L)
return 0; return 0;
} }
int App_set_activeTool(lua_State* L) int App_set_tool(lua_State* L)
{ {
if (auto tool = get_tool_from_arg(L, 2)) if (auto tool = get_tool_from_arg(L, 2))
App::instance()->activeToolManager()->setSelectedTool(tool); App::instance()->activeToolManager()->setSelectedTool(tool);
return 0; return 0;
} }
int App_set_activeBrush(lua_State* L) int App_set_brush(lua_State* L)
{ {
#if ENABLE_UI #if ENABLE_UI
if (auto brush = get_brush_from_arg(L, 2)) { if (auto brush = get_brush_from_arg(L, 2)) {
@ -773,27 +773,39 @@ const luaL_Reg App_methods[] = {
}; };
const Property App_properties[] = { const Property App_properties[] = {
{ "activeSprite", App_get_activeSprite, App_set_activeSprite }, // Deprecated longer fields
{ "activeLayer", App_get_activeLayer, App_set_activeLayer }, { "activeSprite", App_get_sprite, App_set_sprite },
{ "activeFrame", App_get_activeFrame, App_set_activeFrame }, { "activeLayer", App_get_layer, App_set_layer },
{ "activeCel", App_get_activeCel, App_set_activeCel }, { "activeFrame", App_get_frame, App_set_frame },
{ "activeImage", App_get_activeImage, App_set_activeImage }, { "activeCel", App_get_cel, App_set_cel },
{ "activeTag", App_get_activeTag, nullptr }, { "activeImage", App_get_image, App_set_image },
{ "activeTool", App_get_activeTool, App_set_activeTool }, { "activeTag", App_get_tag, nullptr },
{ "activeBrush", App_get_activeBrush, App_set_activeBrush }, { "activeTool", App_get_tool, App_set_tool },
{ "sprites", App_get_sprites, nullptr }, { "activeBrush", App_get_brush, App_set_brush },
{ "fgColor", App_get_fgColor, App_set_fgColor },
{ "bgColor", App_get_bgColor, App_set_bgColor }, // New shorter fields
{ "version", App_get_version, nullptr }, { "sprite", App_get_sprite, App_set_sprite },
{ "apiVersion", App_get_apiVersion, nullptr }, { "layer", App_get_layer, App_set_layer },
{ "site", App_get_site, nullptr }, { "frame", App_get_frame, App_set_frame },
{ "range", App_get_range, nullptr }, { "cel", App_get_cel, App_set_cel },
{ "isUIAvailable", App_get_isUIAvailable, nullptr }, { "image", App_get_image, App_set_image },
{ "tag", App_get_tag, nullptr },
{ "tool", App_get_tool, App_set_tool },
{ "brush", App_get_brush, App_set_brush },
{ "sprites", App_get_sprites, nullptr },
{ "fgColor", App_get_fgColor, App_set_fgColor },
{ "bgColor", App_get_bgColor, App_set_bgColor },
{ "version", App_get_version, nullptr },
{ "apiVersion", App_get_apiVersion, nullptr },
{ "site", App_get_site, nullptr },
{ "range", App_get_range, nullptr },
{ "isUIAvailable", App_get_isUIAvailable, nullptr },
{ "defaultPalette", App_get_defaultPalette, App_set_defaultPalette }, { "defaultPalette", App_get_defaultPalette, App_set_defaultPalette },
{ "events", App_get_events, nullptr }, { "events", App_get_events, nullptr },
{ "theme", App_get_theme, nullptr }, { "theme", App_get_theme, nullptr },
{ "uiScale", App_get_uiScale, nullptr }, { "uiScale", App_get_uiScale, nullptr },
{ nullptr, nullptr, nullptr } { nullptr, nullptr, nullptr }
}; };
} // anonymous namespace } // anonymous namespace

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2019-2022 Igara Studio S.A. // Copyright (C) 2019-2023 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
@ -30,11 +30,18 @@ gfx::Rect Rectangle_new(lua_State* L, int index)
// Convert { x, y, width, height } into a Rectangle // Convert { x, y, width, height } into a Rectangle
else if (lua_istable(L, index)) { else if (lua_istable(L, index)) {
gfx::Rect rc(0, 0, 0, 0); gfx::Rect rc(0, 0, 0, 0);
const int type = lua_getfield(L, index, "x"); int type = lua_getfield(L, index, "x");
if (VALID_LUATYPE(type)) { if (VALID_LUATYPE(type)) {
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
lua_getfield(L, index, "width"); type = lua_getfield(L, index, "width");
lua_getfield(L, index, "height"); if (VALID_LUATYPE(type)) {
lua_getfield(L, index, "height");
}
else {
lua_pop(L, 1);
lua_getfield(L, index, "w");
lua_getfield(L, index, "h");
}
rc.x = lua_tointeger(L, -4); rc.x = lua_tointeger(L, -4);
rc.y = lua_tointeger(L, -3); rc.y = lua_tointeger(L, -3);
rc.w = lua_tointeger(L, -2); rc.w = lua_tointeger(L, -2);
@ -244,6 +251,8 @@ const luaL_Reg Rectangle_methods[] = {
const Property Rectangle_properties[] = { const Property Rectangle_properties[] = {
{ "x", Rectangle_get_x, Rectangle_set_x }, { "x", Rectangle_get_x, Rectangle_set_x },
{ "y", Rectangle_get_y, Rectangle_set_y }, { "y", Rectangle_get_y, Rectangle_set_y },
{ "w", Rectangle_get_width, Rectangle_set_width },
{ "h", Rectangle_get_height, Rectangle_set_height },
{ "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 }, { "origin", Rectangle_get_origin, Rectangle_set_origin },

View File

@ -29,7 +29,7 @@ gfx::Size Size_new(lua_State* L, int index)
} }
// Convert {x=int,y=int} or {int,int} into a Size // Convert {x=int,y=int} or {int,int} into a Size
else if (lua_istable(L, index)) { else if (lua_istable(L, index)) {
const int type = lua_getfield(L, index, "width"); int type = lua_getfield(L, index, "width");
if (VALID_LUATYPE(type)) { if (VALID_LUATYPE(type)) {
lua_getfield(L, index, "height"); lua_getfield(L, index, "height");
sz.w = lua_tointeger(L, -2); sz.w = lua_tointeger(L, -2);
@ -38,11 +38,21 @@ gfx::Size Size_new(lua_State* L, int index)
} }
else { else {
lua_pop(L, 1); lua_pop(L, 1);
lua_geti(L, index, 1); type = lua_getfield(L, index, "w");
lua_geti(L, index, 2); if (VALID_LUATYPE(type)) {
sz.w = lua_tointeger(L, -2); lua_getfield(L, index, "h");
sz.h = lua_tointeger(L, -1); sz.w = lua_tointeger(L, -2);
lua_pop(L, 2); sz.h = lua_tointeger(L, -1);
lua_pop(L, 2);
}
else {
lua_pop(L, 1);
lua_geti(L, index, 1);
lua_geti(L, index, 2);
sz.w = lua_tointeger(L, -2);
sz.h = lua_tointeger(L, -1);
lua_pop(L, 2);
}
} }
} }
else { else {
@ -211,6 +221,8 @@ const luaL_Reg Size_methods[] = {
}; };
const Property Size_properties[] = { const Property Size_properties[] = {
{ "w", Size_get_width, Size_set_width },
{ "h", Size_get_height, Size_set_height },
{ "width", Size_get_width, Size_set_width }, { "width", Size_get_width, Size_set_width },
{ "height", Size_get_height, Size_set_height }, { "height", Size_get_height, Size_set_height },
{ nullptr, nullptr, nullptr } { nullptr, nullptr, nullptr }

View File

@ -1,4 +1,4 @@
Copyright (c) 2018-2022 Igara Studio S.A. Copyright (c) 2018-2023 Igara Studio S.A.
Copyright (c) 2018 David Capello Copyright (c) 2018 David Capello
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining

View File

@ -1,11 +1,13 @@
-- Copyright (C) 2018-2019 Igara Studio S.A. -- Copyright (C) 2018-2023 Igara Studio S.A.
-- --
-- This file is released under the terms of the MIT license. -- This file is released under the terms of the MIT license.
-- Read LICENSE.txt for more information. -- Read LICENSE.txt for more information.
local s = Sprite(32, 64) local s = Sprite(32, 64)
assert(s == app.sprite)
assert(s == app.activeSprite) assert(s == app.activeSprite)
assert(s == app.site.sprite) assert(s == app.site.sprite)
assert(s == app.frame.sprite)
assert(s == app.activeFrame.sprite) assert(s == app.activeFrame.sprite)
assert(s == app.site.frame.sprite) assert(s == app.site.frame.sprite)
assert(1 == app.activeFrame.frameNumber) assert(1 == app.activeFrame.frameNumber)
@ -13,13 +15,15 @@ assert(1 == app.site.frame.frameNumber)
assert(1 == app.site.frameNumber) assert(1 == app.site.frameNumber)
assert(0.100 == app.activeFrame.duration) -- Default frame duration assert(0.100 == app.activeFrame.duration) -- Default frame duration
assert(0.100 == app.site.frame.duration) assert(0.100 == app.site.frame.duration)
assert(s == app.layer.sprite)
assert(s == app.activeLayer.sprite) assert(s == app.activeLayer.sprite)
assert(s == app.site.layer.sprite) assert(s == app.site.layer.sprite)
assert(s == app.cel.sprite)
assert(s == app.activeCel.sprite) assert(s == app.activeCel.sprite)
assert(s == app.site.cel.sprite) assert(s == app.site.cel.sprite)
app.activeFrame.duration = 0.8 app.frame.duration = 0.8
app.command.NewFrame() app.command.NewFrame()
assert(2 == app.activeFrame.frameNumber) assert(2 == app.frame.frameNumber)
assert(0.8 == app.activeFrame.duration) -- Copy frame duration of previous frame assert(0.8 == app.frame.duration) -- Copy frame duration of previous frame

View File

@ -6,6 +6,8 @@
dofile('./test_utils.lua') dofile('./test_utils.lua')
app.activeTool = 'paint_bucket' app.activeTool = 'paint_bucket'
assert(app.tool.id == 'paint_bucket')
assert(app.brush.type == BrushType.CIRCLE)
assert(app.activeTool.id == 'paint_bucket') assert(app.activeTool.id == 'paint_bucket')
assert(app.activeBrush.type == BrushType.CIRCLE) assert(app.activeBrush.type == BrushType.CIRCLE)
assert(app.activeBrush.size == 1) assert(app.activeBrush.size == 1)

View File

@ -1,4 +1,4 @@
-- Copyright (C) 2019-2022 Igara Studio S.A. -- Copyright (C) 2019-2023 Igara Studio S.A.
-- Copyright (C) 2018 David Capello -- Copyright (C) 2018 David Capello
-- --
-- This file is released under the terms of the MIT license. -- This file is released under the terms of the MIT license.
@ -14,7 +14,9 @@ assert(rc.isEmpty)
rc = Rectangle(1, 2, 3, 4) rc = Rectangle(1, 2, 3, 4)
assert(rc.x == 1) assert(rc.x == 1)
assert(rc.y == 2) assert(rc.y == 2)
assert(rc.width == 3) assert(rc.w == 3) -- Short w/h form
assert(rc.h == 4)
assert(rc.width == 3) -- Long width/height form
assert(rc.height == 4) assert(rc.height == 4)
assert(rc.origin == Point(1, 2)) assert(rc.origin == Point(1, 2))
assert(rc.size == Size(3, 4)) assert(rc.size == Size(3, 4))
@ -54,11 +56,17 @@ assert(rc.y == 3)
assert(rc.width == 4) assert(rc.width == 4)
assert(rc.height == 5) assert(rc.height == 5)
rc = Rectangle{x=0, y=1, w=2, h=3}
assert(rc.x == 0)
assert(rc.y == 1)
assert(rc.w == 2)
assert(rc.h == 3)
rc = Rectangle{6, 7, 8, 9} rc = Rectangle{6, 7, 8, 9}
assert(rc.x == 6) assert(rc.x == 6)
assert(rc.y == 7) assert(rc.y == 7)
assert(rc.width == 8) assert(rc.w == 8)
assert(rc.height == 9) assert(rc.h == 9)
rc = Rectangle(Point(2, 3), Size(4, 5)) rc = Rectangle(Point(2, 3), Size(4, 5))
assert(rc.x == 2) assert(rc.x == 2)

View File

@ -9,7 +9,9 @@ assert(sz.width == 0)
assert(sz.height == 0) assert(sz.height == 0)
sz = Size(3, 4) sz = Size(3, 4)
assert(sz.width == 3) assert(sz.w == 3) -- Short w/h form
assert(sz.h == 4)
assert(sz.width == 3) -- Long width/height form
assert(sz.height == 4) assert(sz.height == 4)
assert("Size{ width=3, height=4 }" == tostring(sz)) assert("Size{ width=3, height=4 }" == tostring(sz))
@ -26,9 +28,13 @@ sz = Size{width=10, height=20}
assert(sz.width == 10) assert(sz.width == 10)
assert(sz.height == 20) assert(sz.height == 20)
sz = Size{w=30, h=40}
assert(sz.width == 30)
assert(sz.height == 40)
sz = Size{45, 25} sz = Size{45, 25}
assert(sz.width == 45) assert(sz.w == 45)
assert(sz.height == 25) assert(sz.h == 25)
sz = -sz sz = -sz
assert(sz.width == -45) assert(sz.width == -45)

View File

@ -1,4 +1,4 @@
-- Copyright (C) 2020 Igara Studio S.A. -- Copyright (C) 2020-2023 Igara Studio S.A.
-- Copyright (C) 2018 David Capello -- Copyright (C) 2018 David Capello
-- --
-- This file is released under the terms of the MIT license. -- This file is released under the terms of the MIT license.
@ -15,6 +15,8 @@ do
assert(a.toFrame.frameNumber == 8) assert(a.toFrame.frameNumber == 8)
assert(a.frames == 8) assert(a.frames == 8)
assert(app.tag == a)
a.fromFrame = 2 a.fromFrame = 2
a.toFrame = 5 a.toFrame = 5
assert(a.fromFrame.frameNumber == 2) assert(a.fromFrame.frameNumber == 2)