Advertisement
Guest User

Untitled

a guest
May 15th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. ---- VGUI panel version of the scoreboard, based on TEAM GARRY's sandbox mode
  2. ---- scoreboard.
  3.  
  4. local surface = surface
  5. local draw = draw
  6. local math = math
  7. local string = string
  8. local vgui = vgui
  9.  
  10. local GetTranslation = LANG.GetTranslation
  11. local GetPTranslation = LANG.GetParamTranslation
  12.  
  13. include("sb_team.lua")
  14.  
  15. surface.CreateFont("cool_small", {font = "coolvetica",
  16. size = 20,
  17. weight = 400})
  18. surface.CreateFont("cool_large", {font = "coolvetica",
  19. size = 24,
  20. weight = 400})
  21. surface.CreateFont("treb_small", {font = "Trebuchet18",
  22. size = 14,
  23. weight = 700})
  24.  
  25. local logo = surface.GetTextureID("VGUI/ttt/score_logo")
  26.  
  27. local PANEL = {}
  28.  
  29. local max = math.max
  30. local floor = math.floor
  31. local function UntilMapChange()
  32. local rounds_left = max(0, GetGlobalInt("ttt_rounds_left", 6))
  33. local time_left = floor(max(0, ((GetGlobalInt("ttt_time_limit_minutes") or 60) * 60) - CurTime()))
  34.  
  35. local h = floor(time_left / 3600)
  36. time_left = time_left - floor(h * 3600)
  37. local m = floor(time_left / 60)
  38. time_left = time_left - floor(m * 60)
  39. local s = floor(time_left)
  40.  
  41. return rounds_left, string.format("%02i:%02i:%02i", h, m, s)
  42. end
  43.  
  44.  
  45. GROUP_TERROR = 1
  46. GROUP_NOTFOUND = 2
  47. GROUP_FOUND = 3
  48. GROUP_SPEC = 4
  49.  
  50. GROUP_COUNT = 4
  51.  
  52. function ScoreGroup(p)
  53. if not IsValid(p) then return -1 end -- will not match any group panel
  54.  
  55. if DetectiveMode() then
  56. if p:IsSpec() and (not p:Alive()) then
  57. if p:GetNWBool("body_found", false) then
  58. return GROUP_FOUND
  59. else
  60. local client = LocalPlayer()
  61. -- To terrorists, missing players show as alive
  62. if client:IsSpec() or
  63. client:IsActiveTraitor() or
  64. ((GAMEMODE.round_state != ROUND_ACTIVE) and client:IsTerror()) then
  65. return GROUP_NOTFOUND
  66. else
  67. return GROUP_TERROR
  68. end
  69. end
  70. end
  71. end
  72.  
  73. return p:IsTerror() and GROUP_TERROR or GROUP_SPEC
  74. end
  75.  
  76. function PANEL:Init()
  77.  
  78. self.hostdesc = vgui.Create("DLabel", self)
  79. self.hostdesc:SetText(GetTranslation("sb_playing"))
  80. self.hostdesc:SetContentAlignment(9)
  81.  
  82. self.hostname = vgui.Create( "DLabel", self )
  83. self.hostname:SetText( GetHostName() )
  84. self.hostname:SetContentAlignment(6)
  85.  
  86. self.mapchange = vgui.Create("DLabel", self)
  87. self.mapchange:SetText("Map changes in 00 rounds or in 00:00:00")
  88. self.mapchange:SetContentAlignment(9)
  89.  
  90. self.mapchange.Think = function (sf)
  91. local r, t = UntilMapChange()
  92.  
  93. sf:SetText(GetPTranslation("sb_mapchange",
  94. {num = r, time = t}))
  95. sf:SizeToContents()
  96. end
  97.  
  98.  
  99. self.ply_frame = vgui.Create( "TTTPlayerFrame", self )
  100.  
  101. self.ply_groups = {}
  102.  
  103. local t = vgui.Create("TTTScoreGroup", self.ply_frame:GetCanvas())
  104. t:SetGroupInfo(GetTranslation("terrorists"), Color(0,200,0,100), GROUP_TERROR)
  105. self.ply_groups[GROUP_TERROR] = t
  106.  
  107. t = vgui.Create("TTTScoreGroup", self.ply_frame:GetCanvas())
  108. t:SetGroupInfo(GetTranslation("spectators"), Color(200, 200, 0, 100), GROUP_SPEC)
  109. self.ply_groups[GROUP_SPEC] = t
  110.  
  111. if DetectiveMode() then
  112. t = vgui.Create("TTTScoreGroup", self.ply_frame:GetCanvas())
  113. t:SetGroupInfo(GetTranslation("sb_mia"), Color(130, 190, 130, 100), GROUP_NOTFOUND)
  114. self.ply_groups[GROUP_NOTFOUND] = t
  115.  
  116. t = vgui.Create("TTTScoreGroup", self.ply_frame:GetCanvas())
  117. t:SetGroupInfo(GetTranslation("sb_confirmed"), Color(130, 170, 10, 100), GROUP_FOUND)
  118. self.ply_groups[GROUP_FOUND] = t
  119. end
  120.  
  121. -- the various score column headers
  122. self.cols = {}
  123. self.cols[1] = vgui.Create( "DLabel", self )
  124. self.cols[1]:SetText( GetTranslation("sb_ping") )
  125.  
  126. self.cols[2] = vgui.Create( "DLabel", self )
  127. self.cols[2]:SetText( GetTranslation("sb_deaths") )
  128.  
  129. self.cols[3] = vgui.Create( "DLabel", self )
  130. self.cols[3]:SetText( GetTranslation("sb_score") )
  131.  
  132. self.cols[5] = vgui.Create( "DLabel", self )
  133. self.cols[5]:SetText("Rank")
  134.  
  135. if KARMA.IsEnabled() then
  136. self.cols[4] = vgui.Create("DLabel", self)
  137. self.cols[4]:SetText(GetTranslation("sb_karma"))
  138. end
  139.  
  140. self:UpdateScoreboard()
  141. self:StartUpdateTimer()
  142. end
  143.  
  144. function PANEL:StartUpdateTimer()
  145. if not timer.Exists("TTTScoreboardUpdater") then
  146. timer.Create( "TTTScoreboardUpdater", 0.3, 0,
  147. function()
  148. local pnl = GAMEMODE:GetScoreboardPanel()
  149. if IsValid(pnl) then
  150. pnl:UpdateScoreboard()
  151. end
  152. end)
  153. end
  154. end
  155.  
  156. local colors = {
  157. bg = Color(30,30,30, 235),
  158. bar = Color(0,255,255,255)
  159. };
  160.  
  161. local y_logo_off = 72
  162.  
  163. function PANEL:Paint()
  164. -- Logo sticks out, so always offset bg
  165. draw.RoundedBox( 8, 0, y_logo_off, self:GetWide(), self:GetTall() - y_logo_off, colors.bg)
  166.  
  167. -- Server name is outlined by orange/gold area
  168. draw.RoundedBox( 8, 0, y_logo_off + 25, self:GetWide(), 32, colors.bar)
  169.  
  170. -- TTT Logo
  171. surface.SetTexture( logo )
  172. surface.SetDrawColor( 255, 255, 255, 255 )
  173. surface.DrawTexturedRect( 5, 0, 256, 256 )
  174.  
  175. end
  176.  
  177. function PANEL:PerformLayout()
  178. -- position groups and find their total size
  179. local gy = 0
  180. -- can't just use pairs (undefined ordering) or ipairs (group 2 and 3 might not exist)
  181. for i=1, GROUP_COUNT do
  182. local group = self.ply_groups[i]
  183. if ValidPanel(group) then
  184. if group:HasRows() then
  185. group:SetVisible(true)
  186. group:SetPos(0, gy)
  187. group:SetSize(self.ply_frame:GetWide(), group:GetTall())
  188. group:InvalidateLayout()
  189. gy = gy + group:GetTall() + 5
  190. else
  191. group:SetVisible(false)
  192. end
  193. end
  194. end
  195.  
  196. self.ply_frame:GetCanvas():SetSize(self.ply_frame:GetCanvas():GetWide(), gy)
  197.  
  198. local h = y_logo_off + 110 + self.ply_frame:GetCanvas():GetTall()
  199.  
  200. -- if we will have to clamp our height, enable the mouse so player can scroll
  201. local scrolling = h > ScrH() * 0.95
  202. -- gui.EnableScreenClicker(scrolling)
  203. self.ply_frame:SetScroll(scrolling)
  204.  
  205. h = math.Clamp(h, 110 + y_logo_off, ScrH() * 0.95)
  206.  
  207. local w = math.max(ScrW() * 0.6, 640)
  208.  
  209. self:SetSize(w, h)
  210. self:SetPos( (ScrW() - w) / 2, math.min(72, (ScrH() - h) / 4))
  211.  
  212. self.ply_frame:SetPos(8, y_logo_off + 109)
  213. self.ply_frame:SetSize(self:GetWide() - 16, self:GetTall() - 109 - y_logo_off - 5)
  214.  
  215. -- server stuff
  216. self.hostdesc:SizeToContents()
  217. self.hostdesc:SetPos(w - self.hostdesc:GetWide() - 8, y_logo_off + 5)
  218.  
  219. local hw = w - 180 - 8
  220. self.hostname:SetSize(hw, 32)
  221. self.hostname:SetPos(w - self.hostname:GetWide() - 8, y_logo_off + 27)
  222.  
  223. surface.SetFont("cool_large")
  224. local hname = self.hostname:GetValue()
  225. local tw, _ = surface.GetTextSize(hname)
  226. while tw > hw do
  227. hname = string.sub(hname, 1, -6) .. "..."
  228. tw, th = surface.GetTextSize(hname)
  229. end
  230.  
  231. self.hostname:SetText(hname)
  232.  
  233. self.mapchange:SizeToContents()
  234. self.mapchange:SetPos(w - self.mapchange:GetWide() - 8, y_logo_off + 60)
  235.  
  236. -- score columns
  237. local cy = y_logo_off + 90
  238. for k,v in ipairs(self.cols) do
  239. v:SizeToContents()
  240. v:SetPos( w - (50*k) - v:GetWide()/2 - 8, cy)
  241. end
  242. end
  243.  
  244. function PANEL:ApplySchemeSettings()
  245. self.hostdesc:SetFont("cool_small")
  246. self.hostname:SetFont("cool_large")
  247. self.mapchange:SetFont("treb_small")
  248.  
  249. self.hostdesc:SetTextColor(COLOR_WHITE)
  250. self.hostname:SetTextColor(COLOR_BLACK)
  251. self.mapchange:SetTextColor(COLOR_WHITE)
  252.  
  253. for k,v in pairs(self.cols) do
  254. v:SetFont("treb_small")
  255. v:SetTextColor(COLOR_WHITE)
  256. end
  257. end
  258.  
  259. function PANEL:UpdateScoreboard( force )
  260. if not force and not self:IsVisible() then return end
  261.  
  262. local layout = false
  263.  
  264. -- Put players where they belong. Groups will dump them as soon as they don't
  265. -- anymore.
  266. for k, p in pairs(player.GetAll()) do
  267. if IsValid(p) then
  268. local group = ScoreGroup(p)
  269. if self.ply_groups[group] and not self.ply_groups[group]:HasPlayerRow(p) then
  270. self.ply_groups[group]:AddPlayerRow(p)
  271. layout = true
  272. end
  273. end
  274. end
  275.  
  276. for k, group in pairs(self.ply_groups) do
  277. if ValidPanel(group) then
  278. group:SetVisible( group:HasRows() )
  279. group:UpdatePlayerData()
  280. end
  281. end
  282.  
  283. if layout then
  284. self:PerformLayout()
  285. else
  286. self:InvalidateLayout()
  287. end
  288. end
  289.  
  290. vgui.Register( "TTTScoreboard", PANEL, "Panel" )
  291.  
  292. ---- PlayerFrame is defined in sandbox and is basically a little scrolling
  293. ---- hack. Just putting it here (slightly modified) because it's tiny.
  294.  
  295. local PANEL = {}
  296. function PANEL:Init()
  297. self.pnlCanvas = vgui.Create( "Panel", self )
  298. self.YOffset = 0
  299.  
  300. self.scroll = vgui.Create("DVScrollBar", self)
  301. end
  302.  
  303. function PANEL:GetCanvas() return self.pnlCanvas end
  304.  
  305. function PANEL:OnMouseWheeled( dlta )
  306. self.scroll:AddScroll(dlta * -2)
  307.  
  308. self:InvalidateLayout()
  309. end
  310.  
  311. function PANEL:SetScroll(st)
  312. self.scroll:SetEnabled(st)
  313. end
  314.  
  315. function PANEL:PerformLayout()
  316. self.pnlCanvas:SetVisible(self:IsVisible())
  317.  
  318. -- scrollbar
  319. self.scroll:SetPos(self:GetWide() - 16, 0)
  320. self.scroll:SetSize(16, self:GetTall())
  321.  
  322. local was_on = self.scroll.Enabled
  323. self.scroll:SetUp(self:GetTall(), self.pnlCanvas:GetTall())
  324. self.scroll:SetEnabled(was_on) -- setup mangles enabled state
  325.  
  326. self.YOffset = self.scroll:GetOffset()
  327.  
  328. self.pnlCanvas:SetPos( 0, self.YOffset )
  329. self.pnlCanvas:SetSize( self:GetWide() - (self.scroll.Enabled and 16 or 0), self.pnlCanvas:GetTall() )
  330. end
  331. vgui.Register( "TTTPlayerFrame", PANEL, "Panel" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement