Advertisement
Guest User

Untitled

a guest
May 3rd, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 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. -- Draw for odd numbered columns
  80. for i=1, #sboard_panel.cols, 2 do
  81. surface.DrawRect(self:GetWide() - (50*i) - 25 - scr, 0, 50, self:GetTall())
  82. end
  83. else
  84. -- If columns are not setup yet, fall back to darkening the areas for the
  85. -- default columns
  86. surface.DrawRect(self:GetWide() - 175 - 25 - scr, 0, 50, self:GetTall())
  87. surface.DrawRect(self:GetWide() - 75 - 25 - scr, 0, 50, self:GetTall())
  88. end
  89. end
  90.  
  91. function PANEL:AddPlayerRow(ply)
  92. if ScoreGroup(ply) == self.group and not self.rows[ply] then
  93. local row = vgui.Create("TTTScorePlayerRow", self)
  94. row:SetPlayer(ply)
  95. self.rows[ply] = row
  96. self.rowcount = table.Count(self.rows)
  97.  
  98. -- row:InvalidateLayout()
  99.  
  100. -- must force layout immediately or it takes its sweet time to do so
  101. self:PerformLayout()
  102. --self:InvalidateLayout()
  103. end
  104. end
  105.  
  106. function PANEL:HasPlayerRow(ply)
  107. return self.rows[ply] != nil
  108. end
  109.  
  110. function PANEL:HasRows()
  111. return self.rowcount > 0
  112. end
  113.  
  114. function PANEL:UpdateSortCache()
  115. self.rows_sorted = {}
  116. for k,v in pairs(self.rows) do
  117. table.insert(self.rows_sorted, v)
  118. end
  119.  
  120. table.sort(self.rows_sorted, CompareScore)
  121. end
  122.  
  123. function PANEL:UpdatePlayerData()
  124. local to_remove = {}
  125. for k,v in pairs(self.rows) do
  126. -- Player still belongs in this group?
  127. if ValidPanel(v) and IsValid(v:GetPlayer()) and ScoreGroup(v:GetPlayer()) == self.group then
  128. v:UpdatePlayerData()
  129. else
  130. -- can't remove now, will break pairs
  131. table.insert(to_remove, k)
  132. end
  133. end
  134.  
  135. if #to_remove == 0 then return end
  136.  
  137. for k,ply in pairs(to_remove) do
  138. local pnl = self.rows[ply]
  139. if ValidPanel(pnl) then
  140. pnl:Remove()
  141. end
  142.  
  143. -- print(CurTime(), "Removed player", ply)
  144.  
  145. self.rows[ply] = nil
  146. end
  147. self.rowcount = table.Count(self.rows)
  148.  
  149. self:UpdateSortCache()
  150.  
  151. self:InvalidateLayout()
  152. end
  153.  
  154.  
  155. function PANEL:PerformLayout()
  156. if self.rowcount < 1 then
  157. self:SetVisible(false)
  158. return
  159. end
  160.  
  161. self:SetSize(self:GetWide(), 30 + self.rowcount + self.rowcount * SB_ROW_HEIGHT)
  162.  
  163. -- Sort and layout player rows
  164. self:UpdateSortCache()
  165.  
  166. local y = 24
  167. for k, v in ipairs(self.rows_sorted) do
  168. v:SetPos(0, y)
  169. v:SetSize(self:GetWide(), v:GetTall())
  170.  
  171. y = y + v:GetTall() + 1
  172. end
  173.  
  174. self:SetSize(self:GetWide(), 30 + (y - 24))
  175. end
  176.  
  177. vgui.Register("TTTScoreGroup", PANEL, "Panel")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement