Compare commits

...

4 Commits

Author SHA1 Message Date
Steve Streeting
2ecc91f554
Merge pull request #17 from emistorrs/LoseFocusOnUnhover
Unfocus on Unhover for FocusableButton and FocusableCheckbox
2024-12-04 18:07:42 +00:00
EmiStorrs
6af2ba2766 Prevent Unfocus if the widget is not focused by the current user 2024-12-04 12:46:27 -05:00
Steve Streeting
5ec3dafae4 Fix merging superclass world bounds as local (#13) 2024-12-04 13:00:42 +00:00
EmiStorrs
75f38faa81 Unfocus on Unhover for Button and Checkbox 2024-12-03 17:47:04 -05:00
5 changed files with 75 additions and 3 deletions

View File

@ -137,7 +137,8 @@ FPrimitiveSceneProxy* UStevesEditorVisComponent::CreateSceneProxy()
FBoxSphereBounds UStevesEditorVisComponent::CalcBounds(const FTransform& LocalToWorld) const FBoxSphereBounds UStevesEditorVisComponent::CalcBounds(const FTransform& LocalToWorld) const
{ {
FBoxSphereBounds B = Super::CalcBounds(LocalToWorld); // Get superclass bounds in LOCAL space (don't pass LocalToWorld)
FBoxSphereBounds B = Super::CalcBounds(FTransform::Identity);
// Now we need to merge in all components // Now we need to merge in all components
for (auto& L : Lines) for (auto& L : Lines)

View File

@ -109,5 +109,35 @@ void UFocusableButton::SlateHandleHovered()
void UFocusableButton::SlateHandleUnhovered() void UFocusableButton::SlateHandleUnhovered()
{ {
OnUnhovered.Broadcast(); if (bLoseFocusOnUnhover)
{
Unfocus();
}
OnUnhovered.Broadcast();
}
void UFocusableButton::Unfocus() const
{
APlayerController* OwningPlayer = GetOwningPlayer();
if (OwningPlayer == nullptr || !OwningPlayer->IsLocalController() || OwningPlayer->Player ==
nullptr)
{
return;
}
if (ULocalPlayer* LocalPlayer = OwningPlayer->GetLocalPlayer())
{
TOptional<int32> UserIndex = FSlateApplication::Get().GetUserIndexForController(
LocalPlayer->GetControllerId());
if (UserIndex.IsSet())
{
TSharedPtr<SWidget> SafeWidget = GetCachedWidget();
if (SafeWidget.IsValid())
{
if (SafeWidget->HasUserFocus(UserIndex.GetValue()))
{
FSlateApplication::Get().ClearUserFocus(UserIndex.GetValue());
}
}
}
}
} }

View File

@ -95,5 +95,35 @@ void UFocusableCheckBox::SlateHandleHovered()
void UFocusableCheckBox::SlateHandleUnhovered() void UFocusableCheckBox::SlateHandleUnhovered()
{ {
OnUnhovered.Broadcast(); if (bLoseFocusOnUnhover)
{
Unfocus();
}
OnUnhovered.Broadcast();
}
void UFocusableCheckBox::Unfocus() const
{
APlayerController* OwningPlayer = GetOwningPlayer();
if (OwningPlayer == nullptr || !OwningPlayer->IsLocalController() || OwningPlayer->Player ==
nullptr)
{
return;
}
if (ULocalPlayer* LocalPlayer = OwningPlayer->GetLocalPlayer())
{
TOptional<int32> UserIndex = FSlateApplication::Get().GetUserIndexForController(
LocalPlayer->GetControllerId());
if (UserIndex.IsSet())
{
TSharedPtr<SWidget> SafeWidget = GetCachedWidget();
if (SafeWidget.IsValid())
{
if (SafeWidget->HasUserFocus(UserIndex.GetValue()))
{
FSlateApplication::Get().ClearUserFocus(UserIndex.GetValue());
}
}
}
}
} }

View File

@ -38,6 +38,9 @@ public:
UPROPERTY(BlueprintReadWrite, EditAnywhere) UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bTakeFocusOnHover = true; bool bTakeFocusOnHover = true;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bLoseFocusOnUnhover = true;
// Simulate a button press // Simulate a button press
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
void SimulatePress(); void SimulatePress();
@ -56,6 +59,8 @@ protected:
void SlateHandleHovered(); void SlateHandleHovered();
void SlateHandleUnhovered(); void SlateHandleUnhovered();
void Unfocus() const;
virtual TSharedRef<SWidget> RebuildWidget() override; virtual TSharedRef<SWidget> RebuildWidget() override;

View File

@ -43,6 +43,10 @@ public:
UPROPERTY(BlueprintReadWrite, EditAnywhere) UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bTakeFocusOnHover = true; bool bTakeFocusOnHover = true;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bLoseFocusOnUnhover = true;
protected: protected:
FCheckBoxStyle FocussedStyle; FCheckBoxStyle FocussedStyle;
@ -54,6 +58,8 @@ protected:
void SlateHandleHovered(); void SlateHandleHovered();
void SlateHandleUnhovered(); void SlateHandleUnhovered();
void Unfocus() const;
virtual TSharedRef<SWidget> RebuildWidget() override; virtual TSharedRef<SWidget> RebuildWidget() override;
public: public: