Guest User

My custom HUD

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