Skip to content

Commit

Permalink
Moved animating loading widget into OnPaint function
Browse files Browse the repository at this point in the history
- Removed ActiveTimer function due to possible inactive time
  • Loading branch information
truong-bui committed Aug 29, 2021
1 parent 3af381d commit 1425d18
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

void SHorizontalLoadingWidget::Construct(const FArguments& InArgs, const FLoadingWidgetSettings& Settings)
{
bIsActiveTimerRegistered = false;
bPlayReverse = Settings.ImageSequenceSettings.bPlayReverse;

// Root is a Horizontal Box of course
Expand Down
64 changes: 32 additions & 32 deletions Source/AsyncLoadingScreen/Private/SLoadingWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,42 @@
#include "Widgets/Layout/SSpacer.h"
#include "Engine/Texture2D.h"
#include "MoviePlayer.h"
#include "Widgets/SCompoundWidget.h"

EActiveTimerReturnType SLoadingWidget::AnimatingImageSequence(double InCurrentTime, float InDeltaTime)
{
if (CleanupBrushList.Num() > 1)
int32 SLoadingWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{
TotalDeltaTime += Args.GetDeltaTime();

if (TotalDeltaTime >= Interval)
{
if (bPlayReverse)
{
ImageIndex--;
}
else
{
ImageIndex++;
}

if (ImageIndex >= CleanupBrushList.Num())
if (CleanupBrushList.Num() > 1)
{
ImageIndex = 0;
}
else if (ImageIndex < 0)
{
ImageIndex = CleanupBrushList.Num() - 1;
}
if (bPlayReverse)
{
ImageIndex--;
}
else
{
ImageIndex++;
}

StaticCastSharedRef<SImage>(LoadingIcon)->SetImage(CleanupBrushList[ImageIndex].IsValid() ? CleanupBrushList[ImageIndex]->GetSlateBrush() : nullptr);
if (ImageIndex >= CleanupBrushList.Num())
{
ImageIndex = 0;
}
else if (ImageIndex < 0)
{
ImageIndex = CleanupBrushList.Num() - 1;
}

return EActiveTimerReturnType::Continue;
}
else
{
bIsActiveTimerRegistered = false;
return EActiveTimerReturnType::Stop;
StaticCastSharedRef<SImage>(LoadingIcon)->SetImage(CleanupBrushList[ImageIndex].IsValid() ? CleanupBrushList[ImageIndex]->GetSlateBrush() : nullptr);
}

TotalDeltaTime = 0.0f;
}


return SCompoundWidget::OnPaint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
}

SThrobber::EAnimation SLoadingWidget::GetThrobberAnimation(FThrobberSettings ThrobberSettings) const
Expand Down Expand Up @@ -79,12 +83,8 @@ void SLoadingWidget::ConstructLoadingIcon(const FLoadingWidgetSettings& Settings
LoadingIcon = SNew(SImage)
.Image(CleanupBrushList[ImageIndex]->GetSlateBrush());

// Register animated image sequence active timer event
if (!bIsActiveTimerRegistered)
{
bIsActiveTimerRegistered = true;
RegisterActiveTimer(Settings.ImageSequenceSettings.Interval, FWidgetActiveTimerDelegate::CreateSP(this, &SLoadingWidget::AnimatingImageSequence));
}
// Update play animation interval
Interval = Settings.ImageSequenceSettings.Interval;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

void SVerticalLoadingWidget::Construct(const FArguments& InArgs, const FLoadingWidgetSettings& Settings)
{
bIsActiveTimerRegistered = false;
bPlayReverse = Settings.ImageSequenceSettings.bPlayReverse;

// Root is a Vertical Box
Expand Down
17 changes: 12 additions & 5 deletions Source/AsyncLoadingScreen/Public/SLoadingWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ struct FLoadingWidgetSettings;
class SLoadingWidget : public SCompoundWidget
{
public:
/** Active timer event for animating the image sequence */
EActiveTimerReturnType AnimatingImageSequence(double InCurrentTime, float InDeltaTime);

// SWidgetOverrides
virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;

/** Gets the combined value of the animation properties as a single SThrobber::EAnimation value. */
SThrobber::EAnimation GetThrobberAnimation(FThrobberSettings ThrobberSettings) const;
Expand All @@ -35,12 +36,18 @@ class SLoadingWidget : public SCompoundWidget
TSharedRef<SWidget> LoadingIcon = SNullWidget::NullWidget;
// Image slate brush list
TArray<TSharedPtr<FDeferredCleanupSlateBrush>> CleanupBrushList;
// Current image sequence index
int32 ImageIndex = 0;

// Play image sequence in reverse
bool bPlayReverse = false;

bool bIsActiveTimerRegistered = false;
// Current image sequence index
mutable int32 ImageIndex = 0;

// Current total delta time
mutable float TotalDeltaTime = 0.0f;

//Time in second to update the images, the smaller value the faster of the animation. A zero value will update the images every frame.
float Interval = 0.05f;

// Getter for text visibility
EVisibility GetLoadingWidgetVisibility() const;
Expand Down

0 comments on commit 1425d18

Please sign in to comment.