CapsAdmin

Untitled

Jul 27th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. local urlmat = pac.urlmat or {}
  2.  
  3. urlmat.TextureSize = 1024
  4.  
  5. urlmat.Panels = urlmat.Panels or {}
  6.  
  7. function urlmat.Panic()
  8.     for id, pnl in pairs(urlmat.Panels) do
  9.         if pnl:IsValid() then
  10.             pnl:Remove()
  11.         end
  12.        
  13.         timer.Remove(id)
  14.     end
  15. end
  16.  
  17. function urlmat.GetMaterialFromURL(url, callback, texture_only)
  18.     local id = "html_material_" .. url
  19.    
  20.     if urlmat.Panels[id] and urlmat.Panels[id]:IsValid() then
  21.         urlmat.Panels[id]:Remove()
  22.     end
  23.    
  24.     local pnl= vgui.Create("HTML")
  25.     urlmat.Panels[id] = pnl
  26.     pnl:SetPos(ScrW(),ScrH())
  27.     pnl:SetSize(urlmat.TextureSize, urlmat.TextureSize)
  28.     pnl:SetHTML([[
  29.         <style type="text/css">
  30.         html {         
  31.             background-color:black;
  32.            
  33.             margin-top:0px;
  34.             margin-top:0px;
  35.             margin-top:0px;
  36.             margin-top:0px;
  37.            
  38.             padding-top:0px;
  39.             padding-top:0px;
  40.             padding-top:0px;
  41.             padding-top:0px;
  42.            
  43.             overflow:hidden;
  44.         }
  45.        
  46.         </style>
  47.  
  48.         <body>
  49.             <img src="]] .. url .. [[" alt="" width="]]..urlmat.TextureSize..[[" height="]]..urlmat.TextureSize..[[" />
  50.         </body>
  51.     ]])
  52.    
  53.     pac.dprint("loading material %q", url)
  54.     function pnl.FinishedURL()
  55.         pac.dprint("finished loading material %q", url)
  56.  
  57.         local i = 0
  58.         timer.Create(id, 0, 200, function()
  59.            
  60.             -- timeout
  61.             if i == 200 then
  62.                 if pnl:IsValid() then
  63.                     pnl:Remove()
  64.                 end
  65.                
  66.                 timer.Remove(id)
  67.                 urlmat.Panels[id] = nil
  68.                 return
  69.             end
  70.            
  71.             -- panel is no longer valid
  72.             if not pnl:IsValid() then
  73.                 timer.Remove(id)
  74.                 urlmat.Panels[id] = nil
  75.                 return
  76.             end
  77.            
  78.             local mat = pnl:GetHTMLMaterial()  
  79.             if mat then
  80.                 if texture_only then
  81.                     callback(mat:GetMaterialTexture("$basetexture"))
  82.                 else
  83.                     local newmat = CreateMaterial(name or id, "VertexLitGeneric", shader_params)
  84.                     newmat:SetMaterialTexture("$basetexture",   mat:GetMaterialTexture("$basetexture"))
  85.                
  86.                     callback(newmat)
  87.                 end
  88.                
  89.                 pac.dprint("got material %q", url)
  90.                
  91.                 pnl:Remove()
  92.                 timer.Remove(id)
  93.                 urlmat.Panels[id] = nil
  94.             end
  95.            
  96.             i = i + 1
  97.         end)
  98.     end
  99. end
  100.  
  101. urlmat.Panic()
  102.  
  103. pac.urlmat = urlmat
Advertisement
Add Comment
Please, Sign In to add comment