From b00503fdc67e9ac637024449b6e8b06ebf9b2d7c Mon Sep 17 00:00:00 2001 From: BlueRose <378100977@qq.com> Date: Wed, 3 Sep 2025 13:52:28 +0800 Subject: [PATCH] vault backup: 2025-09-03 13:52:28 --- 03-UnrealEngine/UI/CommonUI.md | 90 +++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/03-UnrealEngine/UI/CommonUI.md b/03-UnrealEngine/UI/CommonUI.md index 5723aa0..101bd6d 100644 --- a/03-UnrealEngine/UI/CommonUI.md +++ b/03-UnrealEngine/UI/CommonUI.md @@ -250,4 +250,92 @@ PS: ## 函数? -KeyBoardFocus \ No newline at end of file +KeyBoardFocus + +```c++ +void UWidget::SetKeyboardFocus() +{ + TSharedPtr SafeWidget = GetCachedWidget(); + if (SafeWidget.IsValid()) + { +#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST) + if ( !SafeWidget->SupportsKeyboardFocus() ) + { + FMessageLog("PIE").Warning(FText::Format(LOCTEXT("ThisWidgetDoesntSupportFocus", "The widget {0} does not support focus. If this is a UserWidget, you should set bIsFocusable to true."), FText::FromString(GetNameSafe(this)))); + } +#endif + + if ( !FSlateApplication::Get().SetKeyboardFocus(SafeWidget) ) + { + if ( UWorld* World = GetWorld() ) + { + if ( ULocalPlayer* LocalPlayer = World->GetFirstLocalPlayerFromController() ) + { + LocalPlayer->GetSlateOperations().SetUserFocus(SafeWidget.ToSharedRef(), EFocusCause::SetDirectly); + } + } + } + } +} + + +void UWidget::SetFocus() +{ + SetUserFocus(GetOwningPlayer()); +} + +void UWidget::SetUserFocus(APlayerController* PlayerController) +{ + if ( PlayerController == nullptr || !PlayerController->IsLocalPlayerController() || PlayerController->Player == nullptr ) + { +#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST) + FMessageLog("PIE").Error()->AddToken(FTextToken::Create(LOCTEXT("NoPlayerControllerToFocus", "The PlayerController is not a valid local player so it can't focus on ")))->AddToken(FUObjectToken::Create(this)); +#endif + return; + } + + TSharedPtr SafeWidget = GetCachedWidget(); + if ( SafeWidget.IsValid() ) + { +#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST) + if ( !SafeWidget->SupportsKeyboardFocus() ) + { + TSharedRef Message = FMessageLog("PIE").Warning()->AddToken(FUObjectToken::Create(this)); +#if WITH_EDITORONLY_DATA + if(UObject* GeneratedBy = WidgetGeneratedBy.Get()) + { + Message->AddToken(FTextToken::Create(FText::FromString(TEXT(" in "))))->AddToken(FUObjectToken::Create(GeneratedBy)); + } +#endif + if (IsA(UUserWidget::StaticClass())) + { + Message->AddToken(FTextToken::Create(LOCTEXT("UserWidgetDoesntSupportFocus", " does not support focus, you should set bIsFocusable to true."))); + } + else + { + Message->AddToken(FTextToken::Create(LOCTEXT("NonUserWidgetDoesntSupportFocus", " does not support focus."))); + } + + } +#endif + + if ( ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer() ) + { + TOptional UserIndex = FSlateApplication::Get().GetUserIndexForController(LocalPlayer->GetControllerId()); + if (UserIndex.IsSet()) + { + FReply& DelayedSlateOperations = LocalPlayer->GetSlateOperations(); + if (FSlateApplication::Get().SetUserFocus(UserIndex.GetValue(), SafeWidget)) + { + DelayedSlateOperations.CancelFocusRequest(); + } + else + { + DelayedSlateOperations.SetUserFocus(SafeWidget.ToSharedRef()); + } + + } + } + } +} +``` \ No newline at end of file