diff --git a/Source/StudyFrameworkPlugin/Private/HUD/SFFadeHandler.cpp b/Source/StudyFrameworkPlugin/Private/HUD/SFFadeHandler.cpp index 5c0c230f30d29e9b03c14ed7f8ead09a1f609e47..7c1609f090860a29cb705a3be3a18619a9998467 100644 --- a/Source/StudyFrameworkPlugin/Private/HUD/SFFadeHandler.cpp +++ b/Source/StudyFrameworkPlugin/Private/HUD/SFFadeHandler.cpp @@ -11,7 +11,7 @@ #include "Help/SFUtils.h" #include "TimerManager.h" // For Tick Timer -#include "HUD/SFGlobalFadeGameViewportClient.h" // For Fade +#include "HUD/SFFadeVisualizer.h" // For Fade #include "Logging/SFLoggingUtils.h" @@ -177,15 +177,9 @@ void USFFadeHandler::FadeOut() float USFFadeHandler::FadeTimeRemaining() const { - const UWorld* World = USFGameInstance::Get()->GetWorld(); - if (World) + if (FadeVisualizer) { - USFGlobalFadeGameViewportClient* GameViewportClient = Cast<USFGlobalFadeGameViewportClient>( - World->GetGameViewport()); - if (GameViewportClient) - { - return GameViewportClient->FadeTimeRemaining(); - } + return FadeVisualizer->FadeTimeRemaining(); } return 0.0f; @@ -206,13 +200,11 @@ void USFFadeHandler::Fade(const float Duration, const bool bToBlack) const } } - if (USFGameInstance::IsInitialized() && USFGameInstance::Get()->GetWorld()) + if (USFGameInstance::IsInitialized()) { - USFGlobalFadeGameViewportClient* GameViewportClient = Cast<USFGlobalFadeGameViewportClient>( - USFGameInstance::Get()->GetWorld()->GetGameViewport()); - if (GameViewportClient) + if (FadeVisualizer) { - GameViewportClient->Fade(Duration, bToBlack, FadeConfig.FadeColor); + FadeVisualizer->Fade(Duration, bToBlack, FadeConfig.FadeColor); } } } @@ -273,6 +265,7 @@ FFadeConfig USFFadeHandler::GetFadeConfig() const void USFFadeHandler::SetFadeConfig(FFadeConfig InFadeConfig) { FadeConfig = InFadeConfig; + FadeVisualizer = NewObject<USFFadeVisualizer>(this, "FadeVisualizer"); Fade(0.0, FadeConfig.bStartFadedOut); } diff --git a/Source/StudyFrameworkPlugin/Private/HUD/SFFadeVisualizer.cpp b/Source/StudyFrameworkPlugin/Private/HUD/SFFadeVisualizer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f12390093614e04b8790f7004e3866b76483925a --- /dev/null +++ b/Source/StudyFrameworkPlugin/Private/HUD/SFFadeVisualizer.cpp @@ -0,0 +1,103 @@ +// Copyright 2015 Moritz Wundke. All Rights Reserved. +// Released under MIT + +#include "HUD/SFFadeVisualizer.h" +#include "Help/SFUtils.h" +#include "Logging/SFLoggingUtils.h" +#include "SFGameInstance.h" +#include "Kismet/GameplayStatics.h" + + +void USFFadeVisualizer::Tick(float DeltaTime) +{ + if (bFading) + { + DrawScreenFade(); + } +} +bool USFFadeVisualizer::IsTickable() const +{ + return true; +} + +bool USFFadeVisualizer::IsTickableInEditor() const +{ + return false; +} + +bool USFFadeVisualizer::IsTickableWhenPaused() const +{ + return false; +} + +ETickableTickType USFFadeVisualizer::GetTickableTickType() const +{ + return ETickableTickType::Always; +} +TStatId USFFadeVisualizer::GetStatId() const +{ + RETURN_QUICK_DECLARE_CYCLE_STAT(USFFadeVisualizer, STATGROUP_Tickables); +} + +void USFFadeVisualizer::ClearFade() +{ + bFading = false; +} + +void USFFadeVisualizer::Fade(const float Duration, const bool bToBlackN, const FLinearColor FadeColorN) +{ + bFading = true; + bToBlack = bToBlackN; + FadeDuration = Duration; + FadeStartTime = FPlatformTime::Seconds(); + FadeColor = FadeColorN; + + DrawScreenFade(); +} + +float USFFadeVisualizer::FadeTimeRemaining() +{ + if (bFading) + { + const double Time = FPlatformTime::Seconds(); + const float TimeLeft = FadeDuration - (Time - FadeStartTime); + if (TimeLeft > 0.0f) + { + return TimeLeft; + } + } + return 0.0f; +} + +void USFFadeVisualizer::DrawScreenFade() +{ + if (!bFading) + { + return; + } + + float Progress = 1.0f; //goes from 0.0 to 1.0 over a fade + if (FadeDuration > 0.0f) + { + Progress = 1.0f - (FadeTimeRemaining() / FadeDuration); + } + + Progress = FMath::Clamp(Progress, 0.0f, 1.0f); + + float FadeAlpha = Progress; + if (!bToBlack) { + FadeAlpha = 1.0f - Progress; + } + + APlayerCameraManager* CameraManager = UGameplayStatics::GetPlayerCameraManager(USFGameInstance::Get()->GetWorld(), 0); + if (CameraManager) { + CameraManager->SetManualCameraFade(FadeAlpha, FadeColor, false); + } + + if (Progress >= 1.0f && !bToBlack) { + ClearFade(); //fading is done + //only "stop fading" when we are fading in, otherwise we stay faded out! + } + +} + diff --git a/Source/StudyFrameworkPlugin/Private/HUD/SFGlobalFadeGameViewportClient.cpp b/Source/StudyFrameworkPlugin/Private/HUD/SFGlobalFadeGameViewportClient.cpp deleted file mode 100644 index f06622e42d98127c357dea79950b7bcdca0008bb..0000000000000000000000000000000000000000 --- a/Source/StudyFrameworkPlugin/Private/HUD/SFGlobalFadeGameViewportClient.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2015 Moritz Wundke. All Rights Reserved. -// Released under MIT - -#include "HUD/SFGlobalFadeGameViewportClient.h" -#include "Help/SFUtils.h" -#include "Logging/SFLoggingUtils.h" - - -#include "Engine/Canvas.h" -#include "HUD/SFMasterHUD.h" - -// Link to the Tutorial of the manual Viewport Client -// https://nerivec.github.io/old-ue4-wiki/pages/global-fade-in-out.html - -// Link for setting manual Viewport Client -// https://forums.unrealengine.com/unreal-engine/announcements-and-releases/1745504-a-new-community-hosted-unreal-engine-wiki - - -void USFGlobalFadeGameViewportClient::PostRender(UCanvas* Canvas) -{ - Super::PostRender(Canvas); - - // Fade if requested, you could use the same DrawScreenFade method from any canvas such as the HUD - if (bFading) - { - DrawScreenFade(Canvas); - } -} - -void USFGlobalFadeGameViewportClient::ClearFade() -{ - bFading = false; -} - -void USFGlobalFadeGameViewportClient::Fade(const float Duration, const bool bToBlackN, const FLinearColor FadeColorN) -{ - bFading = true; - bToBlack = bToBlackN; - FadeDuration = Duration; - FadeStartTime = FPlatformTime::Seconds(); - FadeColor = FadeColorN; -} - -float USFGlobalFadeGameViewportClient::FadeTimeRemaining() -{ - if (bFading) - { - const double Time = FPlatformTime::Seconds(); - const float TimeLeft = FadeDuration - (Time - FadeStartTime); - if (TimeLeft > 0.0f) - { - return TimeLeft; - } - } - return 0.0f; -} - -void USFGlobalFadeGameViewportClient::DrawScreenFade(UCanvas* Canvas) -{ - if (bFading) - { - float Progress = 1.0f; //goes from 0.0 to 1.0 over a fade - if (FadeDuration > 0.0f) - { - Progress = 1.0f - (FadeTimeRemaining() / FadeDuration); - } - - Progress = FMath::Clamp(Progress, 0.0f, 1.0f); - - //FSFLoggingUtils::Log("Fading, progress = " + FString::SanitizeFloat(Progress) + (bToBlack ? " to black" : " from black")); - FLinearColor FadeColorTmp = FadeColor; - //in case we are - FadeColorTmp.A *= bToBlack ? Progress : 1.0f - Progress; - - ASFMasterHUD* MasterHUD = nullptr; - APlayerController* PlayerController = GetWorld()->GetFirstPlayerController(); - if (PlayerController) - { - MasterHUD = Cast<ASFMasterHUD>(PlayerController->GetHUD()); - } - - if (MasterHUD && FSFUtils::IsPrimary()) - { - //if we use the HUD let it do the fading, so it can still be seen when faded out - MasterHUD->SetBackgroundColor(FadeColorTmp); - } - else - { - const FColor OldColor = Canvas->DrawColor; - Canvas->DrawColor = FadeColorTmp.ToFColor(true); - // TheJamsh: "4.10 cannot convert directly to FColor, so need to use FLinearColor::ToFColor() :) - Canvas->DrawTile(Canvas->DefaultTexture, 0, 0, Canvas->ClipX, Canvas->ClipY, 0, 0, - Canvas->DefaultTexture->GetSizeX(), Canvas->DefaultTexture->GetSizeY()); - Canvas->DrawColor = OldColor; - } - - if (Progress >= 1.0f && !bToBlack) { - ClearFade(); //fading is done - //only "stop fading" when we are fading in, otherwise we stay faded out! - } - } -} - diff --git a/Source/StudyFrameworkPlugin/Private/SFGameInstance.cpp b/Source/StudyFrameworkPlugin/Private/SFGameInstance.cpp index cf7606e3893412aecf1a2768905604fe2f7d79ce..2fac5df00ef646a1411ec4673d277a80549d68d7 100644 --- a/Source/StudyFrameworkPlugin/Private/SFGameInstance.cpp +++ b/Source/StudyFrameworkPlugin/Private/SFGameInstance.cpp @@ -6,7 +6,6 @@ #include "IUniversalLogging.h" #include "HUD/SFFadeHandler.h" #include "HUD/SFMasterHUD.h" -#include "HUD/SFGlobalFadeGameViewportClient.h" #include "Help/SFUtils.h" #include "Logging/SFLoggingBPLibrary.h" @@ -24,8 +23,6 @@ void USFGameInstance::Init() FSFLoggingUtils::Log("USFGameInstance::Init"); - GEngine->GameViewportClientClass = USFGlobalFadeGameViewportClient::StaticClass(); - IDisplayClusterClusterManager* ClusterManager = IDisplayCluster::Get().GetClusterMgr(); if (ClusterManager && !ClusterEventListenerDelegate.IsBound()) { diff --git a/Source/StudyFrameworkPlugin/Public/HUD/SFFadeHandler.h b/Source/StudyFrameworkPlugin/Public/HUD/SFFadeHandler.h index 961ba7fdebc3fc5371d5991709331cac2d1c1727..231c8db12998c87ebc124e03d1288d5510501327 100644 --- a/Source/StudyFrameworkPlugin/Public/HUD/SFFadeHandler.h +++ b/Source/StudyFrameworkPlugin/Public/HUD/SFFadeHandler.h @@ -2,6 +2,7 @@ #include "CoreMinimal.h" #include "Engine/World.h" +#include "HUD/SFFadeVisualizer.h" class USFGameInstance; class APlayerCameraManager; @@ -99,5 +100,8 @@ private: UPROPERTY() FTimerHandle TimerHandle; + + UPROPERTY() + USFFadeVisualizer* FadeVisualizer; }; diff --git a/Source/StudyFrameworkPlugin/Public/HUD/SFGlobalFadeGameViewportClient.h b/Source/StudyFrameworkPlugin/Public/HUD/SFFadeVisualizer.h similarity index 56% rename from Source/StudyFrameworkPlugin/Public/HUD/SFGlobalFadeGameViewportClient.h rename to Source/StudyFrameworkPlugin/Public/HUD/SFFadeVisualizer.h index 4132856ef19b12b5d09c50fcb33305c996a3e594..5439128b6835d92e5f4a79aef36f11c80580b66a 100644 --- a/Source/StudyFrameworkPlugin/Public/HUD/SFGlobalFadeGameViewportClient.h +++ b/Source/StudyFrameworkPlugin/Public/HUD/SFFadeVisualizer.h @@ -3,8 +3,8 @@ #pragma once -#include "DisplayClusterViewportClient.h" -#include "SFGlobalFadeGameViewportClient.generated.h" + +#include "SFFadeVisualizer.generated.h" /** * A simple UGameViewportClient used to handle a global fade in/out @@ -13,39 +13,38 @@ */ UCLASS() -class STUDYFRAMEWORKPLUGIN_API USFGlobalFadeGameViewportClient : public UDisplayClusterViewportClient +class STUDYFRAMEWORKPLUGIN_API USFFadeVisualizer : public UObject, public FTickableGameObject { GENERATED_BODY() public: - /** - * Called after rendering the player views and HUDs to render menus, the console, etc. - * This is the last rendering call in the render loop - * - * @param Canvas The canvas to use for rendering. - */ - virtual void PostRender(UCanvas* Canvas) override; + void Tick(float DeltaTime) override; + bool IsTickable() const override; + bool IsTickableInEditor() const override; + bool IsTickableWhenPaused() const override; + ETickableTickType GetTickableTickType() const override; + TStatId GetStatId() const override; /** Clear fading state */ - virtual void ClearFade(); + void ClearFade(); /** Used for Fade to and from black * Call with Duration = 0.0f if you want it e.g. to stay faded out (default starting state) */ - virtual void Fade(const float Duration, const bool bToBlackN, const FLinearColor FadeColorN = FLinearColor::Black); + void Fade(const float Duration, const bool bToBlackN, const FLinearColor FadeColorN = FLinearColor::Black); /** Get how many seconds are left */ float FadeTimeRemaining(); /** Does the actual screen fading */ - void DrawScreenFade(UCanvas* Canvas); + void DrawScreenFade(); private: // Values used by our screen fading // these default values result in a faded out start on intialization of a viewport - bool bFading = true; + bool bFading = false; bool bToBlack = true; double FadeStartTime = 0.0; float FadeDuration = 0.0f;