Fix #6: Change InputImage update delay to a tickable

Previously used a timer but timers cannot run while game is paused, and UI images should update regardless.
This commit is contained in:
Steve Streeting 2022-11-03 12:18:34 +00:00
parent 997eddce01
commit 600603ce92
2 changed files with 59 additions and 10 deletions

View File

@ -107,15 +107,50 @@ void UInputImage::UpdateImage()
SetBrushFromAtlasInterface(Sprite, true);
}
}
DelayedUpdateImageTimer.Invalidate();
bIsDirty = false;
DelayUpdate = 0;
}
void UInputImage::MarkImageDirty()
{
if (!DelayedUpdateImageTimer.IsValid())
{
// Delayed update
GetWorld()->GetTimerManager().SetTimer(DelayedUpdateImageTimer, this, &UInputImage::UpdateImage, 0.5f);
bIsDirty = true;
DelayUpdate = 0.5f;
}
// Tickables
// We need a tick rather than FTimer because timers won't run when game is paused, and UIs are useful while paused!
ETickableTickType UInputImage::GetTickableTickType() const
{
return ETickableTickType::Conditional;
}
bool UInputImage::IsTickableWhenPaused() const
{
// UIs need to update while game is paused
return true;
}
bool UInputImage::IsTickableInEditor() const
{
return false;
}
bool UInputImage::IsTickable() const
{
// Only need to tick while dirty
return bIsDirty;
}
void UInputImage::Tick(float DeltaTime)
{
DelayUpdate -= DeltaTime;
if (DelayUpdate <= 0)
{
UpdateImage();
}
}
TStatId UInputImage::GetStatId() const
{
RETURN_QUICK_DECLARE_CYCLE_STAT( UInputImage, STATGROUP_Tickables );
}
// Tickables

View File

@ -10,7 +10,7 @@
/// A special widget containing an image which populates itself based on an input action / axis and can dynamically
/// change based on the active input method.
UCLASS()
class STEVESUEHELPERS_API UInputImage : public UImage
class STEVESUEHELPERS_API UInputImage : public UImage, public FTickableGameObject
{
GENERATED_BODY()
@ -39,9 +39,13 @@ protected:
UPROPERTY(EditAnywhere)
UUiTheme* CustomTheme;
bool bSubbedToInputEvents = false;
UPROPERTY(EditAnywhere)
bool bUpdateWhilePaused = true;
bool bSubbedToInputEvents = false;
bool bIsDirty = true;
float DelayUpdate = 0;
FTimerHandle DelayedUpdateImageTimer;
public:
/// Tell this image to display the bound action for the current input method
@ -77,6 +81,16 @@ public:
virtual void SetVisibility(ESlateVisibility InVisibility) override;
// FTickableGameObject begin
virtual void Tick(float DeltaTime) override;
virtual TStatId GetStatId() const override;
virtual bool IsTickableWhenPaused() const override;
virtual bool IsTickableInEditor() const override;
virtual bool IsTickable() const override;
virtual ETickableTickType GetTickableTickType() const override;
// FTickableGameObject end
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;