SetBorderImage binded function is reset in Press and Release function

I have a class deriving from SButton

In the Construct, I bind a function for the button’s border image.

SButton::Construct(ButtonArgs);
SetBorderImage(TAttribute<const FSlateBrush*>(this, &SKButton::GetButtonImage));

The problem resides in SButton::Press() and SButton::Release()
Both functions resets the binding

void SButton::Press()
{
if ( !bIsPressed )
{
bIsPressed = true;
PlayPressedSound();
OnPressed.ExecuteIfBound();
UpdatePressStateChanged();
}
}
void SButton::Release()
{
if ( bIsPressed )
{
bIsPressed = false;
OnReleased.ExecuteIfBound();
UpdatePressStateChanged();
}
}
void SButton::UpdatePressStateChanged()
{
UpdatePadding();
UpdateBorderImage();
UpdateForegroundColor();
}

The function UpdateBorderImage() resets the binding

void SButton::UpdateBorderImage()
{
if (!GetShowDisabledEffect() && !IsInteractable())
{
SetBorderImage(&Style->Disabled);
}
else if (IsPressed())
{
SetBorderImage(&Style->Pressed);
}
else if (IsHovered())
{
SetBorderImage(&Style->Hovered);
}
else
{
SetBorderImage(&Style->Normal);
}
}

Therefore I have to set back the binding after Press() and Release()

const FSlateBrush* SKButton::GetButtonImage() const
{
if (IsSelected())
{
if (IsPressed())
{
return &SelectedStyle->Pressed;
}
else if (IsHovered())
{
return &SelectedStyle->Hovered;
}
else
{
return &SelectedStyle->Normal;
}
}
return SBorder::GetBorderImage();
}
void SKButton::Press()
{
SButton::Press();
// We have to set back the function for getting the button image
// since in UE5 after Press() the border attribute is set again
SetBorderImage(TAttribute<const FSlateBrush*>(this, &SKButton::GetButtonImage));
}
void SKButton::Release()
{
SButton::Release();
// We have to set back the function for getting the button image
// since in UE5 after Press() the border attribute is set again
SetBorderImage(TAttribute<const FSlateBrush*>(this, &SKButton::GetButtonImage));
}

To avoid this problem, the function SButton::UpdateBorderImage() should be in protected and overridable or should check if the border image in SBorder is binded to a function.

D.

1 Like