Advertisement
szymski

Untitled

Feb 4th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. Component:CreateSetting("maxurlmaterials", 15)
  2. Component:CreateSetting("maxurlmatsize", 512)
  3.  
  4. function Component:OnRegisterContext(Context)
  5.     Context.Data.Materials = { }
  6. end
  7.  
  8. EXPADV.ClientOperators()
  9.  
  10. local TextureSize = Component:ReadSetting("maxurlmatsize", 512)
  11. local HTML = HTML
  12. local URLQueue = { }
  13. local CanLoad = true
  14.  
  15. local function Download(Context, Name, URL, Width, Height)
  16.     if HTML && IsValid(HTML) then
  17.         HTML:Remove()
  18.     end
  19.  
  20.     local pnl = vgui.Create("HTML")
  21.     pnl:SetVisible(true)
  22.     pnl:SetPos(ScrW()-1, ScrH()-1)
  23.     pnl:SetSize(TextureSize, TextureSize)
  24.     pnl:SetHTML(
  25.         [[     
  26.             <style type="text/css">
  27.                 html
  28.                 {          
  29.                     margin: 0px 0px;
  30.                     overflow:hidden;
  31.                 }
  32.             </style>
  33.  
  34.             <body>
  35.                 <img src="]] .. URL .. '"width="' .. Width .. '" height="' .. Height .. [[" />
  36.             </body>
  37.         ]]
  38.     )
  39.     HTML = pnl
  40.  
  41.     local uid = "ea2urlmaterial_" .. Name .. Context.entity:EntIndex()
  42.  
  43.     local spawn, nextUpdate = RealTime(), RealTime() + 0.5
  44.     hook.Add("Think", uid, function()
  45.         if !IsValid(Context.entity) || !IsValid(pnl) || RealTime() - spawn > 5 then
  46.             pnl:Remove()
  47.             CanLoad = true
  48.             hook.Remove("Think", uid)
  49.             return
  50.         end
  51.  
  52.         if RealTime() < nextUpdate then return end
  53.  
  54.         nextUpdate = RealTime() + 0.1
  55.  
  56.         if pnl:IsLoading() then
  57.             return
  58.         end
  59.  
  60.         local mat = pnl:GetHTMLMaterial()
  61.  
  62.         if !mat then return end
  63.  
  64.         local vertex_mat = CreateMaterial("ea2urlmat_" .. Name, "UnlitGeneric", { ["$vertexcolor"] = 1, ["$vertexalpha"] = 1, ["$ignorez"] = 1, ["$nolod"] = 1 } ) 
  65.         local tex = mat:GetTexture("$basetexture")
  66.         tex:Download()
  67.         vertex_mat:SetTexture("$basetexture", tex)             
  68.         Context.Data.Materials[Name] = vertex_mat
  69.  
  70.         pnl:Remove()
  71.         CanLoad = true
  72.         hook.Remove("Think", uid)
  73.     end)
  74. end
  75.  
  76. hook.Add("Think", "EA2_UrlTexture", function()
  77.     if #URLQueue > 1 && CanLoad then
  78.         CanLoad = false
  79.         Download(unpack(URLQueue[#URLQueue]))
  80.         table.remove(URLQueue, #URLQueue)
  81.         print("Loaded " .. RealTime())
  82.     end
  83.     if #URLQueue == 0 then CanLoad = true end
  84. end)
  85.  
  86. Component:AddVMFunction("downloadURLMaterial", "s,s,n,n", "", function(Context, Trace, Name, URL, Width, Height)
  87.     if (Context.Data.Materials[Name] || #Context.Data.Materials < Component:ReadSetting("maxurlmaterials", 15)) && #URLQueue < 10 then
  88.         Context.Data.Materials[Name] = Context.Data.Materials[Name] or Material("debug/debugempty")
  89.         table.insert(URLQueue, { Context, Name, URL, math.Clamp(Width or TextureSize, 1, TextureSize), math.Clamp(Height or TextureSize, 1, TextureSize) })
  90.     end
  91. end)
  92. EXPADV.AddFunctionAlias("downloadURLMaterial", "s,s")
  93.  
  94. Component:AddFunctionHelper("downloadURLMaterial", "s,s,n,n", "Downloads a material from specified URL (name,url,width,height).")
  95.  
  96. Component:AddPreparedFunction("setURLMaterial", "s", "", [[
  97. if Context.Data.Materials && Context.Data.Materials[@value 1] then
  98.     @define mat = Context.Data.Materials[@value 1]
  99.     $render.SetMaterial(@mat)
  100.     $surface.SetMaterial(@mat)
  101. end
  102. ]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement