Advertisement
Guest User

gmod lua cool chatbox

a guest
Oct 18th, 2011
1,888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.92 KB | None | 0 0
  1. local ChatBox = {}
  2. ChatBox.ChatText = ""
  3. ChatBox.LastScroll = 0
  4. ChatBox.ChatTextShow = ""
  5. ChatBox.ChatTextShow2 = ""
  6. ChatBox.Active = false
  7. ChatBox.Teamsay = false
  8. ChatBox.TextTable = {}
  9. ChatBox.InputBoxCol = 'b'
  10. ChatBox.DrawLine = 0
  11. ChatBox.LastLine = 0
  12. ChatBox.AddTextColor = Color(255,255,255,255)
  13. ChatBox.Width = 0
  14. ChatBox.CleanTimer = 0
  15. ChatBox.TrStr1=string.Explode(" ","? e o o e a i a o u c o u o u a a i ? i e a ? y y ? n i e o u a ? E O O E A I A O U C O U O U A A I ? I E A ? Y ? ? N I E O U A ?")
  16. ChatBox.TrStr2=string.Explode(" ","ё й ц у к е н г ш щ з х ъ ф ы в а п р о л д ж э я ч с м и т ь б ю Й Ц У К Е Н Г Ш Щ З Х Ъ Ф Ы В А П Р О Л Д Ж Э Я Ч С М И Т Ь Б Ю")
  17. ChatBox.Times = {}
  18.  
  19. ChatBox.GroupColors = {OWNER = Color(255,0,0,255), SUPERADMIN = Color(255,130,0,255), ADMIN = Color(255,255,0,255), MODER = Color(0,150,0,255), RESPECTED = Color(0,212,255,255), DONATOR = Color(0,255,127), VIP = Color(255,215,0,255), GIRL = Color(252,15,152)}
  20.  
  21. //ChatBox.Width DEPENDING ON SCREEN RESOLUTION
  22. ChatBox.Width = ScreenScale(300)
  23.  
  24. function ChatBox.BroadcastStatus()
  25.              ChatBoxActive = ChatBox.Active
  26. end
  27. hook.Add("Think", "BroadcastChatboxStatus", ChatBox.BroadcastStatus)
  28.  
  29. function ChatBox.StartChat(teamsay)
  30.              ChatBox.Active = true
  31.              ChatBox.Teamsay = teamsay
  32.              --ChatBox.MenuButton:SetVisible(true)
  33.              --ChatBox.DjButton:SetVisible(true)
  34.              --ChatBox.StopButton:SetVisible(true)
  35.              return true
  36. end
  37.  
  38.  
  39. function ChatBox.FinishChat()
  40.              ChatBox.Active = false
  41.              --ChatBox.MenuButton:SetVisible(false)
  42.              --ChatBox.DjButton:SetVisible(false)
  43.              --ChatBox.StopButton:SetVisible(false)
  44. end
  45.  
  46.  
  47. function ChatBox.ChatTextChanged(text)
  48.              ChatBox.ChatText = text
  49.              surface.SetFont("ChatBoxboxFont")
  50.              if surface.GetTextSize(ChatBox.ChatText) > ChatBox.Width then
  51.                      ChatBox.ChatTextShow = string.sub(ChatBox.ChatText, string.len(text)-ChatBox.Ltl+1, string.len(text))
  52.              else
  53.                      ChatBox.Ltl = string.len(text)
  54.                      ChatBox.ChatTextShow = ChatBox.ChatText
  55.              end
  56.              /*for k,v in pairs(ChatBox.TrStr1) do
  57.                      ChatBox.ChatTextShow = string.Replace(ChatBox.ChatTextShow, v, ChatBox.TrStr2[k])
  58.              end*/
  59. end
  60.  
  61. function ChatBox.ChatTextParse(plyindex, plyname, text, msgtype)
  62.              local texttable = {}
  63.              if msgtype == "none" then
  64.                      table.insert(texttable, Color(255, 120, 0, 255))
  65.                      table.insert(texttable, text)
  66.                      ChatBox.ParseLine(texttable)
  67.              elseif msgtype == "joinleave" then
  68.                      table.insert(texttable, Color(140, 255, 111, 255))
  69.                      table.insert(texttable, text)
  70.                      ChatBox.ParseLine(texttable)
  71.              end
  72. end
  73. hook.Add("ChatText", "ChatBoxText", ChatBox.ChatTextParse)
  74.  
  75. function ChatBox.ParseLine(texttable)
  76.              local texttable1 = {}
  77.              local texttable2 = {}
  78.              local parsetable2 = false
  79.              local table2point = 0
  80.              local len = 0
  81.              surface.SetFont("ChatBoxboxFont")
  82.              for k,v in pairs(texttable) do
  83.                      if type(v) == "string" then
  84.                              if len + surface.GetTextSize(v) > ChatBox.Width then
  85.                                      local s1, s2
  86.                                      for i=string.len(v),1,-1 do
  87.                                              if len + surface.GetTextSize(string.sub(v,1,i)) < ChatBox.Width then
  88.                                                      s1 = string.sub(v,1,i)
  89.                                                      s2 = string.sub(v,i+1,string.len(v))
  90.                                                      break
  91.                                              end
  92.                                      end
  93.                                      table.insert(texttable1, s1)
  94.                                      Msg(s1)
  95.                                      table.insert(texttable2, texttable[k-1])
  96.                                      table.insert(texttable2, s2)
  97.                                      parsetable2 = true
  98.                                      table2point = k
  99.                                      len = 0
  100.                                      break
  101.                              else
  102.                                      len = len + surface.GetTextSize(v)
  103.                                      table.insert(texttable1, v)
  104.                                      Msg(v)
  105.                              end
  106.                      else
  107.                              table.insert(texttable1, v)
  108.                      end
  109.              end
  110.              table.insert(ChatBox.TextTable, texttable1)
  111.              table.insert(ChatBox.Times,os.time()+10)
  112.              if parsetable2 == true then
  113.                      ChatBox.ParseLine(texttable2)
  114.              end
  115.              ChatBox.LastLine = ChatBox.LastLine+1
  116.              ChatBox.DrawLine = ChatBox.LastLine
  117.              if !parsetable2 then
  118.                      Msg("\n")
  119.              end
  120. end
  121.  
  122. function ChatBox.DrawTextTable()
  123.              for i=ChatBox.DrawLine-(ChatBox.Active and 13 or 5), ChatBox.DrawLine do
  124.                      local drawcolor = Color(255,255,255,255)
  125.                      local len = 0
  126.                      local row = ChatBox.TextTable[i]
  127.                      if row and (ChatBox.Active or (ChatBox.Times[i] > os.time())) then
  128.                              for k,v in pairs(row) do
  129.                                      if type(v) == "string" then
  130.                                              --draw.SimpleText(v, "ChatBoxboxFont", 12 + len, ScrH() - ScrH() / 2 + ScreenScale(8) * (i-ChatBox.DrawLine+13), drawcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 1, Color(0, 0, 0,255))
  131.                                              local text = {}
  132.                                              text.pos  = {}
  133.                                              text.text = v
  134.                                              text.pos[1] = 12 + len
  135.                                              text.pos[2] = ScrH() - ScrH() / 2 + ScreenScale(8) * (i-ChatBox.DrawLine+13)
  136.                                              text.xalign = TEXT_ALIGN_LEFT
  137.                                              text.yalign = TEXT_ALIGN_LEFT
  138.                                              text.color = drawcolor
  139.                                              text.font = "ChatBoxboxFont"
  140.                                              draw.TextShadow(text, 1.2, 255)
  141.                                              surface.SetFont("ChatBoxboxFont")
  142.                                              len = len + surface.GetTextSize(v)
  143.                                      else
  144.                                              drawcolor = v
  145.                                      end
  146.                              end
  147.                      end
  148.              end
  149. end
  150.  
  151. function ChatBox.GetLineText(nmLine)
  152.              local row = ChatBox.TextTable[nmLine]
  153.              local str = ""
  154.              if row then
  155.                      for k,v in pairs(row) do
  156.                              if type(v) == "string" then
  157.                                      str=str..v
  158.                              end
  159.                      end
  160.              end
  161.              return str
  162. end
  163.  
  164. function Chatbox_DrawHud()
  165.              surface.SetFont("ChatBoxboxFont")
  166.              ChatBox.DrawTextTable()
  167.              if not ChatBox.Active then return end
  168.              //print("grabbeen peelz")
  169.              if CurTime() > ChatBox.LastScroll then
  170.                      if input.IsKeyDown(KEY_PAGEDOWN) or input.IsKeyDown(KEY_DOWN) then
  171.                              ChatBox.DrawLine = ChatBox.DrawLine +1
  172.                              ChatBox.LastScroll = CurTime()+0.1
  173.                      end
  174.                      if input.IsKeyDown(KEY_PAGEUP) or input.IsKeyDown(KEY_UP) then
  175.                              ChatBox.DrawLine = ChatBox.DrawLine -1
  176.                              ChatBox.LastScroll = CurTime()+0.1
  177.                      end
  178.              end
  179.              if input.IsMouseDown(MOUSE_RIGHT) then
  180.                      local mx, my = gui.MouseX(), gui.MouseY()
  181.                      if mx > 12 and mx < ChatBox.Width and !ChatBox.CMenu then
  182.                              local ln = -1
  183.                              for i = 0,15 do
  184.                                      local ly = ScrH() - ScrH() / 2 + ScreenScale(8) * (i)
  185.                                      if my > ly and my < ly+ScreenScale(8) then
  186.                                              ln = ChatBox.DrawLine-(14-i)+1
  187.                                              break
  188.                                      end
  189.                              end
  190.                              ChatBox.SelLine = ln
  191.                              ChatBox.Menu = DermaMenu()
  192.                              local m = ChatBox.Menu
  193.                              m:AddOption("Scroll up",function() ChatBox.DrawLine = ChatBox.DrawLine -1 end)
  194.                              m:AddOption("Copy message to clipboard", function() SetClipboardText(ChatBox.GetLineText(ChatBox.SelLine)) end )
  195.                              m:AddSpacer()
  196.                              local cs = m:AddSubMenu("Chatbox settings")
  197.                              cs:AddOption("Make input box white",function() ChatBox.InputBoxCol = 'w' end)
  198.                              cs:AddOption("Make input box black",function() ChatBox.InputBoxCol = 'b' end)
  199.                              m:Open()
  200.                      end
  201.              end
  202.              ChatBox.DrawLine = math.Clamp(ChatBox.DrawLine,1,ChatBox.LastLine)
  203.              surface.SetDrawColor(0, 0, 0, 100)
  204.              surface.DrawRect(10, ScrH() - ScrH() / 2, ChatBox.Width, ScreenScale(8) * 14)
  205.              surface.SetDrawColor(0, 0, 0, 255)
  206.              surface.DrawOutlinedRect(10, ScrH() - ScrH() / 2, ChatBox.Width, ScreenScale(8) * 14)
  207.              if ChatBox.InputBoxCol == 'b' then
  208.                      surface.SetDrawColor(0, 0, 0, 175)
  209.              else
  210.                      surface.SetDrawColor(255,255,255,175)
  211.              end
  212.              surface.DrawRect(10, ScrH() - ScrH() / 2 + ScreenScale(8) * 14 + 2, ChatBox.Width - 1, ScreenScale(8))
  213.              if ChatBox.Teamsay then
  214.                      surface.SetDrawColor(team.GetColor(LocalPlayer():Team()))
  215.              else
  216.                      if ChatBox.InputBoxCol == 'b' then
  217.                              surface.SetDrawColor(255,255,255,255)
  218.                      else
  219.                              surface.SetDrawColor(0,0,0,255)
  220.                      end
  221.              end
  222.              surface.DrawOutlinedRect(10, ScrH() - ScrH() / 2 + ScreenScale(8) * 14 + 2, ChatBox.Width, ScreenScale(8))
  223.              if ChatBox.InputBoxCol == 'b' then
  224.                      draw.SimpleText(ChatBox.ChatTextShow, "ChatBoxboxFont", 12, ScrH() - ScrH() / 2 + ScreenScale(8) * 14 + 2, Color(255,255,255,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
  225.              else
  226.                      draw.SimpleText(ChatBox.ChatTextShow, "ChatBoxboxFont", 12, ScrH() - ScrH() / 2 + ScreenScale(8) * 14 + 2, Color(0,0,0,255), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
  227.              end
  228. end
  229.  
  230. function ChatBox.HideShitFagLol(name)
  231.              if name == "CHudChat" and ChatBox.Active == false then
  232.                      return false
  233.              elseif name == "CHudChat" and ChatBox.Active == true then
  234.                      hook.Remove("HUDShouldDraw", "AntiDefaultChat")
  235.              end
  236. end
  237.  
  238. function chat.AddText(...)
  239.              arg = {...}
  240.              local texttable = {}
  241.              for k,v in pairs(arg) do
  242.                      if type(v) == "string" then
  243.                              table.insert(texttable, ChatBox.AddTextColor)
  244.                              table.insert(texttable, v)
  245.                      elseif type(v) == "Player" then
  246.                              local group = v:GetNWString("GA_Group", "USER")
  247.                              if group != "USER" and group != "CONSOLE" and group != "MINGEBAG" then
  248.                              print("GROUP:"..group)
  249.                                      table.insert(texttable,ChatBox.GroupColors[group])
  250.                                      table.insert(texttable,"["..group.."] ")
  251.                              end
  252.                              if teamonly == true then
  253.                                      table.insert(texttable, team.GetColor(v:Team()))
  254.                                      table.insert(texttable, "(TEAM)")
  255.                              end
  256.                              if dead == true then
  257.                                      table.insert(texttable, Color(200, 0, 0, 255))
  258.                                      table.insert(texttable, "*DEAD*")
  259.                              end
  260.                              table.insert(texttable, team.GetColor(v:Team()))
  261.                              table.insert(texttable, v:Name())
  262.                      else
  263.                              ChatBox.AddTextColor = v
  264.                      end
  265.              end
  266.              ChatBox.ParseLine(texttable)
  267. end
  268.  
  269.              --hook.Add("Initialize","ChatBoxInit",function() --we're getting this late enough anyways...
  270.              surface.CreateFont("dejavusans", ScreenScale(8), 600, true, false, "ChatBoxboxFont")
  271.              --ChatBox.InitButtons()
  272.              hook.Add("ChatTextChanged", "ChatBoxTextChanged", ChatBox.ChatTextChanged)
  273.              hook.Add("StartChat", "ChatBoxStartChat", ChatBox.StartChat)
  274.              hook.Add("FinishChat", "ChatBoxFinishChat", ChatBox.FinishChat)
  275.              hook.Add("HUDPaint", "Chatbox_DrawHud", Chatbox_DrawHud)
  276.              hook.Add("GUIMousePressed","ChatboxScroll",ChatBox.MouseWheel)
  277.              hook.Add("HUDShouldDraw", "AntiDefaultChat", ChatBox.HideShitFagLol)
  278.  
  279.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement