diff --git a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp index 4eb03b04e7bfdb3b83eb0d88a70a138efdb68aba..7e32a90d719d6f0fd489352bd9d38d4149baf206 100644 --- a/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp +++ b/Source/RWTHVRToolkit/Private/Pawn/RWTHVRPawn.cpp @@ -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.") + ; } - 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; } /* diff --git a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h index eaf24745465f7b78b3a46630db8d324704a55b60..bad99461296fe3e58a0fb8a0ca45827aa4959f7c 100644 --- a/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h +++ b/Source/RWTHVRToolkit/Public/Pawn/RWTHVRPawn.h @@ -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. */ @@ -30,10 +32,16 @@ public: virtual void Tick(float DeltaSeconds) override; virtual void NotifyControllerChanged() override; - + 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; };