vault backup: 2025-09-03 13:52:28
This commit is contained in:
@@ -250,4 +250,92 @@ PS:
|
||||
|
||||
|
||||
## 函数?
|
||||
KeyBoardFocus
|
||||
KeyBoardFocus
|
||||
|
||||
```c++
|
||||
void UWidget::SetKeyboardFocus()
|
||||
{
|
||||
TSharedPtr<SWidget> 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<SWidget> SafeWidget = GetCachedWidget();
|
||||
if ( SafeWidget.IsValid() )
|
||||
{
|
||||
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
|
||||
if ( !SafeWidget->SupportsKeyboardFocus() )
|
||||
{
|
||||
TSharedRef<FTokenizedMessage> 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<int32> 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user