OLS/Source/ols/Private/Camera/Components/OLSCameraComponent.cpp
LongLy 57b53b9c0c Implemented OLSCameraMode.
Addressed @TODOs related to custom logs and OLSCameraMode
2025-01-20 14:08:07 -07:00

135 lines
4.1 KiB
C++

// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action.
#include "Camera/Components/OLSCameraComponent.h"
#include "Camera/OLSCameraMode.h"
#include "Engine/Canvas.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(OLSCameraComponent)
UOLSCameraComponent::UOLSCameraComponent(const FObjectInitializer& objectInitializer) : Super(objectInitializer)
{
CameraModeStack = nullptr;
FieldOfViewOffset = 0.0f;
}
UOLSCameraComponent* UOLSCameraComponent::FindCameraComponent(const AActor* Actor)
{
return (Actor ? Actor->FindComponentByClass<UOLSCameraComponent>() : nullptr);
}
AActor* UOLSCameraComponent::GetTargetActor() const
{
return GetOwner();
}
void UOLSCameraComponent::AddFieldOfViewOffset(float FovOffset)
{
FieldOfViewOffset += FovOffset;
}
void UOLSCameraComponent::DrawDebug(UCanvas* canvas) const
{
check(canvas);
FDisplayDebugManager& DisplayDebugManager = canvas->DisplayDebugManager;
DisplayDebugManager.SetFont(GEngine->GetSmallFont());
DisplayDebugManager.SetDrawColor(FColor::Yellow);
DisplayDebugManager.DrawString(FString::Printf(TEXT("LyraCameraComponent: %s"), *GetNameSafe(GetTargetActor())));
DisplayDebugManager.SetDrawColor(FColor::White);
DisplayDebugManager.DrawString(FString::Printf(TEXT(" Location: %s"), *GetComponentLocation().ToCompactString()));
DisplayDebugManager.DrawString(FString::Printf(TEXT(" Rotation: %s"), *GetComponentRotation().ToCompactString()));
DisplayDebugManager.DrawString(FString::Printf(TEXT(" FOV: %f"), FieldOfView));
check(CameraModeStack);
CameraModeStack->DrawDebug(canvas);
}
void UOLSCameraComponent::GetBlendInfo(float& outWeightOfTopLayer, FGameplayTag& outTagOfTopLayer) const
{
check(CameraModeStack);
CameraModeStack->GetBlendInfo(/*out*/ outWeightOfTopLayer, /*out*/ outTagOfTopLayer);
}
void UOLSCameraComponent::OnRegister()
{
Super::OnRegister();
if (!CameraModeStack)
{
CameraModeStack = NewObject<UOLSCameraModeStack>(this);
check(CameraModeStack);
}
}
void UOLSCameraComponent::GetCameraView(float deltaTime, FMinimalViewInfo& desiredView)
{
check(CameraModeStack);
UpdateCameraModes();
FOLSCameraModeView cameraModeView;
CameraModeStack->EvaluateStack(deltaTime, cameraModeView);
// Keep player controller in sync with the latest view.
if (APawn* targetPawn = Cast<APawn>(GetTargetActor()))
{
if (APlayerController* playerController = targetPawn->GetController<APlayerController>())
{
playerController->SetControlRotation(cameraModeView.ControlRotation);
}
}
// Apply any offset that was added to the field of view.
cameraModeView.FieldOfView += FieldOfViewOffset;
FieldOfViewOffset = 0.0f;
// Keep camera component in sync with the latest view.
SetWorldLocationAndRotation(cameraModeView.Location, cameraModeView.Rotation);
FieldOfView = cameraModeView.FieldOfView;
// Fill in desired view.
desiredView.Location = cameraModeView.Location;
desiredView.Rotation = cameraModeView.Rotation;
desiredView.FOV = cameraModeView.FieldOfView;
desiredView.OrthoWidth = OrthoWidth;
desiredView.OrthoNearClipPlane = OrthoNearClipPlane;
desiredView.OrthoFarClipPlane = OrthoFarClipPlane;
desiredView.AspectRatio = AspectRatio;
desiredView.bConstrainAspectRatio = bConstrainAspectRatio;
desiredView.bUseFieldOfViewForLOD = bUseFieldOfViewForLOD;
desiredView.ProjectionMode = ProjectionMode;
// See if the CameraActor wants to override the PostProcess settings used.
desiredView.PostProcessBlendWeight = PostProcessBlendWeight;
if (PostProcessBlendWeight > 0.0f)
{
desiredView.PostProcessSettings = PostProcessSettings;
}
if (IsXRHeadTrackedCamera())
{
// In XR much of the camera behavior above is irrellevant, but the post process settings are not.
Super::GetCameraView(deltaTime, desiredView);
}
}
void UOLSCameraComponent::UpdateCameraModes()
{
check(CameraModeStack);
if (CameraModeStack->IsStackActivate())
{
if (DetermineCameraModeDelegate.IsBound())
{
if (const TSubclassOf<UOLSCameraMode> cameraMode = DetermineCameraModeDelegate.Execute())
{
CameraModeStack->PushCameraMode(cameraMode);
}
}
}
}