improve aseprite tile-props extension ux
This commit is contained in:
parent
5637fa18c8
commit
75948c2894
1 changed files with 153 additions and 121 deletions
|
|
@ -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)
|
||||
|
||||
dlg:label{ text="Properties:" }
|
||||
local function build_dialog()
|
||||
local dlg = Dialog("Tile Properties - Tile #" .. tile_index)
|
||||
|
||||
for i, prop in ipairs(properties) do
|
||||
dlg:separator()
|
||||
local id_prefix = "prop_" .. i .. "_"
|
||||
local prop_value = prop
|
||||
|
||||
dlg:separator{ id = id_prefix .. "sep" }
|
||||
dlg:newrow()
|
||||
|
||||
dlg:entry{
|
||||
id = "key_" .. i,
|
||||
id = id_prefix .. "key",
|
||||
label = "Name:",
|
||||
text = prop.key
|
||||
text = prop_value.key
|
||||
}
|
||||
dlg:newrow()
|
||||
|
||||
dlg:combobox{
|
||||
id = "type_" .. i,
|
||||
id = id_prefix .. "type",
|
||||
label = "Type:",
|
||||
option = prop.type,
|
||||
option = prop_value.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
|
||||
|
||||
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
|
||||
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[j].value = dlg.data["value_" .. j] or properties[j].value
|
||||
properties[i].value = ""
|
||||
end
|
||||
|
||||
properties[j].type = newType
|
||||
properties[i].type = new_type
|
||||
rebuild_from_dialog(dlg)
|
||||
end
|
||||
dlg:close()
|
||||
showPropertyEditor(properties)
|
||||
end
|
||||
}
|
||||
dlg:newrow()
|
||||
|
||||
if prop.type == "boolean" then
|
||||
if prop_value.type == "boolean" then
|
||||
dlg:check{
|
||||
id = "value_" .. i,
|
||||
text = "",
|
||||
selected = prop.value
|
||||
}
|
||||
elseif prop.type == "number" then
|
||||
dlg:number{
|
||||
id = "value_" .. i,
|
||||
id = id_prefix .. "value",
|
||||
label = "Value:",
|
||||
text = tostring(prop.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 = "value_" .. i,
|
||||
id = id_prefix .. "value",
|
||||
label = "Value:",
|
||||
text = tostring(prop.value)
|
||||
text = tostring(prop_value.value)
|
||||
}
|
||||
end
|
||||
|
||||
dlg:button{
|
||||
id = "delete_" .. i,
|
||||
id = id_prefix .. "delete",
|
||||
text = "Delete",
|
||||
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
|
||||
sync_from_dialog(dlg)
|
||||
table.remove(properties, i)
|
||||
dlg:close()
|
||||
showPropertyEditor(properties)
|
||||
rebuild_from_dialog(dlg)
|
||||
end
|
||||
}
|
||||
dlg:newrow()
|
||||
end
|
||||
|
||||
dlg:separator()
|
||||
|
||||
dlg:button{
|
||||
id = "add_btn",
|
||||
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
|
||||
end
|
||||
sync_from_dialog(dlg)
|
||||
table.insert(properties, {
|
||||
key = "",
|
||||
type = "string",
|
||||
value = ""
|
||||
})
|
||||
dlg:close()
|
||||
showPropertyEditor(properties)
|
||||
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 = "Apply",
|
||||
text = "OK",
|
||||
focus = true,
|
||||
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
|
||||
})
|
||||
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
|
||||
|
||||
setTileProperties(tileset, tileIndex, newProps)
|
||||
set_tile_properties(tileset, tile_index, new_props)
|
||||
dlg:close()
|
||||
end
|
||||
}
|
||||
|
||||
dlg:button{ text = "Cancel" }
|
||||
|
||||
return dlg
|
||||
end
|
||||
|
||||
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
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue