Files

147 lines
4.0 KiB
Lua

local log = require "lib.log"
local tilesheet = require "lib.tilesheet"
local utf8 = require "utf8"
local utility = require "lib.utility"
log{
keypressed = false,
textinput = false,
}
local window_width, window_height = love.graphics.getDimensions()
local tiles, tiles_json, tile_image, tile_usage
local current_text, current_prompt = "", ""
local current_quad
local current_x, current_y = 1, 1
local flag_new_file -- this automatically chooses a JSON file location for new files
local prompts = {
start_new = "Drag and drop an image or JSON file to name tiles.",
tile_size = "Enter the tile_size for this tilesheet:",
enter_name = "Enter the name for this tile (leave empty to skip):",
}
current_prompt = prompts.start_new
local get_tile_usage = function(tiles_json, tile_image)
local image_width, image_height = tile_image:getDimensions()
local tile_usage = {}
for x = 1, image_width / tiles_json.tile_size do
tile_usage[x] = {}
for y = 1, image_height / tiles_json.tile_size do
tile_usage[x][y] = false
end
end
for name, tile_data in pairs(tiles_json.tiles) do
tile_usage[tile_data.x][tile_data.y] = true
end
return tile_usage
end
local next_unused_tile = function(tiles_json, tile_image, tile_usage)
local tile_size = tiles_json.tile_size
for x = 1, #tile_usage do
for y = 1, #tile_usage[1] do
if not (tile_usage[x][y]) then
current_x = x
current_y = y
current_quad = love.graphics.newQuad( (x - 1) * tile_size, (y - 1) * tile_size, tile_size, tile_size, tile_image )
return current_quad
end
end
end
end
local save_tileset = function()
if not (tiles_json) then return end
if flag_new_file then
utility.save_data(tiles_json, tiles_json.image_path .. ".json")
else
utility.save_data(tiles_json)
end
end
function love.load()
love.keyboard.setKeyRepeat(true)
end
function love.draw()
love.graphics.printf(current_prompt, 0, 0, window_width)
love.graphics.printf(current_text, 0, 20, window_width) -- lazy magic number
if current_quad then
love.graphics.draw(tile_image, current_quad, 0, 40) -- lazy magic number
end
end
function love.textinput(character)
log("textinput", "textinput", character)
current_text = current_text .. character
end
function love.keypressed(key)
log("keypressed", "keypressed", key)
if key == "escape" then
save_tileset()
love.event.quit()
elseif key == "backspace" then
local byteoffset = utf8.offset(current_text, -1)
if byteoffset then
current_text = current_text:sub(1, byteoffset - 1)
end
elseif key == "return" then
-- end of a textinput; what to do depends on what was being asked
if current_prompt == prompts.tile_size then
tiles_json.tile_size = tonumber(current_text)
current_text = ""
tile_usage = get_tile_usage(tiles_json, tile_image)
if next_unused_tile(tiles_json, tile_image, tile_usage) then
current_prompt = prompts.enter_name
end
elseif current_prompt == prompts.enter_name then
if #current_text > 0 then
tiles_json.tiles[current_text] = { x = current_x, y = current_y, }
end
tile_usage[current_x][current_y] = true
current_text = ""
if not (next_unused_tile(tiles_json, tile_image, tile_usage)) then
save_tileset()
current_prompt = prompts.start_new
end
end
end
end
function love.filedropped(dropped_file)
local file_path = dropped_file:getFilename()
local file_extension = file_path:match("%.%w+$")
if file_extension == ".json" then
tiles_json = utility.load_data(file_path)
flag_new_file = false
tiles, tile_image = tilesheet(tiles_json)
tile_usage = get_tile_usage(tiles_json, tile_image)
if next_unused_tile(tiles_json, tile_image, tile_usage) then
current_prompt = prompts.enter_name
end
else
tiles_json = {
image_path = file_path,
tiles = {},
-- tile_size is not defined yet
}
flag_new_file = true
tiles, tile_image = tilesheet(file_path)
current_prompt = prompts.tile_size
current_text = ""
end
end