CapsAdmin

Untitled

Aug 26th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | None | 0 0
  1. setfenv(1, _G)
  2.  
  3. local function to_markup(str)
  4.     local current_tag
  5.     local current_string
  6.     local markup = {}
  7.     local found_tag = false
  8.    
  9.     for char in tostring(str):gmatch("(.)") do
  10.         if char == "<" then
  11.             current_tag = {}
  12.             if current_string then
  13.                 table.insert(markup, {type = "text", text = table.concat(current_string, "")})
  14.                 current_string = nil
  15.             end
  16.         end
  17.        
  18.         if current_tag then
  19.             table.insert(current_tag, char)
  20.            
  21.             if char == ">" then
  22.                 local tag, args = table.concat(current_tag):match("<(%l-)=(.+)>")
  23.                 if tag and args then
  24.                     args = args:Split(",")
  25.                     table.insert(markup, {type = "tag", tag = tag, args = args})
  26.                 end
  27.                 current_tag = nil
  28.                 found_tag = true
  29.             end
  30.         else
  31.             current_string = current_string or {}
  32.             table.insert(current_string, char)
  33.         end
  34.     end
  35.  
  36.     if current_string then
  37.         table.insert(markup, {type = "text", text = table.concat(current_string, "")})
  38.         current_string = nil
  39.     end
  40.  
  41.     if found_tag then
  42.         return markup
  43.     end
  44. end
  45.  
  46. local function GetMaterialFromURL(url, size)
  47.     local mat = CreateMaterial("fishing_hud_rc_" .. util.CRC(url) .. "_" .. FrameNumber(), "UnlitGeneric", {})
  48.  
  49.     pac.urltex.GetMaterialFromURL(url, function(_mat)
  50.         mat:SetTexture("$basetexture", _mat:GetTexture("$basetexture"))
  51.     end, nil, "UnlitGeneric", size, false)
  52.  
  53.     mat:SetFloat("$alpha", 0.999)
  54.  
  55.     return mat
  56. end
  57.    
  58. old_drawtext = old_drawtext or surface.DrawText local old_drawtext = old_drawtext
  59. old_settextpos = old_settextpos or surface.SetTextPos local old_settextpos = old_settextpos
  60. old_settextcolor = old_settextcolor or surface.SetTextColor local old_settextcolor = old_settextcolor
  61.  
  62. local X, Y = 0, 0
  63. local R, G, B, A = 0, 0 ,0 ,0
  64. function surface.SetTextColor(r, g, b, a)
  65.     if type(r) == "table" then
  66.         g = r.g
  67.         b = r.b
  68.         a = r.a
  69.         r = r.r    
  70.     end
  71.     R = r
  72.     G = g
  73.     B = b
  74.     A = a
  75.     return old_settextcolor(r, g, b ,a)
  76. end
  77.  
  78.  
  79. function surface.SetTextPos(x, y)
  80.     X = x
  81.     Y = y
  82.     return old_settextpos(x, y)
  83. end
  84.  
  85. local old_x, old_y, old_r, old_g, old_b, old_a
  86.  
  87.  
  88. local cache = {}
  89.  
  90. function surface.DrawText(str)
  91.    
  92.     local markup = cache[str]
  93.    
  94.     if not markup then
  95.         markup = to_markup(str) or true
  96.         cache[str] = markup
  97.     end
  98.    
  99.     if markup and markup ~= true then
  100.         -- umm
  101.         markup.env = markup.env or {}
  102.        
  103.         local w = 0
  104.        
  105.         old_x = X
  106.         old_y = Y
  107.         old_r = R
  108.         old_g = G
  109.         old_b = B
  110.         old_a = A
  111.        
  112.         for key, data in pairs(markup) do
  113.             if data.type == "text" then
  114.                 old_settextpos(X + w, Y)
  115.                 old_drawtext(data.text)
  116.                 w = w + surface.GetTextSize(data.text)
  117.             elseif data.type == "tag" then
  118.                 if data.tag == "color" then
  119.                     old_settextcolor(
  120.                         tonumber(data.args[1]) or R,
  121.                         tonumber(data.args[2]) or G,
  122.                         tonumber(data.args[3]) or B,
  123.                         tonumber(data.args[4]) or A
  124.                     )  
  125.                 elseif data.tag == "texture" then
  126.                     if not data.mat then
  127.                         if data.args[1]:find("http") then
  128.                             data.mat = GetMaterialFromURL(data.args[1], 128)
  129.                         else
  130.                             local mat = Material(data.args[1])
  131.                                
  132.                             local shader = mat:GetShader()
  133.                            
  134.                             if shader == "VertexLitGeneric" or shader == "Cable" then
  135.                                 local tex_path = mat:GetString("$basetexture")
  136.                                
  137.                                 if tex_path then       
  138.                                     local params = {}
  139.                                    
  140.                                     params["$basetexture"] = tex_path
  141.                                     params["$vertexcolor"] = 1
  142.                                     params["$vertexalpha"] = 1
  143.                                    
  144.                                     mat = CreateMaterial("markup_fixmat_" .. tex_path .. os.clock(), "UnlitGeneric", params)
  145.                                 end    
  146.                             end
  147.                        
  148.                             data.mat = mat
  149.                         end
  150.                     else
  151.                        
  152.                         local _, h = surface.GetTextSize(str)
  153.                         local mult = tonumber(data.args[2])
  154.                         if mult then
  155.                             h = h * mult
  156.                         end
  157.                         surface.SetDrawColor(255, 255, 255, 255)
  158.                         surface.SetMaterial(data.mat)
  159.                         surface.DrawTexturedRect(X + w + 4, Y, h, h)
  160.                                                
  161.                         w = w + h + 8
  162.                     end
  163.                 end
  164.             end
  165.         end
  166.    
  167.         if old_x then
  168.             old_settextpos(old_x, old_y)
  169.         end
  170.        
  171.         if old_r then
  172.             old_settextcolor(old_r, old_g, old_b, old_a)
  173.         end
  174.        
  175.         return
  176.     end
  177.    
  178.     return old_drawtext(str)   
  179. end
  180.  
  181. chat.AddText("LOLOLOL<texture=http://images.wikia.com/uncyclopedia/images/8/80/Smiley.png><color=2,55,52>LOLOLOLOL")
Advertisement
Add Comment
Please, Sign In to add comment