From 0f7cac0f0cc93a9a1e06afe5fef3fd641c92eb3b Mon Sep 17 00:00:00 2001 From: Gaspar Capello Date: Tue, 27 Aug 2024 11:04:56 -0300 Subject: [PATCH] [lua] Add get/set the foreground/background tile (fix #4403) It's now possible to get/set the selected foreground/background tile index. Example of use: app.fgTile = 1 -- the primary tile is '1' print(app.fgTile) -- this will show '1' app.fgTile = 0 -- the primery tile is 'no tile' print(app.fgTile) -- this will show '0' app.bgTile = 2 -- the secondary tile is '2' --- src/app/script/app_object.cpp | 26 ++++++++++++ tests/scripts/tilemap.lua | 75 ++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/app/script/app_object.cpp b/src/app/script/app_object.cpp index 73b63cb32..58af63c4a 100644 --- a/src/app/script/app_object.cpp +++ b/src/app/script/app_object.cpp @@ -644,6 +644,30 @@ int App_set_bgColor(lua_State* L) return 0; } +int App_get_fgTile(lua_State* L) +{ + lua_pushinteger(L, Preferences::instance().colorBar.fgTile()); + return 1; +} + +int App_set_fgTile(lua_State* L) +{ + Preferences::instance().colorBar.fgTile(lua_tointeger(L, 2)); + return 0; +} + +int App_get_bgTile(lua_State* L) +{ + lua_pushinteger(L, Preferences::instance().colorBar.bgTile()); + return 1; +} + +int App_set_bgTile(lua_State* L) +{ + Preferences::instance().colorBar.bgTile(lua_tointeger(L, 2)); + return 0; +} + int App_get_site(lua_State* L) { app::Context* ctx = App::instance()->context(); @@ -828,6 +852,8 @@ const Property App_properties[] = { { "sprites", App_get_sprites, nullptr }, { "fgColor", App_get_fgColor, App_set_fgColor }, { "bgColor", App_get_bgColor, App_set_bgColor }, + { "fgTile", App_get_fgTile, App_set_fgTile }, + { "bgTile", App_get_bgTile, App_set_bgTile }, { "version", App_get_version, nullptr }, { "apiVersion", App_get_apiVersion, nullptr }, { "site", App_get_site, nullptr }, diff --git a/tests/scripts/tilemap.lua b/tests/scripts/tilemap.lua index 80bee88b6..c64f3f0af 100644 --- a/tests/scripts/tilemap.lua +++ b/tests/scripts/tilemap.lua @@ -1,4 +1,4 @@ --- Copyright (C) 2019-2023 Igara Studio S.A. +-- Copyright (C) 2019-2024 Igara Studio S.A. -- -- This file is released under the terms of the MIT license. -- Read LICENSE.txt for more information. @@ -195,6 +195,79 @@ do end +---------------------------------------------------------------------- +-- Tests of get/set app.fgTile and app.bgTile +---------------------------------------------------------------------- + +do + local spr = Sprite(32, 32, ColorMode.INDEXED) + spr.gridBounds = Rectangle{ 0, 0, 2, 2 } + assert(spr.layers[1].isImage) + assert(not spr.layers[1].isTilemap) + + ---------------------------------------------------------------------- + -- Create a tilemap + ---------------------------------------------------------------------- + + app.command.NewLayer{ tilemap=true } + assert(#spr.layers == 2) + app.layer = spr.layers[2] + assert(app.layer.isImage) + assert(app.layer.isTilemap) + assert(#app.layer.cels == 0) + + ---------------------------------------------------------------------- + -- Draw something on the tilemap so new tiles are created + ---------------------------------------------------------------------- + + app.useTool{ + tool='pencil', + color=1, + tilesetMode=TilesetMode.STACK, + points={ Point(2, 0), Point(4, 0), Point(5,1) } + } + assert(#app.layer.cels == 1) + assert(app.layer:cel(1).image.colorMode == ColorMode.TILEMAP) + local tilemapCel = app.layer:cel(1) + assert(tilemapCel.bounds == Rectangle(2, 0, 4, 2)) + assert(#spr.layers[2].tileset == 3) + + app.fgTile = 1 + app.useTool{ + tool='pencil', + tilemapMode=TilemapMode.TILES, + points={ Point(0, 0), Point(5, 0)} + } + + app.fgTile = 2 + app.useTool{ + tool='pencil', + tilemapMode=TilemapMode.TILES, + points={ Point(0, 2), Point(5, 2)} + } + + local cel = spr.layers[2].cels[1] + expect_img(cel.image, { 1,1,1, + 2,2,2 }) + + app.fgTile = 0 + app.useTool{ + tool='pencil', + tilemapMode=TilemapMode.TILES, + points={ Point(0, 0), Point(4, 0), + Point(4, 2), Point(0, 2)} + } + + local cel = spr.layers[2].cels[1] + assert(cel == nil) + local tileImg1 = spr.layers[2].tileset:tile(1).image + expect_img(tileImg1, { 1,1, + 0,0 }) + local tileImg2 = spr.layers[2].tileset:tile(2).image + expect_img(tileImg2, { 1,0, + 0,1 }) +end + ---------------------------------------------------------------------- -- Tests drawing in the tilemap with tiles ----------------------------------------------------------------------