-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz5-aseprite-tool.lua
204 lines (189 loc) · 5.52 KB
/
z5-aseprite-tool.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
json = dofile("json.lual")
pageSize = 15
function readAll(file)
local f = assert(io.open(file, "rb"))
local content = f:read("*all")
f:close()
return content
end
function getCollider(c)
collider = "box"
if c == 1 then
collider = "ramp"
end
if c == 2 then
collider = "half"
end
return collider
end
function addFrame(block)
local sprite = app.activeSprite
block["sprite"]["frames"][#block["sprite"]["frames"]+1]= {}
block["sprite"]["frames"][#block["sprite"]["frames"]]["startX"] = sprite.selection.bounds.x
block["sprite"]["frames"][#block["sprite"]["frames"]]["startY"] = sprite.selection.bounds.y
block["sprite"]["frames"][#block["sprite"]["frames"]]["sizeX"] = 16
block["sprite"]["frames"][#block["sprite"]["frames"]]["sizeY"] = 16
end
function saveBlock(dialog,block)
block["name"]= dialog.data.name
block["visible"]= dialog.data.visible
block["solid"]= dialog.data.solid
block["opaque"]=dialog.data.opaque
block["mass"]=dialog.data.mass
if dialog.data.solid then
block["collider"] = 0
if dialog.data.collider == "ramp" then
block["collider"] = 1
end
if dialog.data.collider == "half" then
block["collider"] = 2
end
end
end
function newBlock(bt)
table.insert(bt,{})
bt[#bt]["ID"] = #bt - 1
bt[#bt]["name"] = "newBlock"
bt[#bt]["visible"] = true
bt[#bt]["solid"] = true
bt[#bt]["opaque"] = true
bt[#bt]["mass"] = 1000
bt[#bt]["collider"] = 0
bt[#bt]["sprite"] = {}
bt[#bt]["sprite"]["path"] = "spritesheet.png"
bt[#bt]["sprite"]["frames"] = {}
end
function editBlock(blockID, bt,parentdlg,minNum,btjname)
local dlg = Dialog()
dlg:label{label="ID: " .. bt[blockID]["ID"]}
dlg:newrow{}
dlg:entry{id="name",label="name",text=bt[blockID]["name"]}
dlg:newrow{}
dlg:check{id="visible",label="visible",selected=bt[blockID]["visible"]}
dlg:newrow{}
dlg:check{id="solid",label="solid",selected=bt[blockID]["solid"]}
dlg:newrow{}
dlg:check{id="opaque",label="opaque",selected=bt[blockID]["opaque"]}
dlg:newrow{}
dlg:number{id="mass",label="mass",text = tostring(bt[blockID]["mass"])}
dlg:newrow{}
dlg:combobox{id="collider",label="collider", option = getCollider(bt[blockID]["collider"]), options={"box","ramp","half"}}
numberOfFrames = 0
for i,frame in pairs(bt[blockID]["sprite"]["frames"]) do
dlg:label{id="fl"..i ,label="frame ".. i,text=frame["startX"]..", "..frame["startY"]}
dlg:button{id="fd"..i,text="-",
onclick=function()
saveBlock(dlg,bt[blockID])
bt[blockID]["sprite"]["frames"][i] = nil
dlg:close{}
editBlock(blockID,bt,parentdlg,minNum,btjname)
end
}
numberOfFrames = numberOfFrames+1
end
dlg:separator{}
dlg:button{id="addframe",text="add frame",
onclick= function ()
addFrame(bt[blockID])
saveBlock(dlg,bt[blockID])
dlg:close{}
editBlock(blockID,bt,parentdlg,minNum,btjname)
end}
dlg:newrow{}
dlg:button{ id="save", text="Save",
onclick= function()
saveBlock(dlg,bt[blockID])
dlg:close{}
parentdlg:close{}
openbt(bt,btjname,minNum)
end
}
dlg:button{ id="cancel", text="Cancel" , onclick=function () dlg:close{} end }
dlg:show{wait = false}
end
function save(bt,btPath)
local outputFile = io.open(btPath, "w")
outputFile:write(json.encode(bt))
outputFile:close()
end
function openbt(bt,btjname,minNum)
local dlg = Dialog()
local counter = 0
local length = 0
for bid,block in pairs(bt) do
length = length + 1
if bid >= minNum and counter < 15 then
dlg:button{id = bid-1, label = bid-1, text = block["name"],
onclick= function ()
editBlock(bid,bt,dlg,minNum,btjname)
end
}
dlg:newrow()
counter = counter + 1
end
end
dlg:separator{}
dlg:button{id = "newBlock", text = "+",
onclick = function()
newBlock(bt)
editBlock(#bt,bt,dlg,minNum,btjname)
end
}
dlg:newrow()
dlg:button{id="prev",text="<",onclick=
function()
local newStart = minNum - pageSize
if newStart < 0 then
newStart = 0
end
dlg:close{}
openbt(bt,btjname,newStart)
end
}
dlg:button{id="next",text=">",onclick=
function()
local newStart = minNum + pageSize
if newStart < 0 then
newStart = 0
end
dlg:close{}
openbt(bt,btjname,newStart)
end
}
dlg:newrow()
dlg:button{ id="save", text="Save",
onclick= function()
save(bt,btjname)
dlg:close()
end
}
dlg:button{ id="cancel", text="Cancel" , onclick=function () dlg:close{} end}
dlg:show{wait = false}
end
--MAIN
local dlg = Dialog()
dlg:file{ id="btjson",
label="Open blocktable.json",
title="Open blockTable.json",
open=true,
save=false,
filename="/home/klanc/Projects/Uni/TFG/Z5/data/blockTable.json",
filetypes={ "json" },
}
dlg:button{ id="ok", text="OK" }
dlg:button{ id="cancel", text="Cancel" }
dlg:show{}
local data = dlg.data
if data.ok then
local fileContents = readAll(data.btjson)
local blockTable = json.decode(fileContents)
openbt(blockTable,data.btjson,0)
end
if data.cancel then
return
end
--[[
TODO
Backup blocktable before editing
Ability to delete blocks
]]