DarkNessProvides

Another simple HUD for GMOD

Apr 7th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.35 KB | None | 0 0
  1. //DarkRP Working Custom HUD
  2. local function Base()
  3.  
  4.     draw.RoundedBox(11, ScrW() / 9 - 160, ScrH () - 150 - 10, 688, 120, Color(194, 194, 194, 255))
  5.     draw.RoundedBox(10, ScrW() / 9 - 160, ScrH () - 150 - 10, 688, 120, Color(255, 0, 0, 250))
  6.     draw.RoundedBox(0, ScrW() / 9 - 160, ScrH () - 110 - 10, 400, 70, Color(255, 255, 255, 255))
  7.     draw.RoundedBox(0, ScrW() / 9 - 160, ScrH () - 70 - 10, 400, 50, Color(255, 255, 255, 255))
  8.     draw.RoundedBox(10, ScrW() / 9 - 160, ScrH () - 70 - 10, 400, 70, Color(255, 255, 255, 255))
  9.     draw.RoundedBox(4, ScrW() / 9 - 153, ScrH () - 115, 380, 40, Color(194, 194, 194, 160))
  10.     draw.RoundedBox(4, ScrW() / 9 - 153, ScrH () - 60, 380, 40, Color(194, 194, 194, 160))
  11.    
  12.     draw.RoundedBox(0, ScrW() / 4 + 6, ScrH () - 110 - 10, 300, 70, Color(255, 255, 255, 255))
  13.     draw.RoundedBox(10, ScrW() / 4 + 6, ScrH () - 70 - 10, 300, 70, Color(255, 255, 255, 255))
  14.     draw.RoundedBox(4, ScrW() / 4 + 10, ScrH () - 105 - 10, 292, 99, Color(194, 194, 194, 160))
  15.     draw.RoundedBox(4, ScrW() / 4 + 12, ScrH () - 103 - 10, 288, 95, Color(255, 0, 0, 220))
  16.    
  17.     local DrawHealth = LocalPlayer():Health() or 0
  18.     local EchoHealth = LocalPlayer():Health() or 0
  19.    
  20.     local DrawArmor = LocalPlayer():Armor() or 0
  21.     local EchoArmor = LocalPlayer():Armor() or 0
  22.    
  23.     if DrawHealth > 100 then DrawHealth = 100 end
  24.     if DrawHealth == 0 then DrawHealth = 0 end
  25.     if DrawHealth < 0 then DrawHealth = 0 end
  26.    
  27.     draw.RoundedBox(4, ScrW() / 9 - 151, ScrH () - 113, (376) * DrawHealth / 100, 36, Color(255, 0, 0, 220))
  28.    
  29.     if DrawArmor > 100 then DrawArmor = 100 end
  30.     if DrawArmor == 0 then DrawArmor = 0 end
  31.     if DrawArmor < 0 then DrawArmor = 0  end
  32.    
  33.     if DrawArmor > 0 then
  34.         draw.RoundedBox(4, ScrW() / 9 - 151, ScrH () - 58, (376) * DrawArmor / 100, 36, Color(0, 0, 255, 220))
  35.     end
  36.    
  37.     draw.DrawText ( "Health: "..EchoHealth, "DermaLarge", ScrW() / 9 - 130, ScrH () - 109, Color(255,255,255,255))
  38.     draw.DrawText ( "Armor: "..EchoArmor, "DermaLarge", ScrW() / 9 - 130, ScrH () - 56, Color(255,255,255,255))
  39.     draw.DrawText ( "Job: "..LocalPlayer():getDarkRPVar( "job" ), "DermaLarge", ScrW() / 4 + 20, ScrH () - 95 - 10, Color(255,255,255,255))
  40.     draw.DrawText ( "Wallet: $"..LocalPlayer():getDarkRPVar( "money" ), "DermaLarge", ScrW() / 4 + 20, ScrH () - 50 - 10, Color(255,255,255,255))
  41.     draw.DrawText ( LocalPlayer():Nick(), "DermaLarge", ScrW() / 9 - 130,  ScrH () - 145 - 10, Color(255,255,255,200))
  42.     draw.DrawText ( "Salary: $"..LocalPlayer():getDarkRPVar( "salary" ), "DermaLarge", ScrW() / 4 + 20, ScrH() - 145 - 10, Color(255,255,255,255))
  43.    
  44. end
  45.  
  46.  
  47. /*---------------------------------------------------------------------------
  48. HUD ConVars
  49. ---------------------------------------------------------------------------*/
  50. local ConVars = {}
  51. local HUDWidth
  52. local HUDHeight
  53.  
  54. local Color = Color
  55. local cvars = cvars
  56. local DarkRP = DarkRP
  57. local CurTime = CurTime
  58. local draw = draw
  59. local GetConVar = GetConVar
  60. local IsValid = IsValid
  61. local Lerp = Lerp
  62. local localplayer
  63. local math = math
  64. local pairs = pairs
  65. local ScrW, ScrH = ScrW, ScrH
  66. local SortedPairs = SortedPairs
  67. local string = string
  68. local surface = surface
  69. local table = table
  70. local timer = timer
  71. local tostring = tostring
  72.  
  73. CreateClientConVar("weaponhud", 0, true, false)
  74.  
  75. local colors = {}
  76. colors.black = Color(0, 0, 0, 255)
  77. colors.blue = Color(0, 0, 255, 255)
  78. colors.brightred = Color(200, 30, 30, 255)
  79. colors.darkred = Color(0, 0, 70, 100)
  80. colors.darkblack = Color(0, 0, 0, 200)
  81. colors.gray1 = Color(0, 0, 0, 155)
  82. colors.gray2 = Color(51, 58, 51,100)
  83. colors.red = Color(255, 0, 0, 255)
  84. colors.white = Color(255, 255, 255, 255)
  85. colors.white1 = Color(255, 255, 255, 200)
  86.  
  87. local function ReloadConVars()
  88.     ConVars = {
  89.         background = {0,0,0,100},
  90.         Healthbackground = {0,0,0,200},
  91.         Healthforeground = {140,0,0,180},
  92.         HealthText = {255,255,255,200},
  93.         Job1 = {0,0,150,200},
  94.         Job2 = {0,0,0,255},
  95.         salary1 = {0,150,0,200},
  96.         salary2 = {0,0,0,255}
  97.     }
  98.  
  99.     for name, Colour in pairs(ConVars) do
  100.         ConVars[name] = {}
  101.         for num, rgb in SortedPairs(Colour) do
  102.             local CVar = GetConVar(name..num) or CreateClientConVar(name..num, rgb, true, false)
  103.             table.insert(ConVars[name], CVar:GetInt())
  104.  
  105.             if not cvars.GetConVarCallbacks(name..num, false) then
  106.                 cvars.AddChangeCallback(name..num, function() timer.Simple(0,ReloadConVars) end)
  107.             end
  108.         end
  109.         ConVars[name] = Color(unpack(ConVars[name]))
  110.     end
  111.  
  112.  
  113.     HUDWidth = (GetConVar("HudW") or  CreateClientConVar("HudW", 240, true, false)):GetInt()
  114.     HUDHeight = (GetConVar("HudH") or CreateClientConVar("HudH", 115, true, false)):GetInt()
  115.  
  116.     if not cvars.GetConVarCallbacks("HudW", false) and not cvars.GetConVarCallbacks("HudH", false) then
  117.         cvars.AddChangeCallback("HudW", function() timer.Simple(0,ReloadConVars) end)
  118.         cvars.AddChangeCallback("HudH", function() timer.Simple(0,ReloadConVars) end)
  119.     end
  120. end
  121. ReloadConVars()
  122.  
  123. local Scrw, Scrh, RelativeX, RelativeY
  124. /*---------------------------------------------------------------------------
  125. HUD Seperate Elements
  126. ---------------------------------------------------------------------------*/
  127.  
  128. local Page = Material("icon16/page_white_text.png")
  129. local function GunLicense()
  130.     if localplayer:getDarkRPVar("HasGunlicense") then
  131.         surface.SetMaterial(Page)
  132.         surface.SetDrawColor(255, 255, 255, 255)
  133.         surface.DrawTexturedRect(RelativeX + HUDWidth, ScrH() - 34, 32, 32)
  134.     end
  135. end
  136.  
  137. local function Agenda()
  138.     local ply = LocalPlayer()
  139.  
  140.     local agenda = ply:getAgendaTable()
  141.     if not agenda then return end
  142.  
  143.     draw.RoundedBox(10, 10, 10, 460, 110, colors.gray1)
  144.     draw.RoundedBox(10, 12, 12, 456, 106, colors.gray2)
  145.     draw.RoundedBox(10, 12, 12, 456, 20, colors.darkred)
  146.  
  147.     draw.DrawNonParsedText(agenda.Title, "DarkRPHUD1", 30, 12, colors.red, 0)
  148.  
  149.     local text = ply:getDarkRPVar("agenda") or ""
  150.  
  151.     text = text:gsub("//", "\n"):gsub("\\n", "\n")
  152.     text = DarkRP.textWrap(text, "DarkRPHUD1", 440)
  153.     draw.DrawNonParsedText(text, "DarkRPHUD1", 30, 35, colors.white, 0)
  154. end
  155.  
  156. local VoiceChatTexture = surface.GetTextureID("voice/icntlk_pl")
  157. local function DrawVoiceChat()
  158.     if localplayer.DRPIsTalking then
  159.         local chbxX, chboxY = chat.GetChatBoxPos()
  160.  
  161.         local Rotating = math.sin(CurTime()*3)
  162.         local backwards = 0
  163.         if Rotating < 0 then
  164.             Rotating = 1-(1+Rotating)
  165.             backwards = 180
  166.         end
  167.         surface.SetTexture(VoiceChatTexture)
  168.         surface.SetDrawColor(ConVars.Healthforeground)
  169.         surface.DrawTexturedRectRotated(ScrW() - 100, chboxY, Rotating*96, 96, backwards)
  170.     end
  171. end
  172.  
  173. CreateConVar("DarkRP_LockDown", 0, {FCVAR_REPLICATED, FCVAR_SERVER_CAN_EXECUTE})
  174. local function LockDown()
  175.     local chbxX, chboxY = chat.GetChatBoxPos()
  176.     if util.tobool(GetConVarNumber("DarkRP_LockDown")) then
  177.         local cin = (math.sin(CurTime()) + 1) / 2
  178.         local chatBoxSize = math.floor(ScrH() / 4)
  179.         draw.DrawNonParsedText(DarkRP.getPhrase("lockdown_started"), "ScoreboardSubtitle", chbxX, chboxY + chatBoxSize, Color(cin * 255, 0, 255 - (cin * 255), 255), TEXT_ALIGN_LEFT)
  180.     end
  181. end
  182.  
  183. local Arrested = function() end
  184.  
  185. usermessage.Hook("GotArrested", function(msg)
  186.     local StartArrested = CurTime()
  187.     local ArrestedUntil = msg:ReadFloat()
  188.  
  189.     Arrested = function()
  190.         if CurTime() - StartArrested <= ArrestedUntil and localplayer:getDarkRPVar("Arrested") then
  191.         draw.DrawNonParsedText(DarkRP.getPhrase("youre_arrested", math.ceil(ArrestedUntil - (CurTime() - StartArrested))), "DarkRPHUD1", ScrW()/2, ScrH() - ScrH()/12, colors.white, 1)
  192.         elseif not localplayer:getDarkRPVar("Arrested") then
  193.             Arrested = function() end
  194.         end
  195.     end
  196. end)
  197.  
  198. local AdminTell = function() end
  199.  
  200. usermessage.Hook("AdminTell", function(msg)
  201.     timer.Destroy("DarkRP_AdminTell")
  202.     local Message = msg:ReadString()
  203.  
  204.     AdminTell = function()
  205.         draw.RoundedBox(4, 10, 10, ScrW() - 20, 100, colors.darkblack)
  206.         draw.DrawNonParsedText(DarkRP.getPhrase("listen_up"), "GModToolName", ScrW() / 2 + 10, 10, colors.white, 1)
  207.         draw.DrawNonParsedText(Message, "ChatFont", ScrW() / 2 + 10, 80, colors.brightred, 1)
  208.     end
  209.  
  210.     timer.Create("DarkRP_AdminTell", 10, 1, function()
  211.         AdminTell = function() end
  212.     end)
  213. end)
  214.  
  215. /*---------------------------------------------------------------------------
  216. Entity HUDPaint things
  217. ---------------------------------------------------------------------------*/
  218. local function DrawPlayerInfo(ply)
  219.     local pos = ply:EyePos()
  220.  
  221.     pos.z = pos.z + 10 -- The position we want is a bit above the position of the eyes
  222.     pos = pos:ToScreen()
  223.     pos.y = pos.y - 50 -- Move the text up a few pixels to compensate for the height of the text
  224.  
  225.     if GAMEMODE.Config.showname and not ply:getDarkRPVar("wanted") then
  226.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1)
  227.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1)
  228.     end
  229.  
  230.     if GAMEMODE.Config.showhealth and not ply:getDarkRPVar("wanted") then
  231.         draw.DrawNonParsedText(DarkRP.getPhrase("health", ply:Health()), "DarkRPHUD2", pos.x + 1, pos.y + 21, colors.black, 1)
  232.         draw.DrawNonParsedText(DarkRP.getPhrase("health", ply:Health()), "DarkRPHUD2", pos.x, pos.y + 20, colors.white1, 1)
  233.     end
  234.  
  235.     if GAMEMODE.Config.showjob then
  236.         local teamname = team.GetName(ply:Team())
  237.         draw.DrawNonParsedText(ply:getDarkRPVar("job") or teamname, "DarkRPHUD2", pos.x + 1, pos.y + 41, colors.black, 1)
  238.         draw.DrawNonParsedText(ply:getDarkRPVar("job") or teamname, "DarkRPHUD2", pos.x, pos.y + 40, colors.white1, 1)
  239.     end
  240.  
  241.     if ply:getDarkRPVar("HasGunlicense") then
  242.         surface.SetMaterial(Page)
  243.         surface.SetDrawColor(255,255,255,255)
  244.         surface.DrawTexturedRect(pos.x-16, pos.y + 60, 32, 32)
  245.     end
  246. end
  247.  
  248. local function DrawWantedInfo(ply)
  249.     if not ply:Alive() then return end
  250.  
  251.     local pos = ply:EyePos()
  252.     if not pos:isInSight({localplayer, ply}) then return end
  253.  
  254.     pos.z = pos.z + 14
  255.     pos = pos:ToScreen()
  256.  
  257.     if GAMEMODE.Config.showname then
  258.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1)
  259.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1)
  260.     end
  261.  
  262.     local wantedText = DarkRP.getPhrase("wanted", tostring(ply:getDarkRPVar("wantedReason")))
  263.  
  264.     draw.DrawNonParsedText(wantedText, "DarkRPHUD2", pos.x, pos.y - 40, colors.white1, 1)
  265.     draw.DrawNonParsedText(wantedText, "DarkRPHUD2", pos.x + 1, pos.y - 41, colors.red, 1)
  266. end
  267.  
  268. /*---------------------------------------------------------------------------
  269. The Entity display: draw HUD information about entities
  270. ---------------------------------------------------------------------------*/
  271. local function DrawEntityDisplay()
  272.  
  273.     local shootPos = localplayer:GetShootPos()
  274.     local aimVec = localplayer:GetAimVector()
  275.  
  276.     for k, ply in pairs(players or player.GetAll()) do
  277.         if not ply:Alive() or ply == localplayer then continue end
  278.         local hisPos = ply:GetShootPos()
  279.         if ply:getDarkRPVar("wanted") then DrawWantedInfo(ply) end
  280.  
  281.         if GAMEMODE.Config.globalshow then
  282.             DrawPlayerInfo(ply)
  283.         -- Draw when you're (almost) looking at him
  284.         elseif not GAMEMODE.Config.globalshow and hisPos:DistToSqr(shootPos) < 160000 then
  285.             local pos = hisPos - shootPos
  286.             local unitPos = pos:GetNormalized()
  287.             if unitPos:Dot(aimVec) > 0.95 then
  288.                 local trace = util.QuickTrace(shootPos, pos, localplayer)
  289.                 if trace.Hit and trace.Entity ~= ply then return end
  290.                 DrawPlayerInfo(ply)
  291.             end
  292.         end
  293.     end
  294.  
  295.     local tr = localplayer:GetEyeTrace()
  296.  
  297.     if IsValid(tr.Entity) and tr.Entity:isKeysOwnable() and tr.Entity:GetPos():Distance(localplayer:GetPos()) < 200 then
  298.         tr.Entity:drawOwnableInfo()
  299.     end
  300. end
  301.  
  302. /*---------------------------------------------------------------------------
  303. Drawing death notices
  304. ---------------------------------------------------------------------------*/
  305. function GAMEMODE:DrawDeathNotice(x, y)
  306.     if not GAMEMODE.Config.showdeaths then return end
  307.     self.BaseClass:DrawDeathNotice(x, y)
  308. end
  309.  
  310. /*---------------------------------------------------------------------------
  311. Display notifications
  312. ---------------------------------------------------------------------------*/
  313. local function DisplayNotify(msg)
  314.     local txt = msg:ReadString()
  315.     GAMEMODE:AddNotify(txt, msg:ReadShort(), msg:ReadLong())
  316.     surface.PlaySound("buttons/lightswitch2.wav")
  317.  
  318.     -- Log to client console
  319.     print(txt)
  320. end
  321. usermessage.Hook("_Notify", DisplayNotify)
  322.  
  323. /*---------------------------------------------------------------------------
  324. Remove some elements from the HUD in favour of the DarkRP HUD
  325. ---------------------------------------------------------------------------*/
  326. function GAMEMODE:HUDShouldDraw(name)
  327.     if name == "CHudHealth" or
  328.         name == "CHudBattery" or
  329.         name == "CHudSuitPower" or
  330.         (HelpToggled and name == "CHudChat") then
  331.             return false
  332.     else
  333.         return true
  334.     end
  335. end
  336.  
  337. /*---------------------------------------------------------------------------
  338. Disable players' names popping up when looking at them
  339. ---------------------------------------------------------------------------*/
  340. function GAMEMODE:HUDDrawTargetID()
  341.    return false
  342. end
  343.  
  344. /*---------------------------------------------------------------------------
  345. Actual HUDPaint hook
  346. ---------------------------------------------------------------------------*/
  347. function DrawHUD()
  348.     localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer()
  349.     if not IsValid(localplayer) then return end
  350.    
  351.     -- Custom
  352.     Base()
  353.    
  354.     -- Default
  355.     GunLicense()
  356.     Agenda()
  357.     DrawVoiceChat()
  358.     LockDown()
  359.     Arrested()
  360.     AdminTell()
  361.     DrawEntityDisplay()
  362.    
  363. end
  364. hook.Add("HUDPaint", "DrawHUD", DrawHUD)
Add Comment
Please, Sign In to add comment