using Sandbox.Engine;
namespace Sandbox.UI;
public partial class Panel
{
///
/// A string to show when hovering over this panel.
///
public string Tooltip { get; set; }
///
/// The created tooltip element will have this class, if set.
///
public string TooltipClass { get; set; }
///
/// You should override and return true if you're overriding .
/// Otherwise this will return true if is not empty.
///
[Hide]
public virtual bool HasTooltip => !string.IsNullOrWhiteSpace( Tooltip );
///
/// Pushes the global context to whatever is suitable for this panel.
/// This should never really have to be called, when panels tick render etc. they'll already be in the right context.
/// This is for when the UI system is used outside of the standard contexts, like tooltips.
///
IDisposable PushGlobalContext()
{
var rootPanel = FindRootPanel();
var isMenu = GlobalContext.Menu.UISystem?.RootPanels.Contains( rootPanel ); // assume game context
return isMenu == true ? GlobalContext.MenuScope() : GlobalContext.GameScope();
}
///
/// Create a tooltip panel. You can override this to create a custom tooltip panel.
/// If you're overriding this and not setting , then you must override and return true in .
///
protected virtual Panel CreateTooltipPanel()
{
if ( string.IsNullOrWhiteSpace( Tooltip ) )
return null;
using var scope = PushGlobalContext();
var p = new Panel( null );
p.AddClass( "tooltip" );
p.AddClass( TooltipClass );
p.SetProperty( "style", "position: absolute; pointer-events: none; z-index: 10000;" );
var textContents = new Label
{
Parent = p,
Text = Tooltip
};
p.Parent = FindRootPanel();
return p;
}
}