Python1320

Untitled

Oct 8th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.38 KB | None | 0 0
  1. local cl_shownewhud=CreateConVar("cl_shownewhud","1",true,false)
  2. local Tag="chathud"
  3.  
  4.  
  5.  
  6.  
  7. ----------------------------------------
  8. -- Chat box thingy for Python's server
  9. -- By Divran
  10. ----------------------------------------
  11.  
  12. ----------------------------------------
  13. -- Options
  14. local startfadetime         = 10    -- Time before the text STARTS to fade (Don't make this smaller than fadeintime)
  15. local fadetime              = 2     -- Time it takes for the text to fade from 255 to 0 alpha
  16. local fadeintime            = 0.5   -- Time it takes for the text to fade from 0 to 255 alpha
  17. local lifetime              = startfadetime + fadetime -- Should be left as is
  18.  
  19. local base_font_size        = 17    -- Base size for chat messages
  20. local chatprint_font_size   = 15    -- Size for messages printed through Lua commands such as ChatPrint
  21.  
  22. local min_x                 = 10    -- Offset from side
  23. local max_x                 = 700   -- max_x - min_x = text width
  24. local min_y                 = 50    -- Offset frop top
  25. local max_y                 = 600   -- max_y - min_y = text height
  26.  
  27. local movespeed             = 150   -- Text animation move speed
  28.  
  29. local chatprint_color       = Color( 201,255,41)
  30. ----------------------------------------
  31.  
  32. ----------------------------------------
  33. -- Localized functions for speed
  34. ----------------------------------------
  35. local surface_CreateFont    = surface.CreateFont
  36. local surface_SetFont       = surface.SetFont
  37. local surface_SetTextPos    = surface.SetTextPos
  38. local surface_DrawText      = surface.DrawText
  39. local surface_SetTextColor  = surface.SetTextColor
  40. local size                  = surface.GetTextSize
  41.  
  42. local table_remove          = table.remove
  43. local table_Copy            = table.Copy
  44. local table_insert          = table.insert
  45.  
  46.  
  47. local string_gmatch         = string.gmatch
  48. local string_gsub           = string.gsub
  49. local string_sub            = string.sub
  50. local string_find           = string.find
  51. local string_match          = string.match
  52. local string_Explode        = string.Explode
  53.  
  54. local math_Clamp            = math.Clamp
  55. local math_Approach         = math.Approach
  56. local math_floor            = math.floor
  57. local math_max              = math.max
  58. local math_min              = math.min
  59.  
  60. local RealTime              = RealTime
  61. local FrameTime             = FrameTime
  62. local type                  = type
  63. local tostring              = tostring
  64.  
  65. local surface_GetTextSize   = function( str ) return size( string_gsub( str, "&", "%" ) ) end
  66.  
  67. ----------------------------------------
  68. -- Fonts
  69. ----------------------------------------
  70.  
  71. local fonts = {}
  72.  
  73.  
  74. local function GetFont( size )
  75.     if tonumber(size) == nil then return size end
  76.     size = tonumber(size)
  77.     if fonts[size] then return fonts[size] end
  78.  
  79.     surface_CreateFont( "Tahoma", size, 700, false, false, "chat_font_" .. size, true, false )
  80.     fonts[size] = "chat_font_"..size
  81.    
  82.     return fonts[size]
  83. end
  84.  
  85. ----------------------------------------
  86. -- Text drawing
  87. ----------------------------------------
  88. local data = {}
  89.  
  90.  
  91.  
  92. local function DrawTexts()
  93.     surface_SetFont( GetFont( base_font_size ) )
  94.    
  95.     for i=#data,1,-1 do
  96.         local line = data[i]
  97.  
  98.         -- Check fading
  99.         if RealTime() > line.lifetime then
  100.             table_remove( data, i )
  101.             continue
  102.         elseif RealTime() > line.startfade then
  103.             line.a = line.a - RealFrameTime()/fadetime*255
  104.             if line.a <= 0 then
  105.                 table_remove( data, i )
  106.                 continue
  107.             end
  108.         elseif line.a < 255 then
  109.             line.a = line.a + RealFrameTime()/fadeintime*255
  110.         end
  111.        
  112.         -- Check animation movement
  113.         if line.y ~= line.cury then
  114.             line.cury = math_Approach( line.cury, line.y, RealFrameTime() * movespeed )
  115.         end
  116.        
  117.         local x = min_x
  118.        
  119.         for j=1,#line do
  120.             local obj = line[j]
  121.            
  122.             local t = type(obj)
  123.             if t == "string" then
  124.                 local w,h = surface_GetTextSize( obj )
  125.                 local y = line.cury - h + line.maxh
  126.                 surface_SetTextPos( x, y )
  127.                 x = x + w
  128.            
  129.                 surface_DrawText( obj )
  130.             elseif t == "table" and obj.r and obj.g and obj.b then
  131.                 surface_SetTextColor( obj.r, obj.g, obj.b, line.a )
  132.             elseif t == "table" and obj[1] then
  133.                 surface_SetFont( GetFont( obj[1] ) )
  134.             end
  135.         end
  136.     end
  137.    
  138.     if #data == 0 then
  139.         hook.Remove( "HUDPaint", Tag )
  140.     end
  141. end
  142.  
  143. ----------------------------------------
  144. -- Word Wrapping
  145. ----------------------------------------
  146. local width = max_x-min_x
  147.  
  148. local function Wrap()  
  149.     local line = data[1]
  150.    
  151.    
  152.     --print("-----------")
  153.     --print("Checking line nr:", 1)
  154.    
  155.     local font = base_font_size
  156.  
  157.     --print("Max allowed width: " .. width)
  158.     local x = 0
  159.     for i=1,#line do
  160.         local text = line[i]
  161.         if type(text) == "table" and text[1] then
  162.             surface_SetFont( GetFont( text[1] ) )
  163.             font = text
  164.         elseif type(text) == "string" then     
  165.        
  166.             local startpos, endpos = string_find( text, "%b{}" )
  167.             if startpos and endpos then
  168.                 local word = string_sub( text, startpos+1, endpos-1 )
  169.                 line[i] = string_sub( text, 1, startpos-1 )
  170.                 local font = tonumber(word) or word
  171.                 table_insert( line, i + 1, { font } )
  172.  
  173.                 surface_SetFont( GetFont(font) )
  174.                 table_insert( line, i + 2, string_sub( text, endpos+1 ) )
  175.                
  176.                 Wrap()
  177.                 return
  178.             end
  179.            
  180.             --print("text:","'".. tostring(text) .. "'")
  181.             if x > width then
  182.                 --print("offset alone was too big")
  183.                 local newline = {startfade = RealTime() + startfadetime, lifetime = RealTime() + lifetime, a = 0,cury = max_y, [1] = font}
  184.                 table_insert( data, 1, newline )
  185.                
  186.                 for j=i,#line do
  187.                     newline[#newline+1] = line[i]
  188.                     line[i] = nil
  189.                 end
  190.                
  191.                 Wrap()
  192.                 return
  193.             end
  194.                
  195.             local oldpos = 0
  196.             for wpos in string_gmatch( text, "[^%s]+()" ) do
  197.                 local w, h = surface_GetTextSize( string_sub( text, oldpos, wpos ) )
  198.  
  199.                 --print("word:", "'" .. tostring(string_sub( text, oldpos, wpos )) .. "'" )
  200.  
  201.                 if x+w > width then
  202.                     --print("too big")
  203.                     local str1, str2
  204.                     if string_find( string_sub( text, 1, wpos ), " " ) and oldpos ~= 0 then
  205.                         --print("using oldpos")
  206.                         str1 = string_sub( text, 1, oldpos )
  207.                         str2 = string_sub( text, oldpos +1 )
  208.                     else
  209.                         --print("manual find")
  210.                         for i=#text,1,-1 do
  211.                             local w,h = surface_GetTextSize( string_sub( text, 1, i ) )
  212.                             if x + w < width then
  213.                                 str1 = string_sub( text, 1, i )
  214.                                 str2 = string_sub( text, i +1 )
  215.                                 break
  216.                             end
  217.                         end
  218.                     end
  219.                    
  220.                     --print( "str1: '" .. tostring(str1) .. "'" )
  221.                     --print( "str2: '" .. tostring(str2) .. "'" )
  222.                    
  223.                     line[i] = str1
  224.                    
  225.                     local font = line[1]
  226.                    
  227.                     local newline = {startfade = RealTime() + startfadetime, lifetime = RealTime() + lifetime, a = 0,cury = max_y, [1] = font}
  228.                     table_insert( data, 1, newline )
  229.                    
  230.                     newline[1] = str2
  231.                    
  232.                     for j=i+1,#line do
  233.                         newline[#newline+1] = line[i]
  234.                         line[i] = nil
  235.                     end
  236.                    
  237.                     Wrap()
  238.                     return
  239.                 end
  240.                
  241.                 x = x + w
  242.                 oldpos = wpos
  243.             end
  244.         end
  245.     end
  246.    
  247.     --print("Passed")
  248. end
  249.  
  250. ----------------------------------------
  251. -- Add Line
  252. ----------------------------------------
  253. local types = {
  254.     ["Player"] = function( obj ) return obj:Nick() end,
  255.     ["Vector"] = tostring,
  256.     ["Angle"] = tostring,
  257. }
  258.  
  259. local function getTallest( line )
  260.     for i=#data,1,-1 do
  261.         local templine = data[i]
  262.         if templine ~= line then
  263.             for j=1,#templine do
  264.                 if type(templine[j]) == "table" and templine[j][1] then
  265.                     surface_SetFont( GetFont( templine[j][1] ) )
  266.                 end
  267.             end
  268.         else
  269.             local maxh = 0
  270.             for j=1,#line do
  271.                 local obj = line[j]
  272.                 if type(obj) == "table" and obj[1] then
  273.                     surface_SetFont( GetFont( obj[1] ) )
  274.                 elseif type(obj) == "string" then
  275.                     local w,h = surface_GetTextSize( obj )
  276.                     if h > maxh then maxh = h end
  277.                 end
  278.             end
  279.             line.maxh = maxh
  280.         end
  281.     end
  282. end
  283.  
  284. local function AddLine( ... )  
  285.     local text = ""
  286.     local textdata = {...}
  287.    
  288.     local font = base_font_size
  289.     if type(textdata[#textdata]) == "string" then -- BIGGER!!!!!!!!
  290.         font = 17 + math_Clamp( #(string_match( textdata[#textdata], "(!*)$" ) or ""), 1, 20 )
  291.     end
  292.    
  293.     local line = {startfade = RealTime() + startfadetime, lifetime = RealTime() + lifetime, a = 0,cury = max_y, [1] = {font}}
  294.     table_insert( data, 1, line )
  295.    
  296.     for i=1,#textdata do
  297.         local obj = textdata[i]
  298.         local t = type(obj)
  299.         if t == "table" and obj.r and obj.g and obj.b and obj.a then
  300.             line[#line+1] = obj
  301.         elseif t == "table" and obj[1] then
  302.             line[#line+1] = obj
  303.         elseif t == "string" then
  304.             local t = string_Explode( "\n", obj )
  305.             if type(line[#line]) == "string" then
  306.                 line[#line] = line[#line] .. obj
  307.                 for i=2,#t do line[#line+1] = obj end
  308.             else
  309.                 for i=1,#t do line[#line+1] = obj end
  310.             end
  311.            
  312.             --Wrap()
  313.         elseif types[t] then
  314.             t = types[t]( obj )
  315.             if type(line[#line]) == "string" then
  316.                 line[#line] = line[#line] .. t
  317.             else
  318.                 line[#line+1] = t
  319.             end
  320.            
  321.             --Wrap()
  322.         end
  323.     end
  324.    
  325.     Wrap()
  326.    
  327.     local offset = 0
  328.     for i=1,#data do
  329.         local line = data[i]
  330.         if not line.maxh then
  331.             getTallest(line)
  332.             offset = offset + line.maxh
  333.             line.y = max_y - offset
  334.         else
  335.             line.y = line.y - offset
  336.         end
  337.     end
  338.  
  339.     hook.Add( "HUDPaint", Tag, DrawTexts )
  340. end
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347. hook.Add("ChatText", Tag, function( _, _, msg, msg_type )  
  348.     if not cl_shownewhud:GetBool() then
  349.         return
  350.     end
  351.     AddLine( {chatprint_font_size}, chatprint_color, msg )
  352. end)
  353.  
  354.  
  355. local function AddTextNew(...)
  356.     if not cl_shownewhud:GetBool() then
  357.         return true
  358.     end
  359.     AddLine(...)
  360.     return true
  361. end
  362. -- helperfunctions/lua/includes/extensions/chat_addtext_hack.lua
  363. _G.PrimaryChatAddText=AddTextNew
  364.  
  365. -- hide dat
  366. hook.Add( "HUDShouldDraw", Tag, function ( name )
  367.     if cl_shownewhud:GetBool() and name == "CHudChat" then
  368.              return false
  369.     end
  370. end )
  371.  
Advertisement
Add Comment
Please, Sign In to add comment