-
Notifications
You must be signed in to change notification settings - Fork 6
/
iso tileset export.lua
125 lines (107 loc) · 3.78 KB
/
iso tileset export.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
local downscale = false
local currentImage = {
width = app.activeSprite.width,
height = app.activeSprite.height,
}
local path,title = app.activeSprite.filename:match("^(.+[/\\])(.-).([^.]*)$")
function ToogleDownscale()
downscale = not downscale
end
function ValidateInput(dialogData)
local tileWidth = dialogData.tileWidth
if(tileWidth % 2 ~= 0) then
print("The Width/Height value must be an even number")
return false
end
return true
end
function CopyImage(fromImage, rect, newImageSize)
local pixelsFromSelection = fromImage:pixels(rect)
local selectedImage = Image(newImageSize.x, newImageSize.y)
for it in pixelsFromSelection do
local pixelValue = it()
selectedImage:putPixel(it.x - rect.x, it.y - rect.y, pixelValue)
end
return selectedImage
end
function PlotImage(fromImage, toImage, putPoint)
for pix in fromImage:pixels() do
local pixelValue = pix()
toImage:putPixel(pix.x + putPoint.x, pix.y + putPoint.y, pixelValue)
end
end
function CreateIsoTile(fromImage, rect, newImageSize)
local pixelsFromSelection = fromImage:pixels(rect)
local selectedImage = Image(newImageSize.x, newImageSize.y)
for it in pixelsFromSelection do
local pixelValue = it()
local newx = (newImageSize.x/2-2)+it.x*2-it.y*2
local newy = it.x+it.y
local stopx = newx+3
for tempx=newx,stopx,1 do
--add one to y axis so the resize would work
selectedImage:putPixel(tempx, newy+1, pixelValue)
end
end
return selectedImage
end
function ConvertToIso(dialogData)
local tileWidth = dialogData.tileWidth
local savePath = dialogData.filename
local sprite = app.activeSprite
local fromImage = Image(sprite)
local numberOfWidthTiles = math.floor(currentImage.width / tileWidth)
local numberOfHeightTiles = math.floor(currentImage.height / tileWidth)
local newWidth = tileWidth * 4
local newHeight = tileWidth * 2
local newImageWidth = numberOfWidthTiles * newWidth
local newImageHeight = numberOfHeightTiles * newHeight
local tileSize = Point(tileWidth * 4,tileWidth * 2)
local newImage = Image(newImageWidth, newImageHeight)
if(downscale) then
newImage:resize(newImage.width/2, newImage.height/2)
newWidth = newWidth/2
newHeight = newHeight/2
end
for columns = 0, numberOfHeightTiles - 1 do
for rows = 0, numberOfWidthTiles - 1 do
local toRect = Rectangle(rows*newWidth, columns*newHeight, newWidth, newHeight)
local fromRect = Rectangle(rows*tileWidth, columns*tileWidth, tileWidth, tileWidth)
--Move current image to 0,0 to skip working with offsets
local resetImage = CopyImage(fromImage, fromRect, Point(tileWidth, tileWidth))
local isometricTile = CreateIsoTile(resetImage, Rectangle(0,0,tileWidth, tileWidth), tileSize)
if downscale then
isometricTile:resize{width=(tileWidth*2),height=tileWidth}
isometricTile = CopyImage(isometricTile, Rectangle(0,1,tileWidth*2,tileWidth), Point(tileWidth * 2, tileWidth))
else
isometricTile = CopyImage(isometricTile, Rectangle(0,1,tileWidth*4,tileWidth*2), tileSize)
end
PlotImage(isometricTile,newImage,toRect)
end
end
newImage:saveAs(savePath)
end
local dialog = Dialog("Isometric Tileset export")
dialog
:slider{id="tileWidth",
min=4,
max=currentImage.width,
value=16,
label="Width/Height of tiles:" }
:newrow()
:check{id="downscale", text="downscale", onclick=ToogleDownscale}
:file{ id="filename",
label="Filename:",
title="Filename to save to",
open=false,
save=true,
filename=path .. "iso-" .. title .. ".png",
filetypes={ "png" }}
:separator()
:button{text="Export",onclick=function()
if(ValidateInput(dialog.data)) then
ConvertToIso(dialog.data)
dialog:close()
end
end}
:show{wait=true}