Skip to content

Instantly share code, notes, and snippets.

@benui-dev
Created October 27, 2021 17:19
Show Gist options
  • Save benui-dev/3ae7e5bd87796564e6ad3e3e1678a5e9 to your computer and use it in GitHub Desktop.
Save benui-dev/3ae7e5bd87796564e6ad3e3e1678a5e9 to your computer and use it in GitHub Desktop.
Hacky way to see if the cursor is over a UI element. Useful for detecting whether to do raycasts into the world or not.
static const FString RootName = "SGameLayerManager";
bool IsOverUIElement()
{
FVector2D CursorPos = FSlateApplication::Get().GetCursorPos();
FWidgetPath WidgetPath = FSlateApplication::Get().LocateWindowUnderMouse( CursorPos, FSlateApplication::Get().GetInteractiveTopLevelWindows() );
if ( WidgetPath.IsValid() )
{
const FArrangedChildren::FArrangedWidgetArray& AllArrangedWidgets = WidgetPath.Widgets.GetInternalArray();
bool FoundRoot = false;
for ( int32 i = 0; i < WidgetPath.Widgets.Num(); ++i )
{
FArrangedWidget& ArrangedWidget = WidgetPath.Widgets[ i ];
TSharedRef<SWidget> Widget = ArrangedWidget.Widget;
if ( Widget->GetTypeAsString() == RootName )
{
// Wait until the next child to return to see if we're over something inside the root
FoundRoot = true;
}
else if ( FoundRoot )
{
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment