/gamemode/modules/hud/cl_hud.lua

https://github.com/Blecky/DarkRP · Lua · 402 lines · 322 code · 63 blank · 17 comment · 92 complexity · 4bdcf1064a197e4bf42f0e7d5a1d87b1 MD5 · raw file

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