#include "StoreFNameInputModifier.h" #include "EnhancedPlayerInput.h" #include "LuprexGameModeBase.h" static const int ENCODE_LIMIT = 0x00FFFFFF; FInputActionValue UPassFNameAsAxis3D::ModifyRaw_Implementation(const UEnhancedPlayerInput* PlayerInput, FInputActionValue CurrentValue, float DeltaTime) { // Internally, an FName is stored as three integers: // ComparisonIndex, DisplayIndex, and Number. These numbers // are only valid within a single unreal process, but that's OK. // We copy these three numbers into an Axis3D. // // Yes, that's ugly. It would be nicer if FName was one // of the types explicitly allowed in an FInputActionValue. // Maybe some day! // // Integers larger than ENCODE_LIMIT cannot be losslessly // converted to float. Such numbers should never occur in // practice: the string table should not contain that many // strings! // uint32 cidx = FNameToStore.GetComparisonIndex().ToUnstableInt(); uint32 didx = FNameToStore.GetDisplayIndex().ToUnstableInt(); uint32 nidx = FNameToStore.GetNumber(); // Make sure the three integers will fit into three floats without rounding or overflow. if ((cidx > ENCODE_LIMIT) || (didx > ENCODE_LIMIT) || (nidx > ENCODE_LIMIT)) { UE_LOG(LogLuprexIntegration, Error, TEXT("Name cannot be converted to FInputActionValue: %s"), *FNameToStore.ToString()); return FInputActionValue(FVector()); } return FInputActionValue(FVector(cidx, didx, nidx)); } FName UPassFNameAsAxis3D::DecodeFNameFromAxis3D(const FVector &Vec) { uint32 cidx = static_cast(Vec.X); uint32 didx = static_cast(Vec.Y); uint32 nidx = static_cast(Vec.Z); FName Result(FNameEntryId::FromUnstableInt(cidx), FNameEntryId::FromUnstableInt(didx), nidx); FName Inverse(FNameEntryId::FromUnstableInt(didx), FNameEntryId::FromUnstableInt(cidx), nidx); if ((double(cidx) != Vec.X) || (double(didx) != Vec.Y) || (double(nidx) != Vec.Z) || (cidx > ENCODE_LIMIT) || (didx > ENCODE_LIMIT) || (nidx > ENCODE_LIMIT) || (!Result.IsValid()) || (!Inverse.IsValid())) { UE_LOG(LogLuprexIntegration, Error, TEXT("FVector is not an encoded FName.")); return NAME_None; } return Result; }