// Fill out your copyright notice in the Description page of Project Settings.
#include "iTweenPCH.h"
#include "iTweenEvent.h"
#include "iTween.h"
UWorld* UiTween::GetWorldLocal()
{
for (TObjectIterator<UGameViewportClient> Itr; Itr; ++Itr)
{
return Itr->GetWorld();
}
return nullptr;
}
AiTweenEvent* UiTween::SpawnEvent(AiTAux* aux)
{
UWorld* world = GetWorldLocal();
if (world)
{
if (aux)
{
FActorSpawnParameters params;
params.Owner = aux;
AiTweenEvent* ie = nullptr;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
ie = world->SpawnActor<AiTweenEvent>(AiTweenEvent::StaticClass(), GetWorldLocal()->GetFirstPlayerController()->GetActorTransform(), params);
ie->tweenIndex = (aux->currentTweens.Add(ie));
return ie;
}
else
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "No iTAux defined");
}
return nullptr;
}
}
else
{
if (aux)
{
Print("No world defined", "error");
}
else
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "No iTAux defined");
}
}
return nullptr;
}
}
AiTAux* UiTween::GetAux()
{
AiTAux* aux = nullptr;
for (TObjectIterator<AiTAux> Itr; Itr; ++Itr)
{
if (Itr->IsA(AiTAux::StaticClass()))
{
return *Itr;
}
}
FActorSpawnParameters params;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
aux = (AiTAux*)(GetWorldLocal()->SpawnActor<AiTAux>(AiTAux::StaticClass(), params));
return aux;
}
UObject* UiTween::FindObjectByName(FString s)
{
FName n = FName(*s);
for (TObjectIterator<UObject> Itr; Itr; ++Itr)
{
FName f = Itr->GetFName();
if (f == n)
{
Print("UObject found!");
return *Itr;
}
else
{
continue;
}
}
Print("No UObject with the specified name was found.", "error");
return nullptr;
}
void UiTween::Print(FString message, FString type, float time, bool printToLog)
{
AiTAux* aux = GetAux();
if (aux && aux->printDebugMessages && type == "debug")
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, time, FColor::Cyan, message);
}
if (printToLog)
{
UE_LOG(LogTemp, All, TEXT("%s"), *message);
}
}
if (aux && aux->printErrorMessages && type == "error")
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, time, FColor::Red, message);
}
if (printToLog)
{
UE_LOG(LogTemp, All, TEXT("%s"), *message);
}
}
}
//Constraints
FVector UiTween::ConstrainVector(FVector inputVector, FVector currentVector, EVectorConstraints::VectorConstraints vectorConstraints)
{
switch (vectorConstraints)
{
case EVectorConstraints::VectorConstraints::none:
return inputVector;
case EVectorConstraints::VectorConstraints::xOnly:
return FVector(inputVector.X, currentVector.Y, currentVector.Z);
case EVectorConstraints::VectorConstraints::yOnly:
return FVector(currentVector.X, inputVector.Y, currentVector.Z);
case EVectorConstraints::VectorConstraints::zOnly:
return FVector(currentVector.X, currentVector.Y, inputVector.Z);
case EVectorConstraints::VectorConstraints::xyOnly:
return FVector(inputVector.X, inputVector.Y, currentVector.Z);
case EVectorConstraints::VectorConstraints::yzOnly:
return FVector(currentVector.X, inputVector.Y, inputVector.Z);
case EVectorConstraints::VectorConstraints::xzOnly:
return FVector(inputVector.X, currentVector.Y, inputVector.Z);
default:
return inputVector;
}
}
FVector2D UiTween::ConstrainVector2D(FVector2D inputVector2D, FVector2D currentVector2D, EVector2DConstraints::Vector2DConstraints vector2DConstraints)
{
switch (vector2DConstraints)
{
case EVector2DConstraints::Vector2DConstraints::none:
return inputVector2D;
case EVector2DConstraints::Vector2DConstraints::xOnly:
return FVector2D(inputVector2D.X, currentVector2D.Y);
case EVector2DConstraints::Vector2DConstraints::yOnly:
return FVector2D(currentVector2D.X, inputVector2D.Y);
default:
return inputVector2D;
}
}
FRotator UiTween::ConstrainRotator(FRotator inputRotator, FRotator currentRotator, ERotatorConstraints::RotatorConstraints rotatorConstraints)
{
switch (rotatorConstraints)
{
case ERotatorConstraints::RotatorConstraints::none:
return inputRotator;
case ERotatorConstraints::RotatorConstraints::pitchOnly:
return FRotator(inputRotator.Pitch, currentRotator.Yaw, currentRotator.Roll);
case ERotatorConstraints::RotatorConstraints::yawOnly:
return FRotator(currentRotator.Pitch, inputRotator.Yaw, currentRotator.Roll);
case ERotatorConstraints::RotatorConstraints::rollOnly:
return FRotator(currentRotator.Pitch, currentRotator.Yaw, inputRotator.Roll);
case ERotatorConstraints::RotatorConstraints::pitchYawOnly:
return FRotator(inputRotator.Pitch, inputRotator.Yaw, currentRotator.Roll);
case ERotatorConstraints::RotatorConstraints::yawRollOnly:
return FRotator(currentRotator.Pitch, inputRotator.Yaw, inputRotator.Roll);
case ERotatorConstraints::RotatorConstraints::pitchRollOnly:
return FRotator(inputRotator.Pitch, currentRotator.Yaw, inputRotator.Roll);
default:
return inputRotator;
}
}
//General Use
void UiTween::GenerateSplineFromVectorArray(AiTSpline* &owningActor, USplineComponent* &splineComponent, FVector referenceVector, FRotator referenceRotator, TArray<FVector> vectorArray, bool localToReference, bool closeSpline)
{
UWorld* world = GetWorldLocal();
AiTSpline* a = nullptr;
FActorSpawnParameters params;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
a = world->SpawnActor<AiTSpline>(AiTSpline::StaticClass(), params);
a->SetActorLocation(referenceVector);
owningActor = a;
splineComponent = a->spline;
splineComponent->ClearSplinePoints();
for (int i = 0; i < vectorArray.Num(); i++)
{
if (localToReference)
{
splineComponent->AddSplineWorldPoint(referenceRotator.RotateVector(vectorArray[i]) + referenceVector);
}
else
{
splineComponent->AddSplineWorldPoint(vectorArray[i]);
}
}
splineComponent->SetClosedLoop(closeSpline);
}
void UiTween::GenerateSplineFromRotatorArray(AiTSpline* &owningActor, USplineComponent* &splineComponent, FVector referenceVector, FRotator referenceRotator, TArray<FRotator> rotatorArray, float generatedPointDistance, bool localToReference, bool closeSpline)
{
UWorld* world = GetWorldLocal();
AiTSpline* a = nullptr;
FActorSpawnParameters params;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
a = world->SpawnActor<AiTSpline>(AiTSpline::StaticClass(), params);
a->SetActorLocation(referenceVector);
owningActor = a;
splineComponent = a->spline;
splineComponent->ClearSplinePoints();
for (int i = 0; i < rotatorArray.Num(); i++)
{
if (localToReference)
{
splineComponent->AddSplineWorldPoint(FRotator(FQuat(referenceRotator) * FQuat(rotatorArray[i])).RotateVector(FVector::ForwardVector) * generatedPointDistance + referenceVector);
}
else
{
splineComponent->AddSplineWorldPoint(rotatorArray[i].RotateVector(FVector::ForwardVector) * generatedPointDistance + referenceVector);
}
}
splineComponent->SetClosedLoop(closeSpline);
}
float UiTween::GetDistanceBetweenTwoVectors(FVector sourceVector, FVector destination)
{
return FMath::Sqrt(FMath::Pow((destination.X - sourceVector.X), 2) + FMath::Pow((destination.Y - sourceVector.Y), 2) + FMath::Pow((destination.Z - sourceVector.Z), 2));
}
//Stopping, Pausing and Resuming Tweens
void UiTween::StopTweeningByIndex(int32 index)
{
GetAux()->currentTweens[index]->EndPhase();
}
void UiTween::StopTweeningByTweeningObjectName(FName objectName)
{
for (AiTweenEvent* e : GetAux()->currentTweens)
{
if (e->actorTweening != nullptr)
{