Ugh. I hate this code but it should function (as long as hitting enter doesn't immediately insert a newline before keypressed can trigger.. or after it actually.. either will break this. :D
129 lines
3.7 KiB
Lua
129 lines
3.7 KiB
Lua
local tilesheet = require "lib.tilesheet"
|
|
local utf8 = require "utf8"
|
|
local utility = require "lib.utility"
|
|
|
|
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 = current_x, #tile_usage do
|
|
for y = current_y, #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
|
|
|
|
|
|
|
|
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)
|
|
current_text = current_text .. character
|
|
end
|
|
|
|
function love.keypressed(key)
|
|
if key == "escape" then
|
|
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 == "enter" 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
|
|
if flag_new_file then
|
|
utility.save_data(tiles_json, tiles_json.image_path .. ".json")
|
|
else
|
|
utility.save_data(tiles_json)
|
|
end
|
|
current_prompt = prompts.start_new
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function love.filedropped(file_path)
|
|
if file_path:sub(-5) == ".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,
|
|
-- 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
|