Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. ---- Unlike sandbox, we have teams to deal with, so here's an extra panel in the
  2. ---- hierarchy that handles a set of player rows belonging to its team.
  3.  
  4. include("sb_row.lua")
  5.  
  6. local function CompareScore(pa, pb)
  7. if not ValidPanel(pa) then return false end
  8. if not ValidPanel(pb) then return true end
  9.  
  10. local a = pa:GetPlayer()
  11. local b = pb:GetPlayer()
  12.  
  13. if not IsValid(a) then return false end
  14. if not IsValid(b) then return true end
  15.  
  16. if a:Frags() == b:Frags() then return a:Deaths() < b:Deaths() end
  17.  
  18. return a:Frags() > b:Frags()
  19. end
  20.  
  21. local PANEL = {}
  22.  
  23. function PANEL:Init()
  24. self.name = "Unnamed"
  25.  
  26. self.color = COLOR_WHITE
  27.  
  28. self.rows = {}
  29. self.rowcount = 0
  30.  
  31. self.rows_sorted = {}
  32.  
  33. self.group = "spec"
  34. end
  35.  
  36. function PANEL:SetGroupInfo(name, color, group)
  37. self.name = name
  38. self.color = color
  39. self.group = group
  40. end
  41.  
  42. local bgcolor = Color(20,20,20, 150)
  43. function PANEL:Paint()
  44. -- Darkened background
  45. draw.RoundedBox(8, 0, 0, self:GetWide(), self:GetTall(), bgcolor)
  46.  
  47. surface.SetFont("treb_small")
  48.  
  49. -- Header bg
  50. local txt = self.name .. " (" .. self.rowcount .. ")"
  51. local w, h = surface.GetTextSize(txt)
  52. draw.RoundedBox(8, 0, 0, w + 24, 20, self.color)
  53.  
  54. -- Shadow
  55. surface.SetTextPos(11, 11 - h/2)
  56. surface.SetTextColor(0,0,0, 200)
  57. surface.DrawText(txt)
  58.  
  59. -- Text
  60. surface.SetTextPos(10, 10 - h/2)
  61. surface.SetTextColor(255,255,255,255)
  62. surface.DrawText(txt)
  63.  
  64. -- Alternating row background
  65. local y = 24
  66. for i, row in ipairs(self.rows_sorted) do
  67. if (i % 2) != 0 then
  68. surface.SetDrawColor(75,75,75, 100)
  69. surface.DrawRect(0, y, self:GetWide(), row:GetTall())
  70. end
  71.  
  72. y = y + row:GetTall() + 1
  73. end
  74.  
  75. -- Column darkening
  76. local scr = sboard_panel.ply_frame.scroll.Enabled and 16 or 0
  77. surface.SetDrawColor(0,0,0, 80)
  78. if sboard_panel.cols then
  79. local cx = self:GetWide() - scr
  80. for k,v in ipairs(sboard_panel.cols) do
  81. cx = cx - v.Width
  82. if k % 2 == 1 then -- Draw for odd numbered columns
  83. surface.DrawRect(cx-v.Width/2, 0, v.Width, self:GetTall())
  84. end
  85. end
  86. else
  87. -- If columns are not setup yet, fall back to darkening the areas for the
  88. -- default columns
  89. surface.DrawRect(self:GetWide() - 175 - 25 - scr, 0, 50, self:GetTall())
  90. surface.DrawRect(self:GetWide() - 75 - 25 - scr, 0, 50, self:GetTall())
  91. end
  92. end
  93.  
  94. function PANEL:AddPlayerRow(ply)
  95. if ScoreGroup(ply) == self.group and not self.rows[ply] then
  96. local row = vgui.Create("TTTScorePlayerRow", self)
  97. row:SetPlayer(ply)
  98. self.rows[ply] = row
  99. self.rowcount = table.Count(self.rows)
  100.  
  101. -- row:InvalidateLayout()
  102.  
  103. -- must force layout immediately or it takes its sweet time to do so
  104. self:PerformLayout()
  105. --self:InvalidateLayout()
  106. end
  107. end
  108.  
  109. function PANEL:HasPlayerRow(ply)
  110. return self.rows[ply] != nil
  111. end
  112.  
  113. function PANEL:HasRows()
  114. return self.rowcount > 0
  115. end
  116.  
  117. function PANEL:UpdateSortCache()
  118. self.rows_sorted = {}
  119. for k,v in pairs(self.rows) do
  120. table.insert(self.rows_sorted, v)
  121. end
  122.  
  123. table.sort(self.rows_sorted, CompareScore)
  124. end
  125.  
  126. function PANEL:UpdatePlayerData()
  127. local to_remove = {}
  128. for k,v in pairs(self.rows) do
  129. -- Player still belongs in this group?
  130. if ValidPanel(v) and IsValid(v:GetPlayer()) and ScoreGroup(v:GetPlayer()) == self.group then
  131. v:UpdatePlayerData()
  132. else
  133. -- can't remove now, will break pairs
  134. table.insert(to_remove, k)
  135. end
  136. end
  137.  
  138. if #to_remove == 0 then return end
  139.  
  140. for k,ply in pairs(to_remove) do
  141. local pnl = self.rows[ply]
  142. if ValidPanel(pnl) then
  143. pnl:Remove()
  144. end
  145.  
  146. -- print(CurTime(), "Removed player", ply)
  147.  
  148. self.rows[ply] = nil
  149. end
  150. self.rowcount = table.Count(self.rows)
  151.  
  152. self:UpdateSortCache()
  153.  
  154. self:InvalidateLayout()
  155. end
  156.  
  157.  
  158. function PANEL:PerformLayout()
  159. if self.rowcount < 1 then
  160. self:SetVisible(false)
  161. return
  162. end
  163.  
  164. self:SetSize(self:GetWide(), 30 + self.rowcount + self.rowcount * SB_ROW_HEIGHT)
  165.  
  166. -- Sort and layout player rows
  167. self:UpdateSortCache()
  168.  
  169. local y = 24
  170. for k, v in ipairs(self.rows_sorted) do
  171. v:SetPos(0, y)
  172. v:SetSize(self:GetWide(), v:GetTall())
  173.  
  174. y = y + v:GetTall() + 1
  175. end
  176.  
  177. self:SetSize(self:GetWide(), 30 + (y - 24))
  178. end
  179.  
  180. vgui.Register("TTTScoreGroup", PANEL, "Panel")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement