Permalink
Browse files

DComboBox now sorts dropdown menu alphabetically

  • Loading branch information...
1 parent c0d265d commit 84d8935344021d70d90704f16e6ff42548ca5797 @robotboy655 robotboy655 committed Aug 27, 2014
Showing with 21 additions and 22 deletions.
  1. +21 −22 garrysmod/lua/vgui/dcombobox.lua
@@ -16,18 +16,18 @@ Derma_Hook( PANEL, "Paint", "Paint", "ComboBox" )
Derma_Install_Convar_Functions( PANEL )
--[[---------------------------------------------------------
-
+ Name: Init
-----------------------------------------------------------]]
function PANEL:Init()
self.DropButton = vgui.Create( "DPanel", self )
self.DropButton.Paint = function( panel, w, h ) derma.SkinHook( "Paint", "ComboDownArrow", panel, w, h ) end
self.DropButton:SetMouseInputEnabled( false )
self.DropButton.ComboBox = self
-
+
self:SetTall( 22 )
self:Clear()
-
+
self:SetContentAlignment( 4 )
self:SetTextInset( 8, 0 )
self:SetIsMenu( true )
@@ -103,16 +103,16 @@ function PANEL:ChooseOptionID( index )
local value = self:GetOptionText( index )
self:ChooseOption( value, index )
-
+
end
--[[---------------------------------------------------------
Name: GetSelected
-----------------------------------------------------------]]
function PANEL:GetSelectedID()
-
+
return self.selected
-
+
end
@@ -144,7 +144,7 @@ end
function PANEL:AddChoice( value, data, select )
local i = table.insert( self.Choices, value )
-
+
if ( data ) then
self.Data[ i ] = data
end
@@ -188,23 +188,22 @@ function PANEL:OpenMenu( pControlOpener )
self.Menu = DermaMenu()
- for k, v in pairs( self.Choices ) do
- self.Menu:AddOption( v, function() self:ChooseOption( v, k ) end )
- end
-
- local x, y = self:LocalToScreen( 0, self:GetTall() )
-
- self.Menu:SetMinimumWidth( self:GetWide() )
- self.Menu:Open( x, y, false, self )
-
+ for k, v in SortedPairsByValue( self.Choices ) do
+ self.Menu:AddOption( v, function() self:ChooseOption( v, k ) end )
+ end
+
+ local x, y = self:LocalToScreen( 0, self:GetTall() )
+
+ self.Menu:SetMinimumWidth( self:GetWide() )
+ self.Menu:Open( x, y, false, self )
end
function PANEL:CloseMenu()
-
+
if ( IsValid( self.Menu ) ) then
self.Menu:Remove()
- end
+ end
end
@@ -236,10 +235,10 @@ end
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
- ctrl:AddChoice( "Some Choice" )
- ctrl:AddChoice( "Another Choice" )
- ctrl:SetWide( 150 )
-
+ ctrl:AddChoice( "Some Choice" )
+ ctrl:AddChoice( "Another Choice" )
+ ctrl:SetWide( 150 )
+
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
end

6 comments on commit 84d8935

@Bo98
Contributor
Bo98 commented on 84d8935 Aug 27, 2014

Tbh it would be nice if this was optional.

@robotboy655
Collaborator

Why? Do you want it to output things randomly?

@Bo98
Contributor
Bo98 commented on 84d8935 Aug 27, 2014

Oh wait I just realised it wasn't in the order you add choices in the first place.

@Lexicality
Contributor

It could have been though - the choices table is an array. Switching to ipairs would have preserved the insert order.

@freemmaann

Any chance to disable the sorting? Messes up a hell alot of things. Especially numbering if it goes past 9th number.

@robotboy655
Collaborator

Override PANEL:OpenMenu in your panel and use whatever sorting you want.

Please sign in to comment.