forked from PKGaspi/AsepriteScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
117 lines (96 loc) · 2.91 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
--[[
Description:
Main Aseprite script to use my other scripts and supply some common functions.
Usage:
Simply run this script and select which script to use in the menu.
About:
Made by Gaspi.
- Web: https://gaspi.games/
- Twitter: @_Gaspi
--]]
-- Check if this is being run directly.
if debug.getinfo(2) == nil then
local dlg = MsgDialog("Error", "Don't run this script directly!")
dlg:show()
return 1
end
-- Auxiliary functions
-- Path handling.
-- Return the path to the dir containing a file.
-- Source: https://stackoverflow.com/questions/9102126/lua-return-directory-path-from-path
function Dirname(str)
return str:match("(.*" .. Sep .. ")")
end
-- Return the name of a file given its full path..
-- Source: https://codereview.stackexchange.com/questions/90177/get-file-name-with-extension-and-get-only-extension
function Basename(str)
return str:match("^.*" .. Sep .. "(.+)$") or str
end
-- Return the name of a file excluding the extension, this being, everything after the dot.
-- Source: https://stackoverflow.com/questions/18884396/extracting-filename-only-with-pattern-matching
function RemoveExtension(str)
return str:match("(.+)%..+")
end
-- Sprite handling.
-- Hides all layers and sub-layers inside a group, returning a list with all
-- initial states of each layer's visibility.
function HideLayers(sprite)
local data = {} -- Save visibility status of each layer here.
for i,layer in ipairs(sprite.layers) do
if layer.isGroup then
-- Recursive for groups.
data[i] = HideLayers(layer)
else
data[i] = layer.isVisible
layer.isVisible = false
end
end
return data
end
-- Restore layers visibility.
function RestoreLayersVisibility(sprite, data)
for i,layer in ipairs(sprite.layers) do
if layer.isGroup then
-- Recursive for groups.
RestoreLayersVisibility(layer, data[i])
else
layer.isVisible = data[i]
end
end
end
-- Dialog
function MsgDialog(title, msg)
local dlg = Dialog(title)
dlg:label{
id = "msg",
text = msg
}
dlg:newrow()
dlg:button{id = "close", text = "Close", onclick = function() dlg:close() end }
return dlg
end
-- Other
function CopyTable(original)
local copy = {}
for i, value in ipairs(original) do
copy[i] = value
end
return copy
end
-- Current sprite.
Sprite = app.activeSprite
if Sprite == nil then
-- Show error, no sprite active.
local dlg = MsgDialog("Error", "No sprite is currently active. Please, open a sprite first and run again.")
dlg:show()
return 1
end
-- Identify operative system.
Sep = string.sub(Sprite.filename, 1, 1) == "/" and "/" or "\\"
if Dirname(Sprite.filename) == nil then
-- Error, can't identify OS when the sprite isn't saved somewhere.
local dlg = MsgDialog("Error", "Current sprite is not associated to a file. Please, save your sprite and run again.")
dlg:show()
return 1
end
return 0