-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvg.lua
56 lines (51 loc) · 1.33 KB
/
svg.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
--SVG.lua
--make this work from anywhere
local __path = ((...):match('(.*%.)[^%.]+$') or '')
local __oldRequire = require
require = function(s) --has to be global to work in other files
return __oldRequire(__path..s)
end
local XmlParser = require "XmlParser"
local Shape = require "shape"
require = __oldRequire --return to normal functionality after
local SVG = {}
SVG.__index = SVG
setmetatable(SVG, {
__call = function (...)
return SVG.new(...)
end,
})
function SVG.new(garbage, path)
local file = love.filesystem.read(path)
local xml = XmlParser:ParseXmlText(file)
local self = setmetatable({}, SVG)
self.width = tonumber(xml.Attributes.width)
self.height = tonumber(xml.Attributes.height)
self.shapes = {}
for i,node in pairs(xml.ChildNodes) do
self.shapes[i] = Shape(node)
end
self.canvas = love.graphics.newCanvas()
self.canvas:setFilter("nearest")
self:repaint()
return self
end
function SVG:repaint()
local oldCanvas = love.graphics.getCanvas()
love.graphics.setCanvas(self.canvas)
for i,v in ipairs(self.shapes) do
--v:repaint()
end
love.graphics.setCanvas(oldCanvas)
end
function SVG:draw(px, py, s)
love.graphics.push()
love.graphics.translate(px,py)
love.graphics.scale(s)
love.graphics.setColor(255,255,255,255)
for i,shape in ipairs(self.shapes) do
shape:draw()
end
love.graphics.pop()
end
return SVG