-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
137 lines (124 loc) · 5 KB
/
Copy pathmain.lua
File metadata and controls
137 lines (124 loc) · 5 KB
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
function love.load()
json = require("dkjson")
drawingCanvas = love.graphics.newCanvas(200,200)
resolutionMult = 1
multiFrame = false
end
function love.update(dt)
end
function love.draw()
local spacing = 16
if not hasFile then
love.graphics.print("Please drop a .json file!",5,5)
love.graphics.print(resolutionMult.."x Resolution",5,5+spacing*1)
love.graphics.print("Scroll to change",300,5+spacing*1)
love.graphics.print("Multiframe: "..tostring(multiFrame),300,5+spacing*2)
love.graphics.print("Press F to toggle Multiframe",250,5+spacing*3)
else
love.graphics.print("Successfully exported at "..resultScale.." x Resolution",5,5)
love.graphics.print(resolutionMult.."x Resolution",5,5+spacing*1)
love.graphics.print("Scroll to change",300,5+spacing*1)
love.graphics.print("Multiframe: "..tostring(multiFrame),300,5+spacing*2)
love.graphics.print("Press F to toggle Multiframe",250,5+spacing*3)
love.graphics.print("Original Scale: "..(resultDimensions[1]/resultScale).." x "..(resultDimensions[2]/resultScale),5,5+spacing*2)
love.graphics.print("Scaled Resolution: "..resultDimensions[1].." x "..resultDimensions[2],5,5+spacing*3)
love.graphics.print(resultName,5,5+spacing*4)
end
end
function love.filedropped(file)
hasFile = true
local shapeTypeOffset = 0
local data = file:read()
local shapes = json.decode(data)
if shapes.shapes then
shapes = shapes["shapes"]
shapeTypeOffset = 1
end
local fileName = file:getFilename()
fileName = fileName:match("([^\\/]+)$").."_"..tostring(resolutionMult).."x_render"
local width = shapes[1]["data"][3]*resolutionMult
local height = shapes[1]["data"][4]*resolutionMult
drawingCanvas = love.graphics.newCanvas(width,height)
love.graphics.setLineWidth(1)
love.graphics.setCanvas(drawingCanvas)
love.graphics.clear()
love.graphics.rectangle("fill",0,0,width,height)
love.graphics.push()
love.graphics.scale(resolutionMult,resolutionMult)
for ishape,shape in ipairs(shapes) do
love.graphics.setColor(shape.color[1]/255,shape.color[2]/255,shape.color[3]/255,shape.color[4]/255)
if shape.type == 0+shapeTypeOffset then --Rectangle
love.graphics.rectangle("fill",shape.data[1],shape.data[2],shape.data[3]-shape.data[1],shape.data[4]-shape.data[2])
elseif shape.type == 1+shapeTypeOffset then --Rotated Rectangle
local x1,y1,x2,y2,angleDeg = unpack(shape.data)
local cx = (x1 + x2) / 2
local cy = (y1 + y2) / 2
local w = math.abs(x2 - x1)
local h = math.abs(y2 - y1)
love.graphics.push()
love.graphics.translate(cx, cy)
love.graphics.rotate(math.rad(angleDeg))
love.graphics.rectangle("fill", -w/2, -h/2, w, h)
love.graphics.pop()
elseif shape.type == 2+shapeTypeOffset then --Triangle
love.graphics.polygon("fill",shape.data)
elseif shape.type == 3+shapeTypeOffset then --Ellipse
love.graphics.ellipse("fill",shape.data[1],shape.data[2],shape.data[3],shape.data[4])
elseif shape.type == 4+shapeTypeOffset then --Rotated Ellipse
love.graphics.push()
love.graphics.translate(shape.data[1], shape.data[2])
love.graphics.rotate(math.rad(shape.data[5]))
love.graphics.ellipse("fill",0,0,shape.data[3],shape.data[4])
love.graphics.pop()
elseif shape.type == 5+shapeTypeOffset then --Circle
love.graphics.circle("fill",shape.data[1],shape.data[2],shape.data[3])
elseif shape.type == 6+shapeTypeOffset then --Line
love.graphics.line(shape.data)
elseif shape.type == 7+shapeTypeOffset then --Quadratic Bezier
local x1,y1,cx,cy,x2,y2 = unpack(shape.data)
local points = {}
local steps = 32 -- more steps = smoother curve
for i = 0, steps do
local t = i / steps
local xt = (1 - t)^2 * x1 + 2 * (1 - t) * t * cx + t^2 * x2
local yt = (1 - t)^2 * y1 + 2 * (1 - t) * t * cy + t^2 * y2
table.insert(points, xt)
table.insert(points, yt)
end
love.graphics.line(points)
elseif shape.type == 256 then --Polyline (why 256??)
love.graphics.line(shape.data)
end
if multiFrame then
love.graphics.setCanvas()
local img = drawingCanvas:newImageData()
img:encode("png", fileName..ishape..".png")
love.graphics.setCanvas(drawingCanvas)
print("Exporting frame "..ishape.."/"..#shapes)
end
end
love.graphics.pop()
love.graphics.setCanvas()
love.graphics.setColor(1,1,1,1)
if not multiFrame then
local img = drawingCanvas:newImageData()
img:encode("png", fileName..".png")
end
resultDimensions = {width,height}
resultName = fileName..".png"
resultScale = resolutionMult
if love.system.getOS() == "Windows" then
local path = love.filesystem.getSaveDirectory()
os.execute('start "" "' .. path .. '"')
end
love.draw()
end
function love.wheelmoved( x, y )
resolutionMult = resolutionMult + y
resolutionMult = math.max(1,resolutionMult)
end
function love.keypressed(key, scancode, isrepeat)
if key == "f" then
multiFrame = not multiFrame
end
end