Add category filter

This commit is contained in:
StyledStrike 2023-02-19 13:37:33 -03:00
parent e209225968
commit d58abafc82
2 changed files with 54 additions and 15 deletions

View File

@ -1,16 +1,22 @@
function CLoadout:InitRegistry()
local registry = {}
local categories = {}
for _, v in pairs( list.Get( "Weapon" ) ) do
if not v.ClassName then continue end
--if not v.Spawnable then continue end
if not v.ClassName or not v.Spawnable then continue end
-- dont list "base" class weapons
if string.find( v.ClassName, "_base", 1, true ) then continue end
local cat = v.Category or "-"
if not isstring( cat ) then
cat = tostring( cat )
end
categories[cat] = true
registry[v.ClassName] = {
admin_only = v.AdminOnly,
name = ( v.PrintName and v.PrintName ~= "" ) and v.PrintName or v.ClassName
adminOnly = v.AdminOnly,
name = ( v.PrintName and v.PrintName ~= "" ) and v.PrintName or v.ClassName,
category = cat
}
if v.Primary and not v.Primary.ClipSize then
@ -22,7 +28,10 @@ function CLoadout:InitRegistry()
end
end
self.categories = table.GetKeys( categories )
self.weaponRegistry = registry
table.sort( self.categories )
end
function CLoadout:GetAmmoLimits()

View File

@ -141,6 +141,9 @@ function CLoadout:UpdateAvailableList()
-- dont list weapons that are on the loadout already
if isOnLoadout( class ) then continue end
-- dont list weapons that dont match the category filter
if self.categoryFilter and v.category ~= self.categoryFilter then continue end
-- dont list weapons that dont match the search filter
if self.filter ~= "" then
local foundClass = string.find( class, self.filter, 1, true )
@ -159,12 +162,12 @@ function CLoadout:UpdateAvailableList()
icon:SetTooltip( langGet( "cloadout.weapon_unavailable" ) )
end
if v.admin_only then
if v.adminOnly then
icon:SetAdminOnly( true )
end
icon.DoClick = function()
if v.admin_only and not localPly:IsAdmin() then
if v.adminOnly and not localPly:IsAdmin() then
Derma_Message(
langGet( "cloadout.admin_only" ),
langGet( "cloadout.weapon_restricted" ),
@ -257,7 +260,7 @@ function CLoadout:UpdateLoadoutList()
continue
end
if regWeapon.admin_only then
if regWeapon.adminOnly then
icon:SetAdminOnly( true )
end
@ -376,13 +379,40 @@ function CLoadout:ShowPanel()
----- LEFT PANEL STUFF
local labelAvailable = vgui.Create( "DLabel", leftPanel )
labelAvailable:SetText( langGet( "cloadout.available_weapons" ) )
labelAvailable:SetFont( "Trebuchet24" )
labelAvailable:SetTextColor( Color( 150, 255, 150 ) )
labelAvailable:Dock( TOP )
labelAvailable:DockMargin( 4, 2, 0, 2 )
-- category combo
self.comboCategory = vgui.Create( "DComboBox", leftPanel )
self.comboCategory:SetFont( "Trebuchet24" )
self.comboCategory:SetSortItems( false )
self.comboCategory:SetTextColor( Color( 150, 255, 150 ) )
self.comboCategory:SetTall( 30 )
self.comboCategory:Dock( TOP )
self.comboCategory:DockMargin( 2, 2, 2, 2 )
self.comboCategory:AddChoice( langGet( "cloadout.available_weapons" ), nil, true )
for _, name in ipairs( self.categories ) do
self.comboCategory:AddChoice( name )
end
self.comboCategory.Paint = function( _, sw, sh )
surface.SetDrawColor( 0, 0, 0, 240 )
surface.DrawRect( 0, 0, sw, sh )
end
self.comboCategory.OnSelect = function( _, index )
-- wtf, sometimes "index" is a string
index = tonumber( index ) - 1
if index == 0 then
self.categoryFilter = nil
else
self.categoryFilter = self.categories[index]
end
self:UpdateLists()
end
-- search box
local entrySearch = vgui.Create( "DTextEntry", leftPanel )
entrySearch:SetFont( "ChatFont" )
entrySearch:SetMaximumCharCount( 64 )