Files
sbox-public/engine/Sandbox.Engine/Systems/UI/TooltipSystem.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

90 lines
1.4 KiB
C#

using Sandbox.Engine;
using Sandbox.Internal;
namespace Sandbox.UI;
internal static class TooltipSystem
{
static IPanel lastHovered;
static IPanel lastTooltip;
internal static void SetHovered( IPanel current )
{
if ( lastHovered == current )
return;
if ( current == null )
{
lastHovered = null;
lastTooltip?.Delete( false );
lastTooltip = null;
return;
}
if ( !current.HasTooltip )
{
SetHovered( current.Parent );
return;
}
lastHovered = current;
if ( current != null )
{
lastTooltip?.Delete( false );
lastTooltip = current.CreateTooltip();
Frame();
}
}
internal static void Frame()
{
if ( !lastTooltip.IsValid() )
return;
if ( !InputRouter.MouseCursorVisible )
{
lastTooltip?.Delete( false );
lastTooltip = null;
lastHovered = null;
return;
}
if ( lastHovered.IsValid() )
{
lastHovered.UpdateTooltip( lastTooltip );
}
//
// Given the mouse position, try to position the tooltip
// so it's not hanging off the screen. Not actually doing any
// kind of restrict to screen or anything.
//
var pos = InputRouter.MouseCursorPosition;
TextFlag align = 0;
if ( pos.x < Screen.Size.x * 0.70f )
{
align |= TextFlag.Right;
}
else
{
align |= TextFlag.Left;
}
if ( pos.y > Screen.Size.y * 0.1f )
{
align |= TextFlag.Top;
}
else
{
align |= TextFlag.Bottom;
}
lastTooltip.SetAbsolutePosition( align, pos, 20 );
}
}