Advertisement
Guest User

cl_entitiestab

a guest
Apr 24th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. /*---------------------------------------------------------------------------
  2. Base panel for custom entities
  3. ---------------------------------------------------------------------------*/
  4. local PANEL = {}
  5.  
  6. function PANEL:Init()
  7. self:EnableVerticalScrollbar()
  8. timer.Simple(0, function() if IsValid(self) then self:generateButtons() self:Refresh() end end)
  9. end
  10.  
  11. function PANEL:Rebuild()
  12. if #self.Items == 0 then return end
  13.  
  14. local lHeight, rHeight = 0, 0
  15. local height = 0
  16. local k = 0
  17. local visibleCount = 0
  18. local lastVisible = 0
  19. for i, item in pairs(self.Items) do
  20. if item:IsVisible() then
  21. visibleCount = visibleCount + 1
  22. lastVisible = i
  23. end
  24. end
  25.  
  26. for i, item in pairs(self.Items) do
  27. if not item:IsVisible() then continue end
  28. k = k + 1
  29. local goRight = k % 2 == 0
  30.  
  31. item:SetWide(self:GetWide() / 2 - 10)
  32. local x = goRight and self:GetWide() / 2 or 0
  33. item:SetPos(x, goRight and rHeight or lHeight)
  34.  
  35. rHeight = goRight and rHeight + item:GetTall() + 2 or rHeight
  36. lHeight = goRight and lHeight or lHeight + item:GetTall() + 2
  37. end
  38.  
  39. -- Make the category stretch if it's the only one
  40. if visibleCount == 1 then
  41. self.Items[lastVisible]:SetWide(self:GetWide())
  42. end
  43.  
  44. height = math.max(lHeight, rHeight)
  45. self:GetCanvas():SetTall(height)
  46. end
  47.  
  48. function PANEL:generateButtons()
  49. -- override this
  50. end
  51.  
  52. function PANEL:isItemHidden(cantBuy, important)
  53. return cantBuy and (GAMEMODE.Config.hideNonBuyable or (important and GAMEMODE.Config.hideTeamUnbuyable))
  54. end
  55.  
  56. function PANEL:shouldHide()
  57. -- override this
  58. end
  59.  
  60. function PANEL:Refresh()
  61. for k,v in pairs(self.Items) do
  62. if v.Refresh then v:Refresh() end
  63. end
  64. self:InvalidateLayout()
  65. end
  66.  
  67. derma.DefineControl("F4MenuEntitiesBase", "", PANEL, "DPanelList")
  68.  
  69. -- Create categories for an entity tab
  70. local function createCategories(self, categories, itemClick, canBuy)
  71. for _, cat in pairs(categories) do
  72. local dCat = vgui.Create("F4MenuCategory", self)
  73.  
  74. dCat:SetButtonFactory(function(item, ui)
  75. local pnl = vgui.Create("F4MenuEntityButton", ui)
  76. pnl:setDarkRPItem(item)
  77. pnl.DoClick = fp{itemClick, item}
  78.  
  79. return pnl
  80. end)
  81.  
  82. dCat:SetPerformLayout(function(contents)
  83. for k,v in pairs(contents.Items) do
  84. local can, important, _, price = canBuy(v.DarkRPItem)
  85. v:SetDisabled(not can, important)
  86. v:updatePrice(price)
  87. end
  88. end)
  89.  
  90. dCat:SetCategory(cat)
  91. self:AddItem(dCat)
  92. end
  93. end
  94.  
  95. /*---------------------------------------------------------------------------
  96. Entities panel
  97. ---------------------------------------------------------------------------*/
  98. PANEL = {}
  99.  
  100. local function canBuyEntity(item)
  101. local ply = LocalPlayer()
  102.  
  103. if istable(item.allowed) and not table.HasValue(item.allowed, ply:Team()) then return false, true end
  104. if item.customCheck and not item.customCheck(ply) then return false, true end
  105.  
  106. local canbuy, suppress, message, price = hook.Call("canBuyCustomEntity", nil, ply, item)
  107. local cost = price or item.getPrice and item.getPrice(ply, item.price) or item.price
  108. if not ply:canAfford(cost) then return false, false, message, cost end
  109.  
  110. if canbuy == false then
  111. return false, suppress, message, cost
  112. end
  113.  
  114. return true, nil, message, cost
  115. end
  116.  
  117. function PANEL:generateButtons()
  118. local categories = DarkRP.getCategories().entities
  119.  
  120. createCategories(self, categories, function(item) RunConsoleCommand("DarkRP", item.cmd) end, canBuyEntity)
  121. end
  122.  
  123. function PANEL:shouldHide()
  124. for k,v in pairs(DarkRPEntities) do
  125. local canBuy, important = canBuyEntity(v)
  126. if not self:isItemHidden(not canBuy, important) then return false end
  127. end
  128. return true
  129. end
  130.  
  131. derma.DefineControl("F4MenuEntities", "", PANEL, "F4MenuEntitiesBase")
  132.  
  133. /*---------------------------------------------------------------------------
  134. Shipments panel
  135. ---------------------------------------------------------------------------*/
  136. PANEL = {}
  137.  
  138. local function canBuyShipment(ship)
  139. local ply = LocalPlayer()
  140.  
  141. if not table.HasValue(ship.allowed, ply:Team()) then return false, true end
  142. if ship.customCheck and not ship.customCheck(ply) then return false, true end
  143.  
  144. local canbuy, suppress, message, price = hook.Call("canBuyShipment", nil, ply, ship)
  145. local cost = price or ship.getPrice and ship.getPrice(ply, ship.price) or ship.price
  146.  
  147. if not ply:canAfford(cost) then return false, false, message, cost end
  148.  
  149. if canbuy == false then
  150. return false, suppress, message, cost
  151. end
  152.  
  153. return true, nil, message, cost
  154. end
  155.  
  156. function PANEL:generateButtons()
  157. local categories = DarkRP.getCategories().shipments
  158.  
  159. createCategories(self, categories, function(item) RunConsoleCommand("DarkRP", "buyshipment", item.name) end, canBuyShipment)
  160. end
  161.  
  162. function PANEL:shouldHide()
  163. local shipments = fn.Filter(fn.Compose{fn.Not, fn.Curry(fn.GetValue, 2)("noship")}, CustomShipments)
  164.  
  165. for k,v in pairs(shipments) do
  166. local canBuy, important = canBuyShipment(v)
  167. if not self:isItemHidden(not canBuy, important) then return false end
  168. end
  169.  
  170. return true
  171. end
  172.  
  173. derma.DefineControl("F4MenuShipments", "", PANEL, "F4MenuEntitiesBase")
  174.  
  175. /*---------------------------------------------------------------------------
  176. Gun buying panel
  177. ---------------------------------------------------------------------------*/
  178. PANEL = {}
  179.  
  180. local function canBuyGun(ship)
  181. local ply = LocalPlayer()
  182.  
  183. if GAMEMODE.Config.restrictbuypistol and not table.HasValue(ship.allowed, ply:Team()) then return false, true end
  184. if ship.customCheck and not ship.customCheck(ply) then return false, true end
  185.  
  186. local canbuy, suppress, message, price = hook.Call("canBuyPistol", nil, ply, ship)
  187. local cost = price or ship.getPrice and ship.getPrice(ply, ship.pricesep) or ship.pricesep
  188.  
  189. if not ply:canAfford(cost) then return false, false, message, cost end
  190.  
  191. if canbuy == false then
  192. return false, suppress, message, cost
  193. end
  194.  
  195. return true, nil, message, cost
  196. end
  197.  
  198. function PANEL:generateButtons()
  199. local categories = DarkRP.getCategories().weapons
  200.  
  201. createCategories(self, categories, function(item) RunConsoleCommand("DarkRP", "buy", item.name) end, canBuyGun)
  202. end
  203.  
  204. function PANEL:shouldHide()
  205. local shipments = fn.Filter(fn.Curry(fn.GetValue, 2)("separate"), CustomShipments)
  206.  
  207. for k,v in pairs(shipments) do
  208. local canBuy, important = canBuyGun(v)
  209.  
  210. if not self:isItemHidden(not canBuy, important) then return false end
  211. end
  212.  
  213. return true
  214. end
  215.  
  216. derma.DefineControl("F4MenuGuns", "", PANEL, "F4MenuEntitiesBase")
  217.  
  218. /*---------------------------------------------------------------------------
  219. Ammo panel
  220. ---------------------------------------------------------------------------*/
  221. PANEL = {}
  222.  
  223. local function canBuyAmmo(item)
  224. local ply = LocalPlayer()
  225.  
  226. if item.customCheck and not item.customCheck(ply) then return false, true end
  227.  
  228. local canbuy, suppress, message, price = hook.Call("canBuyAmmo", nil, ply, item)
  229. local cost = price or item.getPrice and item.getPrice(ply, item.price) or item.price
  230. if not ply:canAfford(cost) then return false, false, message, cost end
  231.  
  232. if canbuy == false then
  233. return false, suppress, message, price
  234. end
  235.  
  236. return true, nil, message, price
  237. end
  238.  
  239. function PANEL:generateButtons()
  240. local categories = DarkRP.getCategories().ammo
  241.  
  242. createCategories(self, categories, function(item) RunConsoleCommand("DarkRP", "buyammo", item.id) end, canBuyAmmo)
  243. end
  244.  
  245. function PANEL:shouldHide()
  246. for k,v in pairs(GAMEMODE.AmmoTypes) do
  247. local canBuy, important = canBuyAmmo(v)
  248. if not self:isItemHidden(not canBuy, important) then return false end
  249. end
  250. return true
  251. end
  252.  
  253. derma.DefineControl("F4MenuAmmo", "", PANEL, "F4MenuEntitiesBase")
  254.  
  255. /*---------------------------------------------------------------------------
  256. Vehicles panel
  257. ---------------------------------------------------------------------------*/
  258. PANEL = {}
  259.  
  260. local function canBuyVehicle(item)
  261. local ply = LocalPlayer()
  262. local cost = item.getPrice and item.getPrice(ply, item.price) or item.price
  263.  
  264. if istable(item.allowed) and not table.HasValue(item.allowed, ply:Team()) then return false, true end
  265. if item.customCheck and not item.customCheck(ply) then return false, true end
  266.  
  267. local canbuy, suppress, message, price = hook.Call("canBuyVehicle", nil, ply, item)
  268.  
  269. cost = price or cost
  270.  
  271. if not ply:canAfford(cost) then return false, false, message, cost end
  272.  
  273. if canbuy == false then
  274. return false, suppress, message, cost
  275. end
  276.  
  277. return true, nil, message, cost
  278. end
  279.  
  280. function PANEL:generateButtons()
  281. local categories = DarkRP.getCategories().vehicles
  282.  
  283. createCategories(self, categories, function(item) RunConsoleCommand("DarkRP", "buyvehicle", item.name) end, canBuyVehicle)
  284. end
  285.  
  286. derma.DefineControl("F4MenuVehicles", "", PANEL, "F4MenuEntitiesBase")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement