Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cl_shownewhud=CreateConVar("cl_shownewhud","1",true,false)
- local Tag="chathud"
- ----------------------------------------
- -- Chat box thingy for Python's server
- -- By Divran
- ----------------------------------------
- ----------------------------------------
- -- Options
- local startfadetime = 10 -- Time before the text STARTS to fade (Don't make this smaller than fadeintime)
- local fadetime = 2 -- Time it takes for the text to fade from 255 to 0 alpha
- local fadeintime = 0.5 -- Time it takes for the text to fade from 0 to 255 alpha
- local lifetime = startfadetime + fadetime -- Should be left as is
- local base_font_size = 17 -- Base size for chat messages
- local chatprint_font_size = 15 -- Size for messages printed through Lua commands such as ChatPrint
- local min_x = 10 -- Offset from side
- local max_x = 700 -- max_x - min_x = text width
- local min_y = 50 -- Offset frop top
- local max_y = 600 -- max_y - min_y = text height
- local movespeed = 150 -- Text animation move speed
- local chatprint_color = Color( 201,255,41)
- ----------------------------------------
- ----------------------------------------
- -- Localized functions for speed
- ----------------------------------------
- local surface_CreateFont = surface.CreateFont
- local surface_SetFont = surface.SetFont
- local surface_SetTextPos = surface.SetTextPos
- local surface_DrawText = surface.DrawText
- local surface_SetTextColor = surface.SetTextColor
- local size = surface.GetTextSize
- local table_remove = table.remove
- local table_Copy = table.Copy
- local table_insert = table.insert
- local string_gmatch = string.gmatch
- local string_gsub = string.gsub
- local string_sub = string.sub
- local string_find = string.find
- local string_match = string.match
- local string_Explode = string.Explode
- local math_Clamp = math.Clamp
- local math_Approach = math.Approach
- local math_floor = math.floor
- local math_max = math.max
- local math_min = math.min
- local RealTime = RealTime
- local FrameTime = FrameTime
- local type = type
- local tostring = tostring
- local surface_GetTextSize = function( str ) return size( string_gsub( str, "&", "%" ) ) end
- ----------------------------------------
- -- Fonts
- ----------------------------------------
- local fonts = {}
- local function GetFont( size )
- if tonumber(size) == nil then return size end
- size = tonumber(size)
- if fonts[size] then return fonts[size] end
- surface_CreateFont( "Tahoma", size, 700, false, false, "chat_font_" .. size, true, false )
- fonts[size] = "chat_font_"..size
- return fonts[size]
- end
- ----------------------------------------
- -- Text drawing
- ----------------------------------------
- local data = {}
- local function DrawTexts()
- surface_SetFont( GetFont( base_font_size ) )
- for i=#data,1,-1 do
- local line = data[i]
- -- Check fading
- if RealTime() > line.lifetime then
- table_remove( data, i )
- continue
- elseif RealTime() > line.startfade then
- line.a = line.a - RealFrameTime()/fadetime*255
- if line.a <= 0 then
- table_remove( data, i )
- continue
- end
- elseif line.a < 255 then
- line.a = line.a + RealFrameTime()/fadeintime*255
- end
- -- Check animation movement
- if line.y ~= line.cury then
- line.cury = math_Approach( line.cury, line.y, RealFrameTime() * movespeed )
- end
- local x = min_x
- for j=1,#line do
- local obj = line[j]
- local t = type(obj)
- if t == "string" then
- local w,h = surface_GetTextSize( obj )
- local y = line.cury - h + line.maxh
- surface_SetTextPos( x, y )
- x = x + w
- surface_DrawText( obj )
- elseif t == "table" and obj.r and obj.g and obj.b then
- surface_SetTextColor( obj.r, obj.g, obj.b, line.a )
- elseif t == "table" and obj[1] then
- surface_SetFont( GetFont( obj[1] ) )
- end
- end
- end
- if #data == 0 then
- hook.Remove( "HUDPaint", Tag )
- end
- end
- ----------------------------------------
- -- Word Wrapping
- ----------------------------------------
- local width = max_x-min_x
- local function Wrap()
- local line = data[1]
- --print("-----------")
- --print("Checking line nr:", 1)
- local font = base_font_size
- --print("Max allowed width: " .. width)
- local x = 0
- for i=1,#line do
- local text = line[i]
- if type(text) == "table" and text[1] then
- surface_SetFont( GetFont( text[1] ) )
- font = text
- elseif type(text) == "string" then
- local startpos, endpos = string_find( text, "%b{}" )
- if startpos and endpos then
- local word = string_sub( text, startpos+1, endpos-1 )
- line[i] = string_sub( text, 1, startpos-1 )
- local font = tonumber(word) or word
- table_insert( line, i + 1, { font } )
- surface_SetFont( GetFont(font) )
- table_insert( line, i + 2, string_sub( text, endpos+1 ) )
- Wrap()
- return
- end
- --print("text:","'".. tostring(text) .. "'")
- if x > width then
- --print("offset alone was too big")
- local newline = {startfade = RealTime() + startfadetime, lifetime = RealTime() + lifetime, a = 0,cury = max_y, [1] = font}
- table_insert( data, 1, newline )
- for j=i,#line do
- newline[#newline+1] = line[i]
- line[i] = nil
- end
- Wrap()
- return
- end
- local oldpos = 0
- for wpos in string_gmatch( text, "[^%s]+()" ) do
- local w, h = surface_GetTextSize( string_sub( text, oldpos, wpos ) )
- --print("word:", "'" .. tostring(string_sub( text, oldpos, wpos )) .. "'" )
- if x+w > width then
- --print("too big")
- local str1, str2
- if string_find( string_sub( text, 1, wpos ), " " ) and oldpos ~= 0 then
- --print("using oldpos")
- str1 = string_sub( text, 1, oldpos )
- str2 = string_sub( text, oldpos +1 )
- else
- --print("manual find")
- for i=#text,1,-1 do
- local w,h = surface_GetTextSize( string_sub( text, 1, i ) )
- if x + w < width then
- str1 = string_sub( text, 1, i )
- str2 = string_sub( text, i +1 )
- break
- end
- end
- end
- --print( "str1: '" .. tostring(str1) .. "'" )
- --print( "str2: '" .. tostring(str2) .. "'" )
- line[i] = str1
- local font = line[1]
- local newline = {startfade = RealTime() + startfadetime, lifetime = RealTime() + lifetime, a = 0,cury = max_y, [1] = font}
- table_insert( data, 1, newline )
- newline[1] = str2
- for j=i+1,#line do
- newline[#newline+1] = line[i]
- line[i] = nil
- end
- Wrap()
- return
- end
- x = x + w
- oldpos = wpos
- end
- end
- end
- --print("Passed")
- end
- ----------------------------------------
- -- Add Line
- ----------------------------------------
- local types = {
- ["Player"] = function( obj ) return obj:Nick() end,
- ["Vector"] = tostring,
- ["Angle"] = tostring,
- }
- local function getTallest( line )
- for i=#data,1,-1 do
- local templine = data[i]
- if templine ~= line then
- for j=1,#templine do
- if type(templine[j]) == "table" and templine[j][1] then
- surface_SetFont( GetFont( templine[j][1] ) )
- end
- end
- else
- local maxh = 0
- for j=1,#line do
- local obj = line[j]
- if type(obj) == "table" and obj[1] then
- surface_SetFont( GetFont( obj[1] ) )
- elseif type(obj) == "string" then
- local w,h = surface_GetTextSize( obj )
- if h > maxh then maxh = h end
- end
- end
- line.maxh = maxh
- end
- end
- end
- local function AddLine( ... )
- local text = ""
- local textdata = {...}
- local font = base_font_size
- if type(textdata[#textdata]) == "string" then -- BIGGER!!!!!!!!
- font = 17 + math_Clamp( #(string_match( textdata[#textdata], "(!*)$" ) or ""), 1, 20 )
- end
- local line = {startfade = RealTime() + startfadetime, lifetime = RealTime() + lifetime, a = 0,cury = max_y, [1] = {font}}
- table_insert( data, 1, line )
- for i=1,#textdata do
- local obj = textdata[i]
- local t = type(obj)
- if t == "table" and obj.r and obj.g and obj.b and obj.a then
- line[#line+1] = obj
- elseif t == "table" and obj[1] then
- line[#line+1] = obj
- elseif t == "string" then
- local t = string_Explode( "\n", obj )
- if type(line[#line]) == "string" then
- line[#line] = line[#line] .. obj
- for i=2,#t do line[#line+1] = obj end
- else
- for i=1,#t do line[#line+1] = obj end
- end
- --Wrap()
- elseif types[t] then
- t = types[t]( obj )
- if type(line[#line]) == "string" then
- line[#line] = line[#line] .. t
- else
- line[#line+1] = t
- end
- --Wrap()
- end
- end
- Wrap()
- local offset = 0
- for i=1,#data do
- local line = data[i]
- if not line.maxh then
- getTallest(line)
- offset = offset + line.maxh
- line.y = max_y - offset
- else
- line.y = line.y - offset
- end
- end
- hook.Add( "HUDPaint", Tag, DrawTexts )
- end
- hook.Add("ChatText", Tag, function( _, _, msg, msg_type )
- if not cl_shownewhud:GetBool() then
- return
- end
- AddLine( {chatprint_font_size}, chatprint_color, msg )
- end)
- local function AddTextNew(...)
- if not cl_shownewhud:GetBool() then
- return true
- end
- AddLine(...)
- return true
- end
- -- helperfunctions/lua/includes/extensions/chat_addtext_hack.lua
- _G.PrimaryChatAddText=AddTextNew
- -- hide dat
- hook.Add( "HUDShouldDraw", Tag, function ( name )
- if cl_shownewhud:GetBool() and name == "CHudChat" then
- return false
- end
- end )
Advertisement
Add Comment
Please, Sign In to add comment