Skip to content

Commit

Permalink
Fixed PR #1007
Browse files Browse the repository at this point in the history
In addition to spacing changes, fixed a case where OldHeight would be
overridden to an incorrect value if SetExpanded was called while the
animation was going ( i.e. while the height of the panel was not what it
should be for self.OldHeight purpose )

Added IsValid() checks for self.Contents, and fixed an animation bug
when in spawnmenu opening the tool categories after first opening the
spawnemenu with saved closed categories would make the animation expand
to a wrong value.

I still hate this panel very much, and none of these patches with #1007
and this one make it perfect.
  • Loading branch information
robotboy655 committed May 13, 2016
1 parent c3b672f commit 893d84e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 96 deletions.
130 changes: 59 additions & 71 deletions garrysmod/lua/vgui/dcategorycollapse.lua
@@ -1,16 +1,5 @@
--[[ _
( )
_| | __ _ __ ___ ___ _ _
/'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |( ___/| | | ( ) ( ) |( (_| |
`\__,_)`\____)(_) (_) (_) (_)`\__,_)

DCollapsibleCategory
--]]
local PANEL =
{
local PANEL = {

Init = function( self )

Expand Down Expand Up @@ -54,17 +43,15 @@ local PANEL =

derma.DefineControl( "DCategoryHeader", "Category Header", PANEL, "DButton" )

local PANEL = {}

AccessorFunc( PANEL, "m_bSizeExpanded", "Expanded", FORCE_BOOL )
AccessorFunc( PANEL, "m_iContentHeight", "StartHeight" )
AccessorFunc( PANEL, "m_fAnimTime", "AnimTime" )
AccessorFunc( PANEL, "m_bDrawBackground", "PaintBackground", FORCE_BOOL )
AccessorFunc( PANEL, "m_bDrawBackground", "DrawBackground", FORCE_BOOL ) -- deprecated
AccessorFunc( PANEL, "m_iPadding", "Padding" )
AccessorFunc( PANEL, "m_pList", "List" )
AccessorFunc( PANEL, "m_bSizeExpanded", "Expanded", FORCE_BOOL )
AccessorFunc( PANEL, "m_iContentHeight", "StartHeight" )
AccessorFunc( PANEL, "m_fAnimTime", "AnimTime" )
AccessorFunc( PANEL, "m_bDrawBackground", "PaintBackground", FORCE_BOOL )
AccessorFunc( PANEL, "m_bDrawBackground", "DrawBackground", FORCE_BOOL ) -- deprecated
AccessorFunc( PANEL, "m_iPadding", "Padding" )
AccessorFunc( PANEL, "m_pList", "List" )

--[[---------------------------------------------------------
Init
Expand All @@ -75,7 +62,7 @@ function PANEL:Init()
self.Header:Dock( TOP )
self.Header:SetSize( 20, 20 )
self:SetSize( 16, 16 );
self:SetSize( 16, 16 )
self:SetExpanded( true )
self:SetMouseInputEnabled( true )
Expand All @@ -84,39 +71,39 @@ function PANEL:Init()
self:SetPaintBackground( true )
self:DockMargin( 0, 0, 0, 2 )
self:DockPadding( 0, 0, 0, 5 )
self:DockPadding( 0, 0, 0, 0 )
end
--[[---------------------------------------------------------
Name: button
Name: button
-----------------------------------------------------------]]
function PANEL:Add( strName )
local button = vgui.Create( "DButton", self )
button.Paint = function( panel, w, h ) derma.SkinHook( "Paint", "CategoryButton", panel, w, h ) end
button.UpdateColours = function( button, skin )
if ( button.AltLine ) then
if ( button.AltLine ) then
if ( button.Depressed || button.m_bSelected ) then return button:SetTextStyleColor( skin.Colours.Category.LineAlt.Text_Selected ) end
if ( hovered ) then return button:SetTextStyleColor( skin.Colours.Category.LineAlt.Text_Hover ) end
return button:SetTextStyleColor( skin.Colours.Category.LineAlt.Text )
if ( button.Depressed || button.m_bSelected ) then return button:SetTextStyleColor( skin.Colours.Category.LineAlt.Text_Selected ) end
if ( hovered ) then return button:SetTextStyleColor( skin.Colours.Category.LineAlt.Text_Hover ) end
return button:SetTextStyleColor( skin.Colours.Category.LineAlt.Text )
end
end
if ( button.Depressed || button.m_bSelected ) then return button:SetTextStyleColor( skin.Colours.Category.Line.Text_Selected ) end
if ( hovered ) then return button:SetTextStyleColor( skin.Colours.Category.Line.Text_Hover ) end
return button:SetTextStyleColor( skin.Colours.Category.Line.Text )
if ( button.Depressed || button.m_bSelected ) then return button:SetTextStyleColor( skin.Colours.Category.Line.Text_Selected ) end
if ( hovered ) then return button:SetTextStyleColor( skin.Colours.Category.Line.Text_Hover ) end
return button:SetTextStyleColor( skin.Colours.Category.Line.Text )
end
end
button:SetHeight( 17 )
button:SetTextInset( 4, 0 )
button:SetContentAlignment( 4 )
button:DockMargin( 1, 0, 1, 0 )
button.DoClickInternal = function()
button.DoClickInternal = function()
if ( self:GetList() ) then
self:GetList():UnselectAll()
Expand Down Expand Up @@ -160,9 +147,8 @@ function PANEL:UpdateAltLines()
end
--[[---------------------------------------------------------
Name: Think
Name: Think
-----------------------------------------------------------]]
function PANEL:Think()
Expand All @@ -185,12 +171,13 @@ end
function PANEL:Paint( w, h )
derma.SkinHook( "Paint", "CollapsibleCategory", self, w, h )
return false
end
--[[---------------------------------------------------------
Name: SetContents
Name: SetContents
-----------------------------------------------------------]]
function PANEL:SetContents( pContents )
Expand All @@ -208,22 +195,21 @@ function PANEL:SetContents( pContents )
end
--[[---------------------------------------------------------
Name: SetContents
Name: SetContents
-----------------------------------------------------------]]
function PANEL:SetExpanded( expanded )
self.m_bSizeExpanded = tobool( expanded )
if ( !self.m_bSizeExpanded ) then
if ( !self.animSlide.Finished && self.OldHeight ) then return end
self.OldHeight = self:GetTall()
end
end
--[[---------------------------------------------------------
Name: Toggle
Name: Toggle
-----------------------------------------------------------]]
function PANEL:Toggle()
Expand All @@ -235,16 +221,16 @@ function PANEL:Toggle()
self:GetParent():InvalidateLayout()
self:GetParent():GetParent():InvalidateLayout()
local cookie = '1'
if ( !self:GetExpanded() ) then cookie = '0' end
self:SetCookie( "Open", cookie )
local open = "1"
if ( !self:GetExpanded() ) then open = "0" end
self:SetCookie( "Open", open )
self:OnToggle( self:GetExpanded( ) )
end
--[[---------------------------------------------------------
Name: OnToggle
Name: OnToggle
-----------------------------------------------------------]]
function PANEL:OnToggle( expanded )
Expand All @@ -253,23 +239,23 @@ function PANEL:OnToggle( expanded )
end
--[[---------------------------------------------------------
Name: DoExpansion
Name: DoExpansion
-----------------------------------------------------------]]
function PANEL:DoExpansion( b )
if ( self.m_bSizeExpanded == b ) then return end
self:Toggle();
self:Toggle()
end
--[[---------------------------------------------------------
Name: PerformLayout
Name: PerformLayout
-----------------------------------------------------------]]
function PANEL:PerformLayout()
local Padding = self:GetPadding() or 0
if ( self.Contents ) then
if ( IsValid( self.Contents ) ) then
if ( self:GetExpanded() ) then
self.Contents:InvalidateLayout( true )
Expand All @@ -294,7 +280,7 @@ function PANEL:PerformLayout()
self.Header:ApplySchemeSettings()
self.animSlide:Run()
self:UpdateAltLines();
self:UpdateAltLines()
end
Expand All @@ -303,35 +289,38 @@ end
-----------------------------------------------------------]]
function PANEL:OnMousePressed( mcode )
if ( !self:GetParent().OnMousePressed ) then return end;
if ( !self:GetParent().OnMousePressed ) then return end
return self:GetParent():OnMousePressed( mcode )
end
--[[---------------------------------------------------------
Name: AnimSlide
Name: AnimSlide
-----------------------------------------------------------]]
function PANEL:AnimSlide( anim, delta, data )
self:InvalidateLayout()
self:InvalidateParent()
if ( anim.Started ) then
if ( !IsValid( self.Contents ) && ( self.OldHeight or 0 ) < self.Header:GetTall() ) then
-- We are not using self.Contents and our designated height is less
-- than the header size, something is clearly wrong, try to rectify
self.OldHeight = 0
for id, pnl in pairs( self:GetChildren() ) do
self.OldHeight = self.OldHeight + pnl:GetTall()
end
end
if ( self:GetExpanded() ) then
data.To = self.OldHeight
else
data.To = self:GetTall()
data.To = self:GetTall()
end
end
if ( self.Contents ) then self.Contents:SetVisible( true ) end
if ( IsValid( self.Contents ) ) then self.Contents:SetVisible( true ) end
self:SetTall( Lerp( delta, data.From, data.To ) )
Expand All @@ -351,23 +340,22 @@ function PANEL:LoadCookies()
end
--[[---------------------------------------------------------
Name: GenerateExample
Name: GenerateExample
-----------------------------------------------------------]]
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
ctrl:SetLabel( "Category List Test Category" )
ctrl:SetSize( 300, 300 )
ctrl:SetPadding( 10 )
-- The contents can be any panel, even a DPanelList
local Contents = vgui.Create( "DButton" )
Contents:SetText( "This is the content of the control" )
ctrl:SetContents( Contents )
ctrl:InvalidateLayout( true )
ctrl:SetLabel( "Category List Test Category" )
ctrl:SetSize( 300, 300 )
ctrl:SetPadding( 10 )
-- The contents can be any panel, even a DPanelList
local Contents = vgui.Create( "DButton" )
Contents:SetText( "This is the content of the control" )
ctrl:SetContents( Contents )
ctrl:InvalidateLayout( true )
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
Expand Down
39 changes: 14 additions & 25 deletions garrysmod/lua/vgui/dcategorylist.lua
@@ -1,19 +1,8 @@
--[[ _
( )
_| | __ _ __ ___ ___ _ _
/'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |( ___/| | | ( ) ( ) |( (_| |
`\__,_)`\____)(_) (_) (_) (_)`\__,_)

DPanelList
A window.
--]]
local PANEL = {}

--[[---------------------------------------------------------
Name: Init
Name: Init
-----------------------------------------------------------]]
function PANEL:Init()
Expand All @@ -22,7 +11,7 @@ function PANEL:Init()
end
--[[---------------------------------------------------------
Name: AddItem
Name: AddItem
-----------------------------------------------------------]]
function PANEL:AddItem( item )
Expand All @@ -33,7 +22,7 @@ function PANEL:AddItem( item )
end
--[[---------------------------------------------------------
Name: Add
Name: Add
-----------------------------------------------------------]]
function PANEL:Add( name )
Expand Down Expand Up @@ -72,20 +61,20 @@ end
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
ctrl:SetSize( 300, 300 )
ctrl:SetSize( 300, 300 )
local Cat = ctrl:Add( "Test category with text contents" )
Cat:Add( "Item 1" )
Cat:Add( "Item 2" )
local Cat = ctrl:Add( "Test category with text contents" )
Cat:Add( "Item 1" )
Cat:Add( "Item 2" )
-- The contents can be any panel, even a DPanelList
local Cat2 = ctrl:Add( "Test category with panel contents" )
Cat2:SetTall( 100 )
local Contents = vgui.Create( "DButton" )
Contents:SetText( "This is the content of the category" )
Cat2:SetContents( Contents )
-- The contents can be any panel, even a DPanelList
local Cat2 = ctrl:Add( "Test category with panel contents" )
Cat2:SetTall( 100 )
local Contents = vgui.Create( "DButton" )
Contents:SetText( "This is the content of the category" )
Cat2:SetContents( Contents )
ctrl:InvalidateLayout( true )
ctrl:InvalidateLayout( true )
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
Expand Down

1 comment on commit 893d84e

@Bo98
Copy link
Contributor

@Bo98 Bo98 commented on 893d84e May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was so many edge cases for this it was unbelievable. And even then it seems I didn't cover them all.

ULX menu was another of those edge cases.

Please sign in to comment.