diff --git a/tools/aseprite/tile-props/main.lua b/tools/aseprite/tile-props/main.lua index 46b6428..6806d9e 100644 --- a/tools/aseprite/tile-props/main.lua +++ b/tools/aseprite/tile-props/main.lua @@ -9,8 +9,8 @@ local function log(msg) end end -local function getSelectedTile() - log("getSelectedTile() called") +local function get_selected_tile() + log("get_selected_tile() called") if not app then log("ERROR: app is nil!") @@ -49,10 +49,10 @@ local function getSelectedTile() return tileset, tileIndex end -local function getTileProperties(tileset, tileIndex) - log("getTileProperties() called for tile index: " .. tostring(tileIndex)) +local function get_tile_properties(tileset, tile_index) + log("get_tile_properties() called for tile index: " .. tostring(tile_index)) - local tile = tileset:tile(tileIndex) + local tile = tileset:tile(tile_index) if not tile then log("Could not get tile object") return {} @@ -83,10 +83,10 @@ local function getTileProperties(tileset, tileIndex) return props end -local function setTileProperties(tileset, tileIndex, props) - log("setTileProperties() called for tile index: " .. tostring(tileIndex)) +local function set_tile_properties(tileset, tile_index, props) + log("set_tile_properties() called for tile index: " .. tostring(tile_index)) - local tile = tileset:tile(tileIndex) + local tile = tileset:tile(tile_index) if not tile then log("Could not get tile object") return @@ -109,10 +109,10 @@ local function setTileProperties(tileset, tileIndex, props) log("Set " .. count .. " properties") end -local function showPropertyEditor(existingProps) - log("showPropertyEditor() called") +local function show_property_editor() + log("show_property_editor() called") - local tileset, tileIndex = getSelectedTile() + local tileset, tile_index = get_selected_tile() if not tileset then log("No tileset selected, showing alert") @@ -121,131 +121,164 @@ local function showPropertyEditor(existingProps) end log("Getting properties for tile") - local properties = existingProps or getTileProperties(tileset, tileIndex) + local properties = get_tile_properties(tileset, tile_index) - local dlg = Dialog("Tile Properties - Tile #" .. tileIndex) + local function build_dialog() + local dlg = Dialog("Tile Properties - Tile #" .. tile_index) - dlg:label{ text="Properties:" } + for i, prop in ipairs(properties) do + local id_prefix = "prop_" .. i .. "_" + local prop_value = prop - for i, prop in ipairs(properties) do - dlg:separator() - dlg:entry{ - id = "key_" .. i, - label = "Name:", - text = prop.key - } - dlg:combobox{ - id = "type_" .. i, - label = "Type:", - option = prop.type, - options = { "boolean", "number", "string" }, - onchange = function() - for j = 1, #properties do - properties[j].key = dlg.data["key_" .. j] or properties[j].key - local newType = dlg.data["type_" .. j] or properties[j].type - local oldType = properties[j].type + dlg:separator{ id = id_prefix .. "sep" } + dlg:newrow() - if newType ~= oldType then - if newType == "boolean" then - properties[j].value = false - elseif newType == "number" then - properties[j].value = 0 - else -- string - properties[j].value = "" - end - else - properties[j].value = dlg.data["value_" .. j] or properties[j].value - end - - properties[j].type = newType - end - dlg:close() - showPropertyEditor(properties) - end - } - - if prop.type == "boolean" then - dlg:check{ - id = "value_" .. i, - text = "", - selected = prop.value - } - elseif prop.type == "number" then - dlg:number{ - id = "value_" .. i, - label = "Value:", - text = tostring(prop.value), - decimals = 0 - } - else dlg:entry{ - id = "value_" .. i, - label = "Value:", - text = tostring(prop.value) + id = id_prefix .. "key", + label = "Name:", + text = prop_value.key } + dlg:newrow() + + dlg:combobox{ + id = id_prefix .. "type", + label = "Type:", + option = prop_value.type, + options = { "boolean", "number", "string" }, + onchange = function() + local new_type = dlg.data[id_prefix .. "type"] + if new_type ~= prop_value.type then + if new_type == "boolean" then + properties[i].value = false + elseif new_type == "number" then + properties[i].value = 0 + else + properties[i].value = "" + end + properties[i].type = new_type + rebuild_from_dialog(dlg) + end + end + } + dlg:newrow() + + if prop_value.type == "boolean" then + dlg:check{ + id = id_prefix .. "value", + label = "Value:", + selected = prop_value.value + } + elseif prop_value.type == "number" then + dlg:number{ + id = id_prefix .. "value", + label = "Value:", + text = tostring(prop_value.value), + decimals = 0 + } + else + dlg:entry{ + id = id_prefix .. "value", + label = "Value:", + text = tostring(prop_value.value) + } + end + + dlg:button{ + id = id_prefix .. "delete", + text = "Delete", + onclick = function() + sync_from_dialog(dlg) + table.remove(properties, i) + rebuild_from_dialog(dlg) + end + } + dlg:newrow() end + dlg:separator() + dlg:button{ - id = "delete_" .. i, - text = "Delete", + id = "add_btn", + text = "Add Property", onclick = function() - for j = 1, #properties do - properties[j].key = dlg.data["key_" .. j] or properties[j].key - properties[j].type = dlg.data["type_" .. j] or properties[j].type - properties[j].value = dlg.data["value_" .. j] or properties[j].value - end - table.remove(properties, i) - dlg:close() - showPropertyEditor(properties) + sync_from_dialog(dlg) + table.insert(properties, { + key = "", + type = "string", + value = "" + }) + rebuild_from_dialog(dlg) end } + + dlg:button{ + text = "Apply Changes", + onclick = function() + sync_from_dialog(dlg) + local new_props = {} + for _, prop in ipairs(properties) do + if prop.key and prop.key ~= "" then + table.insert(new_props, prop) + end + end + set_tile_properties(tileset, tile_index, new_props) + end + } + + dlg:separator() + + dlg:button{ + text = "OK", + focus = true, + onclick = function() + sync_from_dialog(dlg) + local new_props = {} + for _, prop in ipairs(properties) do + if prop.key and prop.key ~= "" then + table.insert(new_props, prop) + end + end + set_tile_properties(tileset, tile_index, new_props) + dlg:close() + end + } + + dlg:button{ text = "Cancel" } + + return dlg end - dlg:separator() - dlg:button{ - text = "Add Property", - onclick = function() - for i = 1, #properties do - properties[i].key = dlg.data["key_" .. i] or properties[i].key - properties[i].type = dlg.data["type_" .. i] or properties[i].type - properties[i].value = dlg.data["value_" .. i] or properties[i].value + function sync_from_dialog(dlg) + log("sync_from_dialog() called") + for i = 1, #properties do + local id_prefix = "prop_" .. i .. "_" + local data = dlg.data + + log(" Property " .. i .. ":") + if data[id_prefix .. "key"] then + properties[i].key = data[id_prefix .. "key"] + log(" key = " .. tostring(properties[i].key)) end - table.insert(properties, { - key = "", - type = "string", - value = "" - }) - dlg:close() - showPropertyEditor(properties) - end - } - - dlg:separator() - dlg:button{ - text = "Apply", - onclick = function() - local newProps = {} - for i = 1, #properties do - local key = dlg.data["key_" .. i] - local propType = dlg.data["type_" .. i] - local value = dlg.data["value_" .. i] - - if key and key ~= "" then - table.insert(newProps, { - key = key, - type = propType, - value = value - }) - end + if data[id_prefix .. "type"] then + properties[i].type = data[id_prefix .. "type"] + log(" type = " .. tostring(properties[i].type)) + end + if data[id_prefix .. "value"] ~= nil then + properties[i].value = data[id_prefix .. "value"] + log(" value = " .. tostring(properties[i].value) .. " (type: " .. type(properties[i].value) .. ")") + else + log(" WARNING: value is nil in dialog data!") end - - setTileProperties(tileset, tileIndex, newProps) - dlg:close() end - } + end - dlg:button{ text = "Cancel" } + function rebuild_from_dialog(old_dlg) + old_dlg:close() + local new_dlg = build_dialog() + new_dlg:show() + end + local dlg = build_dialog() dlg:show() end @@ -255,16 +288,15 @@ function init(plugin) return end - -- register in sprite menu plugin:newCommand{ id = "TilePropertiesEditor", title = "Tile Properties", group = "sprite_properties", onenabled = function() - local tileset, tileIndex = getSelectedTile() - return tileset ~= nil and tileIndex ~= nil + local tileset, tile_index = get_selected_tile() + return tileset ~= nil and tile_index ~= nil end, - onclick = showPropertyEditor + onclick = show_property_editor } end