Update main.lua
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
This commit is contained in:
1 parent
a8d5388faa
commit
df81c8d5d7
1 file changed
+70
-5
@@ -4,8 +4,22 @@ local utility = require "lib.utility"
|
||||
|
||||
local window_width, window_height = love.graphics.getDimensions()
|
||||
|
||||
local tiles, tiles_json, tile_image
|
||||
local current_text = ""
|
||||
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()
|
||||
@@ -24,13 +38,33 @@ local get_tile_usage = function(tiles_json, tile_image)
|
||||
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("Prompt:", 0, 0, window_width)
|
||||
love.graphics.printf(current_text, 0, 20, window_width) -- lazy magic numbers
|
||||
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)
|
||||
@@ -46,18 +80,49 @@ function love.keypressed(key)
|
||||
current_text = current_text:sub(1, byteoffset - 1)
|
||||
end
|
||||
elseif key == "enter" then
|
||||
-- TODO end of a textinput; what to do depends on what was being asked
|
||||
-- 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
|
||||
Reference in new issue
Block a user