Files
sbox-public/engine/Sandbox.Engine/Systems/UI/Panel/Panel.BeforeAfter.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

65 lines
1.6 KiB
C#

using Sandbox.Audio;
namespace Sandbox.UI;
public partial class Panel
{
/// <summary>
/// if we have a ::before element, this is it.
/// </summary>
Panel _beforeElement;
/// <summary>
/// if we have a ::after element, this is it.
/// </summary>
Panel _afterElement;
/// <summary>
/// Called during tick to create or destroy the ::before and ::after elements.
/// </summary>
void UpdateBeforeAfterElements()
{
// Don't do this if we ARE a ::before or ::after element.
if ( PseudoClass.Contains( PseudoClass.Before ) ) return;
if ( PseudoClass.Contains( PseudoClass.After ) ) return;
BuildPseudoElement( Style.HasBeforeElement, PseudoClass.Before, ref _beforeElement );
BuildPseudoElement( Style.HasAfterElement, PseudoClass.After, ref _afterElement );
// Make sure it's always first
if ( _beforeElement.IsValid() ) SetChildIndex( _beforeElement, 0 );
// Make sure it's always last
if ( _afterElement.IsValid() ) SetChildIndex( _afterElement, _children.Count - 1 );
}
/// <summary>
/// Called to update the state of a before or after element. Either destroying it or creating it.
/// </summary>
private void BuildPseudoElement( bool shouldExist, PseudoClass additionalClass, ref Panel panel )
{
// destroy it if it exists
if ( !shouldExist )
{
if ( panel is not null )
{
panel.Delete();
panel = null;
}
return;
}
// create it if it doesn't exist
if ( !panel.IsValid() )
{
panel = new Label();
panel.ElementName = "element";
panel.RemoveClass( "label" );
panel.PseudoClass = additionalClass;
AddChild( panel );
}
}
}