Created
October 27, 2021 17:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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