Skip to content
Snippets Groups Projects
Commit 9b1eb745 authored by Kris Tabea Helwig's avatar Kris Tabea Helwig
Browse files

feat(pawn): Adds OnScaleChanged callback

parent fc4eda4e
Branches
Tags
3 merge requests!118fix: Converted all static meshes to nanite,!115Feature/scaling,!114Feature/scaling
......@@ -42,21 +42,16 @@ ARWTHVRPawn::ARWTHVRPawn(const FObjectInitializer& ObjectInitializer) : Super(Ob
LeftHand = CreateDefaultSubobject<UReplicatedMotionControllerComponent>(TEXT("Left Hand MCC"));
LeftHand->SetupAttachment(RootComponent);
UniformScale = GetActorScale3D().X;
GetRootComponent()->TransformUpdated.AddLambda([this](USceneComponent*, EUpdateTransformFlags, ETeleportType)
{
FVector CurrentScale = this->GetActorScale3D();
if (CurrentScale.X == CurrentScale.Y && CurrentScale.Y == CurrentScale.Z)
if (CurrentScale.X != UniformScale || CurrentScale.Y != UniformScale || CurrentScale.Z != UniformScale)
{
float expectedScale = GetWorldSettings()->WorldToMeters / InitialWorldToMeters;
float ErrorPrecision = 1E-05;
if (FMath::IsNearlyEqual(CurrentScale.X, expectedScale, ErrorPrecision))
{
return;
}
}
UE_LOGFMT(Toolkit, Warning,
"ARWTHVRPawn: Do not adjust the scale of the pawn directly. This will not work in VR. Use ARWTHVRPawn::SetScale(float) instead.")
;
}
});
}
......@@ -84,9 +79,17 @@ void ARWTHVRPawn::Tick(float DeltaSeconds)
*/
void ARWTHVRPawn::SetScale(float NewScale)
{
FVector NewScaleVector = FVector(NewScale, NewScale, NewScale);
GetWorldSettings()->WorldToMeters = InitialWorldToMeters * NewScale;
SetActorScale3D(NewScaleVector);
FVector OldScale = GetActorScale();
UniformScale = NewScale;
FVector NewScaleVector = FVector(UniformScale, UniformScale, UniformScale);
GetWorldSettings()->WorldToMeters = InitialWorldToMeters * UniformScale;
SetActorRelativeScale3D(NewScaleVector);
OnScaleChanged.Broadcast(OldScale, NewScale);
}
float ARWTHVRPawn::GetScale()
{
return UniformScale;
}
/*
......
......@@ -14,6 +14,8 @@ class UCameraComponent;
class UMotionControllerComponent;
struct FLiveLinkTransformStaticData;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnScaleChangedDelegate, FVector, OldScale, float, NewUniformScale);
/**
* Pawn implementation with additional VR functionality, can be used in the Cave, with an HMD and on desktop.
*/
......@@ -34,6 +36,12 @@ public:
UFUNCTION(BlueprintCallable)
void SetScale(float NewScale);
UFUNCTION(BlueprintCallable)
float GetScale();
UPROPERTY(BlueprintAssignable)
FOnScaleChangedDelegate OnScaleChanged;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Pawn|Input")
TArray<UInputMappingContext*> InputMappingContexts;
......@@ -115,4 +123,5 @@ protected:
private:
UInputComponent* ActivePlayerInputComponent;
float InitialWorldToMeters;
float UniformScale;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment