131 lines
4.5 KiB
C++
131 lines
4.5 KiB
C++
// ©2022 Batty Bovine Productions, LLC. All Rights Reserved.
|
|
|
|
#include "BugSubmissionForm.h"
|
|
|
|
#include "BugMarkerActor.h"
|
|
#include "CommonButtonBase.h"
|
|
#include "CommonTextBlock.h"
|
|
#include "HttpModule.h"
|
|
#include "UnrealzillaGlobalSettings.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
|
|
const FText FormatFloatToText(float Float, int32 IntegralDigits, int32 FractionalDigits)
|
|
{
|
|
const float Rounded = FMath::RoundToInt(Float);
|
|
if(FMath::Abs(Float - Rounded) < FMath::Pow(10.0f, -1 * FractionalDigits))
|
|
{
|
|
Float = Rounded;
|
|
}
|
|
FNumberFormattingOptions NumberFormat;
|
|
NumberFormat.MinimumIntegralDigits = IntegralDigits;
|
|
NumberFormat.MaximumIntegralDigits = INT32_MAX;
|
|
NumberFormat.MinimumFractionalDigits = FractionalDigits;
|
|
NumberFormat.MaximumFractionalDigits = FractionalDigits;
|
|
|
|
return FText::AsNumber(Float, &NumberFormat);
|
|
}
|
|
|
|
|
|
void UBugSubmissionForm::NativeOnInitialized()
|
|
{
|
|
this->SubmitButton->OnClicked().AddUObject(this, &UBugSubmissionForm::SubmitForm);
|
|
this->CancelButton->OnClicked().AddUObject(this, &UBugSubmissionForm::CancelForm);
|
|
}
|
|
|
|
|
|
void UBugSubmissionForm::SetMarkerData(ABugMarkerActor *BugMarker)
|
|
{
|
|
this->BugMarkerActor = BugMarker;
|
|
|
|
const FVector MarkerLocation = this->BugMarkerActor->GetActorLocation();
|
|
FFormatNamedArguments MarkerLocationArgs;
|
|
MarkerLocationArgs.Add("X", FormatFloatToText(MarkerLocation.X, 1, 4));
|
|
MarkerLocationArgs.Add("Y", FormatFloatToText(MarkerLocation.Y, 1, 4));
|
|
MarkerLocationArgs.Add("Z", FormatFloatToText(MarkerLocation.Z, 1, 4));
|
|
const FText MarkerLocationText = FText::Format(NSLOCTEXT("BugSubmissionForm", "MarkerLocation", "{X},{Y},{Z}"), MarkerLocationArgs);
|
|
|
|
const FVector MarkerUpVector = this->BugMarkerActor->GetActorUpVector();
|
|
FFormatNamedArguments MarkerUpVectorArgs;
|
|
MarkerUpVectorArgs.Add("X", FormatFloatToText(MarkerUpVector.X, 1, 4));
|
|
MarkerUpVectorArgs.Add("Y", FormatFloatToText(MarkerUpVector.Y, 1, 4));
|
|
MarkerUpVectorArgs.Add("Z", FormatFloatToText(MarkerUpVector.Z, 1, 4));
|
|
const FText MarkerUpVectorText = FText::Format(NSLOCTEXT("BugSubmissionForm", "MarkerUpVector", "{X},{Y},{Z}"), MarkerUpVectorArgs);
|
|
|
|
FFormatNamedArguments MapDataArgs;
|
|
MapDataArgs.Add("MapName", FText::FromString(this->GetWorld()->GetMapName().RightChop(this->GetWorld()->StreamingLevelsPrefix.Len())));
|
|
MapDataArgs.Add("MarkerLocation", MarkerLocationText);
|
|
MapDataArgs.Add("MarkerUpVector", MarkerUpVectorText);
|
|
const FText MapDataText = FText::Format(NSLOCTEXT("BugSubmissionForm", "MapDataFormat", "{MapName}\n{MarkerLocation}\n{MarkerUpVector}"), MapDataArgs);
|
|
|
|
this->LocationValue->SetText(MapDataText);
|
|
}
|
|
|
|
|
|
void UBugSubmissionForm::SubmitForm()
|
|
{
|
|
const FString RESTURL = "/rest.cgi";
|
|
const FString RequestURL = "/bug/7";
|
|
const FString QueryString = "api_key=" + GetDefault<UUnrealzillaGlobalSettings>()->APIKey;
|
|
|
|
FHttpModule &HttpModule = FHttpModule::Get();
|
|
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = HttpModule.CreateRequest();
|
|
Request->SetVerb(TEXT("GET"));
|
|
Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
|
|
Request->SetURL(GetDefault<UUnrealzillaGlobalSettings>()->SubmissionServer + RESTURL + RequestURL
|
|
+ "?" + QueryString);
|
|
Request->OnProcessRequestComplete().BindUObject(this, &UBugSubmissionForm::ServerResponse);
|
|
Request->ProcessRequest();
|
|
|
|
this->OnFormSubmit.ExecuteIfBound();
|
|
|
|
this->CloseForm();
|
|
}
|
|
|
|
void UBugSubmissionForm::CancelForm()
|
|
{
|
|
this->OnFormCancelled.ExecuteIfBound();
|
|
|
|
this->CloseForm();
|
|
|
|
this->BugMarkerActor->Destroy();
|
|
}
|
|
|
|
void UBugSubmissionForm::CloseForm()
|
|
{
|
|
UGameplayStatics::GetPlayerController(this, 0)->SetInputMode(FInputModeGameOnly());
|
|
UGameplayStatics::GetPlayerController(this, 0)->bShowMouseCursor = true;
|
|
this->RemoveFromParent();
|
|
}
|
|
|
|
|
|
void UBugSubmissionForm::ServerResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool Success)
|
|
{
|
|
if (Success)
|
|
{
|
|
UE_LOG(LogTemp, Log, TEXT("%s"), *Response->GetContentAsString());
|
|
}
|
|
else
|
|
{
|
|
switch (Request->GetStatus()) {
|
|
case EHttpRequestStatus::Failed_ConnectionError:
|
|
UE_LOG(LogTemp, Error, TEXT("Unable to connect"));
|
|
break;
|
|
case EHttpRequestStatus::NotStarted:
|
|
UE_LOG(LogTemp, Error, TEXT("Connection not started"));
|
|
break;
|
|
case EHttpRequestStatus::Processing:
|
|
UE_LOG(LogTemp, Error, TEXT("Connection processing"));
|
|
break;
|
|
case EHttpRequestStatus::Failed:
|
|
UE_LOG(LogTemp, Error, TEXT("Connection failed"));
|
|
break;
|
|
case EHttpRequestStatus::Succeeded:
|
|
UE_LOG(LogTemp, Warning, TEXT("Connection succeeded"));
|
|
break;
|
|
default:
|
|
UE_LOG(LogTemp, Error, TEXT("Request failed"));
|
|
}
|
|
}
|
|
}
|