-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slice by.lua
155 lines (141 loc) · 5.14 KB
/
Slice by.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
----------------------------------------------------------------------
-- A slice tool script
-- Autohr: Shitake
----------------------------------------------------------------------
local TYPE_AUTO = "Automatic"
local TYPE_BY_SIZE = "Grid by Cell Size"
local TYPE_BY_COUNT = "Grid by Cell Count"
local METHOD_SMALL = "Small"
local METHOD_SAFE = "Safe"
local sprite = app.activeSprite
local dlg_conf = Dialog("Slice Sprite Type")
dlg_conf
:combobox{ id="type",
label="Type",
option=TYPE_BY_SIZE,
options={ TYPE_BY_SIZE, TYPE_BY_COUNT }}
-- TODO: wait support
-- options={ TYPE_AUTO, TYPE_BY_SIZE, TYPE_BY_COUNT } }
:check{ id="clear", text="Clear old slice", selected=true }
:color{ id="color", color=Color{ r=255, g=255, b=255, a=180 } }
:button{ id="ok", text="&OK", focus=true }
:button{ text="&Cancel" }
local dlg_slice_by_auto_data = Dialog("Auto Slice method")
dlg_slice_by_auto_data
:combobox{ id="method",
label="Method",
option=METHOD_SMALL,
options={ METHOD_SMALL, METHOD_SAFE } }
:separator{ label="Pivot", text="Pivot" }
:number{ id="pivot_x", label="X:", text="0", decimals=integer }
:number{ id="pivot_y", label="Y:", text="0", decimals=integer }
:button{ id="ok", text="&OK", focus=true }
:button{ id="back", text="&Back" }
local dlg_slice_by_size_data = Dialog("Slice Data(Size)")
dlg_slice_by_size_data
:separator{ label="Cell Size", text="Cell Size" }
:number{ id="width", label="Width:", text="8", decimals=integer }
:number{ id="height", label="Height:", text="8", decimals=integer }
:separator{ label="Padding", text="Padding" }
:number{ id="padding_x", label="X:", text="0", decimals=integer }
:number{ id="padding_y", label="Y:", text="0", decimals=integer }
:separator{ label="Pivot", text="Pivot" }
:number{ id="pivot_x", label="X:", text="0", decimals=integer }
:number{ id="pivot_y", label="Y:", text="0", decimals=integer }
:button{ id="ok", text="&OK", focus=true }
:button{ id="back", text="&Back" }
local dlg_slice_by_count_data = Dialog("Slice Data(Count)")
dlg_slice_by_count_data
:number{ id="column", label="Column:", text="1", decimals=integer }
:number{ id="row", label="Row:", text="1", decimals=integer }
:separator{ label="Padding", text="Padding" }
:number{ id="padding_x", label="X:", text="0", decimals=integer }
:number{ id="padding_y", label="Y:", text="0", decimals=integer }
:separator{ label="Pivot", text="Pivot" }
:number{ id="pivot_x", label="X:", text="0", decimals=integer }
:number{ id="pivot_y", label="Y:", text="0", decimals=integer }
:button{ id="ok", text="&OK", focus=true }
:button{ id="back", text="&Back" }
function slice_by_auto_show()
dlg_slice_by_auto_data:show()
if dlg_slice_by_auto_data.data.ok then
slice_by_auto()
end
if dlg_slice_by_auto_data.data.back then main() end
end
function slice_by_size_show()
dlg_slice_by_size_data:show()
if dlg_slice_by_size_data.data.ok then
slice_by_size()
end
if dlg_slice_by_size_data.data.back then main() end
end
function slice_by_count_show()
dlg_slice_by_count_data:show()
if dlg_slice_by_count_data.data.ok then
slice_by_count()
end
if dlg_slice_by_count_data.data.back then main() end
end
function slice_by_auto()
clear_slice()
data = dlg_slice_by_count_data.data
end
function slice_by_size()
clear_slice()
data = dlg_slice_by_size_data.data
cell_width = data.width
cell_height = data.height
index = 0
column = 0
while column * cell_width < sprite.width do
row = 0
while row * cell_height < sprite.height do
slice = sprite:newSlice(Rectangle(
column * cell_width + data.padding_x,
row * cell_height + data.padding_y,
cell_width - data.padding_x * 2,
cell_height - data.padding_y * 2))
slice.color = dlg_conf.data.color
slice.name = sprite.filename .. "_" .. index
slice.pivot = Point(data.pivot_x, data.pivot_y)
index = index + 1
row = row + 1
end
column = column + 1
end
end
function slice_by_count()
clear_slice()
data = dlg_slice_by_count_data.data
cell_width = math.floor(sprite.width / data.column)
cell_height = math.floor(sprite.height / data.row)
index = 0
for column = 0, data.column - 1 do
for row = 0, data.row - 1 do
slice = sprite:newSlice(Rectangle(
column * cell_width + data.padding_x,
row * cell_height + data.padding_y,
cell_width - data.padding_x * 2,
cell_height - data.padding_y * 2))
slice.color = dlg_conf.data.color
slice.name = sprite.filename .. "_" .. index
slice.pivot = Point(data.pivot_x, data.pivot_y)
index = index + 1
end
end
end
function clear_slice()
if not dlg_conf.data.clear then return end
for i, s in ipairs(sprite.slices) do
sprite:deleteSlice(s)
end
end
function main()
dlg_conf:show()
if not dlg_conf.data.ok then return end
if dlg_conf.data.type == TYPE_AUTO then slice_by_auto_show() end
if dlg_conf.data.type == TYPE_BY_SIZE then slice_by_size_show() end
if dlg_conf.data.type == TYPE_BY_COUNT then slice_by_count_show() end
end
main()