fix syntax errors, logic errors, add logging to find errors, test that it works
This commit is contained in:
1 parent
f355cca5bc
commit
12bf6f1bed
4 files changed
+115
-13
No files matched your search
@@ -1,7 +1,13 @@
|
||||
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
|
||||
@@ -13,7 +19,7 @@ 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."
|
||||
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):",
|
||||
}
|
||||
@@ -40,9 +46,9 @@ 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
|
||||
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 )
|
||||
@@ -52,6 +58,15 @@ local next_unused_tile = function(tiles_json, tile_image, tile_usage)
|
||||
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()
|
||||
@@ -68,18 +83,21 @@ function love.draw()
|
||||
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 == "enter" then
|
||||
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)
|
||||
@@ -95,19 +113,18 @@ function love.keypressed(key)
|
||||
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
|
||||
save_tileset()
|
||||
current_prompt = prompts.start_new
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function love.filedropped(file_path)
|
||||
if file_path:sub(-5) == ".json" then
|
||||
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)
|
||||
@@ -118,6 +135,7 @@ function love.filedropped(file_path)
|
||||
else
|
||||
tiles_json = {
|
||||
image_path = file_path,
|
||||
tiles = {},
|
||||
-- tile_size is not defined yet
|
||||
}
|
||||
flag_new_file = true
|
||||
|
||||
Reference in new issue
Block a user