Simulate button press / release

This commit is contained in:
Steve Streeting 2024-06-25 18:05:20 +01:00
parent 78d203e7c1
commit d3045cc463
2 changed files with 32 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "StevesUI/FocusableButton.h"
#include "StevesUI/SFocusableButton.h"
#include "Components/ButtonSlot.h"
#include "Framework/Application/NavigationConfig.h"
UFocusableButton::UFocusableButton(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
@ -43,6 +44,31 @@ void UFocusableButton::RefreshFocussedStyle_Implementation()
FocussedStyle.Normal = FocussedStyle.Hovered;
}
void UFocusableButton::SimulatePress()
{
if (MyButton)
{
// In order to get the visual change we need to call SButton::Press() and SButton::Release(), but they're both private
// The only public method we can use to simulate the click properly is OnKeyDown/Up or OnMouseButtonDown/Up
// Use Virtual_Accept since that is general
FKeyEvent KeyEvent(EKeys::Virtual_Accept, FModifierKeysState(), 0, false, 0, 0);
MyButton->OnKeyDown(MyButton->GetCachedGeometry(), KeyEvent);
}
}
void UFocusableButton::SimulateRelease()
{
if (MyButton)
{
// In order to get the visual change we need to call SButton::Press() and SButton::Release(), but they're both private
// The only public method we can use to simulate the click properly is OnKeyDown/Up or OnMouseButtonDown/Up
// Use Virtual_Accept since that is general
FKeyEvent KeyEvent(EKeys::Virtual_Accept, FModifierKeysState(), 0, false, 0, 0);
MyButton->OnKeyUp(MyButton->GetCachedGeometry(), KeyEvent);
}
}
void UFocusableButton::SlateHandleFocusReceived()
{
ApplyFocusStyle();

View File

@ -38,6 +38,12 @@ public:
UPROPERTY(BlueprintReadWrite, EditAnywhere)
bool bTakeFocusOnHover = true;
// Simulate a button press
UFUNCTION(BlueprintCallable)
void SimulatePress();
// Simulate a button release
UFUNCTION(BlueprintCallable)
void SimulateRelease();
protected:
FButtonStyle FocussedStyle;