diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5c4782 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +assets/** diff --git a/lib/log.lua b/lib/log.lua new file mode 100644 index 0000000..42f03c3 --- /dev/null +++ b/lib/log.lua @@ -0,0 +1,82 @@ +-- USAGE: +-- -- set arbitrary log levels to be printed immediately +-- log{ info = true, warning = true, ducksauce = true, } +-- -- disable storage of messages depending on log level +-- log{ error = false, verbose = false, } +-- -- send anything (except nil) to a log level +-- log("bacom", true, "text", 5, function() end, {}) +-- -- print all messages saved at a particular log level +-- log("error") +-- -- toggle ALL logging on and off with a boolean +-- log(false) -- this deletes all stored messages +-- -- set a maximum number of stored messages per log level +-- log(50) -- warning: reaching large limits makes logging expensive +-- -- check if a log level is being displayed +-- if log.arbitrary_exit then log("arbitrary_exit") os.exit(1) end +-- -- do anything to the stored messages (this example deletes them all) +-- log(function(messages) return {} end) + +local microlog = {} -- display_levels are stored directly here +local stored_messages = {} +local enable_logging = true +local message_limit = math.huge + +return setmetatable(microlog, { + __call = function(self, options, ...) + local options_type = type(options) + + if (options_type == "string") and enable_logging then + if microlog[options] == false then return end + if not stored_messages[options] then stored_messages[options] = {} end + local message_table = stored_messages[options] + + -- turn log message into text + local tab = {...} + for i = 1, #tab do + tab[i] = tostring(tab[i]) + end + local current_message = table.concat(tab, "\t") + + if #current_message == 0 then + -- print all stored_messages of specified level + for i = 1, #message_table do + print(message_table[i]) + end + + else + -- store message (and print if in display_levels) + message_table[#message_table + 1] = current_message + if #message_table > message_limit then table.remove(message_table, 1) end + if microlog[options] then print(current_message) end + end + + -- set display_levels + elseif options_type == "table" then + for k,v in pairs(options) do + microlog[k] = v + end + + elseif options_type == "number" then + message_limit = options + for k,v in pairs(stored_messages) do + while #v > message_limit do + table.remove(v, 1) + end + end + + -- turn all logging off and delete stored messages; or turn it on + elseif options_type == "boolean" then + if options then + enable_logging = true + else + enable_logging = false + stored_messages = {} + end + + -- arbitrary access :D + elseif options_type == "function" then + local result = options(stored_messages) + if type(result) == "table" then stored_messages = result end + end + end +}) diff --git a/lib/tilesheet.lua b/lib/tilesheet.lua index c2fa5cb..a58f7a5 100644 --- a/lib/tilesheet.lua +++ b/lib/tilesheet.lua @@ -18,7 +18,7 @@ local tile_metatable = { } } -local tilesheet.load = function(data_or_path) +tilesheet.load = function(data_or_path) local tile_image, tile_size local tiles = {} diff --git a/main.lua b/main.lua index cd086fe..d1c62bc 100644 --- a/main.lua +++ b/main.lua @@ -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