aboutsummaryrefslogtreecommitdiff
path: root/gamemode/utility/svg/cl_svg.lua
diff options
context:
space:
mode:
Diffstat (limited to 'gamemode/utility/svg/cl_svg.lua')
-rw-r--r--gamemode/utility/svg/cl_svg.lua98
1 files changed, 98 insertions, 0 deletions
diff --git a/gamemode/utility/svg/cl_svg.lua b/gamemode/utility/svg/cl_svg.lua
new file mode 100644
index 0000000..623c9e1
--- /dev/null
+++ b/gamemode/utility/svg/cl_svg.lua
@@ -0,0 +1,98 @@
+local svg = {}
+local fn = nrequire("fn.lua")
+local file_cache_max_size = 100
+local file_cache_size = 0
+local file_cache = {}
+
+local function readsvg(path)
+ if file_cache[path] == nil then
+ if file_cache_size > file_cache_max_size then
+ local k,_ = next(file_cache) --Random replacement
+ file_cache[k] = nil
+ end
+ file_cache[path] = file.Read(path,"GAME")
+ end
+ return file_cache[path]
+end
+
+local function updatesvg(self)
+ if self.path ~= self.svgpath then
+ self.svgdata = readsvg(self.path)
+ assert(self.svgdata ~= nil,"Could not open file:" .. self.path)
+ self.svgpath = self.path
+ end
+ local bgf = ""
+ local fgf = ""
+ if self.bg ~= nil then
+ bgf = string.format("svg{background:%s}",self.bg)
+ end
+ if self.fg ~= nil then
+ fgf = string.format("svg path{fill:%s}",self.fg)
+ end
+ self.html:SetHTML(string.format([[
+<style>%s%sbody{overflow:hidden}</style><body>%s</body>
+]],bgf,fgf,self.svgdata))
+end
+
+local function updatesvgimg(self,newpath)
+ self.path = newpath
+ self:Update()
+end
+
+local function updatesvgfg(self,fore)
+ self.fg = fore
+ self:Update()
+end
+
+local function updatesvgbg(self,back)
+ self.bg = back
+ self:Update()
+end
+
+local toprocess = {}
+function svg.MaterialFromSVG(spath,background,foreground)
+ local html = vgui.Create("DHTML")
+ local svgdata = readsvg(spath)
+ local bgf = ""
+ local fgf = ""
+ if background ~= nil then
+ bgf = string.format("svg{background:%s}",background)
+ end
+ if foreground ~= nil then
+ fgf = string.format("svg path{fill:%s}",foreground)
+ end
+ html:SetHTML(string.format([[
+<style>%s%sbody{overflow:hidden}</style><body>%s</body>
+]],bgf,fgf,svgdata))
+ local mat = {}
+ toprocess[#toprocess + 1] = {mat,html}
+ return mat
+end
+
+hook.Add("Think","process_svg_materials",function()
+ for k,v in ipairs(toprocess) do
+ local hm = v[2]:GetHTMLMaterial()
+ if hm then
+ v[1].material = hm
+ end
+ for i = k,#toprocess do
+ toprocess[i] = toprocess[i + 1]
+ end
+ end
+end)
+
+function svg.SvgOnDpanel(spath,background,foreground,dpanel)
+ local ret = {}
+ ret.html = vgui.Create("DHTML",dpanel)
+ ret.path = spath
+ ret.fg = foreground
+ ret.bg = background
+ ret.UpdateForeground = updatesvgfg
+ ret.UpdateBackground = updatesvgbg
+ ret.UpdateImage = updatesvgimg
+ ret.Update = updatesvg
+ ret:Update()
+ return ret
+end
+
+return svg