Skip to content

Instantly share code, notes, and snippets.

@luke-biel
Last active October 23, 2019 22:47
Show Gist options
  • Save luke-biel/64caa15b816f671d4b912284a46b7333 to your computer and use it in GitHub Desktop.
Save luke-biel/64caa15b816f671d4b912284a46b7333 to your computer and use it in GitHub Desktop.
YAML tilemapper
MIN_SUPPORTED_VERSION = 6
-- Validate whether the version of the scripting api is compatible with this script.
function api_version_valid(version)
return version >= MIN_SUPPORTED_VERSION
end
-- Get all grid cells with atleast single pixel filled
function get_exportable_ranges(image, bounds)
local width = image.width
local height = image.height
local bound_width = bounds.width
local bound_height = bounds.height
local result = {}
for x = 0, width-1, bound_width do
for y = 0, height-1, bound_height do
if is_filled(image, x, y, bound_width, bound_height) then
table.insert(result, { x, y})
end
end
end
return result
end
-- Check whether grid cell contain sany opaque pixels
function is_filled(image, offset_x, offset_y, bound_width, bound_height)
for x = offset_x, offset_x + bound_width - 1 do
for y = offset_y, offset_y + bound_height - 1 do
if image:getPixel(x, y) > 0 then return true end
end
end
return false
end
-- Format gathered data into yaml format
function format_str(image, bounds, exportable_ranges)
local string_builder = { }
string_builder[1] = ".tile_width: &tile_width " .. bounds.width .. "\n.tile_height: &tile_height " .. bounds.height .. "\n"
string_builder[2] = "texture_width: " .. image.width .. "\ntexture_height: " .. image.height .. "\nsprites:"
for _, value in ipairs(exportable_ranges) do
local x = value[1]
local y = value[2]
string_builder[#string_builder+1] = " - x: " .. x .. "\n y: " .. y .. "\n width: *tile_width\n height: *tile_height"
end
string_builder[#string_builder+1] = ""
return table.concat(string_builder, "\n")
end
-- Show save prompt
function prompt_file_save(text)
local filename = ""
local dlg = Dialog()
dlg:label{
id="prompt",
label="",
text="Select place to save your yaml tilemap definition"
}
dlg:file{
id="file_loc",
label="Save location",
title="Select location to save file...",
save=true,
filename="tilemap.yml",
filetypes={ "yml", "yaml" },
onchange=function() save_to_file(dlg, text) end
}
dlg:show()
end
-- Save actual yaml file !!Prompts for user permission!!
function save_to_file(dlg, text)
local file = io.open(dlg.data.file_loc, "w+")
file:write(text)
file:close(file)
dlg:close()
end
function main()
if not api_version_valid(app.apiVersion) then
app.alert("API version must be 6 to run this script. Check for updates or manually override this version check at your own risk.")
return
end
local image = app.activeImage
local sprite = app.activeSprite
if not image or not sprite then
app.alert("No open image")
return
end
local document = app.preferences.document(sprite)
local grid = document.grid
local bounds = grid.bounds
local exportable_ranges = get_exportable_ranges(image, bounds)
local text = format_str(sprite, bounds, exportable_ranges)
prompt_file_save(text)
end
-- Generate yml config file compatible with amethyst format from an image;
-- It creates one tile for each *grid* cell filled by atleast one opaque pixel;
main()

YAML tilemapper

usage

tldr: Place file inside your aseprite scripts folder, restart aseprite, run like all scripts

  • Open aseprite
  • Edit -> Scripts -> Open Scripts Folder
  • Place yaml_tilemapper.lua in scripts folder (you may or may not need to restart aseprite now)
  • File -> Open -> Select tilemap image
  • View -> Grid -> Grid Settings -> Adjust grid to your tiles sizes (yeah, I know, I shouldve made it configurable from dialog window)
  • Edit -> Scripts -> yaml_tilemapper -> Save location -> Save

Why yaml and not ron if this plugin is supposed to be used with amethyst?

Well, I personally hate ron format, so I use yaml in my projects. The plugin can probalby be easily extended to support ron. If you happen to extend this plugin, you can send me your draft to lukasz.p.biel[at]gmail.com and I may include it in official version (this gist) and add you to collaborators list that would appear somewhere in this README.

License

MIT License

Copyright (c) 2019 Łukasz Biel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment