Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added smart cast WhenOrNone trait #58

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Config/DefaultGame.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ bAddPacks = True
InsertPack = (PackSource="StarterContent.upack",PackName="StarterContent")

[/Script/Cloud9.Cloud9DeveloperSettings]
bIsDrawHitCursorLine=False
bIsDrawDeprojectedCursorLine=False
bIsShowMouseCursor=False
NetGraph=1
r.IsDrawHitCursorLine=0
r.IsDrawDeprojectedCursorLine=1
r.IsShowMouseCursor=0
r.NetGraph=1
UnUsedEnum=Everything
UnUsedStruct=(IntField0=3,FloatField1=0.000000)

7 changes: 3 additions & 4 deletions Source/Cloud9/Character/Cloud9Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHit
SetCursorIsHidden(false);
}

let Settings = UCloud9DeveloperSettings::GetCloud9DeveloperSettings();
let Settings = UCloud9DeveloperSettings::Get();

if (Settings->bIsDrawHitCursorLine)
{
Expand Down Expand Up @@ -177,16 +177,15 @@ void ACloud9Character::SetViewDirection(const FHitResult& HitResult, bool bIsHit

void ACloud9Character::SetCameraRotationYaw(float Angle) const
{
auto Rotation = CameraBoom->GetRelativeRotation();
var Rotation = CameraBoom->GetRelativeRotation();
Rotation.Yaw = Angle;
UE_LOG(LogCloud9, Display, TEXT("SetRelativeRotation Pitch: %s"), *Rotation.ToString());
CameraBoom->SetRelativeRotation(Rotation);
}

void ACloud9Character::AddCameraRotationYaw(float Angle) const
{
const FRotator Rotation = {0.0f, Angle, 0.0f};
CameraBoom->AddRelativeRotation(Rotation);
CameraBoom->AddRelativeRotation({0.0f, Angle, 0.0f});
}

float ACloud9Character::GetCameraRotationRoll() const
Expand Down
22 changes: 11 additions & 11 deletions Source/Cloud9/Character/Components/Cloud9SpringArmComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ void UCloud9SpringArmComponent::UpdateDesiredArmLocation(
bool bDoRotationLag,
float DeltaTime)
{
FRotator DesiredRot = GetTargetRotation();
var DesiredRot = GetTargetRotation();

// Apply 'lag' to rotation if desired
if (bDoRotationLag)
{
if (bUseCameraLagSubstepping && DeltaTime > CameraLagMaxTimeStep && CameraRotationLagSpeed > 0.f)
{
const FRotator ArmRotStep = (DesiredRot - PreviousDesiredRot).GetNormalized() * (1.f / DeltaTime);
FRotator LerpTarget = PreviousDesiredRot;
float RemainingTime = DeltaTime;
let ArmRotStep = (DesiredRot - PreviousDesiredRot).GetNormalized() * (1.f / DeltaTime);
var LerpTarget = PreviousDesiredRot;
var RemainingTime = DeltaTime;
while (RemainingTime > KINDA_SMALL_NUMBER)
{
const float LerpAmount = FMath::Min(CameraLagMaxTimeStep, RemainingTime);
let LerpAmount = FMath::Min(CameraLagMaxTimeStep, RemainingTime);
LerpTarget += ArmRotStep * LerpAmount;
RemainingTime -= LerpAmount;

Expand Down Expand Up @@ -80,9 +80,9 @@ void UCloud9SpringArmComponent::UpdateDesiredArmLocation(
PreviousDesiredRot = DesiredRot;

// Get the spring arm 'origin', the target we want to look at
FVector ArmOrigin = GetComponentLocation() + TargetOffset;
var ArmOrigin = GetComponentLocation() + TargetOffset;
// We lag the target, not the actual camera position, so rotating the camera around does not have lag
FVector DesiredLoc = ArmOrigin;
var DesiredLoc = ArmOrigin;
if (bDoLocationLag)
{
let LocationLag = CameraLagSpeed * CameraLagVector;
Expand All @@ -91,13 +91,13 @@ void UCloud9SpringArmComponent::UpdateDesiredArmLocation(
&& DeltaTime > CameraLagMaxTimeStep
&& LocationLag != FVector::ZeroVector)
{
const FVector ArmMovementStep = (DesiredLoc - PreviousDesiredLoc) * (1.f / DeltaTime);
auto LerpTarget = PreviousDesiredLoc;
let ArmMovementStep = (DesiredLoc - PreviousDesiredLoc) * (1.f / DeltaTime);
var LerpTarget = PreviousDesiredLoc;

float RemainingTime = DeltaTime;
var RemainingTime = DeltaTime;
while (RemainingTime > KINDA_SMALL_NUMBER)
{
const float LerpAmount = FMath::Min(CameraLagMaxTimeStep, RemainingTime);
let LerpAmount = FMath::Min(CameraLagMaxTimeStep, RemainingTime);
LerpTarget += ArmMovementStep * LerpAmount;
RemainingTime -= LerpAmount;

Expand Down
2 changes: 0 additions & 2 deletions Source/Cloud9/Character/Components/Cloud9SpringArmComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class CLOUD9_API UCloud9SpringArmComponent : public USpringArmComponent
GENERATED_BODY()

public:
// TODO: CameraLagRotator

/**
* If bEnableCameraLag is true, controls how quickly camera reaches target position by each axis.
* Values of this vector multiplies with @CameraLagSpeed.
Expand Down
101 changes: 91 additions & 10 deletions Source/Cloud9/Game/Cloud9DeveloperSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@

#include "Cloud9/Cloud9.h"
#include "Cloud9/Tools/Extensions/UObject.h"
#include "Cloud9/Tools/Extensions/WhenOrNone.h"

const UCloud9DeveloperSettings* UCloud9DeveloperSettings::GetCloud9DeveloperSettings()
{
static var bIsInitialized = false;
static let Settings = GetDefault<UCloud9DeveloperSettings>();

if (!bIsInitialized)
{
Cast<UCloud9DeveloperSettings>(Settings)->Log();
bIsInitialized = true;
}
// ReSharper disable once CppPossiblyUninitializedMember
UCloud9DeveloperSettings::UCloud9DeveloperSettings(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer) {}

const UCloud9DeveloperSettings* UCloud9DeveloperSettings::Get()
{
static let Settings = StaticClass()->GetDefaultObject<UCloud9DeveloperSettings>();
Settings->InitializeCVars();
return Settings;
}

Expand All @@ -46,8 +44,91 @@ void UCloud9DeveloperSettings::Save()
Log();
}

template <typename TObject, typename TProperty>
auto RegisterConsoleVariable(TObject* Object, TProperty* Property)
{
using TCppType = typename TProperty::TCppType;

let ConsoleVariable = Property->GetMetaData(TEXT("ConsoleVariable"));
let ToolTip = Property->GetMetaData(TEXT("ToolTip"));

let ValuePtr = Property->template ContainerPtrToValuePtr<TCppType>(Object);

let ConsoleManager = &IConsoleManager::Get();

// FAutoConsoleVariableSink
let CVar = ConsoleManager->RegisterConsoleVariableRef(*ConsoleVariable, *ValuePtr, *ToolTip);

CVar->AsVariable()->SetOnChangedCallback(
FConsoleVariableDelegate::CreateLambda([Object](auto Arg) { Object->Save(); })
);

return CVar;
}

void UCloud9DeveloperSettings::InitializeCVars()
{
static var bIsConsoleInitialized = false;

if (!bIsConsoleInitialized)
{
bIsConsoleInitialized = true;

for (TFieldIterator<FProperty> It(GetClass()); It; ++It)
{
if (let Property = *It; Property->HasMetaData(TEXT("ConsoleVariable")))
{
// TODO: Refactor with EachOrNone
let CVar = Property | WhenOrNone{
[this](FBoolProperty* Arg) { return RegisterConsoleVariable(this, Arg); },
[this](FIntProperty* Arg) { return RegisterConsoleVariable(this, Arg); },
[this](FFloatProperty* Arg) { return RegisterConsoleVariable(this, Arg); },
};

if (!CVar)
{
UE_LOG(
LogCloud9,
Warning,
TEXT("Property with meta key 'ConsoleVariable' %s wasn't registered"
" as console variable due to unsupported type '%s'"),
*Property->GetName(),
*Property->GetCPPType()
)
}
}
}

Log();
}
}

void UCloud9DeveloperSettings::Log() const
{
let String = this | EUObject::Stringify();
UE_LOG(LogCloud9, Display, TEXT("%s"), *String);
}

void UCloud9DeveloperSettings::PostInitProperties()
{
InitializeCVars();
Super::PostInitProperties();
#if WITH_EDITOR
if (IsTemplate())
{
ImportConsoleVariableValues();
}
#endif
}

#if WITH_EDITOR
void UCloud9DeveloperSettings::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
InitializeCVars();
Super::PostEditChangeProperty(PropertyChangedEvent);
if (PropertyChangedEvent.Property)
{
ExportValuesToConsoleVariables(PropertyChangedEvent.Property);
}
}
#endif
65 changes: 41 additions & 24 deletions Source/Cloud9/Game/Cloud9DeveloperSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,55 @@ enum class EUnUsedEnum : int32
Whatever = 3,
};

UCLASS(Config=Game, defaultconfig, meta = (DisplayName="Save Game Settings"))
UCLASS(Config=Game, defaultconfig, meta = (DisplayName="Various Developer Settings"))
class CLOUD9_API UCloud9DeveloperSettings : public UDeveloperSettings
{
GENERATED_BODY()
GENERATED_UCLASS_BODY()

public: // properties
UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug, meta=(
ConsoleVariable="r.IsDrawHitCursorLine",
DisplayName="IsDrawHitCursorLine",
ToolTip="Whether to draw line from character to GetHitUnderCursor point"))
int32 bIsDrawHitCursorLine;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug, meta=(
ConsoleVariable="r.IsDrawDeprojectedCursorLine",
DisplayName="IsDrawDeprojectedCursorLine",
ToolTip="Whether to draw line from character to deprojected mouse cursor"))
int32 bIsDrawDeprojectedCursorLine;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug, meta=(
ConsoleVariable="r.IsShowMouseCursor",
DisplayName="IsShowMouseCursor",
ToolTip="Whether to show mouse cursor on screen or not in game"))
int32 bIsShowMouseCursor;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug, meta=(
ConsoleVariable="r.NetGraph",
DisplayName="NetGraph",
ToolTip="Whether to show FPS and other specific debug info"))
int32 NetGraph;

UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
EUnUsedEnum UnUsedEnum;

public:
UFUNCTION(BlueprintCallable, Category=Settings)
static const UCloud9DeveloperSettings* GetCloud9DeveloperSettings();
UPROPERTY(config, EditAnywhere, BlueprintReadWrite, Category=Debug)
FUnUsedStruct UnUsedStruct;

public: // static
UFUNCTION(BlueprintCallable, Category=Settings, DisplayName=GetCloud9DeveloperSettings)
static const UCloud9DeveloperSettings* Get();

public: // function
void InitializeCVars();

UFUNCTION(BlueprintCallable)
void Save();

UFUNCTION(BlueprintCallable)
void Log() const;

// Debug

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
bool bIsDrawHitCursorLine;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
bool bIsDrawDeprojectedCursorLine;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
bool bIsShowMouseCursor;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
int NetGraph;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
EUnUsedEnum UnUsedEnum;

UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "Debug")
FUnUsedStruct UnUsedStruct;
virtual void PostInitProperties() override;
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
};
2 changes: 1 addition & 1 deletion Source/Cloud9/Game/Cloud9GameMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void ACloud9GameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);

let Settings = UCloud9DeveloperSettings::GetCloud9DeveloperSettings();
let Settings = UCloud9DeveloperSettings::Get();

if (Settings->NetGraph > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Cloud9/Game/Cloud9PlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void ACloud9PlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);

let Settings = UCloud9DeveloperSettings::GetCloud9DeveloperSettings();
let Settings = UCloud9DeveloperSettings::Get();
bShowMouseCursor = Settings->bIsShowMouseCursor;
}

Expand Down
28 changes: 28 additions & 0 deletions Source/Cloud9/Tests/Unit/Fake/UWhenOrNoneTestClassA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "UWhenOrNoneTestClassA.h"

// ReSharper disable once CppPossiblyUninitializedMember
UWhenOrNoneTestClassA::UWhenOrNoneTestClassA(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer) {}
44 changes: 44 additions & 0 deletions Source/Cloud9/Tests/Unit/Fake/UWhenOrNoneTestClassA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "UWhenOrNoneTestClassA.generated.h"

UCLASS()
class UWhenOrNoneTestClassA : public UObject
{
GENERATED_UCLASS_BODY()

public: // properties
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Debug)
bool BoolField;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Debug)
int IntField;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Debug)
float FloatField;
};
Loading