Guest User

Untitled

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